read.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * read.c - read the blkid cache from disk, to avoid scanning all devices
  3. *
  4. * Copyright (C) 2001, 2003 Theodore Y. Ts'o
  5. * Copyright (C) 2001 Andreas Dilger
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the
  9. * GNU Lesser General Public License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #if HAVE_ERRNO_H
  21. #include <errno.h>
  22. #endif
  23. #include "blkidP.h"
  24. #include "uuid/uuid.h"
  25. #ifdef HAVE_STRTOULL
  26. #define __USE_ISOC9X
  27. #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
  28. #else
  29. /* FIXME: need to support real strtoull here */
  30. #define STRTOULL strtoul
  31. #endif
  32. #if HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. /*
  36. * File format:
  37. *
  38. * <device [<NAME="value"> ...]>device_name</device>
  39. *
  40. * The following tags are required for each entry:
  41. * <ID="id"> unique (within this file) ID number of this device
  42. * <TIME="time"> (ascii time_t) time this entry was last read from disk
  43. * <TYPE="type"> (detected) type of filesystem/data for this partition
  44. *
  45. * The following tags may be present, depending on the device contents
  46. * <LABEL="label"> (user supplied) label (volume name, etc)
  47. * <UUID="uuid"> (generated) universally unique identifier (serial no)
  48. */
  49. static char *skip_over_blank(char *cp)
  50. {
  51. while (*cp && isspace(*cp))
  52. cp++;
  53. return cp;
  54. }
  55. static char *skip_over_word(char *cp)
  56. {
  57. char ch;
  58. while ((ch = *cp)) {
  59. /* If we see a backslash, skip the next character */
  60. if (ch == '\\') {
  61. cp++;
  62. if (*cp == '\0')
  63. break;
  64. cp++;
  65. continue;
  66. }
  67. if (isspace(ch) || ch == '<' || ch == '>')
  68. break;
  69. cp++;
  70. }
  71. return cp;
  72. }
  73. static char *strip_line(char *line)
  74. {
  75. char *p;
  76. line = skip_over_blank(line);
  77. p = line + strlen(line) - 1;
  78. while (*line) {
  79. if (isspace(*p))
  80. *p-- = '\0';
  81. else
  82. break;
  83. }
  84. return line;
  85. }
  86. #if 0
  87. static char *parse_word(char **buf)
  88. {
  89. char *word, *next;
  90. word = *buf;
  91. if (*word == '\0')
  92. return NULL;
  93. word = skip_over_blank(word);
  94. next = skip_over_word(word);
  95. if (*next) {
  96. char *end = next - 1;
  97. if (*end == '"' || *end == '\'')
  98. *end = '\0';
  99. *next++ = '\0';
  100. }
  101. *buf = next;
  102. if (*word == '"' || *word == '\'')
  103. word++;
  104. return word;
  105. }
  106. #endif
  107. /*
  108. * Start parsing a new line from the cache.
  109. *
  110. * line starts with "<device" return 1 -> continue parsing line
  111. * line starts with "<foo", empty, or # return 0 -> skip line
  112. * line starts with other, return -BLKID_ERR_CACHE -> error
  113. */
  114. static int parse_start(char **cp)
  115. {
  116. char *p;
  117. p = strip_line(*cp);
  118. /* Skip comment or blank lines. We can't just NUL the first '#' char,
  119. * in case it is inside quotes, or escaped.
  120. */
  121. if (*p == '\0' || *p == '#')
  122. return 0;
  123. if (!strncmp(p, "<device", 7)) {
  124. DBG(DEBUG_READ, printf("found device header: %8s\n", p));
  125. p += 7;
  126. *cp = p;
  127. return 1;
  128. }
  129. if (*p == '<')
  130. return 0;
  131. return -BLKID_ERR_CACHE;
  132. }
  133. /* Consume the remaining XML on the line (cosmetic only) */
  134. static int parse_end(char **cp)
  135. {
  136. *cp = skip_over_blank(*cp);
  137. if (!strncmp(*cp, "</device>", 9)) {
  138. DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
  139. *cp += 9;
  140. return 0;
  141. }
  142. return -BLKID_ERR_CACHE;
  143. }
  144. /*
  145. * Allocate a new device struct with device name filled in. Will handle
  146. * finding the device on lines of the form:
  147. * <device foo=bar>devname</device>
  148. * <device>devname<foo>bar</foo></device>
  149. */
  150. static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
  151. {
  152. char *start, *tmp, *end, *name;
  153. int ret;
  154. if ((ret = parse_start(cp)) <= 0)
  155. return ret;
  156. start = tmp = strchr(*cp, '>');
  157. if (!start) {
  158. DBG(DEBUG_READ,
  159. printf("blkid: short line parsing dev: %s\n", *cp));
  160. return -BLKID_ERR_CACHE;
  161. }
  162. start = skip_over_blank(start + 1);
  163. end = skip_over_word(start);
  164. DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
  165. if (**cp == '>')
  166. *cp = end;
  167. else
  168. (*cp)++;
  169. *tmp = '\0';
  170. if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
  171. DBG(DEBUG_READ,
  172. printf("blkid: missing </device> ending: %s\n", end));
  173. } else if (tmp)
  174. *tmp = '\0';
  175. if (end - start <= 1) {
  176. DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
  177. return -BLKID_ERR_CACHE;
  178. }
  179. name = blkid_strndup(start, end-start);
  180. if (name == NULL)
  181. return -BLKID_ERR_MEM;
  182. DBG(DEBUG_READ, printf("found dev %s\n", name));
  183. if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
  184. return -BLKID_ERR_MEM;
  185. free(name);
  186. return 1;
  187. }
  188. /*
  189. * Extract a tag of the form NAME="value" from the line.
  190. */
  191. static int parse_token(char **name, char **value, char **cp)
  192. {
  193. char *end;
  194. if (!name || !value || !cp)
  195. return -BLKID_ERR_PARAM;
  196. if (!(*value = strchr(*cp, '=')))
  197. return 0;
  198. **value = '\0';
  199. *name = strip_line(*cp);
  200. *value = skip_over_blank(*value + 1);
  201. if (**value == '"') {
  202. end = strchr(*value + 1, '"');
  203. if (!end) {
  204. DBG(DEBUG_READ,
  205. printf("unbalanced quotes at: %s\n", *value));
  206. *cp = *value;
  207. return -BLKID_ERR_CACHE;
  208. }
  209. (*value)++;
  210. *end = '\0';
  211. end++;
  212. } else {
  213. end = skip_over_word(*value);
  214. if (*end) {
  215. *end = '\0';
  216. end++;
  217. }
  218. }
  219. *cp = end;
  220. return 1;
  221. }
  222. /*
  223. * Extract a tag of the form <NAME>value</NAME> from the line.
  224. */
  225. /*
  226. static int parse_xml(char **name, char **value, char **cp)
  227. {
  228. char *end;
  229. if (!name || !value || !cp)
  230. return -BLKID_ERR_PARAM;
  231. *name = strip_line(*cp);
  232. if ((*name)[0] != '<' || (*name)[1] == '/')
  233. return 0;
  234. FIXME: finish this.
  235. }
  236. */
  237. /*
  238. * Extract a tag from the line.
  239. *
  240. * Return 1 if a valid tag was found.
  241. * Return 0 if no tag found.
  242. * Return -ve error code.
  243. */
  244. static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
  245. {
  246. char *name;
  247. char *value;
  248. int ret;
  249. if (!cache || !dev)
  250. return -BLKID_ERR_PARAM;
  251. if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
  252. (ret = parse_xml(&name, &value, cp)) <= 0 */)
  253. return ret;
  254. /* Some tags are stored directly in the device struct */
  255. if (!strcmp(name, "DEVNO"))
  256. dev->bid_devno = STRTOULL(value, 0, 0);
  257. else if (!strcmp(name, "PRI"))
  258. dev->bid_pri = strtol(value, 0, 0);
  259. else if (!strcmp(name, "TIME"))
  260. /* FIXME: need to parse a long long eventually */
  261. dev->bid_time = strtol(value, 0, 0);
  262. else
  263. ret = blkid_set_tag(dev, name, value, strlen(value));
  264. DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
  265. return ret < 0 ? ret : 1;
  266. }
  267. /*
  268. * Parse a single line of data, and return a newly allocated dev struct.
  269. * Add the new device to the cache struct, if one was read.
  270. *
  271. * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
  272. *
  273. * Returns -ve value on error.
  274. * Returns 0 otherwise.
  275. * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
  276. * (e.g. comment lines, unknown XML content, etc).
  277. */
  278. static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
  279. {
  280. blkid_dev dev;
  281. int ret;
  282. if (!cache || !dev_p)
  283. return -BLKID_ERR_PARAM;
  284. *dev_p = NULL;
  285. DBG(DEBUG_READ, printf("line: %s\n", cp));
  286. if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
  287. return ret;
  288. dev = *dev_p;
  289. while ((ret = parse_tag(cache, dev, &cp)) > 0) {
  290. ;
  291. }
  292. if (dev->bid_type == NULL) {
  293. DBG(DEBUG_READ,
  294. printf("blkid: device %s has no TYPE\n",dev->bid_name));
  295. blkid_free_dev(dev);
  296. }
  297. DEB_DUMP_DEV(DEBUG_READ, dev);
  298. return ret;
  299. }
  300. /*
  301. * Parse the specified filename, and return the data in the supplied or
  302. * a newly allocated cache struct. If the file doesn't exist, return a
  303. * new empty cache struct.
  304. */
  305. void blkid_read_cache(blkid_cache cache)
  306. {
  307. FILE *file;
  308. char buf[4096];
  309. int fd, lineno = 0;
  310. struct stat st;
  311. if (!cache)
  312. return;
  313. /*
  314. * If the file doesn't exist, then we just return an empty
  315. * struct so that the cache can be populated.
  316. */
  317. if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
  318. return;
  319. if (fstat(fd, &st) < 0)
  320. goto errout;
  321. if ((st.st_mtime == cache->bic_ftime) ||
  322. (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
  323. DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
  324. cache->bic_filename));
  325. goto errout;
  326. }
  327. DBG(DEBUG_CACHE, printf("reading cache file %s\n",
  328. cache->bic_filename));
  329. file = fdopen(fd, "r");
  330. if (!file)
  331. goto errout;
  332. while (fgets(buf, sizeof(buf), file)) {
  333. blkid_dev dev;
  334. unsigned int end;
  335. lineno++;
  336. if (buf[0] == 0)
  337. continue;
  338. end = strlen(buf) - 1;
  339. /* Continue reading next line if it ends with a backslash */
  340. while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
  341. fgets(buf + end, sizeof(buf) - end, file)) {
  342. end = strlen(buf) - 1;
  343. lineno++;
  344. }
  345. if (blkid_parse_line(cache, &dev, buf) < 0) {
  346. DBG(DEBUG_READ,
  347. printf("blkid: bad format on line %d\n", lineno));
  348. continue;
  349. }
  350. }
  351. fclose(file);
  352. /*
  353. * Initially we do not need to write out the cache file.
  354. */
  355. cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
  356. cache->bic_ftime = st.st_mtime;
  357. return;
  358. errout:
  359. close(fd);
  360. return;
  361. }
  362. #ifdef TEST_PROGRAM
  363. int main(int argc, char**argv)
  364. {
  365. blkid_cache cache = NULL;
  366. int ret;
  367. blkid_debug_mask = DEBUG_ALL;
  368. if (argc > 2) {
  369. fprintf(stderr, "Usage: %s [filename]\n"
  370. "Test parsing of the cache (filename)\n", argv[0]);
  371. exit(1);
  372. }
  373. if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
  374. fprintf(stderr, "error %d reading cache file %s\n", ret,
  375. argv[1] ? argv[1] : BLKID_CACHE_FILE);
  376. blkid_put_cache(cache);
  377. return ret;
  378. }
  379. #endif