Merge branch 'vendor/OPENSSH'
[dragonfly.git] / sbin / hammer2 / cmd_cleanup.c
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 docleanup(const char *path);
35 static int sameh2prefix(const char *from);
36 static void saveh2prefix(const char *from);
37 static void freeh2prefixes(void);
38
39 int
40 cmd_cleanup(const char *sel_path)
41 {
42         struct statfs *fsary;
43         char *fstype;
44         char *path;
45         char *from;
46         int i;
47         int n;
48         int r;
49         int rc;
50
51         if (sel_path)
52                 return (docleanup(sel_path));
53
54         n = getmntinfo(&fsary, MNT_NOWAIT);
55         if (n <= 0) {
56                 fprintf(stderr, "hammer2 cleanup: no HAMMER2 mounts\n");
57                 return 0;
58         }
59         rc = 0;
60         for (i = 0; i < n; ++i) {
61                 fstype = fsary[i].f_fstypename;
62                 path = fsary[i].f_mntonname;
63                 from = fsary[i].f_mntfromname;
64
65                 if (strcmp(fstype, "hammer2") != 0)
66                         continue;
67                 if (sameh2prefix(from)) {
68                         printf("hammer2 cleanup \"%s\" (same partition)\n",
69                                 path);
70                 } else {
71                         r = docleanup(path);
72                         if (r)
73                                 rc = r;
74                         saveh2prefix(from);
75                 }
76         }
77         freeh2prefixes();
78
79         return rc;
80 }
81
82 static
83 int
84 docleanup(const char *path)
85 {
86         int rc;
87
88         printf("hammer2 cleanup \"%s\"\n", path);
89         rc = cmd_bulkfree(path);
90
91         return rc;
92 }
93
94 char **h2prefixes;
95 int h2count;
96 int h2limit;
97
98 static
99 int
100 sameh2prefix(const char *from)
101 {
102         char *ptr;
103         int rc = 0;
104         int i;
105
106         ptr = strdup(from);
107         if (strchr(ptr, '@'))
108                 *strchr(ptr, '@') = 0;
109         for (i = 0; i < h2count; ++i) {
110                 if (strcmp(h2prefixes[i], ptr) == 0) {
111                         rc = 1;
112                         break;
113                 }
114         }
115         free(ptr);
116
117         return rc;
118 }
119
120 static
121 void
122 saveh2prefix(const char *from)
123 {
124         char *ptr;
125
126         if (h2count >= h2limit) {
127                 h2limit = (h2limit + 8) << 1;
128                 h2prefixes = realloc(h2prefixes, sizeof(char *) * h2limit);
129         }
130         ptr = strdup(from);
131         if (strchr(ptr, '@'))
132                 *strchr(ptr, '@') = 0;
133         h2prefixes[h2count++] = ptr;
134 }
135
136 static
137 void
138 freeh2prefixes(void)
139 {
140         int i;
141
142         for (i = 0; i < h2count; ++i)
143                 free(h2prefixes[i]);
144         free(h2prefixes);
145         h2prefixes = NULL;
146         h2count = 0;
147         h2limit = 0;
148 }