2003-01-24 00:45:16 +03:00
|
|
|
/*
|
|
|
|
* save.c - write the cache struct to disk
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 by Andreas Dilger
|
2003-02-13 07:51:21 +03:00
|
|
|
* Copyright (C) 2003 Theodore Ts'o
|
2003-01-24 00:45:16 +03:00
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the
|
|
|
|
* GNU Lesser General Public License.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_MKDEV_H
|
|
|
|
#include <sys/mkdev.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2003-01-26 09:54:39 +03:00
|
|
|
#include "blkidP.h"
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-01-26 09:54:39 +03:00
|
|
|
static int save_dev(blkid_dev dev, FILE *file)
|
2003-01-24 00:45:16 +03:00
|
|
|
{
|
|
|
|
struct list_head *p;
|
|
|
|
|
2005-01-06 01:45:32 +03:00
|
|
|
if (!dev || dev->bid_name[0] != '/')
|
2003-01-24 00:45:16 +03:00
|
|
|
return 0;
|
|
|
|
|
2003-02-22 21:19:53 +03:00
|
|
|
DBG(DEBUG_SAVE,
|
|
|
|
printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
|
2003-01-24 00:45:16 +03:00
|
|
|
|
|
|
|
fprintf(file,
|
2006-03-13 07:25:15 +03:00
|
|
|
"<device DEVNO=\"0x%04lx\" TIME=\"%ld\"",
|
2006-05-30 03:20:46 +04:00
|
|
|
(unsigned long) dev->bid_devno, (long) dev->bid_time);
|
2003-02-14 09:31:45 +03:00
|
|
|
if (dev->bid_pri)
|
|
|
|
fprintf(file, " PRI=\"%d\"", dev->bid_pri);
|
2003-01-24 00:45:16 +03:00
|
|
|
list_for_each(p, &dev->bid_tags) {
|
2003-01-26 09:54:39 +03:00
|
|
|
blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
|
2003-02-23 01:15:20 +03:00
|
|
|
fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
|
|
|
fprintf(file, ">%s</device>\n", dev->bid_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the cache struct to the cache file on disk.
|
|
|
|
*/
|
2003-02-13 07:51:21 +03:00
|
|
|
int blkid_flush_cache(blkid_cache cache)
|
2003-01-24 00:45:16 +03:00
|
|
|
{
|
2003-02-13 07:51:21 +03:00
|
|
|
struct list_head *p;
|
2003-01-25 08:26:48 +03:00
|
|
|
char *tmp = NULL;
|
|
|
|
const char *opened = NULL;
|
2003-02-13 07:51:21 +03:00
|
|
|
const char *filename;
|
2003-01-24 00:45:16 +03:00
|
|
|
FILE *file = NULL;
|
2003-02-13 07:51:21 +03:00
|
|
|
int fd, ret = 0;
|
2003-02-23 01:15:20 +03:00
|
|
|
struct stat st;
|
2003-01-24 00:45:16 +03:00
|
|
|
|
|
|
|
if (!cache)
|
|
|
|
return -BLKID_ERR_PARAM;
|
|
|
|
|
|
|
|
if (list_empty(&cache->bic_devs) ||
|
|
|
|
!(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
|
2003-02-23 01:15:20 +03:00
|
|
|
DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
|
2003-01-24 00:45:16 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-13 07:51:21 +03:00
|
|
|
filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
/* If we can't write to the cache file, then don't even try */
|
|
|
|
if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
|
|
|
|
(ret == 0 && access(filename, W_OK) < 0)) {
|
|
|
|
DBG(DEBUG_SAVE,
|
|
|
|
printf("can't write to cache file %s\n", filename));
|
|
|
|
return 0;
|
|
|
|
}
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
/*
|
|
|
|
* Try and create a temporary file in the same directory so
|
|
|
|
* that in case of error we don't overwrite the cache file.
|
|
|
|
* If the cache file doesn't yet exist, it isn't a regular
|
|
|
|
* file (e.g. /dev/null or a socket), or we couldn't create
|
|
|
|
* a temporary file then we open it directly.
|
|
|
|
*/
|
|
|
|
if (ret == 0 && S_ISREG(st.st_mode)) {
|
|
|
|
tmp = malloc(strlen(filename) + 8);
|
|
|
|
if (tmp) {
|
|
|
|
sprintf(tmp, "%s-XXXXXX", filename);
|
|
|
|
fd = mkstemp(tmp);
|
|
|
|
if (fd >= 0) {
|
|
|
|
file = fdopen(fd, "w");
|
|
|
|
opened = tmp;
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
2003-02-23 01:15:20 +03:00
|
|
|
fchmod(fd, 0644);
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
2003-02-23 01:15:20 +03:00
|
|
|
}
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
if (!file) {
|
|
|
|
file = fopen(filename, "w");
|
|
|
|
opened = filename;
|
|
|
|
}
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
DBG(DEBUG_SAVE,
|
|
|
|
printf("writing cache file %s (really %s)\n",
|
|
|
|
filename, opened));
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
if (!file) {
|
|
|
|
ret = errno;
|
|
|
|
goto errout;
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
|
|
|
|
2003-02-13 07:51:21 +03:00
|
|
|
list_for_each(p, &cache->bic_devs) {
|
|
|
|
blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
|
|
|
|
if (!dev->bid_type)
|
|
|
|
continue;
|
|
|
|
if ((ret = save_dev(dev, file)) < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
|
|
|
|
ret = 1;
|
|
|
|
}
|
2003-01-24 00:45:16 +03:00
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
fclose(file);
|
|
|
|
if (opened != filename) {
|
|
|
|
if (ret < 0) {
|
|
|
|
unlink(opened);
|
|
|
|
DBG(DEBUG_SAVE,
|
|
|
|
printf("unlinked temp cache %s\n", opened));
|
|
|
|
} else {
|
|
|
|
char *backup;
|
|
|
|
|
|
|
|
backup = malloc(strlen(filename) + 5);
|
|
|
|
if (backup) {
|
|
|
|
sprintf(backup, "%s.old", filename);
|
|
|
|
unlink(backup);
|
|
|
|
link(filename, backup);
|
|
|
|
free(backup);
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
2003-02-23 01:15:20 +03:00
|
|
|
rename(opened, filename);
|
|
|
|
DBG(DEBUG_SAVE,
|
|
|
|
printf("moved temp cache %s\n", opened));
|
2003-01-24 00:45:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-13 07:51:21 +03:00
|
|
|
errout:
|
2003-01-25 08:26:48 +03:00
|
|
|
if (tmp)
|
|
|
|
free(tmp);
|
2003-01-24 00:45:16 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST_PROGRAM
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2003-01-26 09:54:39 +03:00
|
|
|
blkid_cache cache = NULL;
|
2003-01-24 00:45:16 +03:00
|
|
|
int ret;
|
|
|
|
|
2003-02-22 21:19:53 +03:00
|
|
|
blkid_debug_mask = DEBUG_ALL;
|
2003-01-24 00:45:16 +03:00
|
|
|
if (argc > 2) {
|
|
|
|
fprintf(stderr, "Usage: %s [filename]\n"
|
|
|
|
"Test loading/saving a cache (filename)\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-02-23 01:15:20 +03:00
|
|
|
if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
|
|
|
|
fprintf(stderr, "%s: error creating cache (%d)\n",
|
|
|
|
argv[0], ret);
|
2003-02-13 07:51:21 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((ret = blkid_probe_all(cache)) < 0) {
|
|
|
|
fprintf(stderr, "error (%d) probing devices\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
cache->bic_filename = blkid_strdup(argv[1]);
|
|
|
|
|
|
|
|
if ((ret = blkid_flush_cache(cache)) < 0) {
|
|
|
|
fprintf(stderr, "error (%d) saving cache\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
blkid_put_cache(cache);
|
2003-01-24 00:45:16 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|