forked from miekg/rdup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdup-up.c
277 lines (245 loc) · 7.13 KB
/
rdup-up.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2009 - 2011 Miek Gieben
* See LICENSE for the license
* rdup-up -- update an directory tree with
* and rdup archive
*/
#include "rdup-up.h"
#include "io.h"
char *PROGNAME="rdup-up";
/* options */
gint opt_verbose = 0; /* be more verbose */
gint opt_output = O_RDUP; /* set these 2 so we can use parse_entry */
gint opt_input = I_RDUP;
gboolean opt_quiet = FALSE; /* don't complain about chown() errors */
gboolean opt_dry = FALSE; /* don't touch the filesystem */
gboolean opt_table = FALSE; /* table of contents */
gboolean opt_top = FALSE; /* create top dir if it does not exist */
gboolean opt_chown = TRUE; /* create ._rdup._ files with user/group info */
gchar *opt_path_strip = NULL; /* -r PATH, strip PATH from pathnames */
guint opt_path_strip_len = 0; /* number of path labels in opt_path_strip */
guint opt_strip = 0; /* -s: strippath */
int sig = 0;
GSList *hlink_list = NULL; /* save hardlink for post processing */
extern int opterr;
int opterr = 0;
/* update the directory with the archive */
static gboolean
update(char *path)
{
struct rdup *rdup_entry;
size_t line, i, pathsize;
size_t pathlen;
char *buf, *pathbuf, *n, *p;
char delim;
gboolean ok;
GHashTable *uidhash; /* holds username -> uid */
GHashTable *gidhash; /* holds groupname -> gid */
buf = g_malloc(BUFSIZE + 1);
pathbuf = g_malloc(BUFSIZE + 1);
i = BUFSIZE;
delim = '\n';
line = 0;
ok = TRUE;
uidhash = g_hash_table_new(g_str_hash, g_str_equal);
gidhash = g_hash_table_new(g_str_hash, g_str_equal);
if (path)
pathlen = strlen(path);
else
pathlen = 0;
while ((rdup_getdelim(&buf, &i, delim, stdin)) != -1) {
line++;
n = strrchr(buf, '\n');
if (n)
*n = '\0';
if (!(rdup_entry = parse_entry(buf, line))) {
/* msgs from entry.c */
exit(EXIT_FAILURE);
}
/* we have a valid entry, read the filename */
pathsize = fread(pathbuf, sizeof(char), rdup_entry->f_name_size, stdin);
if (pathsize != rdup_entry->f_name_size) {
msg(_("Reported name size (%zd) does not match actual name size (%zd)"),
rdup_entry->f_name_size, pathsize);
exit(EXIT_FAILURE);
}
pathbuf[pathsize] = '\0';
if (pathbuf[0] != '/') {
msg(_("Pathname does not start with /: `%s\'"), pathbuf);
exit(EXIT_FAILURE);
}
rdup_entry->f_name = pathbuf;
/* extract target from rdup_entry */
if (S_ISLNK(rdup_entry->f_mode) || rdup_entry->f_lnk) {
// filesize is spot where to cut
rdup_entry->f_name[rdup_entry->f_size] = '\0';
rdup_entry->f_target = rdup_entry->f_name +
rdup_entry->f_size + 4;
} else {
rdup_entry->f_target = NULL;
}
/* strippath must be inserted here */
if (opt_strip)
strippath(rdup_entry);
if (opt_path_strip)
strippathname(rdup_entry);
if (!rdup_entry->f_name)
p = NULL;
else {
/* avoid // at the beginning */
if ( (pathlen == 1 && path[0] == '/') || pathlen == 0 ) {
p = g_strdup(rdup_entry->f_name);
} else {
p = g_strdup_printf("%s%s", path, rdup_entry->f_name);
rdup_entry->f_name_size += pathlen;
if (S_ISLNK(rdup_entry->f_mode) || rdup_entry->f_lnk)
rdup_entry->f_size += pathlen;
}
}
rdup_entry->f_name = p;
if (mk_obj(stdin, path, rdup_entry, uidhash, gidhash) == FALSE)
ok = FALSE;
g_free(rdup_entry->f_user);
g_free(rdup_entry->f_group);
g_free(rdup_entry);
}
/* post-process hardlinks */
if (mk_hlink(hlink_list) == FALSE)
ok = FALSE;
g_free(buf);
g_free(pathbuf);
return ok;
}
int
main(int argc, char **argv)
{
struct sigaction sa;
char pwd[BUFSIZE + 1];
int c;
guint i;
char *path;
#ifdef ENABLE_NLS
if (!setlocale(LC_MESSAGES, ""))
msg(_("Locale could not be set"));
bindtextdomain(PACKAGE_NAME, LOCALEROOTDIR);
(void)textdomain(PACKAGE_NAME);
#endif /* ENABLE_NLS */
/* setup our signal handling */
sa.sa_flags = 0;
sigfillset(&sa.sa_mask);
sa.sa_handler = got_sig;
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
if (((getuid() != geteuid()) || (getgid() != getegid()))) {
msg(_("Will not run suid/sgid for safety reasons"));
exit(EXIT_FAILURE);
}
if (!getcwd(pwd, BUFSIZE)) {
msg(_("Could not get current working directory"));
exit(EXIT_FAILURE);
}
for(c = 0; c < argc; c++) {
if (strlen(argv[c]) > BUFSIZE) {
msg(_("Argument length overrun"));
exit(EXIT_FAILURE);
}
}
while ((c = getopt (argc, argv, "thnVvus:r:Tq")) != -1) {
switch (c) {
case 'v':
opt_verbose = 1;
break;
case 'h':
usage_up(stdout);
exit(EXIT_SUCCESS);
case 'n':
opt_dry = TRUE;
break;
case 'T':
opt_table = TRUE;
opt_dry = TRUE;
opt_verbose = 0;
break;
case 'u':
opt_chown = FALSE;
break;
case 's':
opt_strip = abs(atoi(optarg));
break;
case 'r':
/* expand relative paths */
if (!g_path_is_absolute(optarg))
opt_path_strip = abspath(g_strdup_printf("%s/%s", pwd, optarg));
else
opt_path_strip = abspath(optarg);
if (!opt_path_strip) {
msg(_("Failed to expand path `%s\'"), optarg);
exit(EXIT_FAILURE);
}
if (opt_path_strip[strlen(opt_path_strip) - 1] != '/')
opt_path_strip = g_strdup_printf("%s/", opt_path_strip);
/* count the number of labels */
for(i = 0; i < strlen(opt_path_strip); i++) {
if (opt_path_strip[i] == '/')
opt_path_strip_len++;
}
opt_path_strip_len--; /* we added the closing slash, so need -1 here */
break;
case 'q':
opt_quiet = TRUE;
break;
case 't':
opt_top = TRUE;
break;
case 'V':
#ifdef DEBUG
fprintf(stdout, "%s %s (with --enable-debug)\n", PROGNAME, VERSION);
#else
fprintf(stdout, "%s %s\n", PROGNAME, VERSION);
#endif /* DEBUG */
exit(EXIT_SUCCESS);
default:
msg(_("Unknown option seen `%c\'"), optopt);
exit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
if (opt_table) {
path = NULL;
} else {
if (argc != 1) {
msg(_("Destination directory is required"));
exit(EXIT_FAILURE);
}
if (!g_path_is_absolute(argv[0])) {
gchar* full_path = g_strdup_printf("%s/%s", pwd, argv[0]);
path = abspath(full_path);
g_free(full_path);
}
else
path = abspath(argv[0]);
}
if (!opt_dry) {
if (opt_top) {
/* this is not 100%, but better than nothing */
if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
msg(_("Failed to create directory `%s\'"), path);
exit(EXIT_FAILURE);
}
if (mkpath(path, 00777) == -1) {
msg(_("Failed to create directory `%s\': %s"), path, strerror(errno));
exit(EXIT_FAILURE);
}
} else {
if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
msg(_("No such directory: `%s\'"), path);
exit(EXIT_FAILURE);
}
}
}
if (update(path) == FALSE)
exit(EXIT_FAILURE);
g_free(path);
exit(EXIT_SUCCESS);
}