Whamcloud - gitweb
Integrate new blkid library.
[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  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the
8  * GNU Lesser General Public License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
20 #ifdef HAVE_SYS_MKDEV_H
21 #include <sys/mkdev.h>
22 #endif
23 #ifdef HAVE_ERRNO_H
24 #include <errno.h>
25 #endif
26 #include "blkid/blkid.h"
27
28 #ifdef DEBUG_SAVE
29 #define DEB_SAVE(fmt, arg...) printf("save: " fmt, ## arg)
30 #else
31 #define DEB_SAVE(fmt, arg...) do {} while (0)
32 #endif
33
34 static int save_dev(blkid_dev *dev, FILE *file)
35 {
36         struct list_head *p;
37
38         if (!dev)
39                 return 0;
40
41         DEB_SAVE("device %s, type %s\n", dev->bid_name, dev->bid_type);
42
43         fprintf(file,
44                 "<device TYPE=\"%s\" DEVNO=\"0x%04Lx\" ID=\"%d\" TIME=\"%Ld\"",
45                 dev->bid_type, dev->bid_devno,
46                 dev->bid_id, (long long)dev->bid_time);
47         list_for_each(p, &dev->bid_tags) {
48                 blkid_tag *tag = list_entry(p, blkid_tag, bit_tags);
49                 if (strcmp(tag->bit_name, "TYPE"))
50                         fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
51         }
52         fprintf(file, ">%s</device>\n", dev->bid_name);
53
54         return 0;
55 }
56
57 int blkid_save_cache_file(blkid_cache *cache, FILE *file)
58 {
59         struct list_head *p;
60         int ret = 0;
61
62         if (!cache || !file)
63                 return -BLKID_ERR_PARAM;
64
65         if (list_empty(&cache->bic_devs) ||
66             !cache->bic_flags & BLKID_BIC_FL_CHANGED)
67                 return 0;
68
69         list_for_each(p, &cache->bic_devs) {
70                 blkid_dev *dev = list_entry(p, blkid_dev, bid_devs);
71                 if ((ret = save_dev(dev, file)) < 0)
72                         break;
73         }
74
75         if (ret >= 0) {
76                 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
77                 ret = 1;
78         }
79
80         return ret;
81 }
82
83 /*
84  * Write out the cache struct to the cache file on disk.
85  */
86 int blkid_save_cache(blkid_cache *cache, char *filename)
87 {
88         char tmp[4096] = { '\0', };
89         char *opened = NULL;
90         FILE *file = NULL;
91         int ret;
92
93         if (!cache)
94                 return -BLKID_ERR_PARAM;
95
96         if (list_empty(&cache->bic_devs) ||
97             !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
98                 DEB_SAVE("empty cache, not saving\n");
99                 return 0;
100         }
101
102         if (!filename || !strlen(filename))
103                 filename = BLKID_CACHE_FILE;
104
105         if (!strcmp(filename, "-") || !strcmp(filename, "stdout"))
106                 file = stdout;
107         else {
108                 struct stat st;
109
110                 /* If we can't write to the cache file, then don't even try */
111                 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
112                     (ret == 0 && access(filename, W_OK) < 0)) {
113                         DEB_SAVE("can't write to cache file %s\n", filename);
114                         return 0;
115                 }
116
117                 /*
118                  * Try and create a temporary file in the same directory so
119                  * that in case of error we don't overwrite the cache file.
120                  * If the cache file doesn't yet exist, it isn't a regular
121                  * file (e.g. /dev/null or a socket), or we couldn't create
122                  * a temporary file then we open it directly.
123                  */
124                 if (ret == 0 && S_ISREG(st.st_mode)) {
125                         snprintf(tmp, sizeof(tmp) - 1, "%s-XXXXXX", filename);
126                         if (mktemp(tmp) == tmp && *tmp != '\0') {
127                                 file = fopen(tmp, "w");
128                                 opened = tmp;
129                         }
130                 }
131
132                 if (!file) {
133                         file = fopen(filename, "w");
134                         opened = filename;
135                 }
136
137                 DEB_SAVE("cache file %s (really %s)\n", filename, opened);
138
139                 if (!file) {
140                         perror(opened);
141                         return errno;
142                 }
143         }
144
145         ret = blkid_save_cache_file(cache, file);
146
147         if (file != stdout) {
148                 fclose(file);
149                 if (opened != filename) {
150                         if (ret < 0) {
151                                 unlink(opened);
152                                 DEB_SAVE("unlinked temp cache %s\n", opened);
153                         } else {
154                                 char backup[4096];
155
156                                 snprintf(backup, sizeof(backup) - 1, "%s.old",
157                                          filename);
158                                 unlink(backup);
159                                 link(filename, backup);
160                                 rename(opened, filename);
161                                 DEB_SAVE("moved temp cache %s\n", opened);
162                         }
163                 }
164         }
165
166         return ret;
167 }
168
169 #ifdef TEST_PROGRAM
170 int main(int argc, char **argv)
171 {
172         blkid_cache *cache = NULL;
173         int ret;
174
175         if (argc > 2) {
176                 fprintf(stderr, "Usage: %s [filename]\n"
177                         "Test loading/saving a cache (filename)\n", argv[0]);
178                 exit(1);
179         }
180         if ((ret = blkid_probe_all(&cache) < 0))
181                 fprintf(stderr, "error probing devices\n");
182         else if ((ret = blkid_save_cache(cache, argv[1])) < 0)
183                 fprintf(stderr, "error %d saving cache to %s\n", ret,
184                         argv[1] ? argv[1] : BLKID_CACHE_FILE);
185
186         blkid_free_cache(cache);
187
188         return ret;
189 }
190 #endif