ab02181145714b1d2a206aba6b58d6b0df4ab518
[dragonfly.git] / sbin / hammer / cmd_config.c
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);
90         n = read(fd, config.config.text, sizeof(config.config.text) - 1);
91         if (n == sizeof(config.config.text) - 1) {
92                 err(2, "hammer config: config file too big, limit %zu bytes",
93                     sizeof(config.config.text) - 1);
94                 /* not reached */
95         }
96         bzero(config.config.text + n, sizeof(config.config.text) - n);
97         config_set(dirpath, &config);
98         close(fd);
99 }
100
101 /*
102  * hammer viconfig [<fs>]
103  */
104 void
105 hammer_cmd_viconfig(char **av, int ac)
106 {
107         struct hammer_ioc_config config;
108         struct timeval times[2];
109         const char *dirpath;
110         struct stat st;
111         char *runcmd;
112         char path[32];
113         ssize_t n;
114         int fd;
115
116         if (ac > 1) {
117                 errx(1, "hammer viconfig: 0 or 1 argument (<fs>) only");
118                 /* not reached */
119         }
120         if (ac == 0)
121                 dirpath = ".";
122         else
123                 dirpath = av[0];
124         config_get(dirpath, &config);
125         if (config.head.error == ENOENT) {
126                 snprintf(config.config.text, sizeof(config.config.text),
127                         "%s",
128                         "# No configuration present, here are some defaults\n"
129                         "# you can uncomment.  Also remove these instructions\n"
130                         "#\n"
131                         "#snapshots 1d 60d\n"
132                         "#prune     1d 5m\n"
133                         "#rebalance 1d 5m\n"
134                         "#reblock   1d 5m\n"
135                         "#recopy    30d 10m\n"
136                         "#rebalance 1d 5m\n");
137                 config.head.error = 0;
138         }
139         if (config.head.error) {
140                 errx(2, "hammer viconfig: read config failed error: %s",
141                         strerror(config.head.error));
142                 /* not reached */
143         }
144
145         /*
146          * Edit a temporary file and write back if it was modified.
147          * Adjust the mtime back one second so a quick edit is not
148          * improperly detected as not having been modified.
149          */
150         snprintf(path, sizeof(path), "/tmp/configXXXXXXXXXX");
151         mkstemp(path);
152         ConfigPath = path;
153         atexit(config_remove_path);
154
155         fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0600);
156         if (fd < 0)
157                 err(2, "hammer viconfig: creating temporary file %s", path);
158         write(fd, config.config.text, strlen(config.config.text));
159         if (fstat(fd, &st) < 0)
160                 err(2, "hammer viconfig");
161         times[0].tv_sec = st.st_mtime - 1;
162         times[0].tv_usec = 0;
163         times[1] = times[0];
164         close(fd);
165         utimes(path, times);
166
167         asprintf(&runcmd, "vi %s", path);
168         system(runcmd);
169
170         if (stat(path, &st) < 0)
171                 err(2, "hammer viconfig: unable to stat file after vi");
172         if (times[0].tv_sec == st.st_mtime) {
173                 printf("hammer viconfig: no changes were made\n");
174                 remove(path);
175                 return;
176         }
177         fd = open(path, O_RDONLY);
178         if (fd < 0)
179                 err(2, "hammer viconfig: unable to read %s", path);
180         remove(path);
181         n = read(fd, config.config.text, sizeof(config.config.text) - 1);
182         if (n < 0)
183                 err(2, "hammer viconfig: unable to read %s", path);
184         if (n == sizeof(config.config.text) - 1) {
185                 err(2, "hammer config: config file too big, limit %zu bytes",
186                     sizeof(config.config.text) - 1);
187                 /* not reached */
188         }
189         bzero(config.config.text + n, sizeof(config.config.text) - n);
190         config_set(dirpath, &config);
191 }
192
193 static void
194 config_get(const char *dirpath, struct hammer_ioc_config *config)
195 {
196         struct hammer_ioc_version version;
197         int fd;
198
199         bzero(&version, sizeof(version));
200         if ((fd = open(dirpath, O_RDONLY)) < 0)
201                 err(2, "hammer config: unable to open directory %s", dirpath);
202         if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
203                 errx(2, "hammer config: not a HAMMER filesystem!");
204
205         if (ioctl(fd, HAMMERIOC_GET_CONFIG, config) < 0)
206                 errx(2, "hammer config: config_get");
207         close(fd);
208 }
209
210 static void
211 config_set(const char *dirpath, struct hammer_ioc_config *config)
212 {
213         struct hammer_ioc_version version;
214         int fd;
215
216         bzero(&version, sizeof(version));
217         if ((fd = open(dirpath, O_RDONLY)) < 0)
218                 errx(2, "hammer config: unable to open directory %s", dirpath);
219         if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
220                 errx(2, "hammer config: not a HAMMER filesystem!");
221         if (ioctl(fd, HAMMERIOC_SET_CONFIG, config) < 0)
222                 errx(2, "hammer config: config_set");
223         close(fd);
224 }
225
226 static void
227 config_remove_path(void)
228 {
229         remove(ConfigPath);
230 }