xref: /dragonfly/sbin/hammer2/cmd_destroy.c (revision 214bec7437e3d22ea08ddc53eef2eeb6584a28ec)
1 /*
2  * Copyright (c) 2017-2018 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "hammer2.h"
33 
34 static int pathdir(const char *path, const char **lastp);
35 
36 int
cmd_destroy_path(int ac,const char ** av)37 cmd_destroy_path(int ac, const char **av)
38 {
39           hammer2_ioc_destroy_t destroy;
40           const char *last;
41           int i;
42           int fd;
43           int ecode = 0;
44 
45           for (i = 0; i < ac; ++i) {
46                     bzero(&destroy, sizeof(destroy));
47                     destroy.cmd = HAMMER2_DELETE_FILE;
48                     printf("%s\t", av[i]);
49                     fflush(stdout);
50                     fd = pathdir(av[i], &last);
51                     if (fd >= 0) {
52                               snprintf(destroy.path, sizeof(destroy.path),
53                                          "%s", last);
54                               if (ioctl(fd, HAMMER2IOC_DESTROY, &destroy) < 0) {
55                                         printf("%s\n", strerror(errno));
56                                         ecode = 1;
57                               } else {
58                                         printf("ok\n");
59                               }
60                               close(fd);
61                     } else {
62                               printf("%s\n", strerror(errno));
63                               ecode = 1;
64                     }
65           }
66           return ecode;
67 }
68 
69 static
70 int
pathdir(const char * path,const char ** lastp)71 pathdir(const char *path, const char **lastp)
72 {
73           const char *ptr;
74           char *npath;
75           int fd;
76 
77           ptr = path + strlen(path);
78           while (ptr > path && ptr[-1] != '/')
79                     --ptr;
80           *lastp = ptr;
81           if (ptr == path) {
82                     fd = open(".", O_RDONLY);
83           } else {
84                     asprintf(&npath, "%*.*s",
85                               (int)(ptr - path), (int)(ptr - path), path);
86                     fd = open(npath, O_RDONLY);
87                     free(npath);
88           }
89 
90           return fd;
91 }
92 
93 int
cmd_destroy_inum(const char * sel_path,int ac,const char ** av)94 cmd_destroy_inum(const char *sel_path, int ac, const char **av)
95 {
96           hammer2_ioc_destroy_t destroy;
97           int i;
98           int fd;
99           int ecode = 0;
100 
101           fd = hammer2_ioctl_handle(sel_path);
102           if (fd < 0)
103                     return 1;
104 
105           printf("deleting inodes on %s\n", sel_path);
106           for (i = 0; i < ac; ++i) {
107                     bzero(&destroy, sizeof(destroy));
108                     destroy.cmd = HAMMER2_DELETE_INUM;
109                     destroy.inum = strtoul(av[i], NULL, 0);
110                     printf("%16jd ", (intmax_t)destroy.inum);
111                     if (ioctl(fd, HAMMER2IOC_DESTROY, &destroy) < 0) {
112                               printf("%s\n", strerror(errno));
113                               ecode = 1;
114                     } else {
115                               printf("ok\n");
116                     }
117           }
118           close(fd);
119 
120           return ecode;
121 }
122