blob: fc90c0a16c83ffca56de5426f09376c2b77b1d83 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Simple functions to access files, paths maybe be globally prefixed by a
3 * global prefix to read data from alternative destination (e.g. /proc dump for
4 * regression tests).
5 *
6 * Taken from lscpu.c
7 *
8 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
9 * Copyright (C) 2008-2012 Karel Zak <kzak@redhat.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it would be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include <stdarg.h>
27#include <string.h>
28#include <unistd.h>
29#include <stdio.h>
30#include <inttypes.h>
31#include <errno.h>
32
33#include "all-io.h"
34#include "path.h"
35#include "nls.h"
36#include "c.h"
37
38static size_t prefixlen;
39static char pathbuf[PATH_MAX];
40
41static const char *
42path_vcreate(const char *path, va_list ap)
43{
44 if (prefixlen)
45 vsnprintf(pathbuf + prefixlen,
46 sizeof(pathbuf) - prefixlen, path, ap);
47 else
48 vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
49 return pathbuf;
50}
51
bigbiff7b4c7a62015-01-01 19:44:14 -050052char *
53path_strdup(const char *path, ...)
54{
55 const char *p;
56 va_list ap;
57
58 va_start(ap, path);
59 p = path_vcreate(path, ap);
60 va_end(ap);
61
62 return p ? strdup(p) : NULL;
63}
64
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050065static FILE *
66path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
67{
68 FILE *f;
69 const char *p = path_vcreate(path, ap);
70
71 f = fopen(p, mode);
72 if (!f && exit_on_error)
73 err(EXIT_FAILURE, _("cannot open %s"), p);
74 return f;
75}
76
77static int
78path_vopen(int flags, const char *path, va_list ap)
79{
80 int fd;
81 const char *p = path_vcreate(path, ap);
82
83 fd = open(p, flags);
84 if (fd == -1)
85 err(EXIT_FAILURE, _("cannot open %s"), p);
86 return fd;
87}
88
89FILE *
90path_fopen(const char *mode, int exit_on_error, const char *path, ...)
91{
92 FILE *fd;
93 va_list ap;
94
95 va_start(ap, path);
96 fd = path_vfopen(mode, exit_on_error, path, ap);
97 va_end(ap);
98
99 return fd;
100}
101
102void
103path_read_str(char *result, size_t len, const char *path, ...)
104{
105 FILE *fd;
106 va_list ap;
107
108 va_start(ap, path);
109 fd = path_vfopen("r", 1, path, ap);
110 va_end(ap);
111
112 if (!fgets(result, len, fd))
bigbiff7b4c7a62015-01-01 19:44:14 -0500113 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500114 fclose(fd);
115
116 len = strlen(result);
117 if (result[len - 1] == '\n')
118 result[len - 1] = '\0';
119}
120
121int
122path_read_s32(const char *path, ...)
123{
124 FILE *fd;
125 va_list ap;
126 int result;
127
128 va_start(ap, path);
129 fd = path_vfopen("r", 1, path, ap);
130 va_end(ap);
131
132 if (fscanf(fd, "%d", &result) != 1) {
133 if (ferror(fd))
bigbiff7b4c7a62015-01-01 19:44:14 -0500134 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500135 else
136 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
137 }
138 fclose(fd);
139 return result;
140}
141
142uint64_t
143path_read_u64(const char *path, ...)
144{
145 FILE *fd;
146 va_list ap;
147 uint64_t result;
148
149 va_start(ap, path);
150 fd = path_vfopen("r", 1, path, ap);
151 va_end(ap);
152
153 if (fscanf(fd, "%"SCNu64, &result) != 1) {
154 if (ferror(fd))
bigbiff7b4c7a62015-01-01 19:44:14 -0500155 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500156 else
157 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
158 }
159 fclose(fd);
160 return result;
161}
162
163int
164path_write_str(const char *str, const char *path, ...)
165{
166 int fd, result;
167 va_list ap;
168
169 va_start(ap, path);
bigbiff7b4c7a62015-01-01 19:44:14 -0500170 fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500171 va_end(ap);
172 result = write_all(fd, str, strlen(str));
173 close(fd);
174 return result;
175}
176
177int
178path_exist(const char *path, ...)
179{
180 va_list ap;
181 const char *p;
182
183 va_start(ap, path);
184 p = path_vcreate(path, ap);
185 va_end(ap);
186
187 return access(p, F_OK) == 0;
188}
189
190#ifdef HAVE_CPU_SET_T
191
192static cpu_set_t *
193path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
194{
195 FILE *fd;
196 cpu_set_t *set;
197 size_t setsize, len = maxcpus * 7;
198 char buf[len];
199
200 fd = path_vfopen("r", 1, path, ap);
201
202 if (!fgets(buf, len, fd))
bigbiff7b4c7a62015-01-01 19:44:14 -0500203 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500204 fclose(fd);
205
206 len = strlen(buf);
207 if (buf[len - 1] == '\n')
208 buf[len - 1] = '\0';
209
210 set = cpuset_alloc(maxcpus, &setsize, NULL);
211 if (!set)
212 err(EXIT_FAILURE, _("failed to callocate cpu set"));
213
214 if (islist) {
215 if (cpulist_parse(buf, set, setsize, 0))
216 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
217 } else {
218 if (cpumask_parse(buf, set, setsize))
219 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
220 }
221 return set;
222}
223
224cpu_set_t *
225path_read_cpuset(int maxcpus, const char *path, ...)
226{
227 va_list ap;
228 cpu_set_t *set;
229
230 va_start(ap, path);
231 set = path_cpuparse(maxcpus, 0, path, ap);
232 va_end(ap);
233
234 return set;
235}
236
237cpu_set_t *
238path_read_cpulist(int maxcpus, const char *path, ...)
239{
240 va_list ap;
241 cpu_set_t *set;
242
243 va_start(ap, path);
244 set = path_cpuparse(maxcpus, 1, path, ap);
245 va_end(ap);
246
247 return set;
248}
249
250#endif /* HAVE_CPU_SET_T */
251
252void
253path_set_prefix(const char *prefix)
254{
255 prefixlen = strlen(prefix);
256 strncpy(pathbuf, prefix, sizeof(pathbuf));
257 pathbuf[sizeof(pathbuf) - 1] = '\0';
258}