bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 38 | static size_t prefixlen; |
| 39 | static char pathbuf[PATH_MAX]; |
| 40 | |
| 41 | static const char * |
| 42 | path_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 | |
| 52 | static FILE * |
| 53 | path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap) |
| 54 | { |
| 55 | FILE *f; |
| 56 | const char *p = path_vcreate(path, ap); |
| 57 | |
| 58 | f = fopen(p, mode); |
| 59 | if (!f && exit_on_error) |
| 60 | err(EXIT_FAILURE, _("cannot open %s"), p); |
| 61 | return f; |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | path_vopen(int flags, const char *path, va_list ap) |
| 66 | { |
| 67 | int fd; |
| 68 | const char *p = path_vcreate(path, ap); |
| 69 | |
| 70 | fd = open(p, flags); |
| 71 | if (fd == -1) |
| 72 | err(EXIT_FAILURE, _("cannot open %s"), p); |
| 73 | return fd; |
| 74 | } |
| 75 | |
| 76 | FILE * |
| 77 | path_fopen(const char *mode, int exit_on_error, const char *path, ...) |
| 78 | { |
| 79 | FILE *fd; |
| 80 | va_list ap; |
| 81 | |
| 82 | va_start(ap, path); |
| 83 | fd = path_vfopen(mode, exit_on_error, path, ap); |
| 84 | va_end(ap); |
| 85 | |
| 86 | return fd; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | path_read_str(char *result, size_t len, const char *path, ...) |
| 91 | { |
| 92 | FILE *fd; |
| 93 | va_list ap; |
| 94 | |
| 95 | va_start(ap, path); |
| 96 | fd = path_vfopen("r", 1, path, ap); |
| 97 | va_end(ap); |
| 98 | |
| 99 | if (!fgets(result, len, fd)) |
| 100 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); |
| 101 | fclose(fd); |
| 102 | |
| 103 | len = strlen(result); |
| 104 | if (result[len - 1] == '\n') |
| 105 | result[len - 1] = '\0'; |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | path_read_s32(const char *path, ...) |
| 110 | { |
| 111 | FILE *fd; |
| 112 | va_list ap; |
| 113 | int result; |
| 114 | |
| 115 | va_start(ap, path); |
| 116 | fd = path_vfopen("r", 1, path, ap); |
| 117 | va_end(ap); |
| 118 | |
| 119 | if (fscanf(fd, "%d", &result) != 1) { |
| 120 | if (ferror(fd)) |
| 121 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); |
| 122 | else |
| 123 | errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); |
| 124 | } |
| 125 | fclose(fd); |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | uint64_t |
| 130 | path_read_u64(const char *path, ...) |
| 131 | { |
| 132 | FILE *fd; |
| 133 | va_list ap; |
| 134 | uint64_t result; |
| 135 | |
| 136 | va_start(ap, path); |
| 137 | fd = path_vfopen("r", 1, path, ap); |
| 138 | va_end(ap); |
| 139 | |
| 140 | if (fscanf(fd, "%"SCNu64, &result) != 1) { |
| 141 | if (ferror(fd)) |
| 142 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); |
| 143 | else |
| 144 | errx(EXIT_FAILURE, _("parse error: %s"), pathbuf); |
| 145 | } |
| 146 | fclose(fd); |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | int |
| 151 | path_write_str(const char *str, const char *path, ...) |
| 152 | { |
| 153 | int fd, result; |
| 154 | va_list ap; |
| 155 | |
| 156 | va_start(ap, path); |
| 157 | fd = path_vopen(O_WRONLY, path, ap); |
| 158 | va_end(ap); |
| 159 | result = write_all(fd, str, strlen(str)); |
| 160 | close(fd); |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | path_exist(const char *path, ...) |
| 166 | { |
| 167 | va_list ap; |
| 168 | const char *p; |
| 169 | |
| 170 | va_start(ap, path); |
| 171 | p = path_vcreate(path, ap); |
| 172 | va_end(ap); |
| 173 | |
| 174 | return access(p, F_OK) == 0; |
| 175 | } |
| 176 | |
| 177 | #ifdef HAVE_CPU_SET_T |
| 178 | |
| 179 | static cpu_set_t * |
| 180 | path_cpuparse(int maxcpus, int islist, const char *path, va_list ap) |
| 181 | { |
| 182 | FILE *fd; |
| 183 | cpu_set_t *set; |
| 184 | size_t setsize, len = maxcpus * 7; |
| 185 | char buf[len]; |
| 186 | |
| 187 | fd = path_vfopen("r", 1, path, ap); |
| 188 | |
| 189 | if (!fgets(buf, len, fd)) |
| 190 | err(EXIT_FAILURE, _("failed to read: %s"), pathbuf); |
| 191 | fclose(fd); |
| 192 | |
| 193 | len = strlen(buf); |
| 194 | if (buf[len - 1] == '\n') |
| 195 | buf[len - 1] = '\0'; |
| 196 | |
| 197 | set = cpuset_alloc(maxcpus, &setsize, NULL); |
| 198 | if (!set) |
| 199 | err(EXIT_FAILURE, _("failed to callocate cpu set")); |
| 200 | |
| 201 | if (islist) { |
| 202 | if (cpulist_parse(buf, set, setsize, 0)) |
| 203 | errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf); |
| 204 | } else { |
| 205 | if (cpumask_parse(buf, set, setsize)) |
| 206 | errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf); |
| 207 | } |
| 208 | return set; |
| 209 | } |
| 210 | |
| 211 | cpu_set_t * |
| 212 | path_read_cpuset(int maxcpus, const char *path, ...) |
| 213 | { |
| 214 | va_list ap; |
| 215 | cpu_set_t *set; |
| 216 | |
| 217 | va_start(ap, path); |
| 218 | set = path_cpuparse(maxcpus, 0, path, ap); |
| 219 | va_end(ap); |
| 220 | |
| 221 | return set; |
| 222 | } |
| 223 | |
| 224 | cpu_set_t * |
| 225 | path_read_cpulist(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, 1, path, ap); |
| 232 | va_end(ap); |
| 233 | |
| 234 | return set; |
| 235 | } |
| 236 | |
| 237 | #endif /* HAVE_CPU_SET_T */ |
| 238 | |
| 239 | void |
| 240 | path_set_prefix(const char *prefix) |
| 241 | { |
| 242 | prefixlen = strlen(prefix); |
| 243 | strncpy(pathbuf, prefix, sizeof(pathbuf)); |
| 244 | pathbuf[sizeof(pathbuf) - 1] = '\0'; |
| 245 | } |