read.c 9.5 KB

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