save.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * save.c - write the cache struct to disk
  4. *
  5. * Copyright (C) 2001 by Andreas Dilger
  6. * Copyright (C) 2003 Theodore Ts'o
  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 <string.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #ifdef HAVE_SYS_MKDEV_H
  22. #include <sys/mkdev.h>
  23. #endif
  24. #ifdef HAVE_ERRNO_H
  25. #include <errno.h>
  26. #endif
  27. #include "blkidP.h"
  28. static int save_dev(blkid_dev dev, FILE *file)
  29. {
  30. struct list_head *p;
  31. if (!dev || dev->bid_name[0] != '/')
  32. return 0;
  33. DBG(DEBUG_SAVE,
  34. printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
  35. fprintf(file,
  36. "<device DEVNO=\"0x%04lx\" TIME=\"%lu\"",
  37. (unsigned long) dev->bid_devno, dev->bid_time);
  38. if (dev->bid_pri)
  39. fprintf(file, " PRI=\"%d\"", dev->bid_pri);
  40. list_for_each(p, &dev->bid_tags) {
  41. blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
  42. fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
  43. }
  44. fprintf(file, ">%s</device>\n", dev->bid_name);
  45. return 0;
  46. }
  47. /*
  48. * Write out the cache struct to the cache file on disk.
  49. */
  50. int blkid_flush_cache(blkid_cache cache)
  51. {
  52. struct list_head *p;
  53. char *tmp = NULL;
  54. const char *opened = NULL;
  55. const char *filename;
  56. FILE *file = NULL;
  57. int fd, ret = 0;
  58. struct stat st;
  59. if (!cache)
  60. return -BLKID_ERR_PARAM;
  61. if (list_empty(&cache->bic_devs) ||
  62. !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
  63. DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
  64. return 0;
  65. }
  66. filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
  67. /* If we can't write to the cache file, then don't even try */
  68. if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
  69. (ret == 0 && access(filename, W_OK) < 0)) {
  70. DBG(DEBUG_SAVE,
  71. printf("can't write to cache file %s\n", filename));
  72. return 0;
  73. }
  74. /*
  75. * Try and create a temporary file in the same directory so
  76. * that in case of error we don't overwrite the cache file.
  77. * If the cache file doesn't yet exist, it isn't a regular
  78. * file (e.g. /dev/null or a socket), or we couldn't create
  79. * a temporary file then we open it directly.
  80. */
  81. if (ret == 0 && S_ISREG(st.st_mode)) {
  82. tmp = xmalloc(strlen(filename) + 8);
  83. sprintf(tmp, "%s-XXXXXX", filename);
  84. fd = mkstemp(tmp);
  85. if (fd >= 0) {
  86. file = fdopen(fd, "w");
  87. opened = tmp;
  88. }
  89. fchmod(fd, 0644);
  90. }
  91. if (!file) {
  92. file = fopen(filename, "w");
  93. opened = filename;
  94. }
  95. DBG(DEBUG_SAVE,
  96. printf("writing cache file %s (really %s)\n",
  97. filename, opened));
  98. if (!file) {
  99. ret = errno;
  100. goto errout;
  101. }
  102. list_for_each(p, &cache->bic_devs) {
  103. blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
  104. if (!dev->bid_type)
  105. continue;
  106. if ((ret = save_dev(dev, file)) < 0)
  107. break;
  108. }
  109. if (ret >= 0) {
  110. cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
  111. ret = 1;
  112. }
  113. fclose(file);
  114. if (opened != filename) {
  115. if (ret < 0) {
  116. unlink(opened);
  117. DBG(DEBUG_SAVE,
  118. printf("unlinked temp cache %s\n", opened));
  119. } else {
  120. char *backup;
  121. backup = xmalloc(strlen(filename) + 5);
  122. sprintf(backup, "%s.old", filename);
  123. unlink(backup);
  124. link(filename, backup);
  125. free(backup);
  126. rename(opened, filename);
  127. DBG(DEBUG_SAVE,
  128. printf("moved temp cache %s\n", opened));
  129. }
  130. }
  131. errout:
  132. free(tmp);
  133. return ret;
  134. }
  135. #ifdef TEST_PROGRAM
  136. int main(int argc, char **argv)
  137. {
  138. blkid_cache cache = NULL;
  139. int ret;
  140. blkid_debug_mask = DEBUG_ALL;
  141. if (argc > 2) {
  142. fprintf(stderr, "Usage: %s [filename]\n"
  143. "Test loading/saving a cache (filename)\n", argv[0]);
  144. exit(1);
  145. }
  146. if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
  147. fprintf(stderr, "%s: error creating cache (%d)\n",
  148. argv[0], ret);
  149. exit(1);
  150. }
  151. if ((ret = blkid_probe_all(cache)) < 0) {
  152. fprintf(stderr, "error (%d) probing devices\n", ret);
  153. exit(1);
  154. }
  155. cache->bic_filename = blkid_strdup(argv[1]);
  156. if ((ret = blkid_flush_cache(cache)) < 0) {
  157. fprintf(stderr, "error (%d) saving cache\n", ret);
  158. exit(1);
  159. }
  160. blkid_put_cache(cache);
  161. return ret;
  162. }
  163. #endif