blob: 0b3f1ae37f6f60bb4a34e1af5aa0bfdd6135eacf [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
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
34static 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
57int 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 */
86int blkid_save_cache(blkid_cache *cache, char *filename)
87{
88 char tmp[4096] = { '\0', };
89 char *opened = NULL;
90 FILE *file = NULL;
Theodore Ts'o3a1d6f32003-01-24 23:11:29 -050091 int fd, ret;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050092
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);
Theodore Ts'o3a1d6f32003-01-24 23:11:29 -0500126 fd = mkstemp(tmp);
127 if (fd >= 0) {
128 file = fdopen(fd, "w");
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500129 opened = tmp;
130 }
131 }
132
133 if (!file) {
134 file = fopen(filename, "w");
135 opened = filename;
136 }
137
138 DEB_SAVE("cache file %s (really %s)\n", filename, opened);
139
140 if (!file) {
141 perror(opened);
142 return errno;
143 }
144 }
145
146 ret = blkid_save_cache_file(cache, file);
147
148 if (file != stdout) {
149 fclose(file);
150 if (opened != filename) {
151 if (ret < 0) {
152 unlink(opened);
153 DEB_SAVE("unlinked temp cache %s\n", opened);
154 } else {
155 char backup[4096];
156
157 snprintf(backup, sizeof(backup) - 1, "%s.old",
158 filename);
159 unlink(backup);
160 link(filename, backup);
161 rename(opened, filename);
162 DEB_SAVE("moved temp cache %s\n", opened);
163 }
164 }
165 }
166
167 return ret;
168}
169
170#ifdef TEST_PROGRAM
171int main(int argc, char **argv)
172{
173 blkid_cache *cache = NULL;
174 int ret;
175
176 if (argc > 2) {
177 fprintf(stderr, "Usage: %s [filename]\n"
178 "Test loading/saving a cache (filename)\n", argv[0]);
179 exit(1);
180 }
181 if ((ret = blkid_probe_all(&cache) < 0))
182 fprintf(stderr, "error probing devices\n");
183 else if ((ret = blkid_save_cache(cache, argv[1])) < 0)
184 fprintf(stderr, "error %d saving cache to %s\n", ret,
185 argv[1] ? argv[1] : BLKID_CACHE_FILE);
186
187 blkid_free_cache(cache);
188
189 return ret;
190}
191#endif