1 /*
2  * save.c - write the cache struct to disk
3  *
4  * Copyright (C) 2001 by Andreas Dilger
5  * Copyright (C) 2003 Theodore Ts'o
6  *
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
27 #include "blkidP.h"
28 
save_dev(blkid_dev dev,FILE * file)29 static int save_dev(blkid_dev dev, FILE *file)
30 {
31 	struct list_head *p;
32 
33 	if (!dev || dev->bid_name[0] != '/')
34 		return 0;
35 
36 	DBG(DEBUG_SAVE,
37 	    printf("device %s, type %s\n", dev->bid_name, dev->bid_type ?
38 		   dev->bid_type : "(null)"));
39 
40 	fprintf(file,
41 		"<device DEVNO=\"0x%04lx\" TIME=\"%ld\"",
42 		(unsigned long) dev->bid_devno, (long) dev->bid_time);
43 	if (dev->bid_pri)
44 		fprintf(file, " PRI=\"%d\"", dev->bid_pri);
45 	list_for_each(p, &dev->bid_tags) {
46 		blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
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 
54 /*
55  * Write out the cache struct to the cache file on disk.
56  */
blkid_flush_cache(blkid_cache cache)57 int blkid_flush_cache(blkid_cache cache)
58 {
59 	struct list_head *p;
60 	char *tmp = NULL;
61 	const char *opened = NULL;
62 	const char *filename;
63 	FILE *file = NULL;
64 	int fd, ret = 0;
65 	struct stat st;
66 
67 	if (!cache)
68 		return -BLKID_ERR_PARAM;
69 
70 	if (list_empty(&cache->bic_devs) ||
71 	    !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
72 		DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
73 		return 0;
74 	}
75 
76 	filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
77 
78 	/* If we can't write to the cache file, then don't even try */
79 	if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
80 	    (ret == 0 && access(filename, W_OK) < 0)) {
81 		DBG(DEBUG_SAVE,
82 		    printf("can't write to cache file %s\n", filename));
83 		return 0;
84 	}
85 
86 	/*
87 	 * Try and create a temporary file in the same directory so
88 	 * that in case of error we don't overwrite the cache file.
89 	 * If the cache file doesn't yet exist, it isn't a regular
90 	 * file (e.g. /dev/null or a socket), or we couldn't create
91 	 * a temporary file then we open it directly.
92 	 */
93 	if (ret == 0 && S_ISREG(st.st_mode)) {
94 		tmp = malloc(strlen(filename) + 8);
95 		if (tmp) {
96 			sprintf(tmp, "%s-XXXXXX", filename);
97 			fd = mkstemp(tmp);
98 			if (fd >= 0) {
99 				file = fdopen(fd, "w");
100 				opened = tmp;
101 			}
102 			fchmod(fd, 0644);
103 		}
104 	}
105 
106 	if (!file) {
107 		file = fopen(filename, "w");
108 		opened = filename;
109 	}
110 
111 	DBG(DEBUG_SAVE,
112 	    printf("writing cache file %s (really %s)\n",
113 		   filename, opened));
114 
115 	if (!file) {
116 		ret = errno;
117 		goto errout;
118 	}
119 
120 	list_for_each(p, &cache->bic_devs) {
121 		blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
122 		if (!dev->bid_type)
123 			continue;
124 		if ((ret = save_dev(dev, file)) < 0)
125 			break;
126 	}
127 
128 	if (ret >= 0) {
129 		cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
130 		ret = 1;
131 	}
132 
133 	fclose(file);
134 	if (opened != filename) {
135 		if (ret < 0) {
136 			unlink(opened);
137 			DBG(DEBUG_SAVE,
138 			    printf("unlinked temp cache %s\n", opened));
139 		} else {
140 			char *backup;
141 
142 			backup = malloc(strlen(filename) + 5);
143 			if (backup) {
144 				sprintf(backup, "%s.old", filename);
145 				unlink(backup);
146 				link(filename, backup);
147 				free(backup);
148 			}
149 			rename(opened, filename);
150 			DBG(DEBUG_SAVE,
151 			    printf("moved temp cache %s\n", opened));
152 		}
153 	}
154 
155 errout:
156 	free(tmp);
157 	return ret;
158 }
159 
160 #ifdef TEST_PROGRAM
main(int argc,char ** argv)161 int main(int argc, char **argv)
162 {
163 	blkid_cache cache = NULL;
164 	int ret;
165 
166 	blkid_debug_mask = DEBUG_ALL;
167 	if (argc > 2) {
168 		fprintf(stderr, "Usage: %s [filename]\n"
169 			"Test loading/saving a cache (filename)\n", argv[0]);
170 		exit(1);
171 	}
172 
173 	if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
174 		fprintf(stderr, "%s: error creating cache (%d)\n",
175 			argv[0], ret);
176 		exit(1);
177 	}
178 	if ((ret = blkid_probe_all(cache)) < 0) {
179 		fprintf(stderr, "error (%d) probing devices\n", ret);
180 		exit(1);
181 	}
182 	cache->bic_filename = blkid_strdup(argv[1]);
183 
184 	if ((ret = blkid_flush_cache(cache)) < 0) {
185 		fprintf(stderr, "error (%d) saving cache\n", ret);
186 		exit(1);
187 	}
188 
189 	blkid_put_cache(cache);
190 
191 	return ret;
192 }
193 #endif
194