| Commit | Line | Data |
|---|---|---|
| 83f2a3aa MD |
1 | /* |
| 2 | * Copyright (c) 2009 The DragonFly Project. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@backplane.com> | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * | |
| 11 | * 1. Redistributions of source code must retain the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer. | |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer in | |
| 15 | * the documentation and/or other materials provided with the | |
| 16 | * distribution. | |
| 17 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 18 | * contributors may be used to endorse or promote products derived | |
| 19 | * from this software without specific, prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 29 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 32 | * SUCH DAMAGE. | |
| 33 | */ | |
| 34 | ||
| 35 | #include "hammer.h" | |
| 36 | #include <sys/param.h> | |
| 37 | #include <sys/mount.h> | |
| 38 | #include <sys/types.h> | |
| 39 | #include <sys/stat.h> | |
| 40 | #include <unistd.h> | |
| 41 | #include <string.h> | |
| 42 | #include <time.h> | |
| 43 | ||
| 44 | static void config_get(const char *dirpath, struct hammer_ioc_config *config); | |
| 45 | static void config_set(const char *dirpath, struct hammer_ioc_config *config); | |
| 46 | static void config_remove_path(void); | |
| 47 | ||
| 48 | char *ConfigPath; | |
| 49 | ||
| 50 | /* | |
| 51 | * hammer config [<fs> [configfile]] | |
| 52 | * | |
| 53 | * Prints out the hammer cleanup configuration for the specified HAMMER | |
| 54 | * filesystem(s) or the current filesystem. | |
| 55 | */ | |
| 56 | void | |
| 57 | hammer_cmd_config(char **av, int ac) | |
| 58 | { | |
| 59 | struct hammer_ioc_config config; | |
| 60 | char *dirpath; | |
| 61 | ssize_t n; | |
| 62 | int fd; | |
| 63 | ||
| 64 | bzero(&config, sizeof(config)); | |
| 65 | if (ac == 0) { | |
| 66 | config_get(".", &config); | |
| 67 | if (config.head.error == 0) { | |
| 68 | printf("%s", config.config.text); | |
| 69 | } else { | |
| 70 | errx(2, "hammer config: no configuration found"); | |
| 71 | /* not reached */ | |
| 72 | } | |
| 73 | return; | |
| 74 | } | |
| 75 | dirpath = av[0]; | |
| 76 | if (ac == 1) { | |
| 77 | config_get(dirpath, &config); | |
| 78 | if (config.head.error == 0) { | |
| 79 | printf("%s", config.config.text); | |
| 80 | } else { | |
| 81 | errx(2, "hammer config: no configuration found"); | |
| 82 | /* not reached */ | |
| 83 | } | |
| 84 | return; | |
| 85 | } | |
| 86 | config_get(dirpath, &config); /* ignore errors */ | |
| 87 | config.head.error = 0; | |
| 88 | ||
| 89 | fd = open(av[1], O_RDONLY); | |
| 154eb1f4 SK |
90 | if (fd < 0) { |
| 91 | err(2, "hammer config: %s", av[1]); | |
| 92 | /* not reached */ | |
| 93 | } | |
| 83f2a3aa MD |
94 | n = read(fd, config.config.text, sizeof(config.config.text) - 1); |
| 95 | if (n == sizeof(config.config.text) - 1) { | |
| 13a23276 | 96 | err(2, "hammer config: config file too big, limit %zu bytes", |
| 83f2a3aa MD |
97 | sizeof(config.config.text) - 1); |
| 98 | /* not reached */ | |
| 99 | } | |
| 100 | bzero(config.config.text + n, sizeof(config.config.text) - n); | |
| 101 | config_set(dirpath, &config); | |
| 102 | close(fd); | |
| 103 | } | |
| 104 | ||
| 105 | /* | |
| 106 | * hammer viconfig [<fs>] | |
| 107 | */ | |
| 108 | void | |
| 109 | hammer_cmd_viconfig(char **av, int ac) | |
| 110 | { | |
| 111 | struct hammer_ioc_config config; | |
| 112 | struct timeval times[2]; | |
| 113 | const char *dirpath; | |
| 114 | struct stat st; | |
| 93c6d6c7 | 115 | char *runcmd, *editor, *tmp; |
| 83f2a3aa MD |
116 | char path[32]; |
| 117 | ssize_t n; | |
| 118 | int fd; | |
| 119 | ||
| 120 | if (ac > 1) { | |
| 121 | errx(1, "hammer viconfig: 0 or 1 argument (<fs>) only"); | |
| 122 | /* not reached */ | |
| 123 | } | |
| 124 | if (ac == 0) | |
| 125 | dirpath = "."; | |
| 126 | else | |
| 127 | dirpath = av[0]; | |
| 128 | config_get(dirpath, &config); | |
| 129 | if (config.head.error == ENOENT) { | |
| 130 | snprintf(config.config.text, sizeof(config.config.text), | |
| 131 | "%s", | |
| 132 | "# No configuration present, here are some defaults\n" | |
| 133 | "# you can uncomment. Also remove these instructions\n" | |
| 134 | "#\n" | |
| 135 | "#snapshots 1d 60d\n" | |
| 136 | "#prune 1d 5m\n" | |
| 137 | "#rebalance 1d 5m\n" | |
| bb29b5d8 | 138 | "#dedup 1d 5m\n" |
| 83f2a3aa | 139 | "#reblock 1d 5m\n" |
| 42fd37d3 | 140 | "#recopy 30d 10m\n"); |
| 83f2a3aa MD |
141 | config.head.error = 0; |
| 142 | } | |
| 143 | if (config.head.error) { | |
| 144 | errx(2, "hammer viconfig: read config failed error: %s", | |
| 145 | strerror(config.head.error)); | |
| 146 | /* not reached */ | |
| 147 | } | |
| 148 | ||
| 149 | /* | |
| 150 | * Edit a temporary file and write back if it was modified. | |
| 151 | * Adjust the mtime back one second so a quick edit is not | |
| 152 | * improperly detected as not having been modified. | |
| 153 | */ | |
| 154 | snprintf(path, sizeof(path), "/tmp/configXXXXXXXXXX"); | |
| 155 | mkstemp(path); | |
| 156 | ConfigPath = path; | |
| 157 | atexit(config_remove_path); | |
| 158 | ||
| 159 | fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0600); | |
| 160 | if (fd < 0) | |
| 161 | err(2, "hammer viconfig: creating temporary file %s", path); | |
| 162 | write(fd, config.config.text, strlen(config.config.text)); | |
| 163 | if (fstat(fd, &st) < 0) | |
| 164 | err(2, "hammer viconfig"); | |
| 165 | times[0].tv_sec = st.st_mtime - 1; | |
| 166 | times[0].tv_usec = 0; | |
| 167 | times[1] = times[0]; | |
| 168 | close(fd); | |
| 169 | utimes(path, times); | |
| 170 | ||
| 93c6d6c7 AHJ |
171 | if ((tmp = getenv("EDITOR")) != NULL || |
| 172 | (tmp = getenv("VISUAL")) != NULL) | |
| 173 | editor = strdup(tmp); | |
| 174 | else | |
| 175 | editor = strdup("vi"); | |
| 176 | ||
| 177 | asprintf(&runcmd, "%s %s", editor, path); | |
| 83f2a3aa MD |
178 | system(runcmd); |
| 179 | ||
| 180 | if (stat(path, &st) < 0) | |
| 181 | err(2, "hammer viconfig: unable to stat file after vi"); | |
| 182 | if (times[0].tv_sec == st.st_mtime) { | |
| 183 | printf("hammer viconfig: no changes were made\n"); | |
| 184 | remove(path); | |
| 185 | return; | |
| 186 | } | |
| 187 | fd = open(path, O_RDONLY); | |
| 188 | if (fd < 0) | |
| 189 | err(2, "hammer viconfig: unable to read %s", path); | |
| 190 | remove(path); | |
| 191 | n = read(fd, config.config.text, sizeof(config.config.text) - 1); | |
| 192 | if (n < 0) | |
| 193 | err(2, "hammer viconfig: unable to read %s", path); | |
| 194 | if (n == sizeof(config.config.text) - 1) { | |
| 13a23276 | 195 | err(2, "hammer config: config file too big, limit %zu bytes", |
| 83f2a3aa MD |
196 | sizeof(config.config.text) - 1); |
| 197 | /* not reached */ | |
| 198 | } | |
| 199 | bzero(config.config.text + n, sizeof(config.config.text) - n); | |
| 200 | config_set(dirpath, &config); | |
| 93c6d6c7 AHJ |
201 | free(editor); |
| 202 | free(runcmd); | |
| 83f2a3aa MD |
203 | } |
| 204 | ||
| 205 | static void | |
| 206 | config_get(const char *dirpath, struct hammer_ioc_config *config) | |
| 207 | { | |
| 208 | struct hammer_ioc_version version; | |
| 209 | int fd; | |
| 210 | ||
| 211 | bzero(&version, sizeof(version)); | |
| 212 | if ((fd = open(dirpath, O_RDONLY)) < 0) | |
| 213 | err(2, "hammer config: unable to open directory %s", dirpath); | |
| 214 | if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) | |
| 215 | errx(2, "hammer config: not a HAMMER filesystem!"); | |
| 216 | ||
| 217 | if (ioctl(fd, HAMMERIOC_GET_CONFIG, config) < 0) | |
| 218 | errx(2, "hammer config: config_get"); | |
| 219 | close(fd); | |
| 220 | } | |
| 221 | ||
| 222 | static void | |
| 223 | config_set(const char *dirpath, struct hammer_ioc_config *config) | |
| 224 | { | |
| 225 | struct hammer_ioc_version version; | |
| 226 | int fd; | |
| 227 | ||
| 228 | bzero(&version, sizeof(version)); | |
| 229 | if ((fd = open(dirpath, O_RDONLY)) < 0) | |
| 230 | errx(2, "hammer config: unable to open directory %s", dirpath); | |
| 231 | if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) | |
| 232 | errx(2, "hammer config: not a HAMMER filesystem!"); | |
| 233 | if (ioctl(fd, HAMMERIOC_SET_CONFIG, config) < 0) | |
| 93c6d6c7 | 234 | err(2, "hammer config"); |
| 83f2a3aa MD |
235 | close(fd); |
| 236 | } | |
| 237 | ||
| 238 | static void | |
| 239 | config_remove_path(void) | |
| 240 | { | |
| 241 | remove(ConfigPath); | |
| 242 | } |