blob: bdc9c598063426564da82c3ebd31476c80edce2b [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
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050088 if (list_empty(&cache->bic_devs) ||
89 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
bigbiff7b4c7a62015-01-01 19:44:14 -050090 DBG(SAVE, ul_debug("skipping cache file write"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050091 return 0;
92 }
93
94 filename = cache->bic_filename ? cache->bic_filename :
95 blkid_get_cache_filename(NULL);
96 if (!filename)
97 return -BLKID_ERR_PARAM;
98
99 if (strncmp(filename,
100 BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) {
101
102 /* default destination, create the directory if necessary */
103 if (stat(BLKID_RUNTIME_DIR, &st)
104 && errno == ENOENT
105 && mkdir(BLKID_RUNTIME_DIR, S_IWUSR|
106 S_IRUSR|S_IRGRP|S_IROTH|
107 S_IXUSR|S_IXGRP|S_IXOTH) != 0
108 && errno != EEXIST) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500109 DBG(SAVE, ul_debug("can't create %s directory for cache file",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500110 BLKID_RUNTIME_DIR));
111 return 0;
112 }
113 }
114
115 /* If we can't write to the cache file, then don't even try */
116 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
117 (ret == 0 && access(filename, W_OK) < 0)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500118 DBG(SAVE, ul_debug("can't write to cache file %s", filename));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500119 return 0;
120 }
121
122 /*
123 * Try and create a temporary file in the same directory so
124 * that in case of error we don't overwrite the cache file.
125 * If the cache file doesn't yet exist, it isn't a regular
126 * file (e.g. /dev/null or a socket), or we couldn't create
127 * a temporary file then we open it directly.
128 */
129 if (ret == 0 && S_ISREG(st.st_mode)) {
130 tmp = malloc(strlen(filename) + 8);
131 if (tmp) {
132 sprintf(tmp, "%s-XXXXXX", filename);
133 fd = mkstemp(tmp);
134 if (fd >= 0) {
135 if (fchmod(fd, 0644) != 0)
bigbiff7b4c7a62015-01-01 19:44:14 -0500136 DBG(SAVE, ul_debug("%s: fchmod failed", filename));
137 else if ((file = fdopen(fd, "w" UL_CLOEXECSTR)))
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500138 opened = tmp;
139 if (!file)
140 close(fd);
141 }
142 }
143 }
144
145 if (!file) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500146 file = fopen(filename, "w" UL_CLOEXECSTR);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500147 opened = filename;
148 }
149
bigbiff7b4c7a62015-01-01 19:44:14 -0500150 DBG(SAVE, ul_debug("writing cache file %s (really %s)",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500151 filename, opened));
152
153 if (!file) {
154 ret = errno;
155 goto errout;
156 }
157
158 list_for_each(p, &cache->bic_devs) {
159 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
160 if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE))
161 continue;
162 if ((ret = save_dev(dev, file)) < 0)
163 break;
164 }
165
166 if (ret >= 0) {
167 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
168 ret = 1;
169 }
170
bigbiff7b4c7a62015-01-01 19:44:14 -0500171 if (close_stream(file) != 0)
172 DBG(SAVE, ul_debug("write failed: %s", filename));
173
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500174 if (opened != filename) {
175 if (ret < 0) {
176 unlink(opened);
bigbiff7b4c7a62015-01-01 19:44:14 -0500177 DBG(SAVE, ul_debug("unlinked temp cache %s", opened));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500178 } else {
179 char *backup;
180
181 backup = malloc(strlen(filename) + 5);
182 if (backup) {
183 sprintf(backup, "%s.old", filename);
184 unlink(backup);
185 if (link(filename, backup)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500186 DBG(SAVE, ul_debug("can't link %s to %s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500187 filename, backup));
188 }
189 free(backup);
190 }
191 if (rename(opened, filename)) {
192 ret = errno;
bigbiff7b4c7a62015-01-01 19:44:14 -0500193 DBG(SAVE, ul_debug("can't rename %s to %s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500194 opened, filename));
195 } else {
bigbiff7b4c7a62015-01-01 19:44:14 -0500196 DBG(SAVE, ul_debug("moved temp cache %s", opened));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500197 }
198 }
199 }
200
201errout:
202 free(tmp);
203 if (filename != cache->bic_filename)
204 free(filename);
205 return ret;
206}
207
208#ifdef TEST_PROGRAM
209int main(int argc, char **argv)
210{
211 blkid_cache cache = NULL;
212 int ret;
213
bigbiff7b4c7a62015-01-01 19:44:14 -0500214 blkid_init_debug(BLKID_DEBUG_ALL);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500215 if (argc > 2) {
216 fprintf(stderr, "Usage: %s [filename]\n"
217 "Test loading/saving a cache (filename)\n", argv[0]);
218 exit(1);
219 }
220
221 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
222 fprintf(stderr, "%s: error creating cache (%d)\n",
223 argv[0], ret);
224 exit(1);
225 }
226 if ((ret = blkid_probe_all(cache)) < 0) {
227 fprintf(stderr, "error (%d) probing devices\n", ret);
228 exit(1);
229 }
230 cache->bic_filename = strdup(argv[1]);
231
232 if ((ret = blkid_flush_cache(cache)) < 0) {
233 fprintf(stderr, "error (%d) saving cache\n", ret);
234 exit(1);
235 }
236
237 blkid_put_cache(cache);
238
239 return ret;
240}
241#endif