Debian Codegraph @

libblkid/src/save.c

raw
1 /*
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_ERRNO_H
22 #include <errno.h>
23 #endif
24
25 #include "closestream.h"
26 #include "fileutils.h"
27
28 #include "blkidP.h"
29
30
31 static void save_quoted(const char *data, FILE *file)
32 {
33 const char *p;
34
35 fputc('"', file);
36 for (p = data; p && *p; p++) {
37 if ((unsigned char) *p == 0x22 || /* " */
38 (unsigned char) *p == 0x5c) /* \ */
39 fputc('\\', file);
40
41 fputc(*p, file);
42 }
43 fputc('"', file);
44 }
45 static int save_dev(blkid_dev dev, FILE *file)
46 {
47 struct list_head *p;
48
49 if (!dev || dev->bid_name[0] != '/')
50 return 0;
51
52 DBG(SAVE, ul_debug("device %s, type %s", dev->bid_name, dev->bid_type ?
53 dev->bid_type : "(null)"));
54
55 fprintf(file, "<device DEVNO=\"0x%04lx\" TIME=\"%lld.%lld\"",
56 (unsigned long) dev->bid_devno,
57 (long long) dev->bid_time,
58 (long long) dev->bid_utime);
59
60 if (dev->bid_pri)
61 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
62
63 list_for_each(p, &dev->bid_tags) {
64 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
65
66 fputc(' ', file); /* space between tags */
67 fputs(tag->bit_name, file); /* tag NAME */
68 fputc('=', file); /* separator between NAME and VALUE */
69 save_quoted(tag->bit_val, file); /* tag "VALUE" */
70 }
71 fprintf(file, ">%s</device>\n", dev->bid_name);
72
73 return 0;
74 }
75
76 /*
77 * Write out the cache struct to the cache file on disk.
78 */
79 int blkid_flush_cache(blkid_cache cache)
80 {
81 struct list_head *p;
82 char *tmp = NULL;
83 char *opened = NULL;
84 char *filename;
85 FILE *file = NULL;
86 int fd, ret = 0;
87 struct stat st;
88
89 if (list_empty(&cache->bic_devs) ||
90 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
91 DBG(SAVE, ul_debug("skipping cache file write"));
92 return 0;
93 }
94
95 filename = cache->bic_filename ? cache->bic_filename :
96 blkid_get_cache_filename(NULL);
97 if (!filename)
98 return -BLKID_ERR_PARAM;
99
100 if (strncmp(filename,
101 BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) {
102
103 /* default destination, create the directory if necessary */
104 if (stat(BLKID_RUNTIME_DIR, &st)
105 && errno == ENOENT
106 && mkdir(BLKID_RUNTIME_DIR, S_IWUSR|
107 S_IRUSR|S_IRGRP|S_IROTH|
108 S_IXUSR|S_IXGRP|S_IXOTH) != 0
109 && errno != EEXIST) {
110 DBG(SAVE, ul_debug("can't create %s directory for cache file",
111 BLKID_RUNTIME_DIR));
112 ret = 0;
113 goto done;
114 }
115 }
116
117 /* If we can't write to the cache file, then don't even try */
118 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
119 (ret == 0 && access(filename, W_OK) < 0)) {
120 DBG(SAVE, ul_debug("can't write to cache file %s", filename));
121 ret = 0;
122 goto done;
123 }
124
125 /*
126 * Try and create a temporary file in the same directory so
127 * that in case of error we don't overwrite the cache file.
128 * If the cache file doesn't yet exist, it isn't a regular
129 * file (e.g. /dev/null or a socket), or we couldn't create
130 * a temporary file then we open it directly.
131 */
132 if (ret == 0 && S_ISREG(st.st_mode)) {
133 size_t len = strlen(filename) + 8;
134 tmp = malloc(len);
135 if (tmp) {
136 snprintf(tmp, len, "%s-XXXXXX", filename);
137 fd = mkstemp_cloexec(tmp);
138 if (fd >= 0) {
139 if (fchmod(fd, 0644) != 0)
140 DBG(SAVE, ul_debug("%s: fchmod failed", filename));
141 else if ((file = fdopen(fd, "w" UL_CLOEXECSTR)))
142 opened = tmp;
143 if (!file)
144 close(fd);
145 }
146 }
147 }
148
149 if (!file) {
150 file = fopen(filename, "w" UL_CLOEXECSTR);
151 opened = filename;
152 }
153
154 DBG(SAVE, ul_debug("writing cache file %s (really %s)",
155 filename, opened));
156
157 if (!file) {
158 ret = errno;
159 goto done;
160 }
161
162 list_for_each(p, &cache->bic_devs) {
163 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
164 if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE))
165 continue;
166 if ((ret = save_dev(dev, file)) < 0)
167 break;
168 }
169
170 if (ret >= 0) {
171 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
172 ret = 1;
173 }
174
175 if (close_stream(file) != 0)
176 DBG(SAVE, ul_debug("write failed: %s", filename));
177
178 if (opened != filename) {
179 if (ret < 0) {
180 unlink(opened);
181 DBG(SAVE, ul_debug("unlinked temp cache %s", opened));
182 } else {
183 char *backup;
184 size_t len = strlen(filename) + 5;
185
186 backup = malloc(len);
187 if (backup) {
188 snprintf(backup, len, "%s.old", filename);
189 unlink(backup);
190 if (link(filename, backup)) {
191 DBG(SAVE, ul_debug("can't link %s to %s",
192 filename, backup));
193 }
194 free(backup);
195 }
196 if (rename(opened, filename)) {
197 ret = errno;
198 DBG(SAVE, ul_debug("can't rename %s to %s",
199 opened, filename));
200 } else {
201 DBG(SAVE, ul_debug("moved temp cache %s", opened));
202 }
203 }
204 }
205
206 done:
207 free(tmp);
208 if (filename != cache->bic_filename)
209 free(filename);
210 return ret;
211 }
212
213 #ifdef TEST_PROGRAM
214 int main(int argc, char **argv)
215 {
216 blkid_cache cache = NULL;
217 int ret;
218
219 blkid_init_debug(BLKID_DEBUG_ALL);
220 if (argc != 2) {
221 fprintf(stderr, "Usage: %s [filename]\n"
222 "Test loading/saving a cache (filename)\n", argv[0]);
223 exit(1);
224 }
225
226 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
227 fprintf(stderr, "%s: error creating cache (%d)\n",
228 argv[0], ret);
229 exit(1);
230 }
231 if ((ret = blkid_probe_all(cache)) < 0) {
232 fprintf(stderr, "error (%d) probing devices\n", ret);
233 exit(1);
234 }
235 cache->bic_filename = strdup(argv[1]);
236
237 if ((ret = blkid_flush_cache(cache)) < 0) {
238 fprintf(stderr, "error (%d) saving cache\n", ret);
239 exit(1);
240 }
241
242 blkid_put_cache(cache);
243
244 return ret;
245 }
246 #endif