-
Notifications
You must be signed in to change notification settings - Fork 3
/
scan.c
176 lines (157 loc) · 4.1 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* scan.c */
/*
* Copyright (c) 2022 Stephen D. Adams <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ds.h"
#include "muxfs.h"
/*
* 'path' is required to be null-terminated and pointing to a buffer of
* capacity PATH_MAX, 'len' is provided so that 'path' may be mutated, then
* returned to its original state.
*/
static int
muxfs_scan_impl(enum muxfs_scan_mode mode, dind dev_index, char *path,
size_t len)
{
int rc;
struct muxfs_dev *dev;
struct stat st;
struct muxfs_dir dir;
struct dirent *dirent;
size_t i, sublen, dnamelen;
const char *dname;
const char *epath; /* Effective path. */
epath = (len > 0) ? path : ".";
if (muxfs_dev_get(&dev, dev_index, 0))
return 1;
if (fstatat(dev->root_fd, epath, &st, AT_SYMLINK_NOFOLLOW))
return 1;
if (S_ISDIR(st.st_mode)) {
rc = 1;
if (muxfs_pushdir(&dir, dev->root_fd, epath))
goto dirout;
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 2) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6)
== 0))
continue;
sublen = dnamelen;
if (len > 0)
sublen += len + 1;
if (sublen >= PATH_MAX)
goto dirout2;
if (len > 0)
strcat(path, "/");
strcat(path, dname);
if (muxfs_scan_impl(mode, dev_index, path, sublen))
goto dirout2;
path[len] = '\0';
}
if (muxfs_readback(dev_index, epath, 0, NULL)) {
printf("%s/%s\n", dev->root_path, epath);
if ((mode == MUXFS_SCAN_HEAL) &&
muxfs_state_restore_push_back(dev_index, epath))
exit(-1);
}
rc = 0;
dirout2:
if (muxfs_popdir(&dir))
exit(-1);
dirout:
return rc;
}
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
return 1;
if (muxfs_readback(dev_index, epath, 0, NULL)) {
printf("%s/%s\n", dev->root_path, epath);
if ((mode == MUXFS_SCAN_HEAL) &&
muxfs_state_restore_push_back(dev_index, epath))
exit(-1);
}
return 0;
}
static int
muxfs_scan(enum muxfs_scan_mode mode, dind dev_index)
{
char path[PATH_MAX];
memset(path, 0, PATH_MAX);
if (muxfs_scan_impl(mode, dev_index, path, 0))
return 1;
if (mode == MUXFS_SCAN_HEAL)
muxfs_restore_now();
return 0;
}
static void
muxfs_audit_usage(void)
{
fprintf(stderr, "usage: muxfs audit directory ...\n");
}
static void
muxfs_heal_usage(void)
{
fprintf(stderr, "usage: muxfs heal directory ...\n");
}
MUXFS int
muxfs_scan_main(enum muxfs_scan_mode scan_mode, int argc, char *argv[])
{
dind i, dev_count;
if (muxfs_state_syslog_init())
exit(-1);
if (muxfs_dsinit())
exit(-1);
if (muxfs_parse_args(argc, argv, 1)) {
if (scan_mode == MUXFS_SCAN_AUDIT)
muxfs_audit_usage();
else
muxfs_heal_usage();
exit(1);
}
if (muxfs_init(0))
exit(-1);
if ((dev_count = muxfs_dev_count()) == 0) {
dprintf(2, "Error: The directory array is empty.\n");
exit(1);
}
switch (muxfs_dev_seq_check()) {
case 0:
break; /* Match. */
case 1:
exit(-1); /* Error. */
case 2:
exit(1); /* Mismatch. Error message already printed. */
default:
exit(-1); /* Programming error. */
}
for (i = 0; i < dev_count; ++i) {
if (muxfs_scan(scan_mode, i))
exit(-1);
}
if (muxfs_final())
exit(-1);
return 0;
}