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