Whamcloud - gitweb
libblkid: add error checking for rename() while saving the blkid cache
[tools/e2fsprogs.git] / lib / blkid / 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 "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #ifdef HAVE_SYS_MKDEV_H
23 #include <sys/mkdev.h>
24 #endif
25 #ifdef HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #include "blkidP.h"
29
30 static int save_dev(blkid_dev dev, FILE *file)
31 {
32         struct list_head *p;
33
34         if (!dev || dev->bid_name[0] != '/')
35                 return 0;
36
37         DBG(DEBUG_SAVE,
38             printf("device %s, type %s\n", dev->bid_name, dev->bid_type ?
39                    dev->bid_type : "(null)"));
40
41         fprintf(file,
42                 "<device DEVNO=\"0x%04lx\" TIME=\"%ld\"",
43                 (unsigned long) dev->bid_devno, (long) dev->bid_time);
44         if (dev->bid_pri)
45                 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
46         list_for_each(p, &dev->bid_tags) {
47                 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
48                 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
49         }
50         fprintf(file, ">%s</device>\n", dev->bid_name);
51
52         return 0;
53 }
54
55 /*
56  * Write out the cache struct to the cache file on disk.
57  */
58 int blkid_flush_cache(blkid_cache cache)
59 {
60         struct list_head *p;
61         char *tmp = NULL;
62         const char *opened = NULL;
63         const char *filename;
64         FILE *file = NULL;
65         int fd, ret = 0;
66         struct stat st;
67
68         if (!cache)
69                 return -BLKID_ERR_PARAM;
70
71         if (list_empty(&cache->bic_devs) ||
72             !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
73                 DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
74                 return 0;
75         }
76
77         filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
78
79         /* If we can't write to the cache file, then don't even try */
80         if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
81             (ret == 0 && access(filename, W_OK) < 0)) {
82                 DBG(DEBUG_SAVE,
83                     printf("can't write to cache file %s\n", filename));
84                 return 0;
85         }
86
87         /*
88          * Try and create a temporary file in the same directory so
89          * that in case of error we don't overwrite the cache file.
90          * If the cache file doesn't yet exist, it isn't a regular
91          * file (e.g. /dev/null or a socket), or we couldn't create
92          * a temporary file then we open it directly.
93          */
94         if (ret == 0 && S_ISREG(st.st_mode)) {
95                 tmp = malloc(strlen(filename) + 8);
96                 if (tmp) {
97                         mode_t save_umask = umask(022);
98                         sprintf(tmp, "%s-XXXXXX", filename);
99                         fd = mkstemp(tmp);
100                         umask(save_umask);
101                         if (fd >= 0) {
102                                 file = fdopen(fd, "w");
103                                 opened = tmp;
104                         }
105                         fchmod(fd, 0644);
106                 }
107         }
108
109         if (!file) {
110                 file = fopen(filename, "w");
111                 opened = filename;
112         }
113
114         DBG(DEBUG_SAVE,
115             printf("writing cache file %s (really %s)\n",
116                    filename, opened));
117
118         if (!file) {
119                 ret = errno;
120                 goto errout;
121         }
122
123         list_for_each(p, &cache->bic_devs) {
124                 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
125                 if (!dev->bid_type)
126                         continue;
127                 if ((ret = save_dev(dev, file)) < 0)
128                         break;
129         }
130
131         if (ret >= 0) {
132                 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
133                 ret = 1;
134         }
135
136         fclose(file);
137         if (opened != filename) {
138                 if (ret < 0) {
139                         (void) unlink(opened);
140                         DBG(DEBUG_SAVE,
141                             printf("unlinked temp cache %s\n", opened));
142                 } else {
143                         char *backup;
144
145                         backup = malloc(strlen(filename) + 5);
146                         if (backup) {
147                                 sprintf(backup, "%s.old", filename);
148                                 unlink(backup);
149                                 link(filename, backup);
150                                 free(backup);
151                         }
152                         if (rename(opened, filename) < 0)
153                                 (void) unlink(opened);
154                         DBG(DEBUG_SAVE,
155                             printf("moved temp cache %s\n", opened));
156                 }
157         }
158
159 errout:
160         free(tmp);
161         return ret;
162 }
163
164 #ifdef TEST_PROGRAM
165 int main(int argc, char **argv)
166 {
167         blkid_cache cache = NULL;
168         int ret;
169
170         blkid_debug_mask = DEBUG_ALL;
171         if (argc > 2) {
172                 fprintf(stderr, "Usage: %s [filename]\n"
173                         "Test loading/saving a cache (filename)\n", argv[0]);
174                 exit(1);
175         }
176
177         if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
178                 fprintf(stderr, "%s: error creating cache (%d)\n",
179                         argv[0], ret);
180                 exit(1);
181         }
182         if ((ret = blkid_probe_all(cache)) < 0) {
183                 fprintf(stderr, "error (%d) probing devices\n", ret);
184                 exit(1);
185         }
186         cache->bic_filename = blkid_strdup(argv[1]);
187
188         if ((ret = blkid_flush_cache(cache)) < 0) {
189                 fprintf(stderr, "error (%d) saving cache\n", ret);
190                 exit(1);
191         }
192
193         blkid_put_cache(cache);
194
195         return ret;
196 }
197 #endif