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