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