blob: 6f1d24475a9414954d948423bf0c96a483109133 [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * save.c - write the cache struct to disk
3 *
4 * Copyright (C) 2001 by Andreas Dilger
Theodore Ts'o50b380b2003-02-12 23:51:21 -05005 * Copyright (C) 2003 Theodore Ts'o
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05006 *
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_SYS_MKDEV_H
22#include <sys/mkdev.h>
23#endif
24#ifdef HAVE_ERRNO_H
25#include <errno.h>
26#endif
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050027#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050028
29#ifdef DEBUG_SAVE
Theodore Ts'od3f91792003-01-25 00:26:48 -050030#define DBG(x) x
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050031#else
Theodore Ts'od3f91792003-01-25 00:26:48 -050032#define DBG(x)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050033#endif
34
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050035static int save_dev(blkid_dev dev, FILE *file)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050036{
37 struct list_head *p;
38
39 if (!dev)
40 return 0;
41
Theodore Ts'od3f91792003-01-25 00:26:48 -050042 DBG(printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050043
44 fprintf(file,
Theodore Ts'obc40efd2003-02-14 01:40:23 -050045 "<device TYPE=\"%s\" DEVNO=\"0x%04lx\" TIME=\"%lu\"",
46 dev->bid_type, (unsigned long) dev->bid_devno, dev->bid_time);
Theodore Ts'oce72b862003-02-14 01:31:45 -050047 if (dev->bid_pri)
48 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050049 list_for_each(p, &dev->bid_tags) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050050 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050051 if (strcmp(tag->bit_name, "TYPE"))
52 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
53 }
54 fprintf(file, ">%s</device>\n", dev->bid_name);
55
56 return 0;
57}
58
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050059/*
60 * Write out the cache struct to the cache file on disk.
61 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -050062int blkid_flush_cache(blkid_cache cache)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050063{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050064 struct list_head *p;
Theodore Ts'od3f91792003-01-25 00:26:48 -050065 char *tmp = NULL;
66 const char *opened = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050067 const char *filename;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050068 FILE *file = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050069 int fd, ret = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050070
71 if (!cache)
72 return -BLKID_ERR_PARAM;
73
74 if (list_empty(&cache->bic_devs) ||
75 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -050076 DBG(printf("empty cache, not saving\n"));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050077 return 0;
78 }
79
Theodore Ts'o50b380b2003-02-12 23:51:21 -050080 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050081
Theodore Ts'o50b380b2003-02-12 23:51:21 -050082 if (!strcmp(filename, "-"))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050083 file = stdout;
84 else {
85 struct stat st;
86
87 /* If we can't write to the cache file, then don't even try */
88 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
89 (ret == 0 && access(filename, W_OK) < 0)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -050090 DBG(printf("can't write to cache file %s\n", filename));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050091 return 0;
92 }
93
94 /*
95 * Try and create a temporary file in the same directory so
96 * that in case of error we don't overwrite the cache file.
97 * If the cache file doesn't yet exist, it isn't a regular
98 * file (e.g. /dev/null or a socket), or we couldn't create
99 * a temporary file then we open it directly.
100 */
101 if (ret == 0 && S_ISREG(st.st_mode)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500102 tmp = malloc(strlen(filename) + 8);
103 if (tmp) {
104 sprintf(tmp, "%s-XXXXXX", filename);
105 fd = mkstemp(tmp);
106 if (fd >= 0) {
107 file = fdopen(fd, "w");
108 opened = tmp;
109 }
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500110 fchmod(fd, 0644);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500111 }
112 }
113
114 if (!file) {
115 file = fopen(filename, "w");
116 opened = filename;
117 }
118
Theodore Ts'od3f91792003-01-25 00:26:48 -0500119 DBG(printf("cache file %s (really %s)\n", filename, opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500120
121 if (!file) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500122 ret = errno;
123 goto errout;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500124 }
125 }
126
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500127 list_for_each(p, &cache->bic_devs) {
128 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
129 if (!dev->bid_type)
130 continue;
131 if ((ret = save_dev(dev, file)) < 0)
132 break;
133 }
134
135 if (ret >= 0) {
136 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
137 ret = 1;
138 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500139
140 if (file != stdout) {
141 fclose(file);
142 if (opened != filename) {
143 if (ret < 0) {
144 unlink(opened);
Theodore Ts'od3f91792003-01-25 00:26:48 -0500145 DBG(printf("unlinked temp cache %s\n", opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500146 } else {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500147 char *backup;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500148
Theodore Ts'od3f91792003-01-25 00:26:48 -0500149 backup = malloc(strlen(filename) + 5);
150 if (backup) {
151 sprintf(backup, "%s.old", filename);
152 unlink(backup);
153 link(filename, backup);
154 free(backup);
155 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500156 rename(opened, filename);
Theodore Ts'od3f91792003-01-25 00:26:48 -0500157 DBG(printf("moved temp cache %s\n", opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500158 }
159 }
160 }
161
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500162errout:
Theodore Ts'od3f91792003-01-25 00:26:48 -0500163 if (tmp)
164 free(tmp);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500165 return ret;
166}
167
168#ifdef TEST_PROGRAM
169int main(int argc, char **argv)
170{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500171 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500172 int ret;
173
174 if (argc > 2) {
175 fprintf(stderr, "Usage: %s [filename]\n"
176 "Test loading/saving a cache (filename)\n", argv[0]);
177 exit(1);
178 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500179
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500180 if ((cache = blkid_new_cache()) == NULL) {
181 fprintf(stderr, "%s: error creating cache\n", argv[0]);
182 exit(1);
183 }
184 if ((ret = blkid_probe_all(cache)) < 0) {
185 fprintf(stderr, "error (%d) probing devices\n", ret);
186 exit(1);
187 }
188 cache->bic_filename = blkid_strdup(argv[1]);
189
190 if ((ret = blkid_flush_cache(cache)) < 0) {
191 fprintf(stderr, "error (%d) saving cache\n", ret);
192 exit(1);
193 }
194
195 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500196
197 return ret;
198}
199#endif