blob: c2a975348c613fec38dc201e706837cb0d50010e [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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
bigbiff7b4c7a62015-01-01 19:44:14 -050024
25#include "closestream.h"
26
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050027#include "blkidP.h"
28
bigbiff7b4c7a62015-01-01 19:44:14 -050029
30static void save_quoted(const char *data, FILE *file)
31{
32 const char *p;
33
34 fputc('"', file);
35 for (p = data; p && *p; p++) {
36 if ((unsigned char) *p == 0x22 || /* " */
37 (unsigned char) *p == 0x5c) /* \ */
38 fputc('\\', file);
39
40 fputc(*p, file);
41 }
42 fputc('"', file);
43}
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050044static int save_dev(blkid_dev dev, FILE *file)
45{
46 struct list_head *p;
47
48 if (!dev || dev->bid_name[0] != '/')
49 return 0;
50
bigbiff7b4c7a62015-01-01 19:44:14 -050051 DBG(SAVE, ul_debug("device %s, type %s", dev->bid_name, dev->bid_type ?
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050052 dev->bid_type : "(null)"));
53
54 fprintf(file, "<device DEVNO=\"0x%04lx\" TIME=\"%ld.%ld\"",
55 (unsigned long) dev->bid_devno,
56 (long) dev->bid_time,
57 (long) dev->bid_utime);
58
59 if (dev->bid_pri)
60 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
bigbiff7b4c7a62015-01-01 19:44:14 -050061
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050062 list_for_each(p, &dev->bid_tags) {
63 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
bigbiff7b4c7a62015-01-01 19:44:14 -050064
65 fputc(' ', file); /* space between tags */
66 fputs(tag->bit_name, file); /* tag NAME */
67 fputc('=', file); /* separator between NAME and VALUE */
68 save_quoted(tag->bit_val, file); /* tag "VALUE" */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050069 }
70 fprintf(file, ">%s</device>\n", dev->bid_name);
71
72 return 0;
73}
74
75/*
76 * Write out the cache struct to the cache file on disk.
77 */
78int blkid_flush_cache(blkid_cache cache)
79{
80 struct list_head *p;
81 char *tmp = NULL;
82 char *opened = NULL;
83 char *filename;
84 FILE *file = NULL;
85 int fd, ret = 0;
86 struct stat st;
87
88 if (!cache)
89 return -BLKID_ERR_PARAM;
90
91 if (list_empty(&cache->bic_devs) ||
92 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
bigbiff7b4c7a62015-01-01 19:44:14 -050093 DBG(SAVE, ul_debug("skipping cache file write"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050094 return 0;
95 }
96
97 filename = cache->bic_filename ? cache->bic_filename :
98 blkid_get_cache_filename(NULL);
99 if (!filename)
100 return -BLKID_ERR_PARAM;
101
102 if (strncmp(filename,
103 BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) {
104
105 /* default destination, create the directory if necessary */
106 if (stat(BLKID_RUNTIME_DIR, &st)
107 && errno == ENOENT
108 && mkdir(BLKID_RUNTIME_DIR, S_IWUSR|
109 S_IRUSR|S_IRGRP|S_IROTH|
110 S_IXUSR|S_IXGRP|S_IXOTH) != 0
111 && errno != EEXIST) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500112 DBG(SAVE, ul_debug("can't create %s directory for cache file",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500113 BLKID_RUNTIME_DIR));
114 return 0;
115 }
116 }
117
118 /* If we can't write to the cache file, then don't even try */
119 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
120 (ret == 0 && access(filename, W_OK) < 0)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500121 DBG(SAVE, ul_debug("can't write to cache file %s", filename));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500122 return 0;
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 tmp = malloc(strlen(filename) + 8);
134 if (tmp) {
135 sprintf(tmp, "%s-XXXXXX", filename);
136 fd = mkstemp(tmp);
137 if (fd >= 0) {
138 if (fchmod(fd, 0644) != 0)
bigbiff7b4c7a62015-01-01 19:44:14 -0500139 DBG(SAVE, ul_debug("%s: fchmod failed", filename));
140 else if ((file = fdopen(fd, "w" UL_CLOEXECSTR)))
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500141 opened = tmp;
142 if (!file)
143 close(fd);
144 }
145 }
146 }
147
148 if (!file) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500149 file = fopen(filename, "w" UL_CLOEXECSTR);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500150 opened = filename;
151 }
152
bigbiff7b4c7a62015-01-01 19:44:14 -0500153 DBG(SAVE, ul_debug("writing cache file %s (really %s)",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500154 filename, opened));
155
156 if (!file) {
157 ret = errno;
158 goto errout;
159 }
160
161 list_for_each(p, &cache->bic_devs) {
162 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
163 if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE))
164 continue;
165 if ((ret = save_dev(dev, file)) < 0)
166 break;
167 }
168
169 if (ret >= 0) {
170 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
171 ret = 1;
172 }
173
bigbiff7b4c7a62015-01-01 19:44:14 -0500174 if (close_stream(file) != 0)
175 DBG(SAVE, ul_debug("write failed: %s", filename));
176
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500177 if (opened != filename) {
178 if (ret < 0) {
179 unlink(opened);
bigbiff7b4c7a62015-01-01 19:44:14 -0500180 DBG(SAVE, ul_debug("unlinked temp cache %s", opened));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500181 } else {
182 char *backup;
183
184 backup = malloc(strlen(filename) + 5);
185 if (backup) {
186 sprintf(backup, "%s.old", filename);
187 unlink(backup);
188 if (link(filename, backup)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500189 DBG(SAVE, ul_debug("can't link %s to %s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500190 filename, backup));
191 }
192 free(backup);
193 }
194 if (rename(opened, filename)) {
195 ret = errno;
bigbiff7b4c7a62015-01-01 19:44:14 -0500196 DBG(SAVE, ul_debug("can't rename %s to %s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500197 opened, filename));
198 } else {
bigbiff7b4c7a62015-01-01 19:44:14 -0500199 DBG(SAVE, ul_debug("moved temp cache %s", opened));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500200 }
201 }
202 }
203
204errout:
205 free(tmp);
206 if (filename != cache->bic_filename)
207 free(filename);
208 return ret;
209}
210
211#ifdef TEST_PROGRAM
212int main(int argc, char **argv)
213{
214 blkid_cache cache = NULL;
215 int ret;
216
bigbiff7b4c7a62015-01-01 19:44:14 -0500217 blkid_init_debug(BLKID_DEBUG_ALL);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500218 if (argc > 2) {
219 fprintf(stderr, "Usage: %s [filename]\n"
220 "Test loading/saving a cache (filename)\n", argv[0]);
221 exit(1);
222 }
223
224 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
225 fprintf(stderr, "%s: error creating cache (%d)\n",
226 argv[0], ret);
227 exit(1);
228 }
229 if ((ret = blkid_probe_all(cache)) < 0) {
230 fprintf(stderr, "error (%d) probing devices\n", ret);
231 exit(1);
232 }
233 cache->bic_filename = strdup(argv[1]);
234
235 if ((ret = blkid_flush_cache(cache)) < 0) {
236 fprintf(stderr, "error (%d) saving cache\n", ret);
237 exit(1);
238 }
239
240 blkid_put_cache(cache);
241
242 return ret;
243}
244#endif