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

perf buildid-cache: Consolidate .build-id cache path generators

Consolidate .build-id cache path generating routines to
build_id__filename() function. Other functions must use it to get the
buildid cache path (link path) from build-id. This can reduce the risk
of partial-update.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Hemant Kumar <hemant@linux.vnet.ibm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/20150210091853.19264.58513.stgit@localhost.localdomain
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Masami Hiramatsu and committed by
Arnaldo Carvalho de Melo
5cb113fd e35f7362

+41 -17
+41 -17
tools/perf/util/build-id.c
··· 93 93 return raw - build_id; 94 94 } 95 95 96 + /* asnprintf consolidates asprintf and snprintf */ 97 + static int asnprintf(char **strp, size_t size, const char *fmt, ...) 98 + { 99 + va_list ap; 100 + int ret; 101 + 102 + if (!strp) 103 + return -EINVAL; 104 + 105 + va_start(ap, fmt); 106 + if (*strp) 107 + ret = vsnprintf(*strp, size, fmt, ap); 108 + else 109 + ret = vasprintf(strp, fmt, ap); 110 + va_end(ap); 111 + 112 + return ret; 113 + } 114 + 115 + static char *build_id__filename(const char *sbuild_id, char *bf, size_t size) 116 + { 117 + char *tmp = bf; 118 + int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir, 119 + sbuild_id, sbuild_id + 2); 120 + if (ret < 0 || (tmp && size < (unsigned int)ret)) 121 + return NULL; 122 + return bf; 123 + } 124 + 96 125 char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) 97 126 { 98 127 char build_id_hex[BUILD_ID_SIZE * 2 + 1]; ··· 130 101 return NULL; 131 102 132 103 build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex); 133 - if (bf == NULL) { 134 - if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir, 135 - build_id_hex, build_id_hex + 2) < 0) 136 - return NULL; 137 - } else 138 - snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir, 139 - build_id_hex, build_id_hex + 2); 140 - return bf; 104 + return build_id__filename(build_id_hex, bf, size); 141 105 } 142 106 143 107 #define dsos__for_each_with_build_id(pos, head) \ ··· 286 264 { 287 265 const size_t size = PATH_MAX; 288 266 char *realname, *filename = zalloc(size), 289 - *linkname = zalloc(size), *targetname; 267 + *linkname = zalloc(size), *targetname, *tmp; 290 268 int len, err = -1; 291 269 bool slash = is_kallsyms || is_vdso; 292 270 ··· 319 297 goto out_free; 320 298 } 321 299 322 - len = scnprintf(linkname, size, "%s/.build-id/%.2s", 323 - buildid_dir, sbuild_id); 300 + if (!build_id__filename(sbuild_id, linkname, size)) 301 + goto out_free; 302 + tmp = strrchr(linkname, '/'); 303 + *tmp = '\0'; 324 304 325 305 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 326 306 goto out_free; 327 307 328 - snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 308 + *tmp = '/'; 329 309 targetname = filename + strlen(buildid_dir) - 5; 330 310 memcpy(targetname, "../..", 5); 331 311 ··· 356 332 { 357 333 const size_t size = PATH_MAX; 358 334 char *filename = zalloc(size), 359 - *linkname = zalloc(size); 335 + *linkname = zalloc(size), *tmp; 360 336 int err = -1; 361 337 362 338 if (filename == NULL || linkname == NULL) 363 339 goto out_free; 364 340 365 - snprintf(linkname, size, "%s/.build-id/%.2s/%s", 366 - buildid_dir, sbuild_id, sbuild_id + 2); 341 + if (!build_id__filename(sbuild_id, linkname, size)) 342 + goto out_free; 367 343 368 344 if (access(linkname, F_OK)) 369 345 goto out_free; ··· 377 353 /* 378 354 * Since the link is relative, we must make it absolute: 379 355 */ 380 - snprintf(linkname, size, "%s/.build-id/%.2s/%s", 381 - buildid_dir, sbuild_id, filename); 356 + tmp = strrchr(linkname, '/') + 1; 357 + snprintf(tmp, size - (tmp - linkname), "%s", filename); 382 358 383 359 if (unlink(linkname)) 384 360 goto out_free;