sbin/hammer: Redo e4323571 partly (after reverted by 03d5db37)
[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
37 static void config_get(const char *dirpath, struct hammer_ioc_config *config);
38 static void config_set(const char *dirpath, struct hammer_ioc_config *config);
39 static void config_remove_path(void);
40
41 static char *ConfigPath;
42
43 /*
44  * hammer config [<fs> [configfile]]
45  *
46  * Prints out the hammer cleanup configuration for the specified HAMMER
47  * filesystem(s) or the current filesystem.
48  */
49 void
50 hammer_cmd_config(char **av, int ac)
51 {
52         struct hammer_ioc_config config;
53         char *dirpath;
54         ssize_t n;
55         int fd;
56
57         bzero(&config, sizeof(config));
58         if (ac == 0) {
59                 config_get(".", &config);
60                 if (config.head.error == 0) {
61                         printf("%s", config.config.text);
62                 } else {
63                         errx(2, "hammer config: no configuration found");
64                         /* not reached */
65                 }
66                 return;
67         }
68         dirpath = av[0];
69         if (ac == 1) {
70                 config_get(dirpath, &config);
71                 if (config.head.error == 0) {
72                         printf("%s", config.config.text);
73                 } else {
74                         errx(2, "hammer config: no configuration found");
75                         /* not reached */
76                 }
77                 return;
78         }
79         config_get(dirpath, &config);   /* ignore errors */
80         config.head.error = 0;
81
82         fd = open(av[1], O_RDONLY);
83         if (fd < 0) {
84                 err(2, "hammer config: %s", av[1]);
85                 /* not reached */
86         }
87         n = read(fd, config.config.text, sizeof(config.config.text) - 1);
88         if (n == sizeof(config.config.text) - 1) {
89                 err(2, "hammer config: config file too big, limit %zu bytes",
90                     sizeof(config.config.text) - 1);
91                 /* not reached */
92         }
93         bzero(config.config.text + n, sizeof(config.config.text) - n);
94         config_set(dirpath, &config);
95         close(fd);
96 }
97
98 /*
99  * hammer viconfig [<fs>]
100  */
101 void
102 hammer_cmd_viconfig(char **av, int ac)
103 {
104         struct hammer_ioc_config config;
105         struct timeval times[2];
106         const char *dirpath;
107         struct stat st;
108         char *runcmd, *editor, *tmp;
109         char path[32];
110         ssize_t n;
111         int fd;
112
113         if (ac > 1) {
114                 errx(1, "hammer viconfig: 0 or 1 argument (<fs>) only");
115                 /* not reached */
116         }
117         if (ac == 0)
118                 dirpath = ".";
119         else
120                 dirpath = av[0];
121         config_get(dirpath, &config);
122         if (config.head.error == ENOENT) {
123                 snprintf(config.config.text, sizeof(config.config.text),
124                         "%s",
125                         "# No configuration present, here are some defaults\n"
126                         "# you can uncomment.  Also remove these instructions\n"
127                         "#\n"
128                         "#snapshots 1d 60d\n"
129                         "#prune     1d 5m\n"
130                         "#rebalance 1d 5m\n"
131                         "#dedup     1d 5m\n"
132                         "#reblock   1d 5m\n"
133                         "#recopy    30d 10m\n");
134                 config.head.error = 0;
135         }
136         if (config.head.error) {
137                 errx(2, "hammer viconfig: read config failed error: %s",
138                         strerror(config.head.error));
139                 /* not reached */
140         }
141
142         /*
143          * Edit a temporary file and write back if it was modified.
144          * Adjust the mtime back one second so a quick edit is not
145          * improperly detected as not having been modified.
146          */
147         snprintf(path, sizeof(path), "/tmp/configXXXXXXXXXX");
148         mkstemp(path);
149         ConfigPath = path;
150         atexit(config_remove_path);
151
152         fd = open(path, O_RDWR|O_CREAT|O_TRUNC, 0600);
153         if (fd < 0)
154                 err(2, "hammer viconfig: creating temporary file %s", path);
155         write(fd, config.config.text, strlen(config.config.text));
156         if (fstat(fd, &st) < 0)
157                 err(2, "hammer viconfig");
158         times[0].tv_sec = st.st_mtime - 1;
159         times[0].tv_usec = 0;
160         times[1] = times[0];
161         close(fd);
162         utimes(path, times);
163
164         if ((tmp = getenv("EDITOR")) != NULL ||
165             (tmp = getenv("VISUAL")) != NULL)
166                 editor = strdup(tmp);
167         else
168                 editor = strdup("vi");
169
170         asprintf(&runcmd, "%s %s", editor, path);
171         system(runcmd);
172
173         if (stat(path, &st) < 0)
174                 err(2, "hammer viconfig: unable to stat file after vi");
175         if (times[0].tv_sec == st.st_mtime) {
176                 printf("hammer viconfig: no changes were made\n");
177                 remove(path);
178                 return;
179         }
180         fd = open(path, O_RDONLY);
181         if (fd < 0)
182                 err(2, "hammer viconfig: unable to read %s", path);
183         remove(path);
184         n = read(fd, config.config.text, sizeof(config.config.text) - 1);
185         if (n < 0)
186                 err(2, "hammer viconfig: unable to read %s", path);
187         if (n == sizeof(config.config.text) - 1) {
188                 err(2, "hammer config: config file too big, limit %zu bytes",
189                     sizeof(config.config.text) - 1);
190                 /* not reached */
191         }
192         bzero(config.config.text + n, sizeof(config.config.text) - n);
193         config_set(dirpath, &config);
194         free(editor);
195         free(runcmd);
196 }
197
198 static void
199 config_get(const char *dirpath, struct hammer_ioc_config *config)
200 {
201         struct hammer_ioc_version version;
202         int fd;
203
204         bzero(&version, sizeof(version));
205         if ((fd = open(dirpath, O_RDONLY)) < 0)
206                 err(2, "hammer config: unable to open directory %s", dirpath);
207         if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
208                 errx(2, "hammer config: not a HAMMER filesystem!");
209         if (ioctl(fd, HAMMERIOC_GET_CONFIG, config) < 0)
210                 errx(2, "hammer config: config_get");
211         close(fd);
212 }
213
214 static void
215 config_set(const char *dirpath, struct hammer_ioc_config *config)
216 {
217         struct hammer_ioc_version version;
218         int fd;
219
220         bzero(&version, sizeof(version));
221         if ((fd = open(dirpath, O_RDONLY)) < 0)
222                 errx(2, "hammer config: unable to open directory %s", dirpath);
223         if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
224                 errx(2, "hammer config: not a HAMMER filesystem!");
225         if (ioctl(fd, HAMMERIOC_SET_CONFIG, config) < 0)
226                 err(2, "hammer config");
227         close(fd);
228 }
229
230 static void
231 config_remove_path(void)
232 {
233         remove(ConfigPath);
234 }