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