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