1 #include <linux/compiler.h>
2 #include <linux/kernel.h>
11 static bool check_pipe(struct perf_data_file
*file
)
15 int fd
= perf_data_file__is_read(file
) ?
16 STDIN_FILENO
: STDOUT_FILENO
;
19 if (!fstat(fd
, &st
) && S_ISFIFO(st
.st_mode
))
22 if (!strcmp(file
->path
, "-"))
29 return file
->is_pipe
= is_pipe
;
32 static int check_backup(struct perf_data_file
*file
)
36 if (!stat(file
->path
, &st
) && st
.st_size
) {
37 /* TODO check errors properly */
38 char oldname
[PATH_MAX
];
39 snprintf(oldname
, sizeof(oldname
), "%s.old",
42 rename(file
->path
, oldname
);
48 static int open_file_read(struct perf_data_file
*file
)
53 fd
= open(file
->path
, O_RDONLY
);
57 pr_err("failed to open %s: %s", file
->path
, strerror(err
));
58 if (err
== ENOENT
&& !strcmp(file
->path
, "perf.data"))
59 pr_err(" (try 'perf record' first)");
64 if (fstat(fd
, &st
) < 0)
67 if (!file
->force
&& st
.st_uid
&& (st
.st_uid
!= geteuid())) {
68 pr_err("file %s not owned by current user or root\n",
74 pr_info("zero-sized file (%s), nothing to do!\n",
79 file
->size
= st
.st_size
;
87 static int open_file_write(struct perf_data_file
*file
)
89 if (check_backup(file
))
92 return open(file
->path
, O_CREAT
|O_RDWR
|O_TRUNC
, S_IRUSR
|S_IWUSR
);
95 static int open_file(struct perf_data_file
*file
)
99 fd
= perf_data_file__is_read(file
) ?
100 open_file_read(file
) : open_file_write(file
);
103 return fd
< 0 ? -1 : 0;
106 int perf_data_file__open(struct perf_data_file
*file
)
108 if (check_pipe(file
))
112 file
->path
= "perf.data";
114 return open_file(file
);
117 void perf_data_file__close(struct perf_data_file
*file
)
122 ssize_t
perf_data_file__write(struct perf_data_file
*file
,
123 void *buf
, size_t size
)
125 return writen(file
->fd
, buf
, size
);