diff options
author | Adrian Hunter <adrian.hunter@intel.com> | 2013-10-14 06:43:41 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2013-10-14 09:29:23 -0400 |
commit | 9a17d7268d71674f0bbff6821f7d8e6dc0ece19a (patch) | |
tree | 8d07c360badff5c323188e0124f0770924ea8632 /tools | |
parent | 6e427ab02c8886ca6c9ecdbb318e68fe8f605469 (diff) |
perf tools: Add copyfile_mode()
Add a function to copy a file specifying the permissions to use for the
created file.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1381747424-3557-5-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/perf/util/util.c | 18 | ||||
-rw-r--r-- | tools/perf/util/util.h | 1 |
2 files changed, 14 insertions, 5 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index ab71d6216803..8dc8cf39f4ed 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -55,17 +55,20 @@ int mkdir_p(char *path, mode_t mode) | |||
55 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; | 55 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
56 | } | 56 | } |
57 | 57 | ||
58 | static int slow_copyfile(const char *from, const char *to) | 58 | static int slow_copyfile(const char *from, const char *to, mode_t mode) |
59 | { | 59 | { |
60 | int err = 0; | 60 | int err = -1; |
61 | char *line = NULL; | 61 | char *line = NULL; |
62 | size_t n; | 62 | size_t n; |
63 | FILE *from_fp = fopen(from, "r"), *to_fp; | 63 | FILE *from_fp = fopen(from, "r"), *to_fp; |
64 | mode_t old_umask; | ||
64 | 65 | ||
65 | if (from_fp == NULL) | 66 | if (from_fp == NULL) |
66 | goto out; | 67 | goto out; |
67 | 68 | ||
69 | old_umask = umask(mode ^ 0777); | ||
68 | to_fp = fopen(to, "w"); | 70 | to_fp = fopen(to, "w"); |
71 | umask(old_umask); | ||
69 | if (to_fp == NULL) | 72 | if (to_fp == NULL) |
70 | goto out_fclose_from; | 73 | goto out_fclose_from; |
71 | 74 | ||
@@ -82,7 +85,7 @@ out: | |||
82 | return err; | 85 | return err; |
83 | } | 86 | } |
84 | 87 | ||
85 | int copyfile(const char *from, const char *to) | 88 | int copyfile_mode(const char *from, const char *to, mode_t mode) |
86 | { | 89 | { |
87 | int fromfd, tofd; | 90 | int fromfd, tofd; |
88 | struct stat st; | 91 | struct stat st; |
@@ -93,13 +96,13 @@ int copyfile(const char *from, const char *to) | |||
93 | goto out; | 96 | goto out; |
94 | 97 | ||
95 | if (st.st_size == 0) /* /proc? do it slowly... */ | 98 | if (st.st_size == 0) /* /proc? do it slowly... */ |
96 | return slow_copyfile(from, to); | 99 | return slow_copyfile(from, to, mode); |
97 | 100 | ||
98 | fromfd = open(from, O_RDONLY); | 101 | fromfd = open(from, O_RDONLY); |
99 | if (fromfd < 0) | 102 | if (fromfd < 0) |
100 | goto out; | 103 | goto out; |
101 | 104 | ||
102 | tofd = creat(to, 0755); | 105 | tofd = creat(to, mode); |
103 | if (tofd < 0) | 106 | if (tofd < 0) |
104 | goto out_close_from; | 107 | goto out_close_from; |
105 | 108 | ||
@@ -121,6 +124,11 @@ out: | |||
121 | return err; | 124 | return err; |
122 | } | 125 | } |
123 | 126 | ||
127 | int copyfile(const char *from, const char *to) | ||
128 | { | ||
129 | return copyfile_mode(from, to, 0755); | ||
130 | } | ||
131 | |||
124 | unsigned long convert_unit(unsigned long value, char *unit) | 132 | unsigned long convert_unit(unsigned long value, char *unit) |
125 | { | 133 | { |
126 | *unit = ' '; | 134 | *unit = ' '; |
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 1f06ba44cd53..42dfba70fbfc 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
@@ -243,6 +243,7 @@ static inline int sane_case(int x, int high) | |||
243 | 243 | ||
244 | int mkdir_p(char *path, mode_t mode); | 244 | int mkdir_p(char *path, mode_t mode); |
245 | int copyfile(const char *from, const char *to); | 245 | int copyfile(const char *from, const char *to); |
246 | int copyfile_mode(const char *from, const char *to, mode_t mode); | ||
246 | 247 | ||
247 | s64 perf_atoll(const char *str); | 248 | s64 perf_atoll(const char *str); |
248 | char **argv_split(const char *str, int *argcp); | 249 | char **argv_split(const char *str, int *argcp); |