aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/util.c
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2019-09-24 14:14:12 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2019-09-25 08:51:49 -0400
commit32ff3fec07b6d8e6c5cc2342f6cbbdcb224d484c (patch)
tree7b4ecd5ec18e1c0c07f74fb3b6486c66f24f2d5e /tools/perf/util/util.c
parent80ab2987a016f774201d4f3509118047f9d58175 (diff)
perf copyfile: Move copyfile routines to separate files
Further reducing the util.c hodgepodge files. Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Link: https://lkml.kernel.org/n/tip-0i62zh7ok25znibyebgq0qs4@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/util.c')
-rw-r--r--tools/perf/util/util.c135
1 files changed, 0 insertions, 135 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index cb6fa4c98470..5eda6e19c947 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -2,10 +2,7 @@
2#include "util.h" 2#include "util.h"
3#include "debug.h" 3#include "debug.h"
4#include "event.h" 4#include "event.h"
5#include "namespaces.h"
6#include <internal/lib.h>
7#include <api/fs/fs.h> 5#include <api/fs/fs.h>
8#include <sys/mman.h>
9#include <sys/stat.h> 6#include <sys/stat.h>
10#include <sys/utsname.h> 7#include <sys/utsname.h>
11#include <dirent.h> 8#include <dirent.h>
@@ -233,138 +230,6 @@ out:
233 return list; 230 return list;
234} 231}
235 232
236static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
237{
238 int err = -1;
239 char *line = NULL;
240 size_t n;
241 FILE *from_fp, *to_fp;
242 struct nscookie nsc;
243
244 nsinfo__mountns_enter(nsi, &nsc);
245 from_fp = fopen(from, "r");
246 nsinfo__mountns_exit(&nsc);
247 if (from_fp == NULL)
248 goto out;
249
250 to_fp = fopen(to, "w");
251 if (to_fp == NULL)
252 goto out_fclose_from;
253
254 while (getline(&line, &n, from_fp) > 0)
255 if (fputs(line, to_fp) == EOF)
256 goto out_fclose_to;
257 err = 0;
258out_fclose_to:
259 fclose(to_fp);
260 free(line);
261out_fclose_from:
262 fclose(from_fp);
263out:
264 return err;
265}
266
267int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
268{
269 void *ptr;
270 loff_t pgoff;
271
272 pgoff = off_in & ~(page_size - 1);
273 off_in -= pgoff;
274
275 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
276 if (ptr == MAP_FAILED)
277 return -1;
278
279 while (size) {
280 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
281 if (ret < 0 && errno == EINTR)
282 continue;
283 if (ret <= 0)
284 break;
285
286 size -= ret;
287 off_in += ret;
288 off_out += ret;
289 }
290 munmap(ptr, off_in + size);
291
292 return size ? -1 : 0;
293}
294
295static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
296 struct nsinfo *nsi)
297{
298 int fromfd, tofd;
299 struct stat st;
300 int err;
301 char *tmp = NULL, *ptr = NULL;
302 struct nscookie nsc;
303
304 nsinfo__mountns_enter(nsi, &nsc);
305 err = stat(from, &st);
306 nsinfo__mountns_exit(&nsc);
307 if (err)
308 goto out;
309 err = -1;
310
311 /* extra 'x' at the end is to reserve space for '.' */
312 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
313 tmp = NULL;
314 goto out;
315 }
316 ptr = strrchr(tmp, '/');
317 if (!ptr)
318 goto out;
319 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
320 *ptr = '.';
321
322 tofd = mkstemp(tmp);
323 if (tofd < 0)
324 goto out;
325
326 if (fchmod(tofd, mode))
327 goto out_close_to;
328
329 if (st.st_size == 0) { /* /proc? do it slowly... */
330 err = slow_copyfile(from, tmp, nsi);
331 goto out_close_to;
332 }
333
334 nsinfo__mountns_enter(nsi, &nsc);
335 fromfd = open(from, O_RDONLY);
336 nsinfo__mountns_exit(&nsc);
337 if (fromfd < 0)
338 goto out_close_to;
339
340 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
341
342 close(fromfd);
343out_close_to:
344 close(tofd);
345 if (!err)
346 err = link(tmp, to);
347 unlink(tmp);
348out:
349 free(tmp);
350 return err;
351}
352
353int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
354{
355 return copyfile_mode_ns(from, to, 0755, nsi);
356}
357
358int copyfile_mode(const char *from, const char *to, mode_t mode)
359{
360 return copyfile_mode_ns(from, to, mode, NULL);
361}
362
363int copyfile(const char *from, const char *to)
364{
365 return copyfile_mode(from, to, 0755);
366}
367
368size_t hex_width(u64 v) 233size_t hex_width(u64 v)
369{ 234{
370 size_t n = 1; 235 size_t n = 1;