Whamcloud - gitweb
build: fix compile warnings on OSX
[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                         sprintf(tmp, "%s-XXXXXX", filename);
98                         fd = mkstemp(tmp);
99                         if (fd >= 0) {
100                                 file = fdopen(fd, "w");
101                                 opened = tmp;
102                         }
103                         fchmod(fd, 0644);
104                 }
105         }
106
107         if (!file) {
108                 file = fopen(filename, "w");
109                 opened = filename;
110         }
111
112         DBG(DEBUG_SAVE,
113             printf("writing cache file %s (really %s)\n",
114                    filename, opened));
115
116         if (!file) {
117                 ret = errno;
118                 goto errout;
119         }
120
121         list_for_each(p, &cache->bic_devs) {
122                 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
123                 if (!dev->bid_type)
124                         continue;
125                 if ((ret = save_dev(dev, file)) < 0)
126                         break;
127         }
128
129         if (ret >= 0) {
130                 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
131                 ret = 1;
132         }
133
134         fclose(file);
135         if (opened != filename) {
136                 if (ret < 0) {
137                         unlink(opened);
138                         DBG(DEBUG_SAVE,
139                             printf("unlinked temp cache %s\n", opened));
140                 } else {
141                         char *backup;
142
143                         backup = malloc(strlen(filename) + 5);
144                         if (backup) {
145                                 sprintf(backup, "%s.old", filename);
146                                 unlink(backup);
147                                 link(filename, backup);
148                                 free(backup);
149                         }
150                         rename(opened, filename);
151                         DBG(DEBUG_SAVE,
152                             printf("moved temp cache %s\n", opened));
153                 }
154         }
155
156 errout:
157         free(tmp);
158         return ret;
159 }
160
161 #ifdef TEST_PROGRAM
162 int main(int argc, char **argv)
163 {
164         blkid_cache cache = NULL;
165         int ret;
166
167         blkid_debug_mask = DEBUG_ALL;
168         if (argc > 2) {
169                 fprintf(stderr, "Usage: %s [filename]\n"
170                         "Test loading/saving a cache (filename)\n", argv[0]);
171                 exit(1);
172         }
173
174         if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
175                 fprintf(stderr, "%s: error creating cache (%d)\n",
176                         argv[0], ret);
177                 exit(1);
178         }
179         if ((ret = blkid_probe_all(cache)) < 0) {
180                 fprintf(stderr, "error (%d) probing devices\n", ret);
181                 exit(1);
182         }
183         cache->bic_filename = blkid_strdup(argv[1]);
184
185         if ((ret = blkid_flush_cache(cache)) < 0) {
186                 fprintf(stderr, "error (%d) saving cache\n", ret);
187                 exit(1);
188         }
189
190         blkid_put_cache(cache);
191
192         return ret;
193 }
194 #endif