tools api fs: Avoid reading whole file for a 1 byte bool
authorIan Rogers <irogers@google.com>
Mon, 27 Nov 2023 22:08:17 +0000 (14:08 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 30 Nov 2023 22:25:19 +0000 (19:25 -0300)
sysfs__read_bool() used the first byte from a fully read file into a
string. It then looked at the first byte's value. Avoid doing this and
just read the first byte.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Guilherme Amadio <amadio@gentoo.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Li Dong <lidong@vivo.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20231127220902.1315692-6-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/api/fs/fs.c

index 004f2af5504b6dda602c276eeacdba0eb619d5b0..337fde770e45fe031c8fbb399529bb385d15ac76 100644 (file)
@@ -447,15 +447,22 @@ int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
 
 int sysfs__read_bool(const char *entry, bool *value)
 {
-       char *buf;
-       size_t size;
-       int ret;
+       struct io io;
+       char bf[16];
+       int ret = 0;
+       char path[PATH_MAX];
+       const char *sysfs = sysfs__mountpoint();
+
+       if (!sysfs)
+               return -1;
 
-       ret = sysfs__read_str(entry, &buf, &size);
-       if (ret < 0)
-               return ret;
+       snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+       io.fd = open(path, O_RDONLY);
+       if (io.fd < 0)
+               return -errno;
 
-       switch (buf[0]) {
+       io__init(&io, io.fd, bf, sizeof(bf));
+       switch (io__get_char(&io)) {
        case '1':
        case 'y':
        case 'Y':
@@ -469,8 +476,7 @@ int sysfs__read_bool(const char *entry, bool *value)
        default:
                ret = -1;
        }
-
-       free(buf);
+       close(io.fd);
 
        return ret;
 }