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
|
/* $Source: /u/mark/src/pax/RCS/link.c,v $
*
* $Revision: 1.2 $
*
* link.c - functions for handling multiple file links
*
* DESCRIPTION
*
* These function manage the link chains which are used to keep track
* of outstanding links during archive reading and writing.
*
* AUTHOR
*
* Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
*
* Sponsored by The USENIX Association for public distribution.
*
* Copyright (c) 1989 Mark H. Colburn.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice is duplicated in all such
* forms and that any documentation, advertising materials, and other
* materials related to such distribution and use acknowledge that the
* software was developed * by Mark H. Colburn and sponsored by The
* USENIX Association.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Log: link.c,v $
* Revision 1.2 89/02/12 10:04:38 mark
* 1.2 release fixes
*
* Revision 1.1 88/12/23 18:02:12 mark
* Initial revision
*
*/
#ifndef lint
static char *ident = "$Id: link.c,v 1.2 89/02/12 10:04:38 mark Exp $";
static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
#endif /* ! lint */
/* Headers */
#include "pax.h"
/* Defines */
/*
* Address link information base.
*/
#define LINKHASH(ino) (linkbase + (ino) % NEL(linkbase))
/*
* Number of array elements.
*/
#define NEL(a) (sizeof(a) / sizeof(*(a)))
/* Internal Identifiers */
static Link *linkbase[256]; /* Unresolved link information */
/* linkfrom - find a file to link from
*
* DESCRIPTION
*
* Linkfrom searches the link chain to see if there is a file in the
* link chain which has the same inode number as the file specified
* by the stat block pointed at by asb. If a file is found, the
* name is returned to the caller, otherwise a NULL is returned.
*
* PARAMETERS
*
* char *name - name of the file which we are attempting
* to find a link for
* Stat *asb - stat structure of file to find a link to
*
* RETURNS
*
* Returns a pointer to a link structure, or NULL if unsuccessful.
*
*/
#ifdef __STDC__
Link *linkfrom(char *name, Stat *asb)
#else
Link *linkfrom(name, asb)
char *name;
Stat *asb;
#endif
{
Link *linkp;
Link *linknext;
Path *path;
Path *pathnext;
Link **abase;
for (linkp = *(abase = LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
if (linkp->l_nlink == 0) {
if (linkp->l_name) {
free((char *) linkp->l_name);
}
if (linknext = linkp->l_forw) {
linknext->l_back = linkp->l_back;
}
if (linkp->l_back) {
linkp->l_back->l_forw = linkp->l_forw;
}
free((char *) linkp);
*abase = (Link *)NULL;
} else if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
/*
* check to see if a file with the name "name" exists in the
* chain of files which we have for this particular link
*/
for (path = linkp->l_path; path; path = pathnext) {
if (strcmp(path->p_name, name) == 0) {
--linkp->l_nlink;
if (path->p_name) {
free(path->p_name);
}
if (pathnext = path->p_forw) {
pathnext->p_back = path->p_back;
}
if (path->p_back) {
path->p_back->p_forw = pathnext;
}
if (linkp->l_path == path) {
linkp->l_path = pathnext;
}
free(path);
return (linkp);
}
pathnext = path->p_forw;
}
return((Link *)NULL);
} else {
linknext = linkp->l_forw;
}
}
return ((Link *)NULL);
}
/* islink - determine whether a given file really a link
*
* DESCRIPTION
*
* Islink searches the link chain to see if there is a file in the
* link chain which has the same inode number as the file specified
* by the stat block pointed at by asb. If a file is found, a
* non-zero value is returned to the caller, otherwise a 0 is
* returned.
*
* PARAMETERS
*
* char *name - name of file to check to see if it is link.
* Stat *asb - stat structure of file to find a link to
*
* RETURNS
*
* Returns a pointer to a link structure, or NULL if unsuccessful.
*
*/
#ifdef __STDC__
Link *islink(char *name, Stat *asb)
#else
Link *islink(name, asb)
char *name;
Stat *asb;
#endif
{
Link *linkp;
Link *linknext;
for (linkp = *(LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
if (strcmp(name, linkp->l_name) == 0) {
return ((Link *)NULL);
}
return (linkp);
} else {
linknext = linkp->l_forw;
}
}
return ((Link *)NULL);
}
/* linkto - remember a file with outstanding links
*
* DESCRIPTION
*
* Linkto adds the specified file to the link chain. Any subsequent
* calls to linkfrom which have the same inode will match the file
* just entered. If not enough space is available to make the link
* then the item is not added to the link chain, and a NULL is
* returned to the calling function.
*
* PARAMETERS
*
* char *name - name of file to remember
* Stat *asb - pointer to stat structure of file to remember
*
* RETURNS
*
* Returns a pointer to the associated link structure, or NULL when
* linking is not possible.
*
*/
#ifdef __STDC__
Link *linkto(char *name, Stat *asb)
#else
Link *linkto(name, asb)
char *name;
Stat *asb;
#endif
{
Link *linkp;
Link *linknext;
Path *path;
Link **abase;
for (linkp = *(LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
if ((path = (Path *) mem_get(sizeof(Path))) == (Path *)NULL ||
(path->p_name = mem_str(name)) == (char *)NULL) {
return((Link *)NULL);
}
if (path->p_forw = linkp->l_path) {
if (linkp->l_path->p_forw) {
linkp->l_path->p_forw->p_back = path;
}
} else {
linkp->l_path = path;
}
path->p_back = (Path *)NULL;
return(linkp);
} else {
linknext = linkp->l_forw;
}
}
/*
* This is a brand new link, for which there is no other information
*/
if ((asb->sb_mode & S_IFMT) == S_IFDIR
|| (linkp = (Link *) mem_get(sizeof(Link))) == (Link *)NULL
|| (linkp->l_name = mem_str(name)) == (char *)NULL) {
return ((Link *)NULL);
}
linkp->l_dev = asb->sb_dev;
linkp->l_ino = asb->sb_ino;
linkp->l_nlink = asb->sb_nlink - 1;
linkp->l_size = asb->sb_size;
linkp->l_path = (Path *)NULL;
if (linkp->l_forw = *(abase = LINKHASH(asb->sb_ino))) {
linkp->l_forw->l_back = linkp;
} else {
*abase = linkp;
}
linkp->l_back = (Link *)NULL;
return (linkp);
}
/* linkleft - complain about files with unseen links
*
* DESCRIPTION
*
* Linksleft scans through the link chain to see if there were any
* files which have outstanding links that were not processed by the
* archive. For each file in the link chain for which there was not
* a file, and error message is printed.
*/
#ifdef __STDC__
void linkleft(void)
#else
void linkleft()
#endif
{
Link *lp;
Link **base;
for (base = linkbase; base < linkbase + NEL(linkbase); ++base) {
for (lp = *base; lp; lp = lp->l_forw) {
if (lp->l_nlink) {
warn(lp->l_path->p_name, "Unseen link(s)");
}
}
}
}
|