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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/* $Xorg: mkshadow.c,v 1.3 2000/08/17 19:41:53 cpqbld Exp $ */
/* mkshadow.c - make a "shadow copy" of a directory tree with symlinks.
Copyright 1990, 1993 Free Software Foundation, Inc.
Permission to use, copy, modify, and distribute this program for
any purpose and without fee is hereby granted, provided that this
copyright and permission notice appear on all copies, and that
notice be given that copying and distribution is by permission of
the Free Software Foundation. The Free Software Foundation makes
no representations about the suitability of this software for any
purpose. It is provided "as is" without expressed or implied
warranty.
(The FSF has modified its usual distribution terms, for this file,
as a courtesy to the X project.) */
/*
* Usage: mkshadow [-X exclude_file] [-x exclude_pattern] ... MASTER [SHADOW]
* Makes SHADOW be a "shadow copy" of MASTER. SHADOW defaults to the current
* directory. Sort of like a recursive copy of MASTER to SHADOW.
* However, symbolic links are used instead of actually
* copying (non-directory) files.
* Also, directories named RCS or SCCS are shared (with a symbolic link).
* Warning messages are printed for files (and directories) in .
* that don't match a corresponding file in MASTER (though
* symbolic links are silently removed).
* Also, a warning message is printed for non-directory files
* under SHADOW that are not symbolic links.
*
* Files and directories can be excluded from the sharing
* with the -X and -x flags. The flag `-x pattern' (or `-xpattern')
* means that mkshadow should ignore any file whose name matches
* the pattern. The pattern is a "globbing" pattern, i.e. the
* characters *?[^-] are interpreted as by the shell.
* If the pattern contains a '/' is is matched against the complete
* current path (relative to '.'); otherwise, it is matched
* against the last component of the path.
* A `-X filename' flag means to read a set of exclusion patterns
* from the named file, one pattern to a line.
*
* Originally written by Per Bothner at University of Wisconsin-Madison,
* inspired by the lndir script distributed with X11.
* Modified by Per Bothner <bothner@cygnus.com> November 1993
* to more-or-less follow Posix.
*/
#include <sys/types.h>
#include <stdio.h>
#ifdef BSD
#include <strings.h>
#define strchr index
#else
#include <string.h>
#endif
#include <sys/stat.h>
#if defined(S_IFDIR) && !defined(S_ISDIR)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#if defined(S_IFLNK) && !defined(S_ISLNK)
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
#endif
#ifndef S_ISLNK
#define lstat stat
#endif
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
#include <errno.h>
#ifndef errno
extern int errno;
#endif
extern char * savedir();
fatal(msg)
char *msg;
{
if (errno) perror(msg ? msg : "");
else if (msg) fprintf(stderr, "mkshadow: %s\n", msg);
exit(-1);
}
/* When handling symbolic links to relative directories,
* we need to prepend "../" to the "source".
* We preallocate MAX_DEPTH repetations of "../" using a simple trick.
*/
#define MAX_DEPTH 20
#define PREPEND_BUFFER_SIZE (MAX_DEPTH*3)
char master_buffer[MAXPATHLEN+PREPEND_BUFFER_SIZE] =
"../../../../../../../../../../../../../../../../../../../../";
/* The logical start of the master_buffer is defined by
* master_start, which skips the fixed prepend area.
*/
#define master_start (master_buffer+PREPEND_BUFFER_SIZE)
char shadow_buffer[MAXPATHLEN];
void bad_args(msg)
{
if (msg) fprintf(stderr, "%s\n", msg);
fprintf (stderr, "usage: mkshadow [-X exclude_file] [-x exclude_pattern]");
fprintf (stderr, " master [shadow]\n");
exit(-1);
}
int exclude_count = 0;
char **exclude_patterns = NULL;
int exclude_limit = 0;
void add_exclude(pattern)
char *pattern;
{
if (exclude_limit == 0) {
exclude_limit = 100;
exclude_patterns = (char**)malloc(exclude_limit * sizeof(char*));
} else if (exclude_count + 1 >= exclude_limit) {
exclude_limit += 100;
exclude_patterns = (char**)realloc(exclude_patterns,
exclude_limit * sizeof(char*));
}
exclude_patterns[exclude_count] = pattern;
exclude_count++;
}
void add_exclude_file(name)
char *name;
{
char buf[MAXPATHLEN];
FILE *file = fopen(name, "r");
if (file == NULL) fatal("failed to find -X (exclude) file");
for (;;) {
int len;
char *str = fgets(buf, MAXPATHLEN, file);
if (str == NULL) break;
len = strlen(str);
if (len && str[len-1] == '\n') str[--len] = 0;
if (!len) continue;
str = (char*)malloc(len+1);
strcpy(str, buf);
add_exclude(str);
}
fclose(file);
}
main(argc, argv)
char **argv;
{
char *master_name = NULL;
char *shadow_name = NULL;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch(argv[i][1]) {
case 'X':
if (argv[i][2]) add_exclude_file(&argv[i][2]);
else if (++i >= argc) bad_args(NULL);
else add_exclude_file(argv[i]);
break;
case 'x':
if (argv[i][2]) add_exclude(&argv[i][2]);
else if (++i >= argc) bad_args(NULL);
else add_exclude(argv[i]);
break;
default:
bad_args(NULL);
}
} else if (master_name == NULL)
master_name = argv[i];
else if (shadow_name == NULL)
shadow_name = argv[i];
else bad_args (NULL);
}
if (master_name == NULL) bad_args(NULL);
if (shadow_name == NULL)
shadow_name = ".";
else if ((shadow_name[0] != '.' || shadow_name[1])
&& master_name[0] != '/') {
fprintf(stderr, "Shadowing a relative directory pathname to a \n");
fprintf(stderr, "shadow other than '.' is not supported!\n");
exit(-1);
}
strcpy(shadow_buffer, shadow_name);
strcpy(master_start, master_name);
DoCopy(master_start, shadow_buffer, 0);
return 0;
}
int compare_strings(ptr1, ptr2)
char **ptr1, **ptr2;
{
return strcmp(*ptr1, *ptr2);
}
void MakeLink(master, current, depth)
char *master;
char *current;
int depth;
{
if (master[0] != '/') {
/* Source directory was specified with a relative pathname. */
if (master != master_start) {
fatal("Internal bug: bad string buffer use");
}
/* Pre-pend "../" depth times. This compensates for
* the directories we've entered. */
master -= 3 * depth;
}
if (symlink(master, current)) {
fprintf(stderr, "Failed to create symbolic link %s->%s\n",
current, master);
exit (-1);
}
}
/* Get a sorted NULL_terminator array of (char*) using 'names'
* (created by save_dir) as data.
*/
char ** get_name_pointers(names)
char *names;
{
int n_names = 0;
int names_buf_size = 64;
char *namep;
char ** pointers = (char**)malloc(names_buf_size * sizeof(char*));
if (!names || !pointers) fatal("virtual memory exhausted");
for (namep = names; *namep; namep += strlen(namep) + 1) {
if (n_names + 1 >= names_buf_size) {
names_buf_size *= 2;
pointers = (char**)realloc(pointers,
names_buf_size * sizeof(char*));
if (!pointers) fatal("virtual memory exhausted");
}
pointers[n_names++] = namep;
}
pointers[n_names] = 0;
qsort(pointers, n_names, sizeof(char*), compare_strings);
return pointers;
}
/* Recursively shadow the directory whose name is in MASTER
* (which is == MASTER_START) into the destination directory named CURRENT.
*/
DoCopy(master, current, depth)
char *master; /* The source directory. */
char *current; /* The destination directory. */
int depth;
{
struct stat stat_master, stat_current;
char **master_pointer, **current_pointer;
char **master_names, **current_names;
char *master_end, *current_end;
char *master_name_buf, *current_name_buf;
master_end = master + strlen(master);
current_end = current + strlen(current);
/* Get rid of terminal '/' */
if (master_end[-1] == '/' && master != master_end - 1)
*--master_end = 0;
if (current_end[-1] == '/' && current != current_end - 1)
*--current_end = 0;
if (depth >= MAX_DEPTH) {
fprintf(stderr,
"Nesting too deep (depth %d at %s). Probable circularity.\n",
depth, master);
exit(-1);
}
master_name_buf = savedir(master, 500);
if (master_name_buf == NULL) {
fprintf(stderr, "Not enough memory or no such directory: %s\n",
master);
exit(-1);
}
current_name_buf = savedir(current, 500);
if (current_name_buf == NULL) {
fprintf(stderr, "Not enough memory or no such directory: %s\n",
current);
exit(-1);
}
master_names = get_name_pointers(master_name_buf);
current_names = get_name_pointers(current_name_buf);
master_pointer = master_names;
current_pointer = current_names;
for (;;) {
int cmp, ipat;
int in_master, in_current;
char *cur_name;
if (*master_pointer == NULL && *current_pointer == NULL)
break;
if (*master_pointer == NULL) cmp = 1;
else if (*current_pointer == NULL) cmp = -1;
else cmp = strcmp(*master_pointer, *current_pointer);
if (cmp < 0) { /* file only exists in master directory */
in_master = 1; in_current = 0;
} else if (cmp == 0) { /* file exists in both directories */
in_master = 1; in_current = 1;
} else { /* file only exists in current directory */
in_current = 1; in_master = 0;
}
cur_name = in_master ? *master_pointer : *current_pointer;
sprintf(master_end, "/%s", cur_name);
sprintf(current_end, "/%s", cur_name);
for (ipat = 0; ipat < exclude_count; ipat++) {
char *pat = exclude_patterns[ipat];
char *cur;
if (strchr(pat, '/')) cur = current + 2; /* Skip initial "./" */
else cur = cur_name;
if (wildmat(cur, pat)) goto skip;
}
if (in_master)
if (lstat(master, &stat_master) != 0) fatal("stat failed");
if (in_current)
if (lstat(current, &stat_current) != 0) fatal("stat failed");
if (in_current && !in_master) {
if (S_ISLNK(stat_current.st_mode))
if (unlink(current)) {
fprintf(stderr, "Failed to remove symbolic link %s.\n",
current);
}
else
fprintf(stderr, "Removed symbolic link %s.\n",
current);
else {
fprintf(stderr,
"The file %s does not exist in the master tree.\n",
current);
}
}
else if (S_ISDIR(stat_master.st_mode)
&& strcmp(cur_name, "RCS") != 0
&& strcmp(cur_name, "SCCS") != 0) {
if (!in_current) {
if (mkdir(current, 0775)) fatal("mkdir failed");
}
else if (stat(current, &stat_current)) fatal("stat failed");
if (!in_current || stat_current.st_dev != stat_master.st_dev
|| stat_current.st_ino != stat_master.st_ino)
DoCopy(master, current, depth+1);
else
fprintf(stderr, "Link %s is the same as directory %s.\n",
current, master);
}
else {
if (!in_current)
MakeLink(master, current, depth);
else if (!S_ISLNK(stat_current.st_mode)) {
fprintf(stderr, "Existing file %s is not a symbolic link.\n",
current);
} else {
if (stat(current, &stat_current) || stat(master, &stat_master))
fatal("stat failed");
if (stat_current.st_dev != stat_master.st_dev
|| stat_current.st_ino != stat_master.st_ino) {
fprintf(stderr, "Fixing incorrect symbolic link %s.\n",
current);
if (unlink(current)) {
fprintf(stderr, "Failed to remove symbolic link %s.\n",
current);
}
else
MakeLink(master, current, depth);
}
}
}
skip:
if (in_master) master_pointer++;
if (in_current) current_pointer++;
}
free(master_names); free(current_names);
free(master_name_buf); free(current_name_buf);
}
|