Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+164 -142
+1
tools/perf/util/Build
··· 3 3 perf-y += build-id.o 4 4 perf-y += cacheline.o 5 5 perf-y += config.o 6 + perf-y += copyfile.o 6 7 perf-y += ctype.o 7 8 perf-y += db-export.o 8 9 perf-y += env.o
+2 -1
tools/perf/util/build-id.c
··· 7 7 * Copyright (C) 2009, 2010 Red Hat Inc. 8 8 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> 9 9 */ 10 - #include "util.h" // copyfile_ns(), lsdir(), mkdir_p(), rm_rf() 10 + #include "util.h" // lsdir(), mkdir_p(), rm_rf() 11 11 #include <dirent.h> 12 12 #include <errno.h> 13 13 #include <stdio.h> 14 14 #include <sys/stat.h> 15 15 #include <sys/types.h> 16 + #include "util/copyfile.h" 16 17 #include "dso.h" 17 18 #include "build-id.h" 18 19 #include "event.h"
+144
tools/perf/util/copyfile.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include "util/copyfile.h" 3 + #include "util/namespaces.h" 4 + #include <internal/lib.h> 5 + #include <sys/mman.h> 6 + #include <sys/stat.h> 7 + #include <errno.h> 8 + #include <fcntl.h> 9 + #include <stdio.h> 10 + #include <stdlib.h> 11 + #include <string.h> 12 + #include <unistd.h> 13 + 14 + static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi) 15 + { 16 + int err = -1; 17 + char *line = NULL; 18 + size_t n; 19 + FILE *from_fp, *to_fp; 20 + struct nscookie nsc; 21 + 22 + nsinfo__mountns_enter(nsi, &nsc); 23 + from_fp = fopen(from, "r"); 24 + nsinfo__mountns_exit(&nsc); 25 + if (from_fp == NULL) 26 + goto out; 27 + 28 + to_fp = fopen(to, "w"); 29 + if (to_fp == NULL) 30 + goto out_fclose_from; 31 + 32 + while (getline(&line, &n, from_fp) > 0) 33 + if (fputs(line, to_fp) == EOF) 34 + goto out_fclose_to; 35 + err = 0; 36 + out_fclose_to: 37 + fclose(to_fp); 38 + free(line); 39 + out_fclose_from: 40 + fclose(from_fp); 41 + out: 42 + return err; 43 + } 44 + 45 + int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) 46 + { 47 + void *ptr; 48 + loff_t pgoff; 49 + 50 + pgoff = off_in & ~(page_size - 1); 51 + off_in -= pgoff; 52 + 53 + ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); 54 + if (ptr == MAP_FAILED) 55 + return -1; 56 + 57 + while (size) { 58 + ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); 59 + if (ret < 0 && errno == EINTR) 60 + continue; 61 + if (ret <= 0) 62 + break; 63 + 64 + size -= ret; 65 + off_in += ret; 66 + off_out += ret; 67 + } 68 + munmap(ptr, off_in + size); 69 + 70 + return size ? -1 : 0; 71 + } 72 + 73 + static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, 74 + struct nsinfo *nsi) 75 + { 76 + int fromfd, tofd; 77 + struct stat st; 78 + int err; 79 + char *tmp = NULL, *ptr = NULL; 80 + struct nscookie nsc; 81 + 82 + nsinfo__mountns_enter(nsi, &nsc); 83 + err = stat(from, &st); 84 + nsinfo__mountns_exit(&nsc); 85 + if (err) 86 + goto out; 87 + err = -1; 88 + 89 + /* extra 'x' at the end is to reserve space for '.' */ 90 + if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { 91 + tmp = NULL; 92 + goto out; 93 + } 94 + ptr = strrchr(tmp, '/'); 95 + if (!ptr) 96 + goto out; 97 + ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); 98 + *ptr = '.'; 99 + 100 + tofd = mkstemp(tmp); 101 + if (tofd < 0) 102 + goto out; 103 + 104 + if (fchmod(tofd, mode)) 105 + goto out_close_to; 106 + 107 + if (st.st_size == 0) { /* /proc? do it slowly... */ 108 + err = slow_copyfile(from, tmp, nsi); 109 + goto out_close_to; 110 + } 111 + 112 + nsinfo__mountns_enter(nsi, &nsc); 113 + fromfd = open(from, O_RDONLY); 114 + nsinfo__mountns_exit(&nsc); 115 + if (fromfd < 0) 116 + goto out_close_to; 117 + 118 + err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); 119 + 120 + close(fromfd); 121 + out_close_to: 122 + close(tofd); 123 + if (!err) 124 + err = link(tmp, to); 125 + unlink(tmp); 126 + out: 127 + free(tmp); 128 + return err; 129 + } 130 + 131 + int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi) 132 + { 133 + return copyfile_mode_ns(from, to, 0755, nsi); 134 + } 135 + 136 + int copyfile_mode(const char *from, const char *to, mode_t mode) 137 + { 138 + return copyfile_mode_ns(from, to, mode, NULL); 139 + } 140 + 141 + int copyfile(const char *from, const char *to) 142 + { 143 + return copyfile_mode(from, to, 0755); 144 + }
+16
tools/perf/util/copyfile.h
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #ifndef PERF_COPYFILE_H_ 3 + #define PERF_COPYFILE_H_ 4 + 5 + #include <linux/types.h> 6 + #include <sys/types.h> 7 + #include <fcntl.h> 8 + 9 + struct nsinfo; 10 + 11 + int copyfile(const char *from, const char *to); 12 + int copyfile_mode(const char *from, const char *to, mode_t mode); 13 + int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi); 14 + int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size); 15 + 16 + #endif // PERF_COPYFILE_H_
+1 -1
tools/perf/util/symbol-elf.c
··· 17 17 #include "machine.h" 18 18 #include "vdso.h" 19 19 #include "debug.h" 20 - #include "util.h" 20 + #include "util/copyfile.h" 21 21 #include <linux/ctype.h> 22 22 #include <linux/kernel.h> 23 23 #include <linux/zalloc.h>
-135
tools/perf/util/util.c
··· 2 2 #include "util.h" 3 3 #include "debug.h" 4 4 #include "event.h" 5 - #include "namespaces.h" 6 - #include <internal/lib.h> 7 5 #include <api/fs/fs.h> 8 - #include <sys/mman.h> 9 6 #include <sys/stat.h> 10 7 #include <sys/utsname.h> 11 8 #include <dirent.h> ··· 228 231 out: 229 232 closedir(dir); 230 233 return list; 231 - } 232 - 233 - static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi) 234 - { 235 - int err = -1; 236 - char *line = NULL; 237 - size_t n; 238 - FILE *from_fp, *to_fp; 239 - struct nscookie nsc; 240 - 241 - nsinfo__mountns_enter(nsi, &nsc); 242 - from_fp = fopen(from, "r"); 243 - nsinfo__mountns_exit(&nsc); 244 - if (from_fp == NULL) 245 - goto out; 246 - 247 - to_fp = fopen(to, "w"); 248 - if (to_fp == NULL) 249 - goto out_fclose_from; 250 - 251 - while (getline(&line, &n, from_fp) > 0) 252 - if (fputs(line, to_fp) == EOF) 253 - goto out_fclose_to; 254 - err = 0; 255 - out_fclose_to: 256 - fclose(to_fp); 257 - free(line); 258 - out_fclose_from: 259 - fclose(from_fp); 260 - out: 261 - return err; 262 - } 263 - 264 - int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) 265 - { 266 - void *ptr; 267 - loff_t pgoff; 268 - 269 - pgoff = off_in & ~(page_size - 1); 270 - off_in -= pgoff; 271 - 272 - ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); 273 - if (ptr == MAP_FAILED) 274 - return -1; 275 - 276 - while (size) { 277 - ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); 278 - if (ret < 0 && errno == EINTR) 279 - continue; 280 - if (ret <= 0) 281 - break; 282 - 283 - size -= ret; 284 - off_in += ret; 285 - off_out += ret; 286 - } 287 - munmap(ptr, off_in + size); 288 - 289 - return size ? -1 : 0; 290 - } 291 - 292 - static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, 293 - struct nsinfo *nsi) 294 - { 295 - int fromfd, tofd; 296 - struct stat st; 297 - int err; 298 - char *tmp = NULL, *ptr = NULL; 299 - struct nscookie nsc; 300 - 301 - nsinfo__mountns_enter(nsi, &nsc); 302 - err = stat(from, &st); 303 - nsinfo__mountns_exit(&nsc); 304 - if (err) 305 - goto out; 306 - err = -1; 307 - 308 - /* extra 'x' at the end is to reserve space for '.' */ 309 - if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { 310 - tmp = NULL; 311 - goto out; 312 - } 313 - ptr = strrchr(tmp, '/'); 314 - if (!ptr) 315 - goto out; 316 - ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); 317 - *ptr = '.'; 318 - 319 - tofd = mkstemp(tmp); 320 - if (tofd < 0) 321 - goto out; 322 - 323 - if (fchmod(tofd, mode)) 324 - goto out_close_to; 325 - 326 - if (st.st_size == 0) { /* /proc? do it slowly... */ 327 - err = slow_copyfile(from, tmp, nsi); 328 - goto out_close_to; 329 - } 330 - 331 - nsinfo__mountns_enter(nsi, &nsc); 332 - fromfd = open(from, O_RDONLY); 333 - nsinfo__mountns_exit(&nsc); 334 - if (fromfd < 0) 335 - goto out_close_to; 336 - 337 - err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); 338 - 339 - close(fromfd); 340 - out_close_to: 341 - close(tofd); 342 - if (!err) 343 - err = link(tmp, to); 344 - unlink(tmp); 345 - out: 346 - free(tmp); 347 - return err; 348 - } 349 - 350 - int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi) 351 - { 352 - return copyfile_mode_ns(from, to, 0755, nsi); 353 - } 354 - 355 - int copyfile_mode(const char *from, const char *to, mode_t mode) 356 - { 357 - return copyfile_mode_ns(from, to, mode, NULL); 358 - } 359 - 360 - int copyfile(const char *from, const char *to) 361 - { 362 - return copyfile_mode(from, to, 0755); 363 234 } 364 235 365 236 size_t hex_width(u64 v)
-5
tools/perf/util/util.h
··· 17 17 void die(const char *err, ...) __noreturn __printf(1, 2); 18 18 19 19 struct dirent; 20 - struct nsinfo; 21 20 struct strlist; 22 21 23 22 int mkdir_p(char *path, mode_t mode); ··· 24 25 int rm_rf_perf_data(const char *path); 25 26 struct strlist *lsdir(const char *name, bool (*filter)(const char *, struct dirent *)); 26 27 bool lsdir_no_dot_filter(const char *name, struct dirent *d); 27 - int copyfile(const char *from, const char *to); 28 - int copyfile_mode(const char *from, const char *to, mode_t mode); 29 - int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi); 30 - int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size); 31 28 32 29 size_t hex_width(u64 v); 33 30