blob: 82b9b775412d968382198fd382b742dedbdd926d [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
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050029static int save_dev(blkid_dev dev, FILE *file)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050030{
31 struct list_head *p;
32
33 if (!dev)
34 return 0;
35
Theodore Ts'of0a22d02003-02-22 13:19:53 -050036 DBG(DEBUG_SAVE,
37 printf("device %s, type %s\n", dev->bid_name, dev->bid_type));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050038
39 fprintf(file,
Theodore Ts'obc40efd2003-02-14 01:40:23 -050040 "<device TYPE=\"%s\" DEVNO=\"0x%04lx\" TIME=\"%lu\"",
41 dev->bid_type, (unsigned long) dev->bid_devno, dev->bid_time);
Theodore Ts'oce72b862003-02-14 01:31:45 -050042 if (dev->bid_pri)
43 fprintf(file, " PRI=\"%d\"", dev->bid_pri);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050044 list_for_each(p, &dev->bid_tags) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050045 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050046 if (strcmp(tag->bit_name, "TYPE"))
47 fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
48 }
49 fprintf(file, ">%s</device>\n", dev->bid_name);
50
51 return 0;
52}
53
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050054/*
55 * Write out the cache struct to the cache file on disk.
56 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -050057int blkid_flush_cache(blkid_cache cache)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050058{
Theodore Ts'o50b380b2003-02-12 23:51:21 -050059 struct list_head *p;
Theodore Ts'od3f91792003-01-25 00:26:48 -050060 char *tmp = NULL;
61 const char *opened = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050062 const char *filename;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050063 FILE *file = NULL;
Theodore Ts'o50b380b2003-02-12 23:51:21 -050064 int fd, ret = 0;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050065
66 if (!cache)
67 return -BLKID_ERR_PARAM;
68
69 if (list_empty(&cache->bic_devs) ||
70 !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -050071 DBG(DEBUG_SAVE, printf("empty cache, not saving\n"));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050072 return 0;
73 }
74
Theodore Ts'o50b380b2003-02-12 23:51:21 -050075 filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050076
Theodore Ts'o50b380b2003-02-12 23:51:21 -050077 if (!strcmp(filename, "-"))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050078 file = stdout;
79 else {
80 struct stat st;
81
82 /* If we can't write to the cache file, then don't even try */
83 if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
84 (ret == 0 && access(filename, W_OK) < 0)) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -050085 DBG(DEBUG_SAVE,
86 printf("can't write to cache file %s\n", filename));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050087 return 0;
88 }
89
90 /*
91 * Try and create a temporary file in the same directory so
92 * that in case of error we don't overwrite the cache file.
93 * If the cache file doesn't yet exist, it isn't a regular
94 * file (e.g. /dev/null or a socket), or we couldn't create
95 * a temporary file then we open it directly.
96 */
97 if (ret == 0 && S_ISREG(st.st_mode)) {
Theodore Ts'od3f91792003-01-25 00:26:48 -050098 tmp = malloc(strlen(filename) + 8);
99 if (tmp) {
100 sprintf(tmp, "%s-XXXXXX", filename);
101 fd = mkstemp(tmp);
102 if (fd >= 0) {
103 file = fdopen(fd, "w");
104 opened = tmp;
105 }
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500106 fchmod(fd, 0644);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500107 }
108 }
109
110 if (!file) {
111 file = fopen(filename, "w");
112 opened = filename;
113 }
114
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500115 DBG(DEBUG_SAVE,
116 printf("cache file %s (really %s)\n", filename, opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500117
118 if (!file) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500119 ret = errno;
120 goto errout;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500121 }
122 }
123
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500124 list_for_each(p, &cache->bic_devs) {
125 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
126 if (!dev->bid_type)
127 continue;
128 if ((ret = save_dev(dev, file)) < 0)
129 break;
130 }
131
132 if (ret >= 0) {
133 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
134 ret = 1;
135 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500136
137 if (file != stdout) {
138 fclose(file);
139 if (opened != filename) {
140 if (ret < 0) {
141 unlink(opened);
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500142 DBG(DEBUG_SAVE,
143 printf("unlinked temp cache %s\n", opened));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500144 } else {
Theodore Ts'od3f91792003-01-25 00:26:48 -0500145 char *backup;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500146
Theodore Ts'od3f91792003-01-25 00:26:48 -0500147 backup = malloc(strlen(filename) + 5);
148 if (backup) {
149 sprintf(backup, "%s.old", filename);
150 unlink(backup);
151 link(filename, backup);
152 free(backup);
153 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500154 rename(opened, filename);
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500155 DBG(DEBUG_SAVE,
156 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
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500173 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500174 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