2 * save.c - write the cache struct to disk
4 * Copyright (C) 2001 by Andreas Dilger
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
20 #ifdef HAVE_SYS_MKDEV_H
21 #include <sys/mkdev.h>
26 #include "blkid/blkid.h"
29 #define DEB_SAVE(fmt, arg...) printf("save: " fmt, ## arg)
31 #define DEB_SAVE(fmt, arg...) do {} while (0)
34 static int save_dev(blkid_dev *dev, FILE *file)
41 DEB_SAVE("device %s, type %s\n", dev->bid_name, dev->bid_type);
44 "<device TYPE=\"%s\" DEVNO=\"0x%04Lx\" ID=\"%d\" TIME=\"%Ld\"",
45 dev->bid_type, dev->bid_devno,
46 dev->bid_id, (long long)dev->bid_time);
47 list_for_each(p, &dev->bid_tags) {
48 blkid_tag *tag = list_entry(p, blkid_tag, bit_tags);
49 if (strcmp(tag->bit_name, "TYPE"))
50 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
52 fprintf(file, ">%s</device>\n", dev->bid_name);
57 int blkid_save_cache_file(blkid_cache *cache, FILE *file)
63 return -BLKID_ERR_PARAM;
65 if (list_empty(&cache->bic_devs) ||
66 !cache->bic_flags & BLKID_BIC_FL_CHANGED)
69 list_for_each(p, &cache->bic_devs) {
70 blkid_dev *dev = list_entry(p, blkid_dev, bid_devs);
71 if ((ret = save_dev(dev, file)) < 0)
76 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
84 * Write out the cache struct to the cache file on disk.
86 int blkid_save_cache(blkid_cache *cache, char *filename)
88 char tmp[4096] = { '\0', };
94 return -BLKID_ERR_PARAM;
96 if (list_empty(&cache->bic_devs) ||
97 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
98 DEB_SAVE("empty cache, not saving\n");
102 if (!filename || !strlen(filename))
103 filename = BLKID_CACHE_FILE;
105 if (!strcmp(filename, "-") || !strcmp(filename, "stdout"))
110 /* If we can't write to the cache file, then don't even try */
111 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
112 (ret == 0 && access(filename, W_OK) < 0)) {
113 DEB_SAVE("can't write to cache file %s\n", filename);
118 * Try and create a temporary file in the same directory so
119 * that in case of error we don't overwrite the cache file.
120 * If the cache file doesn't yet exist, it isn't a regular
121 * file (e.g. /dev/null or a socket), or we couldn't create
122 * a temporary file then we open it directly.
124 if (ret == 0 && S_ISREG(st.st_mode)) {
125 snprintf(tmp, sizeof(tmp) - 1, "%s-XXXXXX", filename);
126 if (mktemp(tmp) == tmp && *tmp != '\0') {
127 file = fopen(tmp, "w");
133 file = fopen(filename, "w");
137 DEB_SAVE("cache file %s (really %s)\n", filename, opened);
145 ret = blkid_save_cache_file(cache, file);
147 if (file != stdout) {
149 if (opened != filename) {
152 DEB_SAVE("unlinked temp cache %s\n", opened);
156 snprintf(backup, sizeof(backup) - 1, "%s.old",
159 link(filename, backup);
160 rename(opened, filename);
161 DEB_SAVE("moved temp cache %s\n", opened);
170 int main(int argc, char **argv)
172 blkid_cache *cache = NULL;
176 fprintf(stderr, "Usage: %s [filename]\n"
177 "Test loading/saving a cache (filename)\n", argv[0]);
180 if ((ret = blkid_probe_all(&cache) < 0))
181 fprintf(stderr, "error probing devices\n");
182 else if ((ret = blkid_save_cache(cache, argv[1])) < 0)
183 fprintf(stderr, "error %d saving cache to %s\n", ret,
184 argv[1] ? argv[1] : BLKID_CACHE_FILE);
186 blkid_free_cache(cache);