提交 9c9f5a2f 编写于 作者: N Namhyung Kim 提交者: Arnaldo Carvalho de Melo

perf tools: Introduce copyfile_offset() function

The copyfile_offset() function is to copy source data from given offset
to a destination file with an offset.  It'll be used to build an indexed
data file.
Signed-off-by: NNamhyung Kim <namhyung@kernel.org>
Acked-by: NJiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20150304145824.GD7519@krava.brq.redhat.com
Link: http://lkml.kernel.org/r/1431909055-21442-4-git-send-email-namhyung@kernel.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
上级 0b1de0be
...@@ -145,11 +145,38 @@ static int slow_copyfile(const char *from, const char *to, mode_t mode) ...@@ -145,11 +145,38 @@ static int slow_copyfile(const char *from, const char *to, mode_t mode)
return err; return err;
} }
int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
{
void *ptr;
loff_t pgoff;
pgoff = off_in & ~(page_size - 1);
off_in -= pgoff;
ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
if (ptr == MAP_FAILED)
return -1;
while (size) {
ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
if (ret < 0 && errno == EINTR)
continue;
if (ret <= 0)
break;
size -= ret;
off_in += ret;
off_out -= ret;
}
munmap(ptr, off_in + size);
return size ? -1 : 0;
}
int copyfile_mode(const char *from, const char *to, mode_t mode) int copyfile_mode(const char *from, const char *to, mode_t mode)
{ {
int fromfd, tofd; int fromfd, tofd;
struct stat st; struct stat st;
void *addr;
int err = -1; int err = -1;
if (stat(from, &st)) if (stat(from, &st))
...@@ -166,15 +193,8 @@ int copyfile_mode(const char *from, const char *to, mode_t mode) ...@@ -166,15 +193,8 @@ int copyfile_mode(const char *from, const char *to, mode_t mode)
if (tofd < 0) if (tofd < 0)
goto out_close_from; goto out_close_from;
addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0); err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
if (addr == MAP_FAILED)
goto out_close_to;
if (write(tofd, addr, st.st_size) == st.st_size)
err = 0;
munmap(addr, st.st_size);
out_close_to:
close(tofd); close(tofd);
if (err) if (err)
unlink(to); unlink(to);
......
...@@ -252,6 +252,7 @@ int mkdir_p(char *path, mode_t mode); ...@@ -252,6 +252,7 @@ int mkdir_p(char *path, mode_t mode);
int rm_rf(char *path); int rm_rf(char *path);
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); int copyfile_mode(const char *from, const char *to, mode_t mode);
int copyfile_offset(int fromfd, loff_t from_ofs, int tofd, loff_t to_ofs, u64 size);
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% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册