fileaccess.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "fileaccess.h"
  15. #include "log.h"
  16. #include "util.h"
  17. #include <limits.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <dirent.h>
  25. #define PROCFS_BASE "/proc"
  26. #define SYSFS_BASE "/sys"
  27. static void file_rewind(struct fileaccess *fa)
  28. {
  29. rewind(fa->stream);
  30. lseek(fa->fd, 0, SEEK_SET);
  31. }
  32. void file_close(struct fileaccess *fa)
  33. {
  34. if (fa) {
  35. fclose(fa->stream);
  36. close(fa->fd);
  37. free(fa);
  38. }
  39. }
  40. struct fileaccess * file_open(int flags, const char *path_fmt, ...)
  41. {
  42. char path[PATH_MAX + 1];
  43. va_list ap;
  44. int fd;
  45. FILE *stream;
  46. struct fileaccess *fa;
  47. const char *opentype;
  48. va_start(ap, path_fmt);
  49. vsnprintf(path, sizeof(path), path_fmt, ap);
  50. va_end(ap);
  51. fd = open(path, flags);
  52. if (fd < 0)
  53. goto error;
  54. if (flags == O_RDONLY)
  55. opentype = "r";
  56. else if (flags == O_WRONLY)
  57. opentype = "w";
  58. else if (flags == O_RDWR)
  59. opentype = "w+";
  60. else
  61. goto err_close;
  62. stream = fdopen(fd, opentype);
  63. if (!stream)
  64. goto err_close;
  65. fa = zalloc(sizeof(*fa));
  66. if (!fa)
  67. goto err_fclose;
  68. fa->fd = fd;
  69. fa->stream = stream;
  70. return fa;
  71. err_fclose:
  72. fclose(stream);
  73. err_close:
  74. close(fd);
  75. error:
  76. return NULL;
  77. }
  78. struct fileaccess * sysfs_file_open(int flags, const char *path_fmt, ...)
  79. {
  80. char path[PATH_MAX + 1];
  81. va_list ap;
  82. va_start(ap, path_fmt);
  83. vsnprintf(path, sizeof(path), path_fmt, ap);
  84. va_end(ap);
  85. return file_open(flags, "%s/%s", SYSFS_BASE, path);
  86. }
  87. struct fileaccess * procfs_file_open(int flags, const char *path_fmt, ...)
  88. {
  89. char path[PATH_MAX + 1];
  90. va_list ap;
  91. va_start(ap, path_fmt);
  92. vsnprintf(path, sizeof(path), path_fmt, ap);
  93. va_end(ap);
  94. return file_open(flags, "%s/%s", PROCFS_BASE, path);
  95. }
  96. int file_read_buf(struct fileaccess *fa, char *buf, size_t size)
  97. {
  98. ssize_t count;
  99. size_t pos = 0;
  100. if (!size)
  101. return 0;
  102. file_rewind(fa);
  103. while (size) {
  104. count = read(fa->fd, buf + pos, size);
  105. if (count < 0)
  106. return -errno;
  107. if (!count)
  108. break;
  109. size -= count;
  110. pos += count;
  111. }
  112. return pos;
  113. }
  114. int file_read_string(struct fileaccess *fa, char *buf, size_t size)
  115. {
  116. int count;
  117. if (!size)
  118. return 0;
  119. count = file_read_buf(fa, buf, size - 1);
  120. if (count < 0)
  121. return count;
  122. buf[count] = '\0';
  123. return count;
  124. }
  125. int file_read_int(struct fileaccess *fa, int *value, int base)
  126. {
  127. char buf[64];
  128. int count;
  129. long val;
  130. char *tail;
  131. count = file_read_buf(fa, buf, sizeof(buf) - 1);
  132. if (count < 0)
  133. return count;
  134. buf[count] = '\0';
  135. errno = 0;
  136. val = strtol(buf, &tail, base);
  137. if (errno || (*tail != '\0' && *tail != '\n'))
  138. return -ETXTBSY;
  139. *value = val;
  140. return 0;
  141. }
  142. int file_write_int(struct fileaccess *fa, int value, int base)
  143. {
  144. char buf[64];
  145. const char *buf_ptr;
  146. const char *fmt;
  147. size_t count;
  148. ssize_t res;
  149. if (base == 0 || base == 10)
  150. fmt = "%d";
  151. else if (base == 16)
  152. fmt = "0x%X";
  153. else
  154. return -EINVAL;
  155. snprintf(buf, sizeof(buf), fmt, value);
  156. file_rewind(fa);
  157. count = strlen(buf);
  158. buf_ptr = buf;
  159. while (count) {
  160. res = write(fa->fd, buf_ptr, count);
  161. if (res < 0)
  162. return -ETXTBSY;
  163. count -= (size_t)res;
  164. buf_ptr += (size_t)res;
  165. }
  166. return 0;
  167. }
  168. int file_read_bool(struct fileaccess *fa, int *value)
  169. {
  170. int val, err;
  171. err = file_read_int(fa, &val, 0);
  172. if (err)
  173. return err;
  174. if (val < 0)
  175. return -ETXTBSY;
  176. *value = !!val;
  177. return 0;
  178. }
  179. int file_write_bool(struct fileaccess *fa, int value)
  180. {
  181. return file_write_int(fa, !!value, 0);
  182. }
  183. int file_read_text_lines(struct fileaccess *fa, struct list_head *lines_list,
  184. int strip_whitespace)
  185. {
  186. char *lineptr = NULL, *str;
  187. size_t size = 0;
  188. ssize_t count;
  189. struct text_line *tl;
  190. int err;
  191. INIT_LIST_HEAD(lines_list);
  192. file_rewind(fa);
  193. while (1) {
  194. count = getline(&lineptr, &size, fa->stream);
  195. if (count <= 0)
  196. break;
  197. while (count > 0 &&
  198. (lineptr[count - 1] == '\r' ||
  199. lineptr[count - 1] == '\n')) {
  200. lineptr[count - 1] = '\0';
  201. count--;
  202. }
  203. tl = malloc(sizeof(*tl));
  204. if (!tl) {
  205. err = -ENOMEM;
  206. goto error_unwind;
  207. }
  208. if (strip_whitespace)
  209. str = string_strip(lineptr);
  210. else
  211. str = lineptr;
  212. tl->text = strdup(str);
  213. if (!tl->text) {
  214. err = -ENOMEM;
  215. free(tl);
  216. goto error_unwind;
  217. }
  218. list_add_tail(&tl->list, lines_list);
  219. }
  220. free(lineptr);
  221. return 0;
  222. error_unwind:
  223. text_lines_free(lines_list);
  224. free(lineptr);
  225. return err;
  226. }
  227. void text_line_free(struct text_line *tl)
  228. {
  229. if (tl) {
  230. list_del(&tl->list);
  231. free(tl->text);
  232. free(tl);
  233. }
  234. }
  235. void text_lines_free(struct list_head *lines_list)
  236. {
  237. struct text_line *tl, *tl_tmp;
  238. if (lines_list) {
  239. list_for_each_entry_safe(tl, tl_tmp, lines_list, list)
  240. text_line_free(tl);
  241. INIT_LIST_HEAD(lines_list);
  242. }
  243. }
  244. int list_directory(struct list_head *dir_entries, const char *path_fmt, ...)
  245. {
  246. char path[PATH_MAX + 1];
  247. va_list ap;
  248. DIR *dir;
  249. struct dirent *dirent;
  250. int err;
  251. struct dir_entry *de;
  252. int count = 0;
  253. va_start(ap, path_fmt);
  254. vsnprintf(path, sizeof(path), path_fmt, ap);
  255. va_end(ap);
  256. dir = opendir(path);
  257. if (!dir)
  258. return -errno;
  259. INIT_LIST_HEAD(dir_entries);
  260. while (1) {
  261. errno = 0;
  262. dirent = readdir(dir);
  263. if (!dirent) {
  264. err = errno;
  265. if (err)
  266. goto error_unwind;
  267. break;
  268. }
  269. if (strcmp(dirent->d_name, ".") == 0 ||
  270. strcmp(dirent->d_name, "..") == 0)
  271. continue;
  272. de = malloc(sizeof(*de));
  273. if (!de) {
  274. err = -ENOMEM;
  275. goto error_unwind;
  276. }
  277. de->name = strdup(dirent->d_name);
  278. if (!de->name) {
  279. err = -ENOMEM;
  280. free(de);
  281. goto error_unwind;
  282. }
  283. de->type = dirent->d_type;
  284. list_add_tail(&de->list, dir_entries);
  285. count++;
  286. }
  287. closedir(dir);
  288. return count;
  289. error_unwind:
  290. dir_entries_free(dir_entries);
  291. closedir(dir);
  292. return err;
  293. }
  294. int list_sysfs_directory(struct list_head *dir_entries, const char *path_fmt, ...)
  295. {
  296. char path[PATH_MAX + 1];
  297. va_list ap;
  298. va_start(ap, path_fmt);
  299. vsnprintf(path, sizeof(path), path_fmt, ap);
  300. va_end(ap);
  301. return list_directory(dir_entries, "%s/%s", SYSFS_BASE, path);
  302. }
  303. int list_procfs_directory(struct list_head *dir_entries, const char *path_fmt, ...)
  304. {
  305. char path[PATH_MAX + 1];
  306. va_list ap;
  307. va_start(ap, path_fmt);
  308. vsnprintf(path, sizeof(path), path_fmt, ap);
  309. va_end(ap);
  310. return list_directory(dir_entries, "%s/%s", PROCFS_BASE, path);
  311. }
  312. void dir_entry_free(struct dir_entry *dir_entry)
  313. {
  314. if (dir_entry) {
  315. list_del(&dir_entry->list);
  316. free(dir_entry->name);
  317. free(dir_entry);
  318. }
  319. }
  320. void dir_entries_free(struct list_head *dir_entries)
  321. {
  322. struct dir_entry *d, *d_tmp;
  323. if (dir_entries) {
  324. list_for_each_entry_safe(d, d_tmp, dir_entries, list)
  325. dir_entry_free(d);
  326. INIT_LIST_HEAD(dir_entries);
  327. }
  328. }