Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2015 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/file.h"
7 : : #include "spdk/string.h"
8 : :
9 : : void *
10 : 1097 : spdk_posix_file_load(FILE *file, size_t *size)
11 : : {
12 : 1097 : uint8_t *newbuf, *buf = NULL;
13 : 1097 : size_t rc, buf_size, cur_size = 0;
14 : :
15 : 1097 : *size = 0;
16 : 1097 : buf_size = 128 * 1024;
17 : :
18 [ + - ]: 1097 : while (buf_size <= 1024 * 1024 * 1024) {
19 : 1097 : newbuf = realloc(buf, buf_size);
20 [ - + ]: 1097 : if (newbuf == NULL) {
21 : 0 : free(buf);
22 : 0 : return NULL;
23 : : }
24 : 1097 : buf = newbuf;
25 : :
26 : 1097 : rc = fread(buf + cur_size, 1, buf_size - cur_size, file);
27 : 1097 : cur_size += rc;
28 : :
29 [ + - ]: 1097 : if (feof(file)) {
30 : 1097 : *size = cur_size;
31 : 1097 : return buf;
32 : : }
33 : :
34 [ # # ]: 0 : if (ferror(file)) {
35 : 0 : free(buf);
36 : 0 : return NULL;
37 : : }
38 : :
39 : 0 : buf_size *= 2;
40 : : }
41 : :
42 : 0 : free(buf);
43 : 0 : return NULL;
44 : : }
45 : :
46 : : void *
47 : 1098 : spdk_posix_file_load_from_name(const char *file_name, size_t *size)
48 : : {
49 : 1098 : FILE *file = fopen(file_name, "r");
50 : : void *data;
51 : :
52 [ + + ]: 1098 : if (file == NULL) {
53 : 1 : return NULL;
54 : : }
55 : :
56 : 1097 : data = spdk_posix_file_load(file, size);
57 : 1097 : fclose(file);
58 : :
59 : 1097 : return data;
60 : : }
61 : :
62 : : static int
63 : 1192 : read_sysfs_attribute(char **attribute_p, const char *format, va_list args)
64 : : {
65 : : char *attribute;
66 : : FILE *file;
67 : : char *path;
68 : 1192 : size_t len = 0;
69 : : ssize_t read;
70 : :
71 : 1192 : path = spdk_vsprintf_alloc(format, args);
72 [ - + ]: 1192 : if (path == NULL) {
73 : 0 : return -ENOMEM;
74 : : }
75 : :
76 : 1192 : file = fopen(path, "r");
77 : 1192 : free(path);
78 [ + + ]: 1192 : if (file == NULL) {
79 : 768 : return -errno;
80 : : }
81 : :
82 : 424 : *attribute_p = NULL;
83 : 424 : read = getline(attribute_p, &len, file);
84 : 424 : fclose(file);
85 : 424 : attribute = *attribute_p;
86 [ - + ]: 424 : if (read == -1) {
87 : : /* getline man page says line should be freed even on failure. */
88 : 0 : free(attribute);
89 : 0 : return -errno;
90 : : }
91 : :
92 : : /* len is the length of the allocated buffer, which may be more than
93 : : * the string's length. Reuse len to hold the actual strlen.
94 : : */
95 [ - + ]: 424 : len = strlen(attribute);
96 [ + - ]: 424 : if (attribute[len - 1] == '\n') {
97 : 424 : attribute[len - 1] = '\0';
98 : : }
99 : :
100 : 424 : return 0;
101 : : }
102 : :
103 : : int
104 : 0 : spdk_read_sysfs_attribute(char **attribute_p, const char *path_format, ...)
105 : : {
106 : 0 : va_list args;
107 : : int rc;
108 : :
109 : 0 : va_start(args, path_format);
110 : 0 : rc = read_sysfs_attribute(attribute_p, path_format, args);
111 : 0 : va_end(args);
112 : :
113 : 0 : return rc;
114 : : }
115 : :
116 : : int
117 : 1192 : spdk_read_sysfs_attribute_uint32(uint32_t *attribute, const char *path_format, ...)
118 : : {
119 : 1192 : char *attribute_str = NULL;
120 : : long long int val;
121 : 48 : va_list args;
122 : : int rc;
123 : :
124 : 1192 : va_start(args, path_format);
125 : 1192 : rc = read_sysfs_attribute(&attribute_str, path_format, args);
126 : 1192 : va_end(args);
127 : :
128 [ + + ]: 1192 : if (rc != 0) {
129 : 768 : return rc;
130 : : }
131 : :
132 : 424 : val = spdk_strtoll(attribute_str, 0);
133 : 424 : free(attribute_str);
134 [ + - - + ]: 424 : if (val < 0 || val > UINT32_MAX) {
135 : 0 : return -EINVAL;
136 : : }
137 : :
138 : 424 : *attribute = (uint32_t)val;
139 : 424 : return 0;
140 : : }
|