| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 1 | /* |
| 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 |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 29 | #define DBG(x) x |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 30 | #else |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 31 | #define DBG(x) |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | static int save_dev(blkid_dev *dev, FILE *file) |
| 35 | { |
| 36 | struct list_head *p; |
| 37 | |
| 38 | if (!dev) |
| 39 | return 0; |
| 40 | |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 41 | DBG(printf("device %s, type %s\n", dev->bid_name, dev->bid_type)); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 42 | |
| 43 | fprintf(file, |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 44 | "<device TYPE=\"%s\" DEVNO=\"0x%04lx\" ID=\"%d\" TIME=\"%lu\"", |
| 45 | dev->bid_type, (unsigned long) dev->bid_devno, |
| 46 | dev->bid_id, dev->bid_time); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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 | |
| 57 | int 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 | */ |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 86 | int blkid_save_cache(blkid_cache *cache, const char *filename) |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 87 | { |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 88 | char *tmp = NULL; |
| 89 | const char *opened = NULL; |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 90 | FILE *file = NULL; |
| Theodore Ts'o | 3a1d6f3 | 2003-01-24 23:11:29 -0500 | [diff] [blame] | 91 | int fd, ret; |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 92 | |
| 93 | if (!cache) |
| 94 | return -BLKID_ERR_PARAM; |
| 95 | |
| 96 | if (list_empty(&cache->bic_devs) || |
| 97 | !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) { |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 98 | DBG(printf("empty cache, not saving\n")); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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)) { |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 113 | DBG(printf("can't write to cache file %s\n", filename)); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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)) { |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 125 | tmp = malloc(strlen(filename) + 8); |
| 126 | if (tmp) { |
| 127 | sprintf(tmp, "%s-XXXXXX", filename); |
| 128 | fd = mkstemp(tmp); |
| 129 | if (fd >= 0) { |
| 130 | file = fdopen(fd, "w"); |
| 131 | opened = tmp; |
| 132 | } |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | if (!file) { |
| 137 | file = fopen(filename, "w"); |
| 138 | opened = filename; |
| 139 | } |
| 140 | |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 141 | DBG(printf("cache file %s (really %s)\n", filename, opened)); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 142 | |
| 143 | if (!file) { |
| 144 | perror(opened); |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 145 | if (tmp) |
| 146 | free(tmp); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 147 | return errno; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | ret = blkid_save_cache_file(cache, file); |
| 152 | |
| 153 | if (file != stdout) { |
| 154 | fclose(file); |
| 155 | if (opened != filename) { |
| 156 | if (ret < 0) { |
| 157 | unlink(opened); |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 158 | DBG(printf("unlinked temp cache %s\n", opened)); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 159 | } else { |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 160 | char *backup; |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 161 | |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 162 | backup = malloc(strlen(filename) + 5); |
| 163 | if (backup) { |
| 164 | sprintf(backup, "%s.old", filename); |
| 165 | unlink(backup); |
| 166 | link(filename, backup); |
| 167 | free(backup); |
| 168 | } |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 169 | rename(opened, filename); |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 170 | DBG(printf("moved temp cache %s\n", opened)); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| Theodore Ts'o | d3f9179 | 2003-01-25 00:26:48 -0500 | [diff] [blame^] | 175 | if (tmp) |
| 176 | free(tmp); |
| Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | #ifdef TEST_PROGRAM |
| 181 | int main(int argc, char **argv) |
| 182 | { |
| 183 | blkid_cache *cache = NULL; |
| 184 | int ret; |
| 185 | |
| 186 | if (argc > 2) { |
| 187 | fprintf(stderr, "Usage: %s [filename]\n" |
| 188 | "Test loading/saving a cache (filename)\n", argv[0]); |
| 189 | exit(1); |
| 190 | } |
| 191 | if ((ret = blkid_probe_all(&cache) < 0)) |
| 192 | fprintf(stderr, "error probing devices\n"); |
| 193 | else if ((ret = blkid_save_cache(cache, argv[1])) < 0) |
| 194 | fprintf(stderr, "error %d saving cache to %s\n", ret, |
| 195 | argv[1] ? argv[1] : BLKID_CACHE_FILE); |
| 196 | |
| 197 | blkid_free_cache(cache); |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | #endif |