1 /* vi: set sw=4 ts=4: */
3 * save.c - write the cache struct to disk
5 * Copyright (C) 2001 by Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
9 * This file may be redistributed under the terms of the
10 * 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
));
41 "<device DEVNO=\"0x%04lx\" TIME=\"%lu\"",
42 (unsigned long) dev
->bid_devno
, dev
->bid_time
);
44 fprintf(file
, " PRI=\"%d\"", dev
->bid_pri
);
45 list_for_each(p
, &dev
->bid_tags
) {
46 blkid_tag tag
= list_entry(p
, struct blkid_struct_tag
, bit_tags
);
47 fprintf(file
, " %s=\"%s\"", tag
->bit_name
,tag
->bit_val
);
49 fprintf(file
, ">%s</device>\n", dev
->bid_name
);
55 * Write out the cache struct to the cache file on disk.
57 int blkid_flush_cache(blkid_cache cache
)
61 const char *opened
= NULL
;
68 return -BLKID_ERR_PARAM
;
70 if (list_empty(&cache
->bic_devs
) ||
71 !(cache
->bic_flags
& BLKID_BIC_FL_CHANGED
)) {
72 DBG(DEBUG_SAVE
, printf("skipping cache file write\n"));
76 filename
= cache
->bic_filename
? cache
->bic_filename
: BLKID_CACHE_FILE
;
78 /* If we can't write to the cache file, then don't even try */
79 if (((ret
= stat(filename
, &st
)) < 0 && errno
!= ENOENT
) ||
80 (ret
== 0 && access(filename
, W_OK
) < 0)) {
82 printf("can't write to cache file %s\n", filename
));
87 * Try and create a temporary file in the same directory so
88 * that in case of error we don't overwrite the cache file.
89 * If the cache file doesn't yet exist, it isn't a regular
90 * file (e.g. /dev/null or a socket), or we couldn't create
91 * a temporary file then we open it directly.
93 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
94 tmp
= xmalloc(strlen(filename
) + 8);
95 sprintf(tmp
, "%s-XXXXXX", filename
);
98 file
= xfdopen_for_write(fd
);
105 file
= fopen_for_write(filename
);
110 printf("writing cache file %s (really %s)\n",
118 list_for_each(p
, &cache
->bic_devs
) {
119 blkid_dev dev
= list_entry(p
, struct blkid_struct_dev
, bid_devs
);
122 if ((ret
= save_dev(dev
, file
)) < 0)
127 cache
->bic_flags
&= ~BLKID_BIC_FL_CHANGED
;
132 if (opened
!= filename
) {
136 printf("unlinked temp cache %s\n", opened
));
140 backup
= xmalloc(strlen(filename
) + 5);
141 sprintf(backup
, "%s.old", filename
);
143 link(filename
, backup
);
145 rename(opened
, filename
);
147 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
);