linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	linux-kernel@vger.kernel.org, David Ahern <dsahern@gmail.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>, Mike Galbraith <efault@gmx.de>,
	Namhyung Kim <namhyung@gmail.com>,
	Paul Mackerras <paulus@samba.org>,
	Stephane Eranian <eranian@google.com>
Subject: [PATCH V7 4/7] perf tools: add copyfile_mode()
Date: Mon, 14 Oct 2013 13:43:41 +0300	[thread overview]
Message-ID: <1381747424-3557-5-git-send-email-adrian.hunter@intel.com> (raw)
In-Reply-To: <1381747424-3557-1-git-send-email-adrian.hunter@intel.com>

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>
---
 tools/perf/util/util.c | 18 +++++++++++++-----
 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 ab71d62..8dc8cf3 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)
 	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;
 	size_t n;
 	FILE *from_fp = fopen(from, "r"), *to_fp;
+	mode_t old_umask;
 
 	if (from_fp == NULL)
 		goto out;
 
+	old_umask = umask(mode ^ 0777);
 	to_fp = fopen(to, "w");
+	umask(old_umask);
 	if (to_fp == NULL)
 		goto out_fclose_from;
 
@@ -82,7 +85,7 @@ out:
 	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;
 	struct stat st;
@@ -93,13 +96,13 @@ int copyfile(const char *from, const char *to)
 		goto out;
 
 	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);
 	if (fromfd < 0)
 		goto out;
 
-	tofd = creat(to, 0755);
+	tofd = creat(to, mode);
 	if (tofd < 0)
 		goto out_close_from;
 
@@ -121,6 +124,11 @@ out:
 	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)
 {
 	*unit = ' ';
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 1f06ba4..42dfba7 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)
 
 int mkdir_p(char *path, mode_t mode);
 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);
 char **argv_split(const char *str, int *argcp);
-- 
1.7.11.7


  parent reply	other threads:[~2013-10-14 10:45 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-14 10:43 [PATCH V7 0/7] perf tools: kcore improvements Adrian Hunter
2013-10-14 10:43 ` [PATCH V7 1/7] perf tools: Add map_groups__find_ams() Adrian Hunter
2013-10-15  5:33   ` [tip:perf/core] perf symbols: " tip-bot for Arnaldo Carvalho de Melo
2013-10-14 10:43 ` [PATCH V7 2/7] perf tools: fix annotate_browser__callq() Adrian Hunter
2013-10-15  5:35   ` [tip:perf/core] perf annotate: Fix annotate_browser__callq() tip-bot for Adrian Hunter
2013-10-14 10:43 ` [PATCH V7 3/7] perf tools: find kcore symbols on other maps Adrian Hunter
2013-10-15  5:34   ` [tip:perf/core] perf annotate: Find " tip-bot for Adrian Hunter
2013-10-14 10:43 ` Adrian Hunter [this message]
2013-10-15  5:35   ` [tip:perf/core] perf tools: Add copyfile_mode() tip-bot for Adrian Hunter
2013-10-14 10:43 ` [PATCH V7 5/7] perf buildid-cache: add ability to add kcore to the cache Adrian Hunter
2013-10-14 13:13   ` Arnaldo Carvalho de Melo
2013-10-14 13:57     ` Adrian Hunter
2013-10-15  5:35       ` [tip:perf/core] perf buildid-cache: Add " tip-bot for Adrian Hunter
2013-10-14 10:43 ` [PATCH V7 6/7] perf tools: add ability to find kcore in build-id cache Adrian Hunter
2013-10-15  5:35   ` [tip:perf/core] perf symbols: Add " tip-bot for Adrian Hunter
2013-10-14 10:43 ` [PATCH V7 7/7] perf tools: another fix for annotate_browser__callq() Adrian Hunter
2013-10-15  5:35   ` [tip:perf/core] perf annotate: Another " tip-bot for Adrian Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1381747424-3557-5-git-send-email-adrian.hunter@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=dsahern@gmail.com \
    --cc=efault@gmx.de \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@gmail.com \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).