blob: 5a8771e78286da4b6e26a1c1a7dd008715e5d1f2 [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'od3f91792003-01-25 00:26:48 -050045 "<device TYPE=\"%s\" DEVNO=\"0x%04lx\" ID=\"%d\" TIME=\"%lu\"",
46 dev->bid_type, (unsigned long) dev->bid_devno,
47 dev->bid_id, dev->bid_time);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050048 list_for_each(p, &dev->bid_tags) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050049 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050050 if (strcmp(tag->bit_name, "TYPE"))
51 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
52 }
53 fprintf(file, ">%s</device>\n", dev->bid_name);
54
55 return 0;
56}
57
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050058/*
59 * Write out the cache struct to the cache file on disk.
60 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -050061int blkid_flush_cache(blkid_cache cache)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050062{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050063 struct list_head *p;
Theodore Ts'od3f91792003-01-25 00:26:48 -050064 char *tmp = NULL;
65 const char *opened = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050066 const char *filename;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050067 FILE *file = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050068 int fd, ret = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050069
70 if (!cache)
71 return -BLKID_ERR_PARAM;
72
73 if (list_empty(&cache->bic_devs) ||
74 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -050075 DBG(printf("empty cache, not saving\n"));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050076 return 0;
77 }
78
Theodore Ts'o50b380b2003-02-12 23:51:21 -050079 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050080
Theodore Ts'o50b380b2003-02-12 23:51:21 -050081 if (!strcmp(filename, "-"))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050082 file = stdout;
83 else {
84 struct stat st;
85
86 /* If we can't write to the cache file, then don't even try */
87 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
88 (ret == 0 && access(filename, W_OK) < 0)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -050089 DBG(printf("can't write to cache file %s\n", filename));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050090 return 0;
91 }
92
93 /*
94 * Try and create a temporary file in the same directory so
95 * that in case of error we don't overwrite the cache file.
96 * If the cache file doesn't yet exist, it isn't a regular
97 * file (e.g. /dev/null or a socket), or we couldn't create
98 * a temporary file then we open it directly.
99 */
100 if (ret == 0 && S_ISREG(st.st_mode)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500101 tmp = malloc(strlen(filename) + 8);
102 if (tmp) {
103 sprintf(tmp, "%s-XXXXXX", filename);
104 fd = mkstemp(tmp);
105 if (fd >= 0) {
106 file = fdopen(fd, "w");
107 opened = tmp;
108 }
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500109 fchmod(fd, 0644);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500110 }
111 }
112
113 if (!file) {
114 file = fopen(filename, "w");
115 opened = filename;
116 }
117
Theodore Ts'od3f91792003-01-25 00:26:48 -0500118 DBG(printf("cache file %s (really %s)\n", filename, opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500119
120 if (!file) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500121 ret = errno;
122 goto errout;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500123 }
124 }
125
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500126 list_for_each(p, &cache->bic_devs) {
127 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
128 if (!dev->bid_type)
129 continue;
130 if ((ret = save_dev(dev, file)) < 0)
131 break;
132 }
133
134 if (ret >= 0) {
135 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
136 ret = 1;
137 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500138
139 if (file != stdout) {
140 fclose(file);
141 if (opened != filename) {
142 if (ret < 0) {
143 unlink(opened);
Theodore Ts'od3f91792003-01-25 00:26:48 -0500144 DBG(printf("unlinked temp cache %s\n", opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500145 } else {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500146 char *backup;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500147
Theodore Ts'od3f91792003-01-25 00:26:48 -0500148 backup = malloc(strlen(filename) + 5);
149 if (backup) {
150 sprintf(backup, "%s.old", filename);
151 unlink(backup);
152 link(filename, backup);
153 free(backup);
154 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500155 rename(opened, filename);
Theodore Ts'od3f91792003-01-25 00:26:48 -0500156 DBG(printf("moved temp cache %s\n", opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500157 }
158 }
159 }
160
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500161errout:
Theodore Ts'od3f91792003-01-25 00:26:48 -0500162 if (tmp)
163 free(tmp);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500164 return ret;
165}
166
167#ifdef TEST_PROGRAM
168int main(int argc, char **argv)
169{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500170 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500171 int ret;
172
173 if (argc > 2) {
174 fprintf(stderr, "Usage: %s [filename]\n"
175 "Test loading/saving a cache (filename)\n", argv[0]);
176 exit(1);
177 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500178
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500179 if ((cache = blkid_new_cache()) == NULL) {
180 fprintf(stderr, "%s: error creating cache\n", argv[0]);
181 exit(1);
182 }
183 if ((ret = blkid_probe_all(cache)) < 0) {
184 fprintf(stderr, "error (%d) probing devices\n", ret);
185 exit(1);
186 }
187 cache->bic_filename = blkid_strdup(argv[1]);
188
189 if ((ret = blkid_flush_cache(cache)) < 0) {
190 fprintf(stderr, "error (%d) saving cache\n", ret);
191 exit(1);
192 }
193
194 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500195
196 return ret;
197}
198#endif