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>
This commit is contained in:
parent
975f14fa8f
commit
607bfbd7ff
|
@ -13,6 +13,7 @@
|
|||
#include <sys/mount.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "debug-internal.h"
|
||||
|
||||
#define _STR(x) #x
|
||||
#define STR(x) _STR(x)
|
||||
|
@ -300,6 +301,56 @@ int filename__read_ull(const char *filename, unsigned long long *value)
|
|||
return err;
|
||||
}
|
||||
|
||||
#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
|
||||
|
||||
int filename__read_str(const char *filename, char **buf, size_t *sizep)
|
||||
{
|
||||
size_t size = 0, alloc_size = 0;
|
||||
void *bf = NULL, *nbf;
|
||||
int fd, n, err = 0;
|
||||
char sbuf[STRERR_BUFSIZE];
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
do {
|
||||
if (size == alloc_size) {
|
||||
alloc_size += BUFSIZ;
|
||||
nbf = realloc(bf, alloc_size);
|
||||
if (!nbf) {
|
||||
err = -ENOMEM;
|
||||
break;
|
||||
}
|
||||
|
||||
bf = nbf;
|
||||
}
|
||||
|
||||
n = read(fd, bf + size, alloc_size - size);
|
||||
if (n < 0) {
|
||||
if (size) {
|
||||
pr_warning("read failed %d: %s\n", errno,
|
||||
strerror_r(errno, sbuf, sizeof(sbuf)));
|
||||
err = 0;
|
||||
} else
|
||||
err = -errno;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
size += n;
|
||||
} while (n > 0);
|
||||
|
||||
if (!err) {
|
||||
*sizep = size;
|
||||
*buf = bf;
|
||||
} else
|
||||
free(bf);
|
||||
|
||||
close(fd);
|
||||
return err;
|
||||
}
|
||||
|
||||
int sysfs__read_ull(const char *entry, unsigned long long *value)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define __API_FS__
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
* On most systems <limits.h> would have given us this, but not on some systems
|
||||
|
@ -26,6 +27,7 @@ FS(tracefs)
|
|||
|
||||
int filename__read_int(const char *filename, int *value);
|
||||
int filename__read_ull(const char *filename, unsigned long long *value);
|
||||
int filename__read_str(const char *filename, char **buf, size_t *sizep);
|
||||
|
||||
int sysctl__read_int(const char *sysctl, int *value);
|
||||
int sysfs__read_int(const char *entry, int *value);
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <linux/err.h>
|
||||
#include <traceevent/event-parse.h>
|
||||
#include <api/fs/tracing_path.h>
|
||||
#include <api/fs/fs.h>
|
||||
#include "trace-event.h"
|
||||
#include "machine.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -507,54 +507,6 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int filename__read_str(const char *filename, char **buf, size_t *sizep)
|
||||
{
|
||||
size_t size = 0, alloc_size = 0;
|
||||
void *bf = NULL, *nbf;
|
||||
int fd, n, err = 0;
|
||||
char sbuf[STRERR_BUFSIZE];
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
do {
|
||||
if (size == alloc_size) {
|
||||
alloc_size += BUFSIZ;
|
||||
nbf = realloc(bf, alloc_size);
|
||||
if (!nbf) {
|
||||
err = -ENOMEM;
|
||||
break;
|
||||
}
|
||||
|
||||
bf = nbf;
|
||||
}
|
||||
|
||||
n = read(fd, bf + size, alloc_size - size);
|
||||
if (n < 0) {
|
||||
if (size) {
|
||||
pr_warning("read failed %d: %s\n", errno,
|
||||
strerror_r(errno, sbuf, sizeof(sbuf)));
|
||||
err = 0;
|
||||
} else
|
||||
err = -errno;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
size += n;
|
||||
} while (n > 0);
|
||||
|
||||
if (!err) {
|
||||
*sizep = size;
|
||||
*buf = bf;
|
||||
} else
|
||||
free(bf);
|
||||
|
||||
close(fd);
|
||||
return err;
|
||||
}
|
||||
|
||||
const char *get_filename_for_perf_kvm(void)
|
||||
{
|
||||
const char *filename;
|
||||
|
|
|
@ -303,7 +303,6 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
|
|||
bool show_sym, bool unwind_inlines);
|
||||
void free_srcline(char *srcline);
|
||||
|
||||
int filename__read_str(const char *filename, char **buf, size_t *sizep);
|
||||
int perf_event_paranoid(void);
|
||||
|
||||
void mem_bswap_64(void *src, int byte_size);
|
||||
|
|
Loading…
Reference in New Issue