2 * save.c - write the cache struct to disk
4 * Copyright (C) 2001 by Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
22 #ifdef HAVE_SYS_MKDEV_H
23 #include <sys/mkdev.h>
30 static int save_dev(blkid_dev dev, FILE *file)
34 if (!dev || dev->bid_name[0] != '/')
38 printf("device %s, type %s\n", dev->bid_name, dev->bid_type ?
39 dev->bid_type : "(null)"));
42 "<device DEVNO=\"0x%04lx\" TIME=\"%ld\"",
43 (unsigned long) dev->bid_devno, (long) dev->bid_time);
45 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
46 list_for_each(p, &dev->bid_tags) {
47 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
48 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
50 fprintf(file, ">%s</device>\n", dev->bid_name);
56 * Write out the cache struct to the cache file on disk.
58 int blkid_flush_cache(blkid_cache cache)
62 const char *opened = NULL;
69 return -BLKID_ERR_PARAM;
71 if (list_empty(&cache->bic_devs) ||
72 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
73 DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
77 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
79 /* If we can't write to the cache file, then don't even try */
80 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
81 (ret == 0 && access(filename, W_OK) < 0)) {
83 printf("can't write to cache file %s\n", filename));
88 * Try and create a temporary file in the same directory so
89 * that in case of error we don't overwrite the cache file.
90 * If the cache file doesn't yet exist, it isn't a regular
91 * file (e.g. /dev/null or a socket), or we couldn't create
92 * a temporary file then we open it directly.
94 if (ret == 0 && S_ISREG(st.st_mode)) {
95 tmp = malloc(strlen(filename) + 8);
97 mode_t save_umask = umask(022);
98 sprintf(tmp, "%s-XXXXXX", filename);
102 file = fdopen(fd, "w");
110 file = fopen(filename, "w");
115 printf("writing cache file %s (really %s)\n",
123 list_for_each(p, &cache->bic_devs) {
124 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
127 if ((ret = save_dev(dev, file)) < 0)
132 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
137 if (opened != filename) {
139 (void) unlink(opened);
141 printf("unlinked temp cache %s\n", opened));
145 backup = malloc(strlen(filename) + 5);
147 sprintf(backup, "%s.old", filename);
149 (void) link(filename, backup);
152 if (rename(opened, filename) < 0)
153 (void) unlink(opened);
155 printf("moved temp cache %s\n", opened));
165 int main(int argc, char **argv)
167 blkid_cache cache = NULL;
170 blkid_debug_mask = DEBUG_ALL;
172 fprintf(stderr, "Usage: %s [filename]\n"
173 "Test loading/saving a cache (filename)\n", argv[0]);
177 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
178 fprintf(stderr, "%s: error creating cache (%d)\n",
182 if ((ret = blkid_probe_all(cache)) < 0) {
183 fprintf(stderr, "error (%d) probing devices\n", ret);
186 cache->bic_filename = blkid_strdup(argv[1]);
188 if ((ret = blkid_flush_cache(cache)) < 0) {
189 fprintf(stderr, "error (%d) saving cache\n", ret);
193 blkid_put_cache(cache);