Install dma(8) config files with mode 640 as root:mail to prevent ``normal''
[dragonfly.git] / sbin / hammer / cmd_prune.c
blobd529fc98e5127f6acb03fe466115106e6824e64e
1 /*
2 * Copyright (c) 2008 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:
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.
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.
34 * $DragonFly: src/sbin/hammer/Attic/cmd_prune.c,v 1.1 2008/02/04 08:34:22 dillon Exp $
37 #include "hammer.h"
39 static void hammer_prune_load_file(hammer_tid_t now_tid,
40 struct hammer_ioc_prune *prune,
41 const char *filesystem, const char *filename);
42 static int hammer_prune_parse_line(hammer_tid_t now_tid,
43 struct hammer_ioc_prune *prune,
44 const char *filesystem, char **av, int ac);
45 static int parse_modulo_time(const char *str, u_int64_t *delta);
46 static char *tid_to_stamp_str(hammer_tid_t tid);
47 static void prune_usage(int code);
50 * prune <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
51 * prune <filesystem> [using <filename>]
53 void
54 hammer_cmd_prune(char **av, int ac)
56 struct hammer_ioc_prune prune;
57 const char *filesystem;
58 int fd;
59 hammer_tid_t now_tid = (hammer_tid_t)time(NULL) * 1000000000LL;
61 bzero(&prune, sizeof(prune));
62 prune.nelms = 0;
63 prune.beg_objid = HAMMER_MIN_OBJID;
64 prune.cur_objid = prune.beg_objid;
65 prune.end_objid = HAMMER_MAX_OBJID;
66 prune.cur_key = HAMMER_MIN_KEY;
68 if (ac == 0)
69 prune_usage(1);
70 filesystem = av[0];
71 if (ac == 1) {
72 hammer_prune_load_file(now_tid, &prune, filesystem,
73 "/etc/hammer.conf");
74 } else if (strcmp(av[1], "using") == 0) {
75 if (ac == 2)
76 prune_usage(1);
77 hammer_prune_load_file(now_tid, &prune, filesystem, av[2]);
78 } else {
79 if (hammer_prune_parse_line(now_tid, &prune, filesystem,
80 av, ac) < 0) {
81 prune_usage(1);
84 fd = open(filesystem, O_RDONLY);
85 if (fd < 0)
86 err(1, "Unable to open %s", filesystem);
87 if (ioctl(fd, HAMMERIOC_PRUNE, &prune) < 0)
88 printf("Prune %s failed: %s\n", filesystem, strerror(errno));
89 else
90 printf("Prune %s succeeded\n", filesystem);
91 close(fd);
92 printf("Pruned %lld records (%lld directory entries) and %lld bytes\n",
93 prune.stat_rawrecords,
94 prune.stat_dirrecords,
95 prune.stat_bytes
99 static void
100 hammer_prune_load_file(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
101 const char *filesystem, const char *filename)
103 char buf[256];
104 FILE *fp;
105 char *av[16];
106 int ac;
107 int lineno;
109 if ((fp = fopen(filename, "r")) == NULL)
110 err(1, "Unable to read %s", filename);
111 lineno = 0;
112 while (fgets(buf, sizeof(buf), fp) != NULL) {
113 ++lineno;
114 if (strncmp(buf, "prune", 5) != 0)
115 continue;
116 ac = 0;
117 av[ac] = strtok(buf, " \t\r\n");
118 while (av[ac] != NULL) {
119 ++ac;
120 if (ac == 16) {
121 fclose(fp);
122 errx(1, "Malformed prune directive in %s "
123 "line %d\n", filename, lineno);
125 av[ac] = strtok(NULL, " \t\r\n");
127 if (ac == 0)
128 continue;
129 if (strcmp(av[0], "prune") != 0)
130 continue;
131 if (hammer_prune_parse_line(now_tid, prune, filesystem,
132 av + 1, ac - 1) < 0) {
133 errx(1, "Malformed prune directive in %s line %d\n",
134 filename, lineno);
137 fclose(fp);
140 static __inline
141 const char *
142 plural(int notplural)
144 return(notplural ? "" : "s");
148 * Parse the following parameters:
150 * <filesystem> from <modulo_time> to <modulo_time> every <modulo_time>
152 static int
153 hammer_prune_parse_line(hammer_tid_t now_tid, struct hammer_ioc_prune *prune,
154 const char *filesystem, char **av, int ac)
156 struct hammer_ioc_prune_elm *elm;
157 u_int64_t from_time;
158 u_int64_t to_time;
159 u_int64_t every_time;
160 char *from_stamp_str;
161 char *to_stamp_str;
163 if (ac != 7)
164 return(-1);
165 if (strcmp(av[0], filesystem) != 0)
166 return(0);
167 if (strcmp(av[1], "from") != 0)
168 return(-1);
169 if (strcmp(av[3], "to") != 0)
170 return(-1);
171 if (strcmp(av[5], "every") != 0)
172 return(-1);
173 if (parse_modulo_time(av[2], &from_time) < 0)
174 return(-1);
175 if (parse_modulo_time(av[4], &to_time) < 0)
176 return(-1);
177 if (parse_modulo_time(av[6], &every_time) < 0)
178 return(-1);
179 if (from_time > to_time)
180 return(-1);
181 if (from_time == 0 || to_time == 0) {
182 fprintf(stderr, "Bad from or to time specification.\n");
183 return(-1);
185 if (to_time % from_time != 0) {
186 fprintf(stderr, "Bad TO time specification.\n"
187 "It must be an integral multiple of FROM time\n");
188 return(-1);
190 if (every_time == 0 ||
191 from_time % every_time != 0 ||
192 to_time % every_time != 0) {
193 fprintf(stderr, "Bad 'every <modulo_time>' specification.\n"
194 "It must be an integral subdivision of FROM and TO\n");
195 return(-1);
197 if (prune->nelms == HAMMER_MAX_PRUNE_ELMS) {
198 fprintf(stderr, "Too many prune specifications in file! "
199 "Max is %d\n", HAMMER_MAX_PRUNE_ELMS);
200 return(-1);
204 * Example: from 1m to 60m every 5m
206 elm = &prune->elms[prune->nelms++];
207 elm->beg_tid = now_tid - now_tid % to_time;
208 if (now_tid - elm->beg_tid < to_time)
209 elm->beg_tid -= to_time;
211 elm->end_tid = now_tid - now_tid % from_time;
212 if (now_tid - elm->end_tid < from_time)
213 elm->end_tid -= from_time;
215 elm->mod_tid = every_time;
216 assert(elm->beg_tid < elm->end_tid);
219 * Convert back to local time for pretty printing
221 from_stamp_str = tid_to_stamp_str(elm->beg_tid);
222 to_stamp_str = tid_to_stamp_str(elm->end_tid);
223 printf("Prune %s to %s every ", from_stamp_str, to_stamp_str);
225 every_time /= 1000000000;
226 if (every_time < 60)
227 printf("%lld second%s\n", every_time, plural(every_time == 1));
228 every_time /= 60;
229 if (every_time && every_time < 60)
230 printf("%lld minute%s\n", every_time, plural(every_time == 1));
231 every_time /= 60;
232 if (every_time && every_time < 24)
233 printf("%lld hour%s\n", every_time, plural(every_time == 1));
234 every_time /= 24;
235 if (every_time)
236 printf("%lld day%s\n", every_time, plural(every_time == 1));
238 free(from_stamp_str);
239 free(to_stamp_str);
240 return(0);
243 static
245 parse_modulo_time(const char *str, u_int64_t *delta)
247 char *term;
249 *delta = strtoull(str, &term, 10);
251 switch(*term) {
252 case 'y':
253 *delta *= 12;
254 /* fall through */
255 case 'M':
256 *delta *= 30;
257 /* fall through */
258 case 'd':
259 *delta *= 24;
260 /* fall through */
261 case 'h':
262 *delta *= 60;
263 /* fall through */
264 case 'm':
265 *delta *= 60;
266 /* fall through */
267 case 's':
268 break;
269 default:
270 return(-1);
272 *delta *= 1000000000LL; /* TID's are in nanoseconds */
273 return(0);
276 static char *
277 tid_to_stamp_str(hammer_tid_t tid)
279 struct tm *tp;
280 char *buf = malloc(256);
281 time_t t;
283 t = (time_t)(tid / 1000000000);
284 tp = localtime(&t);
285 strftime(buf, 256, "%e-%b-%Y %H:%M:%S %Z", tp);
286 return(buf);
289 static void
290 prune_usage(int code)
292 fprintf(stderr, "Bad prune directive, specify one of:\n"
293 "prune filesystem [using filename]\n"
294 "prune filesystem from <modulo_time> to <modulo_time> every <modulo_time>\n");
295 exit(code);