blob: 98d8d0260d91f6c5624c59203fe7faeab06b445b [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
2 * save.c - write the cache struct to disk
3 *
4 * Copyright (C) 2001 by Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <sys/types.h>
18#ifdef HAVE_SYS_STAT_H
19#include <sys/stat.h>
20#endif
21#ifdef HAVE_SYS_MKDEV_H
22#include <sys/mkdev.h>
23#endif
24#ifdef HAVE_ERRNO_H
25#include <errno.h>
26#endif
27#include "blkidP.h"
28
29static int save_dev(blkid_dev dev, FILE *file)
30{
31 struct list_head *p;
32
33 if (!dev || dev->bid_name[0] != '/')
34 return 0;
35
36 DBG(DEBUG_SAVE,
37 printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
38
39 fprintf(file,
40 "<device DEVNO=\"0x%04lx\" TIME=\"%lu\"",
41 (unsigned long) dev->bid_devno, dev->bid_time);
42 if (dev->bid_pri)
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);
47 }
48 fprintf(file, ">%s</device>\n", dev->bid_name);
49
50 return 0;
51}
52
53/*
54 * Write out the cache struct to the cache file on disk.
55 */
56int blkid_flush_cache(blkid_cache cache)
57{
58 struct list_head *p;
59 char *tmp = NULL;
60 const char *opened = NULL;
61 const char *filename;
62 FILE *file = NULL;
63 int fd, ret = 0;
64 struct stat st;
65
66 if (!cache)
67 return -BLKID_ERR_PARAM;
68
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"));
72 return 0;
73 }
74
75 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
76
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)) {
80 DBG(DEBUG_SAVE,
81 printf("can't write to cache file %s\n", filename));
82 return 0;
83 }
84
85 /*
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.
91 */
92 if (ret == 0 && S_ISREG(st.st_mode)) {
Mike Frysinger7fde8de2005-06-11 22:37:25 +000093 tmp = xmalloc(strlen(filename) + 8);
94 sprintf(tmp, "%s-XXXXXX", filename);
95 fd = mkstemp(tmp);
96 if (fd >= 0) {
97 file = fdopen(fd, "w");
98 opened = tmp;
Mike Frysinger38a33f92005-05-09 22:13:22 +000099 }
Mike Frysinger7fde8de2005-06-11 22:37:25 +0000100 fchmod(fd, 0644);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000101 }
102
103 if (!file) {
104 file = fopen(filename, "w");
105 opened = filename;
106 }
107
108 DBG(DEBUG_SAVE,
109 printf("writing cache file %s (really %s)\n",
110 filename, opened));
111
112 if (!file) {
113 ret = errno;
114 goto errout;
115 }
116
117 list_for_each(p, &cache->bic_devs) {
118 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
119 if (!dev->bid_type)
120 continue;
121 if ((ret = save_dev(dev, file)) < 0)
122 break;
123 }
124
125 if (ret >= 0) {
126 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
127 ret = 1;
128 }
129
130 fclose(file);
131 if (opened != filename) {
132 if (ret < 0) {
133 unlink(opened);
134 DBG(DEBUG_SAVE,
135 printf("unlinked temp cache %s\n", opened));
136 } else {
137 char *backup;
138
Mike Frysinger7fde8de2005-06-11 22:37:25 +0000139 backup = xmalloc(strlen(filename) + 5);
140 sprintf(backup, "%s.old", filename);
141 unlink(backup);
142 link(filename, backup);
143 free(backup);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000144 rename(opened, filename);
145 DBG(DEBUG_SAVE,
146 printf("moved temp cache %s\n", opened));
147 }
148 }
149
150errout:
Rob Landleye7c43b62006-03-01 16:39:45 +0000151 free(tmp);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000152 return ret;
153}
154
155#ifdef TEST_PROGRAM
156int main(int argc, char **argv)
157{
158 blkid_cache cache = NULL;
159 int ret;
160
161 blkid_debug_mask = DEBUG_ALL;
162 if (argc > 2) {
163 fprintf(stderr, "Usage: %s [filename]\n"
164 "Test loading/saving a cache (filename)\n", argv[0]);
165 exit(1);
166 }
167
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000168 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
Mike Frysinger38a33f92005-05-09 22:13:22 +0000169 fprintf(stderr, "%s: error creating cache (%d)\n",
170 argv[0], ret);
171 exit(1);
172 }
173 if ((ret = blkid_probe_all(cache)) < 0) {
174 fprintf(stderr, "error (%d) probing devices\n", ret);
175 exit(1);
176 }
177 cache->bic_filename = blkid_strdup(argv[1]);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000178
Mike Frysinger38a33f92005-05-09 22:13:22 +0000179 if ((ret = blkid_flush_cache(cache)) < 0) {
180 fprintf(stderr, "error (%d) saving cache\n", ret);
181 exit(1);
182 }
183
184 blkid_put_cache(cache);
185
186 return ret;
187}
188#endif