save.c 4.1 KB

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