Commit 9a17d726 authored by Adrian Hunter's avatar Adrian Hunter Committed by Arnaldo Carvalho de Melo

perf tools: Add copyfile_mode()

Add a function to copy a file specifying the permissions to use for the
created file.
Signed-off-by: default avatarAdrian 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.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 6e427ab0
...@@ -55,17 +55,20 @@ int mkdir_p(char *path, mode_t mode) ...@@ -55,17 +55,20 @@ int mkdir_p(char *path, mode_t mode)
return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
} }
static int slow_copyfile(const char *from, const char *to) static int slow_copyfile(const char *from, const char *to, mode_t mode)
{ {
int err = 0; int err = -1;
char *line = NULL; char *line = NULL;
size_t n; size_t n;
FILE *from_fp = fopen(from, "r"), *to_fp; FILE *from_fp = fopen(from, "r"), *to_fp;
mode_t old_umask;
if (from_fp == NULL) if (from_fp == NULL)
goto out; goto out;
old_umask = umask(mode ^ 0777);
to_fp = fopen(to, "w"); to_fp = fopen(to, "w");
umask(old_umask);
if (to_fp == NULL) if (to_fp == NULL)
goto out_fclose_from; goto out_fclose_from;
...@@ -82,7 +85,7 @@ static int slow_copyfile(const char *from, const char *to) ...@@ -82,7 +85,7 @@ static int slow_copyfile(const char *from, const char *to)
return err; return err;
} }
int copyfile(const char *from, const char *to) int copyfile_mode(const char *from, const char *to, mode_t mode)
{ {
int fromfd, tofd; int fromfd, tofd;
struct stat st; struct stat st;
...@@ -93,13 +96,13 @@ int copyfile(const char *from, const char *to) ...@@ -93,13 +96,13 @@ int copyfile(const char *from, const char *to)
goto out; goto out;
if (st.st_size == 0) /* /proc? do it slowly... */ if (st.st_size == 0) /* /proc? do it slowly... */
return slow_copyfile(from, to); return slow_copyfile(from, to, mode);
fromfd = open(from, O_RDONLY); fromfd = open(from, O_RDONLY);
if (fromfd < 0) if (fromfd < 0)
goto out; goto out;
tofd = creat(to, 0755); tofd = creat(to, mode);
if (tofd < 0) if (tofd < 0)
goto out_close_from; goto out_close_from;
...@@ -121,6 +124,11 @@ int copyfile(const char *from, const char *to) ...@@ -121,6 +124,11 @@ int copyfile(const char *from, const char *to)
return err; return err;
} }
int copyfile(const char *from, const char *to)
{
return copyfile_mode(from, to, 0755);
}
unsigned long convert_unit(unsigned long value, char *unit) unsigned long convert_unit(unsigned long value, char *unit)
{ {
*unit = ' '; *unit = ' ';
......
...@@ -243,6 +243,7 @@ static inline int sane_case(int x, int high) ...@@ -243,6 +243,7 @@ static inline int sane_case(int x, int high)
int mkdir_p(char *path, mode_t mode); int mkdir_p(char *path, mode_t mode);
int copyfile(const char *from, const char *to); int copyfile(const char *from, const char *to);
int copyfile_mode(const char *from, const char *to, mode_t mode);
s64 perf_atoll(const char *str); s64 perf_atoll(const char *str);
char **argv_split(const char *str, int *argcp); char **argv_split(const char *str, int *argcp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment