diff options
author | Jiri Olsa <jolsa@kernel.org> | 2016-02-14 11:03:43 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-02-16 15:12:56 -0500 |
commit | 607bfbd7ffc60156ae0831c917497dc91a57dd8d (patch) | |
tree | aa97aa79ad0d1b0cc2e488b15b96402417b0155e /tools/lib/api | |
parent | 975f14fa8f2996604f248552eee4403def34bf5e (diff) |
tools lib api fs: Adopt filename__read_str from perf
We already moved similar functions in here, also it'll be useful for
sysfs__read_str addition in following patch.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1455465826-8426-3-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/api')
-rw-r--r-- | tools/lib/api/fs/fs.c | 51 | ||||
-rw-r--r-- | tools/lib/api/fs/fs.h | 2 |
2 files changed, 53 insertions, 0 deletions
diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 459599d1b6c4..2cbf6773ca5d 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c | |||
@@ -13,6 +13,7 @@ | |||
13 | #include <sys/mount.h> | 13 | #include <sys/mount.h> |
14 | 14 | ||
15 | #include "fs.h" | 15 | #include "fs.h" |
16 | #include "debug-internal.h" | ||
16 | 17 | ||
17 | #define _STR(x) #x | 18 | #define _STR(x) #x |
18 | #define STR(x) _STR(x) | 19 | #define STR(x) _STR(x) |
@@ -300,6 +301,56 @@ int filename__read_ull(const char *filename, unsigned long long *value) | |||
300 | return err; | 301 | return err; |
301 | } | 302 | } |
302 | 303 | ||
304 | #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */ | ||
305 | |||
306 | int filename__read_str(const char *filename, char **buf, size_t *sizep) | ||
307 | { | ||
308 | size_t size = 0, alloc_size = 0; | ||
309 | void *bf = NULL, *nbf; | ||
310 | int fd, n, err = 0; | ||
311 | char sbuf[STRERR_BUFSIZE]; | ||
312 | |||
313 | fd = open(filename, O_RDONLY); | ||
314 | if (fd < 0) | ||
315 | return -errno; | ||
316 | |||
317 | do { | ||
318 | if (size == alloc_size) { | ||
319 | alloc_size += BUFSIZ; | ||
320 | nbf = realloc(bf, alloc_size); | ||
321 | if (!nbf) { | ||
322 | err = -ENOMEM; | ||
323 | break; | ||
324 | } | ||
325 | |||
326 | bf = nbf; | ||
327 | } | ||
328 | |||
329 | n = read(fd, bf + size, alloc_size - size); | ||
330 | if (n < 0) { | ||
331 | if (size) { | ||
332 | pr_warning("read failed %d: %s\n", errno, | ||
333 | strerror_r(errno, sbuf, sizeof(sbuf))); | ||
334 | err = 0; | ||
335 | } else | ||
336 | err = -errno; | ||
337 | |||
338 | break; | ||
339 | } | ||
340 | |||
341 | size += n; | ||
342 | } while (n > 0); | ||
343 | |||
344 | if (!err) { | ||
345 | *sizep = size; | ||
346 | *buf = bf; | ||
347 | } else | ||
348 | free(bf); | ||
349 | |||
350 | close(fd); | ||
351 | return err; | ||
352 | } | ||
353 | |||
303 | int sysfs__read_ull(const char *entry, unsigned long long *value) | 354 | int sysfs__read_ull(const char *entry, unsigned long long *value) |
304 | { | 355 | { |
305 | char path[PATH_MAX]; | 356 | char path[PATH_MAX]; |
diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h index d024a7f682f6..858922b61141 100644 --- a/tools/lib/api/fs/fs.h +++ b/tools/lib/api/fs/fs.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define __API_FS__ | 2 | #define __API_FS__ |
3 | 3 | ||
4 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | #include <unistd.h> | ||
5 | 6 | ||
6 | /* | 7 | /* |
7 | * On most systems <limits.h> would have given us this, but not on some systems | 8 | * On most systems <limits.h> would have given us this, but not on some systems |
@@ -26,6 +27,7 @@ FS(tracefs) | |||
26 | 27 | ||
27 | int filename__read_int(const char *filename, int *value); | 28 | int filename__read_int(const char *filename, int *value); |
28 | int filename__read_ull(const char *filename, unsigned long long *value); | 29 | int filename__read_ull(const char *filename, unsigned long long *value); |
30 | int filename__read_str(const char *filename, char **buf, size_t *sizep); | ||
29 | 31 | ||
30 | int sysctl__read_int(const char *sysctl, int *value); | 32 | int sysctl__read_int(const char *sysctl, int *value); |
31 | int sysfs__read_int(const char *entry, int *value); | 33 | int sysfs__read_int(const char *entry, int *value); |