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