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.
17 #include <sys/types.h>
18 #ifdef HAVE_SYS_STAT_H
21 #ifdef HAVE_SYS_MKDEV_H
22 #include <sys/mkdev.h>
29 static int save_dev(blkid_dev dev, FILE *file)
33 if (!dev || dev->bid_name[0] != '/')
37 printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
40 "<device DEVNO=\"0x%04lx\" TIME=\"%lu\"",
41 (unsigned long) dev->bid_devno, dev->bid_time);
43 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
44 list_for_each(p, &dev->bid_tags) {
45 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
46 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
48 fprintf(file, ">%s</device>\n", dev->bid_name);
54 * Write out the cache struct to the cache file on disk.
56 int blkid_flush_cache(blkid_cache cache)
60 const char *opened = NULL;
67 return -BLKID_ERR_PARAM;
69 if (list_empty(&cache->bic_devs) ||
70 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
71 DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
75 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
77 /* If we can't write to the cache file, then don't even try */
78 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
79 (ret == 0 && access(filename, W_OK) < 0)) {
81 printf("can't write to cache file %s\n", filename));
86 * Try and create a temporary file in the same directory so
87 * that in case of error we don't overwrite the cache file.
88 * If the cache file doesn't yet exist, it isn't a regular
89 * file (e.g. /dev/null or a socket), or we couldn't create
90 * a temporary file then we open it directly.
92 if (ret == 0 && S_ISREG(st.st_mode)) {
93 tmp = xmalloc(strlen(filename) + 8);
94 sprintf(tmp, "%s-XXXXXX", filename);
97 file = fdopen(fd, "w");
104 file = fopen(filename, "w");
109 printf("writing cache file %s (really %s)\n",
117 list_for_each(p, &cache->bic_devs) {
118 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
121 if ((ret = save_dev(dev, file)) < 0)
126 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
131 if (opened != filename) {
135 printf("unlinked temp cache %s\n", opened));
139 backup = xmalloc(strlen(filename) + 5);
140 sprintf(backup, "%s.old", filename);
142 link(filename, backup);
144 rename(opened, filename);
146 printf("moved temp cache %s\n", opened));
157 int main(int argc, char **argv)
159 blkid_cache cache = NULL;
162 blkid_debug_mask = DEBUG_ALL;
164 fprintf(stderr, "Usage: %s [filename]\n"
165 "Test loading/saving a cache (filename)\n", argv[0]);
169 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
170 fprintf(stderr, "%s: error creating cache (%d)\n",
174 if ((ret = blkid_probe_all(cache)) < 0) {
175 fprintf(stderr, "error (%d) probing devices\n", ret);
178 cache->bic_filename = blkid_strdup(argv[1]);
180 if ((ret = blkid_flush_cache(cache)) < 0) {
181 fprintf(stderr, "error (%d) saving cache\n", ret);
185 blkid_put_cache(cache);