bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 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_ERRNO_H |
| 22 | #include <errno.h> |
| 23 | #endif |
| 24 | #include "blkidP.h" |
| 25 | |
| 26 | static int save_dev(blkid_dev dev, FILE *file) |
| 27 | { |
| 28 | struct list_head *p; |
| 29 | |
| 30 | if (!dev || dev->bid_name[0] != '/') |
| 31 | return 0; |
| 32 | |
| 33 | DBG(DEBUG_SAVE, |
| 34 | printf("device %s, type %s\n", dev->bid_name, dev->bid_type ? |
| 35 | dev->bid_type : "(null)")); |
| 36 | |
| 37 | fprintf(file, "<device DEVNO=\"0x%04lx\" TIME=\"%ld.%ld\"", |
| 38 | (unsigned long) dev->bid_devno, |
| 39 | (long) dev->bid_time, |
| 40 | (long) dev->bid_utime); |
| 41 | |
| 42 | if (dev->bid_pri) |
| 43 | fprintf(file, " PRI=\"%d\"", dev->bid_pri); |
| 44 | list_for_each(p, &dev->bid_tags) { |
| 45 | blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags); |
| 46 | fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val); |
| 47 | } |
| 48 | fprintf(file, ">%s</device>\n", dev->bid_name); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Write out the cache struct to the cache file on disk. |
| 55 | */ |
| 56 | int blkid_flush_cache(blkid_cache cache) |
| 57 | { |
| 58 | struct list_head *p; |
| 59 | char *tmp = NULL; |
| 60 | char *opened = NULL; |
| 61 | char *filename; |
| 62 | FILE *file = NULL; |
| 63 | int fd, ret = 0; |
| 64 | struct stat st; |
| 65 | |
| 66 | if (!cache) |
| 67 | return -BLKID_ERR_PARAM; |
| 68 | |
| 69 | if (list_empty(&cache->bic_devs) || |
| 70 | !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) { |
| 71 | DBG(DEBUG_SAVE, printf("skipping cache file write\n")); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | filename = cache->bic_filename ? cache->bic_filename : |
| 76 | blkid_get_cache_filename(NULL); |
| 77 | if (!filename) |
| 78 | return -BLKID_ERR_PARAM; |
| 79 | |
| 80 | if (strncmp(filename, |
| 81 | BLKID_RUNTIME_DIR "/", sizeof(BLKID_RUNTIME_DIR)) == 0) { |
| 82 | |
| 83 | /* default destination, create the directory if necessary */ |
| 84 | if (stat(BLKID_RUNTIME_DIR, &st) |
| 85 | && errno == ENOENT |
| 86 | && mkdir(BLKID_RUNTIME_DIR, S_IWUSR| |
| 87 | S_IRUSR|S_IRGRP|S_IROTH| |
| 88 | S_IXUSR|S_IXGRP|S_IXOTH) != 0 |
| 89 | && errno != EEXIST) { |
| 90 | DBG(DEBUG_SAVE, |
| 91 | printf("can't create %s directory for cache file\n", |
| 92 | BLKID_RUNTIME_DIR)); |
| 93 | return 0; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* If we can't write to the cache file, then don't even try */ |
| 98 | if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) || |
| 99 | (ret == 0 && access(filename, W_OK) < 0)) { |
| 100 | DBG(DEBUG_SAVE, |
| 101 | printf("can't write to cache file %s\n", filename)); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Try and create a temporary file in the same directory so |
| 107 | * that in case of error we don't overwrite the cache file. |
| 108 | * If the cache file doesn't yet exist, it isn't a regular |
| 109 | * file (e.g. /dev/null or a socket), or we couldn't create |
| 110 | * a temporary file then we open it directly. |
| 111 | */ |
| 112 | if (ret == 0 && S_ISREG(st.st_mode)) { |
| 113 | tmp = malloc(strlen(filename) + 8); |
| 114 | if (tmp) { |
| 115 | sprintf(tmp, "%s-XXXXXX", filename); |
| 116 | fd = mkstemp(tmp); |
| 117 | if (fd >= 0) { |
| 118 | if (fchmod(fd, 0644) != 0) |
| 119 | DBG(DEBUG_SAVE, printf("%s: fchmod failed\n", filename)); |
| 120 | else if ((file = fdopen(fd, "w"))) |
| 121 | opened = tmp; |
| 122 | if (!file) |
| 123 | close(fd); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (!file) { |
| 129 | file = fopen(filename, "w"); |
| 130 | opened = filename; |
| 131 | } |
| 132 | |
| 133 | DBG(DEBUG_SAVE, |
| 134 | printf("writing cache file %s (really %s)\n", |
| 135 | filename, opened)); |
| 136 | |
| 137 | if (!file) { |
| 138 | ret = errno; |
| 139 | goto errout; |
| 140 | } |
| 141 | |
| 142 | list_for_each(p, &cache->bic_devs) { |
| 143 | blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs); |
| 144 | if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE)) |
| 145 | continue; |
| 146 | if ((ret = save_dev(dev, file)) < 0) |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (ret >= 0) { |
| 151 | cache->bic_flags &= ~BLKID_BIC_FL_CHANGED; |
| 152 | ret = 1; |
| 153 | } |
| 154 | |
| 155 | fclose(file); |
| 156 | if (opened != filename) { |
| 157 | if (ret < 0) { |
| 158 | unlink(opened); |
| 159 | DBG(DEBUG_SAVE, |
| 160 | printf("unlinked temp cache %s\n", opened)); |
| 161 | } else { |
| 162 | char *backup; |
| 163 | |
| 164 | backup = malloc(strlen(filename) + 5); |
| 165 | if (backup) { |
| 166 | sprintf(backup, "%s.old", filename); |
| 167 | unlink(backup); |
| 168 | if (link(filename, backup)) { |
| 169 | DBG(DEBUG_SAVE, |
| 170 | printf("can't link %s to %s\n", |
| 171 | filename, backup)); |
| 172 | } |
| 173 | free(backup); |
| 174 | } |
| 175 | if (rename(opened, filename)) { |
| 176 | ret = errno; |
| 177 | DBG(DEBUG_SAVE, |
| 178 | printf("can't rename %s to %s\n", |
| 179 | opened, filename)); |
| 180 | } else { |
| 181 | DBG(DEBUG_SAVE, |
| 182 | printf("moved temp cache %s\n", opened)); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | errout: |
| 188 | free(tmp); |
| 189 | if (filename != cache->bic_filename) |
| 190 | free(filename); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | #ifdef TEST_PROGRAM |
| 195 | int main(int argc, char **argv) |
| 196 | { |
| 197 | blkid_cache cache = NULL; |
| 198 | int ret; |
| 199 | |
| 200 | blkid_init_debug(DEBUG_ALL); |
| 201 | if (argc > 2) { |
| 202 | fprintf(stderr, "Usage: %s [filename]\n" |
| 203 | "Test loading/saving a cache (filename)\n", argv[0]); |
| 204 | exit(1); |
| 205 | } |
| 206 | |
| 207 | if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { |
| 208 | fprintf(stderr, "%s: error creating cache (%d)\n", |
| 209 | argv[0], ret); |
| 210 | exit(1); |
| 211 | } |
| 212 | if ((ret = blkid_probe_all(cache)) < 0) { |
| 213 | fprintf(stderr, "error (%d) probing devices\n", ret); |
| 214 | exit(1); |
| 215 | } |
| 216 | cache->bic_filename = strdup(argv[1]); |
| 217 | |
| 218 | if ((ret = blkid_flush_cache(cache)) < 0) { |
| 219 | fprintf(stderr, "error (%d) saving cache\n", ret); |
| 220 | exit(1); |
| 221 | } |
| 222 | |
| 223 | blkid_put_cache(cache); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | #endif |