Repository navigation
Expand file tree
/
Copy pathqueue.c
More file actions
223 lines (175 loc) · 4.14 KB
/
Copy pathqueue.c
File metadata and controls
223 lines (175 loc) · 4.14 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* SPDX-License-Identifier: GPL-2.0 */
#include "compat.h"
#include "crc32sum.h"
#include "queue.h"
#include "error.h"
struct schedule_status {
char *path;
int error;
};
static int schedule_file(struct queue *queue, const char *path,
unsigned long size, unsigned int sum)
{
struct file *file;
queue->files = realloc(queue->files, (queue->nfiles + 1) * sizeof(*file));
file = &queue->files[queue->nfiles++];
file->path = strdup(path);
file->size = size;
file->sum = sum;
queue->nbytes += size;
return 0;
}
static inline int is_dot_or_dotdot(const char *name)
{
return name[0] == '.' && (name[1] == '\0' ||
(name[1] == '.' && name[2] == '\0'));
}
void queue_init(struct queue *queue)
{
queue->files = NULL;
queue->nfiles = 0;
queue->nbytes = 0;
}
void queue_clear(struct queue *queue)
{
free(queue->files);
queue_init(queue);
}
int queue_schedule_regular_file(struct queue *queue, const char *path,
unsigned int sum)
{
struct stat sb;
if (stat(path, &sb) != 0)
return -errno;
if (S_ISREG(sb.st_mode) == 0)
return -EINVAL;
return schedule_file(queue, path, sb.st_size, sum);
}
static inline void set_error(struct schedule_status *status, const char *path,
int code)
{
status->path = strdup(path);
status->error = code;
}
static inline bool has_flag(unsigned int flags, unsigned int flag)
{
return (flags & flag) != 0;
}
static void schedule_path(struct schedule_status *status, struct queue *queue,
const char *path, unsigned int flags);
static void schedule_directory(struct schedule_status *status, struct queue *queue,
const char *path, unsigned int flags)
{
unsigned int len;
char *child;
DIR *dir;
if (has_flag(flags, CRC32SUM_RECURSIVE) == false) {
set_error(status, path, EISDIR);
return;
}
dir = opendir(path);
if (dir == NULL) {
set_error(status, path, errno);
return;
}
while (1) {
struct dirent *de = readdir(dir);
if (de == NULL)
break;
if (is_dot_or_dotdot(de->d_name))
continue;
len = strlen(path) + strlen(de->d_name) + 3;
child = malloc(len);
snprintf(child, len, "%s/%s", path, de->d_name);
schedule_path(status, queue, child, flags);
free(child);
if (status->error) {
break;
}
}
closedir(dir);
}
static inline int is_loop(const char *path, const char *target)
{
unsigned int i = 0;
if (!path || !target)
return false;
if (strcmp(target, "/") == 0)
return true;
while (1) {
if (path[i] == '/' && target[i] == '\0')
return true;
if (path[i] != target[i])
return false;
if (path[i] == '\0' || target[i] == '\0')
break;
i++;
}
return false;
}
static int check_link(const char *path, struct schedule_status *status)
{
char *target;
int ret = 0;
target = realpath(path, NULL);
if (target == NULL) {
set_error(status, path, ENOLINK);
return 1;
}
if (is_loop(path, target)) {
set_error(status, path, ELOOP);
ret = 2;
}
free(target);
return ret;
}
static void schedule_symlink(struct schedule_status *status, struct queue *queue,
const char *path, unsigned int flags)
{
struct stat sb;
if (check_link(path, status) != 0) {
/* error is already set */
return;
}
if (stat(path, &sb) != 0) {
set_error(status, path, errno);
return;
}
if (S_ISDIR(sb.st_mode) && has_flag(flags, CRC32SUM_FOLLOW)) {
schedule_directory(status, queue, path, flags);
return;
}
schedule_file(queue, path, sb.st_size, 0);
}
static void schedule_path(struct schedule_status *status, struct queue *queue,
const char *path, unsigned int flags)
{
struct stat sb;
if (status->error)
return;
if (lstat(path, &sb) != 0) {
set_error(status, path, errno);
return;
}
if (S_ISDIR(sb.st_mode)) {
schedule_directory(status, queue, path, flags);
} else if (S_ISLNK(sb.st_mode)) {
schedule_symlink(status, queue, path, flags);
} else if (S_ISREG(sb.st_mode)) {
schedule_file(queue, path, sb.st_size, 0);
}
}
int queue_schedule_path(struct queue *queue, const char *path,
unsigned int flags)
{
struct schedule_status status = {
.path = NULL,
.error = 0
};
schedule_path(&status, queue, path, flags);
if (status.error) {
errno_to_error(status.error, status.path);
free(status.path);
}
return -status.error;
}