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 : 752 : spdk_posix_file_load(FILE *file, size_t *size)
11 : : {
12 : 752 : uint8_t *newbuf, *buf = NULL;
13 : 752 : size_t rc, buf_size, cur_size = 0;
14 : :
15 : 752 : *size = 0;
16 : 752 : buf_size = 128 * 1024;
17 : :
18 [ + - ]: 752 : while (buf_size <= 1024 * 1024 * 1024) {
19 : 752 : newbuf = realloc(buf, buf_size);
20 [ - + ]: 752 : if (newbuf == NULL) {
21 : 0 : free(buf);
22 : 0 : return NULL;
23 : : }
24 : 752 : buf = newbuf;
25 : :
26 [ - + ]: 752 : rc = fread(buf + cur_size, 1, buf_size - cur_size, file);
27 : 752 : cur_size += rc;
28 : :
29 [ + + + - ]: 752 : if (feof(file)) {
30 : 752 : *size = cur_size;
31 : 752 : 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 : 753 : spdk_posix_file_load_from_name(const char *file_name, size_t *size)
48 : : {
49 : 753 : FILE *file = fopen(file_name, "r");
50 : : void *data;
51 : :
52 [ + + ]: 753 : if (file == NULL) {
53 : 1 : return NULL;
54 : : }
55 : :
56 : 752 : data = spdk_posix_file_load(file, size);
57 [ - + ]: 752 : fclose(file);
58 : :
59 : 752 : return data;
60 : : }
61 : :
62 : : static int
63 : 5 : read_sysfs_attribute(char **attribute_p, const char *format, va_list args)
64 : : {
65 : : char *attribute;
66 : : FILE *file;
67 : : char *path;
68 : 5 : size_t len = 0;
69 : : ssize_t read;
70 : : int errsv;
71 : :
72 : 5 : path = spdk_vsprintf_alloc(format, args);
73 [ - + ]: 5 : if (path == NULL) {
74 : 0 : return -ENOMEM;
75 : : }
76 : :
77 : 5 : file = fopen(path, "r");
78 : 5 : errsv = errno;
79 : 5 : free(path);
80 [ - + ]: 5 : if (file == NULL) {
81 [ # # ]: 0 : assert(errsv != 0);
82 : 0 : return -errsv;
83 : : }
84 : :
85 : 5 : *attribute_p = NULL;
86 [ # # ]: 5 : read = getline(attribute_p, &len, file);
87 : 5 : errsv = errno;
88 [ - + ]: 5 : fclose(file);
89 : 5 : attribute = *attribute_p;
90 [ - + ]: 5 : if (read == -1) {
91 : : /* getline man page says line should be freed even on failure. */
92 : 0 : free(attribute);
93 [ # # ]: 0 : assert(errsv != 0);
94 : 0 : return -errsv;
95 : : }
96 : :
97 : : /* len is the length of the allocated buffer, which may be more than
98 : : * the string's length. Reuse len to hold the actual strlen.
99 : : */
100 [ - + ]: 5 : len = strlen(attribute);
101 [ + - ]: 5 : if (attribute[len - 1] == '\n') {
102 : 5 : attribute[len - 1] = '\0';
103 : : }
104 : :
105 : 5 : return 0;
106 : : }
107 : :
108 : : int
109 : 0 : spdk_read_sysfs_attribute(char **attribute_p, const char *path_format, ...)
110 : : {
111 : 0 : va_list args;
112 : : int rc;
113 : :
114 : 0 : va_start(args, path_format);
115 : 0 : rc = read_sysfs_attribute(attribute_p, path_format, args);
116 : 0 : va_end(args);
117 : :
118 : 0 : return rc;
119 : : }
120 : :
121 : : int
122 : 5 : spdk_read_sysfs_attribute_uint32(uint32_t *attribute, const char *path_format, ...)
123 : : {
124 : 5 : char *attribute_str = NULL;
125 : : long long int val;
126 : 5 : va_list args;
127 : : int rc;
128 : :
129 : 5 : va_start(args, path_format);
130 : 5 : rc = read_sysfs_attribute(&attribute_str, path_format, args);
131 : 5 : va_end(args);
132 : :
133 [ - + ]: 5 : if (rc != 0) {
134 : 0 : return rc;
135 : : }
136 : :
137 : 5 : val = spdk_strtoll(attribute_str, 0);
138 : 5 : free(attribute_str);
139 [ + - - + ]: 5 : if (val < 0 || val > UINT32_MAX) {
140 : 0 : return -EINVAL;
141 : : }
142 : :
143 : 5 : *attribute = (uint32_t)val;
144 : 5 : return 0;
145 : : }
|