/src/util-linux/libblkid/src/save.c
Line | Count | Source |
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 | 0 | { |
33 | 0 | const char *p; |
34 | |
|
35 | 0 | fputc('"', file); |
36 | 0 | for (p = data; p && *p; p++) { |
37 | 0 | if ((unsigned char) *p == 0x22 || /* " */ |
38 | 0 | (unsigned char) *p == 0x5c) /* \ */ |
39 | 0 | fputc('\\', file); |
40 | |
|
41 | 0 | fputc(*p, file); |
42 | 0 | } |
43 | 0 | fputc('"', file); |
44 | 0 | } |
45 | | static int save_dev(blkid_dev dev, FILE *file) |
46 | 0 | { |
47 | 0 | struct list_head *p; |
48 | |
|
49 | 0 | if (!dev || dev->bid_name[0] != '/') |
50 | 0 | return 0; |
51 | | |
52 | 0 | DBG(SAVE, ul_debug("device %s, type %s", dev->bid_name, dev->bid_type ? |
53 | 0 | dev->bid_type : "(null)")); |
54 | |
|
55 | 0 | fprintf(file, "<device DEVNO=\"0x%04lx\" TIME=\"%lld.%lld\"", |
56 | 0 | (unsigned long) dev->bid_devno, |
57 | 0 | (long long) dev->bid_time, |
58 | 0 | (long long) dev->bid_utime); |
59 | |
|
60 | 0 | if (dev->bid_pri) |
61 | 0 | fprintf(file, " PRI=\"%d\"", dev->bid_pri); |
62 | |
|
63 | 0 | list_for_each(p, &dev->bid_tags) { |
64 | 0 | blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags); |
65 | |
|
66 | 0 | fputc(' ', file); /* space between tags */ |
67 | 0 | fputs(tag->bit_name, file); /* tag NAME */ |
68 | 0 | fputc('=', file); /* separator between NAME and VALUE */ |
69 | 0 | save_quoted(tag->bit_val, file); /* tag "VALUE" */ |
70 | 0 | } |
71 | 0 | fprintf(file, ">%s</device>\n", dev->bid_name); |
72 | |
|
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * Write out the cache struct to the cache file on disk. |
78 | | */ |
79 | | int blkid_flush_cache(blkid_cache cache) |
80 | 0 | { |
81 | 0 | struct list_head *p; |
82 | 0 | char *tmp = NULL; |
83 | 0 | char *opened = NULL; |
84 | 0 | char *filename; |
85 | 0 | FILE *file = NULL; |
86 | 0 | int fd, ret = 0; |
87 | 0 | struct stat st; |
88 | |
|
89 | 0 | if (list_empty(&cache->bic_devs) || |
90 | 0 | !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) { |
91 | 0 | DBG(SAVE, ul_debug("skipping cache file write")); |
92 | 0 | return 0; |
93 | 0 | } |
94 | | |
95 | 0 | filename = cache->bic_filename ? cache->bic_filename : |
96 | 0 | blkid_get_cache_filename(NULL); |
97 | 0 | if (!filename) |
98 | 0 | return -BLKID_ERR_PARAM; |
99 | | |
100 | 0 | if (strncmp(filename, |
101 | 0 | BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) { |
102 | | |
103 | | /* default destination, create the directory if necessary */ |
104 | 0 | if (stat(BLKID_RUNTIME_DIR, &st) |
105 | 0 | && errno == ENOENT |
106 | 0 | && mkdir(BLKID_RUNTIME_DIR, S_IWUSR| |
107 | 0 | S_IRUSR|S_IRGRP|S_IROTH| |
108 | 0 | S_IXUSR|S_IXGRP|S_IXOTH) != 0 |
109 | 0 | && errno != EEXIST) { |
110 | 0 | DBG(SAVE, ul_debug("can't create %s directory for cache file", |
111 | 0 | BLKID_RUNTIME_DIR)); |
112 | 0 | ret = 0; |
113 | 0 | goto done; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | /* If we can't write to the cache file, then don't even try */ |
118 | 0 | if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) || |
119 | 0 | (ret == 0 && access(filename, W_OK) < 0)) { |
120 | 0 | DBG(SAVE, ul_debug("can't write to cache file %s", filename)); |
121 | 0 | ret = 0; |
122 | 0 | goto done; |
123 | 0 | } |
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 | 0 | if (ret == 0 && S_ISREG(st.st_mode)) { |
133 | 0 | size_t len = strlen(filename) + 8; |
134 | 0 | tmp = malloc(len); |
135 | 0 | if (tmp) { |
136 | 0 | snprintf(tmp, len, "%s-XXXXXX", filename); |
137 | 0 | fd = mkstemp_cloexec(tmp); |
138 | 0 | if (fd >= 0) { |
139 | 0 | if (fchmod(fd, 0644) != 0) |
140 | 0 | DBG(SAVE, ul_debug("%s: fchmod failed", filename)); |
141 | 0 | else if ((file = fdopen(fd, "w" UL_CLOEXECSTR))) |
142 | 0 | opened = tmp; |
143 | 0 | if (!file) |
144 | 0 | close(fd); |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | 0 | if (!file) { |
150 | 0 | file = fopen(filename, "w" UL_CLOEXECSTR); |
151 | 0 | opened = filename; |
152 | 0 | } |
153 | |
|
154 | 0 | DBG(SAVE, ul_debug("writing cache file %s (really %s)", |
155 | 0 | filename, opened)); |
156 | |
|
157 | 0 | if (!file) { |
158 | 0 | ret = errno; |
159 | 0 | goto done; |
160 | 0 | } |
161 | | |
162 | 0 | list_for_each(p, &cache->bic_devs) { |
163 | 0 | blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs); |
164 | 0 | if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE)) |
165 | 0 | continue; |
166 | 0 | if ((ret = save_dev(dev, file)) < 0) |
167 | 0 | break; |
168 | 0 | } |
169 | |
|
170 | 0 | if (ret >= 0) { |
171 | 0 | cache->bic_flags &= ~BLKID_BIC_FL_CHANGED; |
172 | 0 | ret = 1; |
173 | 0 | } |
174 | |
|
175 | 0 | if (close_stream(file) != 0) |
176 | 0 | DBG(SAVE, ul_debug("write failed: %s", filename)); |
177 | |
|
178 | 0 | if (opened != filename) { |
179 | 0 | if (ret < 0) { |
180 | 0 | unlink(opened); |
181 | 0 | DBG(SAVE, ul_debug("unlinked temp cache %s", opened)); |
182 | 0 | } else { |
183 | 0 | char *backup; |
184 | 0 | size_t len = strlen(filename) + 5; |
185 | |
|
186 | 0 | backup = malloc(len); |
187 | 0 | if (backup) { |
188 | 0 | snprintf(backup, len, "%s.old", filename); |
189 | 0 | unlink(backup); |
190 | 0 | if (link(filename, backup)) { |
191 | 0 | DBG(SAVE, ul_debug("can't link %s to %s", |
192 | 0 | filename, backup)); |
193 | 0 | } |
194 | 0 | free(backup); |
195 | 0 | } |
196 | 0 | if (rename(opened, filename)) { |
197 | 0 | ret = errno; |
198 | 0 | DBG(SAVE, ul_debug("can't rename %s to %s", |
199 | 0 | opened, filename)); |
200 | 0 | } else { |
201 | 0 | DBG(SAVE, ul_debug("moved temp cache %s", opened)); |
202 | 0 | } |
203 | 0 | } |
204 | 0 | } |
205 | |
|
206 | 0 | done: |
207 | 0 | free(tmp); |
208 | 0 | if (filename != cache->bic_filename) |
209 | 0 | free(filename); |
210 | 0 | return ret; |
211 | 0 | } |
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 |