GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: file_subs.c,v 1.48 2016/02/16 04:30:07 guenther Exp $ */ |
||
2 |
/* $NetBSD: file_subs.c,v 1.4 1995/03/21 09:07:18 cgd Exp $ */ |
||
3 |
|||
4 |
/*- |
||
5 |
* Copyright (c) 1992 Keith Muller. |
||
6 |
* Copyright (c) 1992, 1993 |
||
7 |
* The Regents of the University of California. All rights reserved. |
||
8 |
* |
||
9 |
* This code is derived from software contributed to Berkeley by |
||
10 |
* Keith Muller of the University of California, San Diego. |
||
11 |
* |
||
12 |
* Redistribution and use in source and binary forms, with or without |
||
13 |
* modification, are permitted provided that the following conditions |
||
14 |
* are met: |
||
15 |
* 1. Redistributions of source code must retain the above copyright |
||
16 |
* notice, this list of conditions and the following disclaimer. |
||
17 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
18 |
* notice, this list of conditions and the following disclaimer in the |
||
19 |
* documentation and/or other materials provided with the distribution. |
||
20 |
* 3. Neither the name of the University nor the names of its contributors |
||
21 |
* may be used to endorse or promote products derived from this software |
||
22 |
* without specific prior written permission. |
||
23 |
* |
||
24 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
25 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
28 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
29 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
30 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
32 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
33 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
34 |
* SUCH DAMAGE. |
||
35 |
*/ |
||
36 |
|||
37 |
#include <sys/time.h> |
||
38 |
#include <sys/stat.h> |
||
39 |
#include <err.h> |
||
40 |
#include <errno.h> |
||
41 |
#include <fcntl.h> |
||
42 |
#include <limits.h> |
||
43 |
#include <stdio.h> |
||
44 |
#include <stdlib.h> |
||
45 |
#include <string.h> |
||
46 |
#include <unistd.h> |
||
47 |
#include "pax.h" |
||
48 |
#include "options.h" |
||
49 |
#include "extern.h" |
||
50 |
|||
51 |
static int |
||
52 |
mk_link(char *, struct stat *, char *, int); |
||
53 |
|||
54 |
/* |
||
55 |
* routines that deal with file operations such as: creating, removing; |
||
56 |
* and setting access modes, uid/gid and times of files |
||
57 |
*/ |
||
58 |
|||
59 |
/* |
||
60 |
* file_creat() |
||
61 |
* Create and open a file. |
||
62 |
* Return: |
||
63 |
* file descriptor or -1 for failure |
||
64 |
*/ |
||
65 |
|||
66 |
int |
||
67 |
file_creat(ARCHD *arcn) |
||
68 |
6 |
{ |
|
69 |
6 |
int fd = -1; |
|
70 |
mode_t file_mode; |
||
71 |
int oerrno; |
||
72 |
|||
73 |
/* |
||
74 |
* Assume file doesn't exist, so just try to create it, most times this |
||
75 |
* works. We have to take special handling when the file does exist. To |
||
76 |
* detect this, we use O_EXCL. For example when trying to create a |
||
77 |
* file and a character device or fifo exists with the same name, we |
||
78 |
* can accidently open the device by mistake (or block waiting to open). |
||
79 |
* If we find that the open has failed, then spend the effort to |
||
80 |
* figure out why. This strategy was found to have better average |
||
81 |
* performance in common use than checking the file (and the path) |
||
82 |
* first with lstat. |
||
83 |
*/ |
||
84 |
6 |
file_mode = arcn->sb.st_mode & FILEBITS; |
|
85 |
✓✗ | 6 |
if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_EXCL, |
86 |
file_mode)) >= 0) |
||
87 |
6 |
return(fd); |
|
88 |
|||
89 |
/* |
||
90 |
* the file seems to exist. First we try to get rid of it (found to be |
||
91 |
* the second most common failure when traced). If this fails, only |
||
92 |
* then we go to the expense to check and create the path to the file |
||
93 |
*/ |
||
94 |
if (unlnk_exist(arcn->name, arcn->type) != 0) |
||
95 |
return(-1); |
||
96 |
|||
97 |
for (;;) { |
||
98 |
/* |
||
99 |
* try to open it again, if this fails, check all the nodes in |
||
100 |
* the path and give it a final try. if chk_path() finds that |
||
101 |
* it cannot fix anything, we will skip the last attempt |
||
102 |
*/ |
||
103 |
if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC, |
||
104 |
file_mode)) >= 0) |
||
105 |
break; |
||
106 |
oerrno = errno; |
||
107 |
if (nodirs || chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { |
||
108 |
syswarn(1, oerrno, "Unable to create %s", arcn->name); |
||
109 |
return(-1); |
||
110 |
} |
||
111 |
} |
||
112 |
return(fd); |
||
113 |
} |
||
114 |
|||
115 |
/* |
||
116 |
* file_close() |
||
117 |
* Close file descriptor to a file just created by pax. Sets modes, |
||
118 |
* ownership and times as required. |
||
119 |
* Return: |
||
120 |
* 0 for success, -1 for failure |
||
121 |
*/ |
||
122 |
|||
123 |
void |
||
124 |
file_close(ARCHD *arcn, int fd) |
||
125 |
6 |
{ |
|
126 |
6 |
int res = 0; |
|
127 |
|||
128 |
✗✓ | 6 |
if (fd < 0) |
129 |
return; |
||
130 |
|||
131 |
/* |
||
132 |
* set owner/groups first as this may strip off mode bits we want |
||
133 |
* then set file permission modes. Then set file access and |
||
134 |
* modification times. |
||
135 |
*/ |
||
136 |
✗✓ | 6 |
if (pids) |
137 |
res = fset_ids(arcn->name, fd, arcn->sb.st_uid, |
||
138 |
arcn->sb.st_gid); |
||
139 |
|||
140 |
/* |
||
141 |
* IMPORTANT SECURITY NOTE: |
||
142 |
* if not preserving mode or we cannot set uid/gid, then PROHIBIT |
||
143 |
* set uid/gid bits |
||
144 |
*/ |
||
145 |
✓✗ | 6 |
if (!pmode || res) |
146 |
6 |
arcn->sb.st_mode &= ~(SETBITS); |
|
147 |
✗✓ | 6 |
if (pmode) |
148 |
fset_pmode(arcn->name, fd, arcn->sb.st_mode); |
||
149 |
✗✓✗✗ |
6 |
if (patime || pmtime) |
150 |
6 |
fset_ftime(arcn->name, fd, &arcn->sb.st_mtim, |
|
151 |
&arcn->sb.st_atim, 0); |
||
152 |
✗✓ | 6 |
if (close(fd) < 0) |
153 |
syswarn(0, errno, "Unable to close file descriptor on %s", |
||
154 |
arcn->name); |
||
155 |
} |
||
156 |
|||
157 |
/* |
||
158 |
* lnk_creat() |
||
159 |
* Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name |
||
160 |
* must exist; |
||
161 |
* Return: |
||
162 |
* 0 if ok, -1 otherwise |
||
163 |
*/ |
||
164 |
|||
165 |
int |
||
166 |
lnk_creat(ARCHD *arcn) |
||
167 |
4 |
{ |
|
168 |
struct stat sb; |
||
169 |
int res; |
||
170 |
|||
171 |
/* |
||
172 |
* we may be running as root, so we have to be sure that link target |
||
173 |
* is not a directory, so we lstat and check |
||
174 |
*/ |
||
175 |
✗✓ | 4 |
if (lstat(arcn->ln_name, &sb) < 0) { |
176 |
syswarn(1,errno,"Unable to link to %s from %s", arcn->ln_name, |
||
177 |
arcn->name); |
||
178 |
return(-1); |
||
179 |
} |
||
180 |
|||
181 |
✗✓ | 4 |
if (S_ISDIR(sb.st_mode)) { |
182 |
paxwarn(1, "A hard link to the directory %s is not allowed", |
||
183 |
arcn->ln_name); |
||
184 |
return(-1); |
||
185 |
} |
||
186 |
|||
187 |
4 |
res = mk_link(arcn->ln_name, &sb, arcn->name, 0); |
|
188 |
✓✗ | 4 |
if (res == 0) { |
189 |
/* check for a hardlink to a placeholder symlink */ |
||
190 |
4 |
res = sltab_add_link(arcn->name, &sb); |
|
191 |
|||
192 |
✗✓ | 4 |
if (res < 0) { |
193 |
/* arrgh, it failed, clean up */ |
||
194 |
unlink(arcn->name); |
||
195 |
} |
||
196 |
} |
||
197 |
|||
198 |
4 |
return (res); |
|
199 |
} |
||
200 |
|||
201 |
/* |
||
202 |
* cross_lnk() |
||
203 |
* Create a hard link to arcn->org_name from arcn->name. Only used in copy |
||
204 |
* with the -l flag. No warning or error if this does not succeed (we will |
||
205 |
* then just create the file) |
||
206 |
* Return: |
||
207 |
* 1 if copy() should try to create this file node |
||
208 |
* 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self). |
||
209 |
*/ |
||
210 |
|||
211 |
int |
||
212 |
cross_lnk(ARCHD *arcn) |
||
213 |
{ |
||
214 |
/* |
||
215 |
* try to make a link to original file (-l flag in copy mode). make |
||
216 |
* sure we do not try to link to directories in case we are running as |
||
217 |
* root (and it might succeed). |
||
218 |
*/ |
||
219 |
if (arcn->type == PAX_DIR) |
||
220 |
return(1); |
||
221 |
return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1)); |
||
222 |
} |
||
223 |
|||
224 |
/* |
||
225 |
* chk_same() |
||
226 |
* In copy mode if we are not trying to make hard links between the src |
||
227 |
* and destinations, make sure we are not going to overwrite ourselves by |
||
228 |
* accident. This slows things down a little, but we have to protect all |
||
229 |
* those people who make typing errors. |
||
230 |
* Return: |
||
231 |
* 1 the target does not exist, go ahead and copy |
||
232 |
* 0 skip it file exists (-k) or may be the same as source file |
||
233 |
*/ |
||
234 |
|||
235 |
int |
||
236 |
chk_same(ARCHD *arcn) |
||
237 |
{ |
||
238 |
struct stat sb; |
||
239 |
|||
240 |
/* |
||
241 |
* if file does not exist, return. if file exists and -k, skip it |
||
242 |
* quietly |
||
243 |
*/ |
||
244 |
if (lstat(arcn->name, &sb) < 0) |
||
245 |
return(1); |
||
246 |
if (kflag) |
||
247 |
return(0); |
||
248 |
|||
249 |
/* |
||
250 |
* better make sure the user does not have src == dest by mistake |
||
251 |
*/ |
||
252 |
if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) { |
||
253 |
paxwarn(1, "Unable to copy %s, file would overwrite itself", |
||
254 |
arcn->name); |
||
255 |
return(0); |
||
256 |
} |
||
257 |
return(1); |
||
258 |
} |
||
259 |
|||
260 |
/* |
||
261 |
* mk_link() |
||
262 |
* try to make a hard link between two files. if ign set, we do not |
||
263 |
* complain. |
||
264 |
* Return: |
||
265 |
* 0 if successful (or we are done with this file but no error, such as |
||
266 |
* finding the from file exists and the user has set -k). |
||
267 |
* 1 when ign was set to indicates we could not make the link but we |
||
268 |
* should try to copy/extract the file as that might work (and is an |
||
269 |
* allowed option). -1 an error occurred. |
||
270 |
*/ |
||
271 |
|||
272 |
static int |
||
273 |
mk_link(char *to, struct stat *to_sb, char *from, int ign) |
||
274 |
4 |
{ |
|
275 |
struct stat sb; |
||
276 |
int oerrno; |
||
277 |
|||
278 |
/* |
||
279 |
* if from file exists, it has to be unlinked to make the link. If the |
||
280 |
* file exists and -k is set, skip it quietly |
||
281 |
*/ |
||
282 |
✗✓ | 4 |
if (lstat(from, &sb) == 0) { |
283 |
if (kflag) |
||
284 |
return(0); |
||
285 |
|||
286 |
/* |
||
287 |
* make sure it is not the same file, protect the user |
||
288 |
*/ |
||
289 |
if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) { |
||
290 |
paxwarn(1, "Unable to link file %s to itself", to); |
||
291 |
return(-1); |
||
292 |
} |
||
293 |
|||
294 |
/* |
||
295 |
* try to get rid of the file, based on the type |
||
296 |
*/ |
||
297 |
if (S_ISDIR(sb.st_mode)) { |
||
298 |
if (rmdir(from) < 0) { |
||
299 |
syswarn(1, errno, "Unable to remove %s", from); |
||
300 |
return(-1); |
||
301 |
} |
||
302 |
delete_dir(sb.st_dev, sb.st_ino); |
||
303 |
} else if (unlink(from) < 0) { |
||
304 |
if (!ign) { |
||
305 |
syswarn(1, errno, "Unable to remove %s", from); |
||
306 |
return(-1); |
||
307 |
} |
||
308 |
return(1); |
||
309 |
} |
||
310 |
} |
||
311 |
|||
312 |
/* |
||
313 |
* from file is gone (or did not exist), try to make the hard link. |
||
314 |
* if it fails, check the path and try it again (if chk_path() says to |
||
315 |
* try again) |
||
316 |
*/ |
||
317 |
for (;;) { |
||
318 |
✗✓ | 4 |
if (linkat(AT_FDCWD, to, AT_FDCWD, from, 0) == 0) |
319 |
4 |
break; |
|
320 |
oerrno = errno; |
||
321 |
if (!nodirs && chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0) |
||
322 |
continue; |
||
323 |
if (!ign) { |
||
324 |
syswarn(1, oerrno, "Could not link to %s from %s", to, |
||
325 |
from); |
||
326 |
return(-1); |
||
327 |
} |
||
328 |
return(1); |
||
329 |
} |
||
330 |
|||
331 |
/* |
||
332 |
* all right the link was made |
||
333 |
*/ |
||
334 |
4 |
return(0); |
|
335 |
} |
||
336 |
|||
337 |
/* |
||
338 |
* node_creat() |
||
339 |
* create an entry in the file system (other than a file or hard link). |
||
340 |
* If successful, sets uid/gid modes and times as required. |
||
341 |
* Return: |
||
342 |
* 0 if ok, -1 otherwise |
||
343 |
*/ |
||
344 |
|||
345 |
int |
||
346 |
node_creat(ARCHD *arcn) |
||
347 |
4 |
{ |
|
348 |
int res; |
||
349 |
4 |
int ign = 0; |
|
350 |
int oerrno; |
||
351 |
4 |
int pass = 0; |
|
352 |
mode_t file_mode; |
||
353 |
struct stat sb; |
||
354 |
char target[PATH_MAX]; |
||
355 |
4 |
char *nm = arcn->name; |
|
356 |
4 |
int len, defer_pmode = 0; |
|
357 |
|||
358 |
/* |
||
359 |
* create node based on type, if that fails try to unlink the node and |
||
360 |
* try again. finally check the path and try again. As noted in the |
||
361 |
* file and link creation routines, this method seems to exhibit the |
||
362 |
* best performance in general use workloads. |
||
363 |
*/ |
||
364 |
4 |
file_mode = arcn->sb.st_mode & FILEBITS; |
|
365 |
|||
366 |
for (;;) { |
||
367 |
✓✗✗✗ ✗✗✗ |
4 |
switch (arcn->type) { |
368 |
case PAX_DIR: |
||
369 |
/* |
||
370 |
* If -h (or -L) was given in tar-mode, follow the |
||
371 |
* potential symlink chain before trying to create the |
||
372 |
* directory. |
||
373 |
*/ |
||
374 |
✓✗✗✓ |
4 |
if (strcmp(NM_TAR, argv0) == 0 && Lflag) { |
375 |
while (lstat(nm, &sb) == 0 && |
||
376 |
S_ISLNK(sb.st_mode)) { |
||
377 |
len = readlink(nm, target, |
||
378 |
sizeof target - 1); |
||
379 |
if (len == -1) { |
||
380 |
syswarn(0, errno, |
||
381 |
"cannot follow symlink %s in chain for %s", |
||
382 |
nm, arcn->name); |
||
383 |
res = -1; |
||
384 |
goto badlink; |
||
385 |
} |
||
386 |
target[len] = '\0'; |
||
387 |
nm = target; |
||
388 |
} |
||
389 |
} |
||
390 |
4 |
res = mkdir(nm, file_mode); |
|
391 |
|||
392 |
4 |
badlink: |
|
393 |
✗✓ | 4 |
if (ign) |
394 |
res = 0; |
||
395 |
break; |
||
396 |
case PAX_CHR: |
||
397 |
file_mode |= S_IFCHR; |
||
398 |
res = mknod(nm, file_mode, arcn->sb.st_rdev); |
||
399 |
break; |
||
400 |
case PAX_BLK: |
||
401 |
file_mode |= S_IFBLK; |
||
402 |
res = mknod(nm, file_mode, arcn->sb.st_rdev); |
||
403 |
break; |
||
404 |
case PAX_FIF: |
||
405 |
res = mkfifo(nm, file_mode); |
||
406 |
break; |
||
407 |
case PAX_SCK: |
||
408 |
/* |
||
409 |
* Skip sockets, operation has no meaning under BSD |
||
410 |
*/ |
||
411 |
paxwarn(0, |
||
412 |
"%s skipped. Sockets cannot be copied or extracted", |
||
413 |
nm); |
||
414 |
return(-1); |
||
415 |
case PAX_SLK: |
||
416 |
if (arcn->ln_name[0] != '/' && |
||
417 |
!has_dotdot(arcn->ln_name)) |
||
418 |
res = symlink(arcn->ln_name, nm); |
||
419 |
else { |
||
420 |
/* |
||
421 |
* absolute symlinks and symlinks with ".." |
||
422 |
* have to be deferred to prevent the archive |
||
423 |
* from bootstrapping itself to outside the |
||
424 |
* working directory. |
||
425 |
*/ |
||
426 |
res = sltab_add_sym(nm, arcn->ln_name, |
||
427 |
arcn->sb.st_mode); |
||
428 |
if (res == 0) |
||
429 |
defer_pmode = 1; |
||
430 |
} |
||
431 |
break; |
||
432 |
case PAX_CTG: |
||
433 |
case PAX_HLK: |
||
434 |
case PAX_HRG: |
||
435 |
case PAX_REG: |
||
436 |
default: |
||
437 |
/* |
||
438 |
* we should never get here |
||
439 |
*/ |
||
440 |
paxwarn(0, "%s has an unknown file type, skipping", |
||
441 |
nm); |
||
442 |
return(-1); |
||
443 |
} |
||
444 |
|||
445 |
/* |
||
446 |
* if we were able to create the node break out of the loop, |
||
447 |
* otherwise try to unlink the node and try again. if that |
||
448 |
* fails check the full path and try a final time. |
||
449 |
*/ |
||
450 |
✗✓ | 4 |
if (res == 0) |
451 |
4 |
break; |
|
452 |
|||
453 |
/* |
||
454 |
* we failed to make the node |
||
455 |
*/ |
||
456 |
oerrno = errno; |
||
457 |
if ((ign = unlnk_exist(nm, arcn->type)) < 0) |
||
458 |
return(-1); |
||
459 |
|||
460 |
if (++pass <= 1) |
||
461 |
continue; |
||
462 |
|||
463 |
if (nodirs || chk_path(nm,arcn->sb.st_uid,arcn->sb.st_gid) < 0) { |
||
464 |
syswarn(1, oerrno, "Could not create: %s", nm); |
||
465 |
return(-1); |
||
466 |
} |
||
467 |
} |
||
468 |
|||
469 |
/* |
||
470 |
* we were able to create the node. set uid/gid, modes and times |
||
471 |
*/ |
||
472 |
✗✓ | 4 |
if (pids) |
473 |
res = set_ids(nm, arcn->sb.st_uid, arcn->sb.st_gid); |
||
474 |
else |
||
475 |
4 |
res = 0; |
|
476 |
|||
477 |
/* |
||
478 |
* IMPORTANT SECURITY NOTE: |
||
479 |
* if not preserving mode or we cannot set uid/gid, then PROHIBIT any |
||
480 |
* set uid/gid bits |
||
481 |
*/ |
||
482 |
✓✗ | 4 |
if (!pmode || res) |
483 |
4 |
arcn->sb.st_mode &= ~(SETBITS); |
|
484 |
✗✓ | 4 |
if (pmode && !defer_pmode) |
485 |
set_pmode(nm, arcn->sb.st_mode); |
||
486 |
|||
487 |
✓✗✓✗ |
4 |
if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) { |
488 |
/* |
||
489 |
* Dirs must be processed again at end of extract to set times |
||
490 |
* and modes to agree with those stored in the archive. However |
||
491 |
* to allow extract to continue, we may have to also set owner |
||
492 |
* rights. This allows nodes in the archive that are children |
||
493 |
* of this directory to be extracted without failure. Both time |
||
494 |
* and modes will be fixed after the entire archive is read and |
||
495 |
* before pax exits. To do that safely, we want the dev+ino |
||
496 |
* of the directory we created. |
||
497 |
*/ |
||
498 |
✗✓ | 4 |
if (lstat(nm, &sb) < 0) { |
499 |
syswarn(0, errno,"Could not access %s (stat)", nm); |
||
500 |
✗✓ | 4 |
} else if (access(nm, R_OK | W_OK | X_OK) < 0) { |
501 |
/* |
||
502 |
* We have to add rights to the dir, so we make |
||
503 |
* sure to restore the mode. The mode must be |
||
504 |
* restored AS CREATED and not as stored if |
||
505 |
* pmode is not set. |
||
506 |
*/ |
||
507 |
set_pmode(nm, |
||
508 |
((sb.st_mode & FILEBITS) | S_IRWXU)); |
||
509 |
if (!pmode) |
||
510 |
arcn->sb.st_mode = sb.st_mode; |
||
511 |
|||
512 |
/* |
||
513 |
* we have to force the mode to what was set |
||
514 |
* here, since we changed it from the default |
||
515 |
* as created. |
||
516 |
*/ |
||
517 |
arcn->sb.st_dev = sb.st_dev; |
||
518 |
arcn->sb.st_ino = sb.st_ino; |
||
519 |
add_dir(nm, &(arcn->sb), 1); |
||
520 |
✓✗✗✓ ✗✗ |
4 |
} else if (pmode || patime || pmtime) { |
521 |
4 |
arcn->sb.st_dev = sb.st_dev; |
|
522 |
4 |
arcn->sb.st_ino = sb.st_ino; |
|
523 |
4 |
add_dir(nm, &(arcn->sb), 0); |
|
524 |
} |
||
525 |
} |
||
526 |
|||
527 |
✗✓✗✗ |
4 |
if (patime || pmtime) |
528 |
4 |
set_ftime(nm, &arcn->sb.st_mtim, &arcn->sb.st_atim, 0); |
|
529 |
4 |
return(0); |
|
530 |
} |
||
531 |
|||
532 |
/* |
||
533 |
* unlnk_exist() |
||
534 |
* Remove node from file system with the specified name. We pass the type |
||
535 |
* of the node that is going to replace it. When we try to create a |
||
536 |
* directory and find that it already exists, we allow processing to |
||
537 |
* continue as proper modes etc will always be set for it later on. |
||
538 |
* Return: |
||
539 |
* 0 is ok to proceed, no file with the specified name exists |
||
540 |
* -1 we were unable to remove the node, or we should not remove it (-k) |
||
541 |
* 1 we found a directory and we were going to create a directory. |
||
542 |
*/ |
||
543 |
|||
544 |
int |
||
545 |
unlnk_exist(char *name, int type) |
||
546 |
{ |
||
547 |
struct stat sb; |
||
548 |
|||
549 |
/* |
||
550 |
* the file does not exist, or -k we are done |
||
551 |
*/ |
||
552 |
if (lstat(name, &sb) < 0) |
||
553 |
return(0); |
||
554 |
if (kflag) |
||
555 |
return(-1); |
||
556 |
|||
557 |
if (S_ISDIR(sb.st_mode)) { |
||
558 |
/* |
||
559 |
* try to remove a directory, if it fails and we were going to |
||
560 |
* create a directory anyway, tell the caller (return a 1) |
||
561 |
*/ |
||
562 |
if (rmdir(name) < 0) { |
||
563 |
if (type == PAX_DIR) |
||
564 |
return(1); |
||
565 |
syswarn(1,errno,"Unable to remove directory %s", name); |
||
566 |
return(-1); |
||
567 |
} |
||
568 |
delete_dir(sb.st_dev, sb.st_ino); |
||
569 |
return(0); |
||
570 |
} |
||
571 |
|||
572 |
/* |
||
573 |
* try to get rid of all non-directory type nodes |
||
574 |
*/ |
||
575 |
if (unlink(name) < 0) { |
||
576 |
syswarn(1, errno, "Could not unlink %s", name); |
||
577 |
return(-1); |
||
578 |
} |
||
579 |
return(0); |
||
580 |
} |
||
581 |
|||
582 |
/* |
||
583 |
* chk_path() |
||
584 |
* We were trying to create some kind of node in the file system and it |
||
585 |
* failed. chk_path() makes sure the path up to the node exists and is |
||
586 |
* writeable. When we have to create a directory that is missing along the |
||
587 |
* path somewhere, the directory we create will be set to the same |
||
588 |
* uid/gid as the file has (when uid and gid are being preserved). |
||
589 |
* NOTE: this routine is a real performance loss. It is only used as a |
||
590 |
* last resort when trying to create entries in the file system. |
||
591 |
* Return: |
||
592 |
* -1 when it could find nothing it is allowed to fix. |
||
593 |
* 0 otherwise |
||
594 |
*/ |
||
595 |
|||
596 |
int |
||
597 |
chk_path(char *name, uid_t st_uid, gid_t st_gid) |
||
598 |
{ |
||
599 |
char *spt = name; |
||
600 |
char *next; |
||
601 |
struct stat sb; |
||
602 |
int retval = -1; |
||
603 |
|||
604 |
/* |
||
605 |
* watch out for paths with nodes stored directly in / (e.g. /bozo) |
||
606 |
*/ |
||
607 |
while (*spt == '/') |
||
608 |
++spt; |
||
609 |
|||
610 |
for (;;) { |
||
611 |
/* |
||
612 |
* work forward from the first / and check each part of the path |
||
613 |
*/ |
||
614 |
spt = strchr(spt, '/'); |
||
615 |
if (spt == NULL) |
||
616 |
break; |
||
617 |
|||
618 |
/* |
||
619 |
* skip over duplicate slashes; stop if there're only |
||
620 |
* trailing slashes left |
||
621 |
*/ |
||
622 |
next = spt + 1; |
||
623 |
while (*next == '/') |
||
624 |
next++; |
||
625 |
if (*next == '\0') |
||
626 |
break; |
||
627 |
|||
628 |
*spt = '\0'; |
||
629 |
|||
630 |
/* |
||
631 |
* if it exists we assume it is a directory, it is not within |
||
632 |
* the spec (at least it seems to read that way) to alter the |
||
633 |
* file system for nodes NOT EXPLICITLY stored on the archive. |
||
634 |
* If that assumption is changed, you would test the node here |
||
635 |
* and figure out how to get rid of it (probably like some |
||
636 |
* recursive unlink()) or fix up the directory permissions if |
||
637 |
* required (do an access()). |
||
638 |
*/ |
||
639 |
if (lstat(name, &sb) == 0) { |
||
640 |
*spt = '/'; |
||
641 |
spt = next; |
||
642 |
continue; |
||
643 |
} |
||
644 |
|||
645 |
/* |
||
646 |
* the path fails at this point, see if we can create the |
||
647 |
* needed directory and continue on |
||
648 |
*/ |
||
649 |
if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { |
||
650 |
*spt = '/'; |
||
651 |
retval = -1; |
||
652 |
break; |
||
653 |
} |
||
654 |
|||
655 |
/* |
||
656 |
* we were able to create the directory. We will tell the |
||
657 |
* caller that we found something to fix, and it is ok to try |
||
658 |
* and create the node again. |
||
659 |
*/ |
||
660 |
retval = 0; |
||
661 |
if (pids) |
||
662 |
(void)set_ids(name, st_uid, st_gid); |
||
663 |
|||
664 |
/* |
||
665 |
* make sure the user doesn't have some strange umask that |
||
666 |
* causes this newly created directory to be unusable. We fix |
||
667 |
* the modes and restore them back to the creation default at |
||
668 |
* the end of pax |
||
669 |
*/ |
||
670 |
if ((access(name, R_OK | W_OK | X_OK) < 0) && |
||
671 |
(lstat(name, &sb) == 0)) { |
||
672 |
set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU)); |
||
673 |
add_dir(name, &sb, 1); |
||
674 |
} |
||
675 |
*spt = '/'; |
||
676 |
spt = next; |
||
677 |
continue; |
||
678 |
} |
||
679 |
return(retval); |
||
680 |
} |
||
681 |
|||
682 |
/* |
||
683 |
* set_ftime() |
||
684 |
* Set the access time and modification time for a named file. If frc |
||
685 |
* is non-zero we force these times to be set even if the user did not |
||
686 |
* request access and/or modification time preservation (this is also |
||
687 |
* used by -t to reset access times). |
||
688 |
* When ign is zero, only those times the user has asked for are set, the |
||
689 |
* other ones are left alone. |
||
690 |
*/ |
||
691 |
|||
692 |
void |
||
693 |
set_ftime(const char *fnm, const struct timespec *mtimp, |
||
694 |
const struct timespec *atimp, int frc) |
||
695 |
4 |
{ |
|
696 |
struct timespec tv[2]; |
||
697 |
|||
698 |
4 |
tv[0] = *atimp; |
|
699 |
4 |
tv[1] = *mtimp; |
|
700 |
|||
701 |
✓✗ | 4 |
if (!frc) { |
702 |
/* |
||
703 |
* if we are not forcing, only set those times the user wants |
||
704 |
* set. |
||
705 |
*/ |
||
706 |
✗✓ | 4 |
if (!patime) |
707 |
tv[0].tv_nsec = UTIME_OMIT; |
||
708 |
✗✓ | 4 |
if (!pmtime) |
709 |
tv[1].tv_nsec = UTIME_OMIT; |
||
710 |
} |
||
711 |
|||
712 |
/* |
||
713 |
* set the times |
||
714 |
*/ |
||
715 |
✗✓ | 4 |
if (utimensat(AT_FDCWD, fnm, tv, AT_SYMLINK_NOFOLLOW) < 0) |
716 |
syswarn(1, errno, "Access/modification time set failed on: %s", |
||
717 |
fnm); |
||
718 |
4 |
} |
|
719 |
|||
720 |
void |
||
721 |
fset_ftime(const char *fnm, int fd, const struct timespec *mtimp, |
||
722 |
const struct timespec *atimp, int frc) |
||
723 |
8 |
{ |
|
724 |
struct timespec tv[2]; |
||
725 |
|||
726 |
|||
727 |
8 |
tv[0] = *atimp; |
|
728 |
8 |
tv[1] = *mtimp; |
|
729 |
|||
730 |
✓✗ | 8 |
if (!frc) { |
731 |
/* |
||
732 |
* if we are not forcing, only set those times the user wants |
||
733 |
* set. |
||
734 |
*/ |
||
735 |
✗✓ | 8 |
if (!patime) |
736 |
tv[0].tv_nsec = UTIME_OMIT; |
||
737 |
✗✓ | 8 |
if (!pmtime) |
738 |
tv[1].tv_nsec = UTIME_OMIT; |
||
739 |
} |
||
740 |
/* |
||
741 |
* set the times |
||
742 |
*/ |
||
743 |
✗✓ | 8 |
if (futimens(fd, tv) < 0) |
744 |
syswarn(1, errno, "Access/modification time set failed on: %s", |
||
745 |
fnm); |
||
746 |
8 |
} |
|
747 |
|||
748 |
/* |
||
749 |
* set_ids() |
||
750 |
* set the uid and gid of a file system node |
||
751 |
* Return: |
||
752 |
* 0 when set, -1 on failure |
||
753 |
*/ |
||
754 |
|||
755 |
int |
||
756 |
set_ids(char *fnm, uid_t uid, gid_t gid) |
||
757 |
{ |
||
758 |
if (fchownat(AT_FDCWD, fnm, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) { |
||
759 |
/* |
||
760 |
* ignore EPERM unless in verbose mode or being run by root. |
||
761 |
* if running as pax, POSIX requires a warning. |
||
762 |
*/ |
||
763 |
if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || |
||
764 |
geteuid() == 0) |
||
765 |
syswarn(1, errno, "Unable to set file uid/gid of %s", |
||
766 |
fnm); |
||
767 |
return(-1); |
||
768 |
} |
||
769 |
return(0); |
||
770 |
} |
||
771 |
|||
772 |
int |
||
773 |
fset_ids(char *fnm, int fd, uid_t uid, gid_t gid) |
||
774 |
{ |
||
775 |
if (fchown(fd, uid, gid) < 0) { |
||
776 |
/* |
||
777 |
* ignore EPERM unless in verbose mode or being run by root. |
||
778 |
* if running as pax, POSIX requires a warning. |
||
779 |
*/ |
||
780 |
if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag || |
||
781 |
geteuid() == 0) |
||
782 |
syswarn(1, errno, "Unable to set file uid/gid of %s", |
||
783 |
fnm); |
||
784 |
return(-1); |
||
785 |
} |
||
786 |
return(0); |
||
787 |
} |
||
788 |
|||
789 |
/* |
||
790 |
* set_pmode() |
||
791 |
* Set file access mode |
||
792 |
*/ |
||
793 |
|||
794 |
void |
||
795 |
set_pmode(char *fnm, mode_t mode) |
||
796 |
{ |
||
797 |
mode &= ABITS; |
||
798 |
if (fchmodat(AT_FDCWD, fnm, mode, AT_SYMLINK_NOFOLLOW) < 0) |
||
799 |
syswarn(1, errno, "Could not set permissions on %s", fnm); |
||
800 |
} |
||
801 |
|||
802 |
void |
||
803 |
fset_pmode(char *fnm, int fd, mode_t mode) |
||
804 |
{ |
||
805 |
mode &= ABITS; |
||
806 |
if (fchmod(fd, mode) < 0) |
||
807 |
syswarn(1, errno, "Could not set permissions on %s", fnm); |
||
808 |
} |
||
809 |
|||
810 |
/* |
||
811 |
* set_attr() |
||
812 |
* Given a DIRDATA, restore the mode and times as indicated, but |
||
813 |
* only after verifying that it's the directory that we wanted. |
||
814 |
*/ |
||
815 |
int |
||
816 |
set_attr(const struct file_times *ft, int force_times, mode_t mode, |
||
817 |
int do_mode, int in_sig) |
||
818 |
4 |
{ |
|
819 |
struct stat sb; |
||
820 |
int fd, r; |
||
821 |
|||
822 |
✓✗✗✓ ✗✗ |
4 |
if (!do_mode && !force_times && !patime && !pmtime) |
823 |
return (0); |
||
824 |
|||
825 |
/* |
||
826 |
* We could legitimately go through a symlink here, |
||
827 |
* so do *not* use O_NOFOLLOW. The dev+ino check will |
||
828 |
* protect us from evil. |
||
829 |
*/ |
||
830 |
4 |
fd = open(ft->ft_name, O_RDONLY | O_DIRECTORY); |
|
831 |
✗✓ | 4 |
if (fd == -1) { |
832 |
if (!in_sig) |
||
833 |
syswarn(1, errno, "Unable to restore mode and times" |
||
834 |
" for directory: %s", ft->ft_name); |
||
835 |
return (-1); |
||
836 |
} |
||
837 |
|||
838 |
✗✓ | 4 |
if (fstat(fd, &sb) == -1) { |
839 |
if (!in_sig) |
||
840 |
syswarn(1, errno, "Unable to stat directory: %s", |
||
841 |
ft->ft_name); |
||
842 |
r = -1; |
||
843 |
✓✗✗✓ |
4 |
} else if (ft->ft_ino != sb.st_ino || ft->ft_dev != sb.st_dev) { |
844 |
if (!in_sig) |
||
845 |
paxwarn(1, "Directory vanished before restoring" |
||
846 |
" mode and times: %s", ft->ft_name); |
||
847 |
r = -1; |
||
848 |
} else { |
||
849 |
/* Whew, it's a match! Is there anything to change? */ |
||
850 |
✗✓✗✗ |
4 |
if (do_mode && (mode & ABITS) != (sb.st_mode & ABITS)) |
851 |
fset_pmode(ft->ft_name, fd, mode); |
||
852 |
✓✗✓✗ ✓✗✓✗ ✗✗✓✗ ✓✗✓✓ ✗✓✓✗ |
4 |
if (((force_times || patime) && |
853 |
timespeccmp(&ft->ft_atim, &sb.st_atim, !=)) || |
||
854 |
((force_times || pmtime) && |
||
855 |
timespeccmp(&ft->ft_mtim, &sb.st_mtim, !=))) |
||
856 |
2 |
fset_ftime(ft->ft_name, fd, &ft->ft_mtim, |
|
857 |
&ft->ft_atim, force_times); |
||
858 |
4 |
r = 0; |
|
859 |
} |
||
860 |
4 |
close(fd); |
|
861 |
|||
862 |
4 |
return (r); |
|
863 |
} |
||
864 |
|||
865 |
|||
866 |
/* |
||
867 |
* file_write() |
||
868 |
* Write/copy a file (during copy or archive extract). This routine knows |
||
869 |
* how to copy files with lseek holes in it. (Which are read as file |
||
870 |
* blocks containing all 0's but do not have any file blocks associated |
||
871 |
* with the data). Typical examples of these are files created by dbm |
||
872 |
* variants (.pag files). While the file size of these files are huge, the |
||
873 |
* actual storage is quite small (the files are sparse). The problem is |
||
874 |
* the holes read as all zeros so are probably stored on the archive that |
||
875 |
* way (there is no way to determine if the file block is really a hole, |
||
876 |
* we only know that a file block of all zero's can be a hole). |
||
877 |
* At this writing, no major archive format knows how to archive files |
||
878 |
* with holes. However, on extraction (or during copy, -rw) we have to |
||
879 |
* deal with these files. Without detecting the holes, the files can |
||
880 |
* consume a lot of file space if just written to disk. This replacement |
||
881 |
* for write when passed the basic allocation size of a file system block, |
||
882 |
* uses lseek whenever it detects the input data is all 0 within that |
||
883 |
* file block. In more detail, the strategy is as follows: |
||
884 |
* While the input is all zero keep doing an lseek. Keep track of when we |
||
885 |
* pass over file block boundaries. Only write when we hit a non zero |
||
886 |
* input. once we have written a file block, we continue to write it to |
||
887 |
* the end (we stop looking at the input). When we reach the start of the |
||
888 |
* next file block, start checking for zero blocks again. Working on file |
||
889 |
* block boundaries significantly reduces the overhead when copying files |
||
890 |
* that are NOT very sparse. This overhead (when compared to a write) is |
||
891 |
* almost below the measurement resolution on many systems. Without it, |
||
892 |
* files with holes cannot be safely copied. It does has a side effect as |
||
893 |
* it can put holes into files that did not have them before, but that is |
||
894 |
* not a problem since the file contents are unchanged (in fact it saves |
||
895 |
* file space). (Except on paging files for diskless clients. But since we |
||
896 |
* cannot determine one of those file from here, we ignore them). If this |
||
897 |
* ever ends up on a system where CTG files are supported and the holes |
||
898 |
* are not desired, just do a conditional test in those routines that |
||
899 |
* call file_write() and have it call write() instead. BEFORE CLOSING THE |
||
900 |
* FILE, make sure to call file_flush() when the last write finishes with |
||
901 |
* an empty block. A lot of file systems will not create an lseek hole at |
||
902 |
* the end. In this case we drop a single 0 at the end to force the |
||
903 |
* trailing 0's in the file. |
||
904 |
* ---Parameters--- |
||
905 |
* rem: how many bytes left in this file system block |
||
906 |
* isempt: have we written to the file block yet (is it empty) |
||
907 |
* sz: basic file block allocation size |
||
908 |
* cnt: number of bytes on this write |
||
909 |
* str: buffer to write |
||
910 |
* Return: |
||
911 |
* number of bytes written, -1 on write (or lseek) error. |
||
912 |
*/ |
||
913 |
|||
914 |
int |
||
915 |
file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz, |
||
916 |
char *name) |
||
917 |
{ |
||
918 |
char *pt; |
||
919 |
char *end; |
||
920 |
int wcnt; |
||
921 |
char *st = str; |
||
922 |
char **strp; |
||
923 |
|||
924 |
/* |
||
925 |
* while we have data to process |
||
926 |
*/ |
||
927 |
while (cnt) { |
||
928 |
if (!*rem) { |
||
929 |
/* |
||
930 |
* We are now at the start of file system block again |
||
931 |
* (or what we think one is...). start looking for |
||
932 |
* empty blocks again |
||
933 |
*/ |
||
934 |
*isempt = 1; |
||
935 |
*rem = sz; |
||
936 |
} |
||
937 |
|||
938 |
/* |
||
939 |
* only examine up to the end of the current file block or |
||
940 |
* remaining characters to write, whatever is smaller |
||
941 |
*/ |
||
942 |
wcnt = MINIMUM(cnt, *rem); |
||
943 |
cnt -= wcnt; |
||
944 |
*rem -= wcnt; |
||
945 |
if (*isempt) { |
||
946 |
/* |
||
947 |
* have not written to this block yet, so we keep |
||
948 |
* looking for zero's |
||
949 |
*/ |
||
950 |
pt = st; |
||
951 |
end = st + wcnt; |
||
952 |
|||
953 |
/* |
||
954 |
* look for a zero filled buffer |
||
955 |
*/ |
||
956 |
while ((pt < end) && (*pt == '\0')) |
||
957 |
++pt; |
||
958 |
|||
959 |
if (pt == end) { |
||
960 |
/* |
||
961 |
* skip, buf is empty so far |
||
962 |
*/ |
||
963 |
if (fd > -1 && |
||
964 |
lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) { |
||
965 |
syswarn(1,errno,"File seek on %s", |
||
966 |
name); |
||
967 |
return(-1); |
||
968 |
} |
||
969 |
st = pt; |
||
970 |
continue; |
||
971 |
} |
||
972 |
/* |
||
973 |
* drat, the buf is not zero filled |
||
974 |
*/ |
||
975 |
*isempt = 0; |
||
976 |
} |
||
977 |
|||
978 |
/* |
||
979 |
* have non-zero data in this file system block, have to write |
||
980 |
*/ |
||
981 |
switch (fd) { |
||
982 |
case -1: |
||
983 |
strp = &gnu_name_string; |
||
984 |
break; |
||
985 |
case -2: |
||
986 |
strp = &gnu_link_string; |
||
987 |
break; |
||
988 |
default: |
||
989 |
strp = NULL; |
||
990 |
break; |
||
991 |
} |
||
992 |
if (strp) { |
||
993 |
if (*strp) |
||
994 |
err(1, "WARNING! Major Internal Error! GNU hack Failing!"); |
||
995 |
*strp = malloc(wcnt + 1); |
||
996 |
if (*strp == NULL) { |
||
997 |
paxwarn(1, "Out of memory"); |
||
998 |
return(-1); |
||
999 |
} |
||
1000 |
memcpy(*strp, st, wcnt); |
||
1001 |
(*strp)[wcnt] = '\0'; |
||
1002 |
break; |
||
1003 |
} else if (write(fd, st, wcnt) != wcnt) { |
||
1004 |
syswarn(1, errno, "Failed write to file %s", name); |
||
1005 |
return(-1); |
||
1006 |
} |
||
1007 |
st += wcnt; |
||
1008 |
} |
||
1009 |
return(st - str); |
||
1010 |
} |
||
1011 |
|||
1012 |
/* |
||
1013 |
* file_flush() |
||
1014 |
* when the last file block in a file is zero, many file systems will not |
||
1015 |
* let us create a hole at the end. To get the last block with zeros, we |
||
1016 |
* write the last BYTE with a zero (back up one byte and write a zero). |
||
1017 |
*/ |
||
1018 |
|||
1019 |
void |
||
1020 |
file_flush(int fd, char *fname, int isempt) |
||
1021 |
{ |
||
1022 |
static char blnk[] = "\0"; |
||
1023 |
|||
1024 |
/* |
||
1025 |
* silly test, but make sure we are only called when the last block is |
||
1026 |
* filled with all zeros. |
||
1027 |
*/ |
||
1028 |
if (!isempt) |
||
1029 |
return; |
||
1030 |
|||
1031 |
/* |
||
1032 |
* move back one byte and write a zero |
||
1033 |
*/ |
||
1034 |
if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) { |
||
1035 |
syswarn(1, errno, "Failed seek on file %s", fname); |
||
1036 |
return; |
||
1037 |
} |
||
1038 |
|||
1039 |
if (write(fd, blnk, 1) < 0) |
||
1040 |
syswarn(1, errno, "Failed write to file %s", fname); |
||
1041 |
} |
||
1042 |
|||
1043 |
/* |
||
1044 |
* rdfile_close() |
||
1045 |
* close a file we have been reading (to copy or archive). If we have to |
||
1046 |
* reset access time (tflag) do so (the times are stored in arcn). |
||
1047 |
*/ |
||
1048 |
|||
1049 |
void |
||
1050 |
rdfile_close(ARCHD *arcn, int *fd) |
||
1051 |
18 |
{ |
|
1052 |
/* |
||
1053 |
* make sure the file is open |
||
1054 |
*/ |
||
1055 |
✓✓ | 18 |
if (*fd < 0) |
1056 |
4 |
return; |
|
1057 |
|||
1058 |
/* |
||
1059 |
* user wants last access time reset |
||
1060 |
*/ |
||
1061 |
✗✓ | 14 |
if (tflag) |
1062 |
fset_ftime(arcn->org_name, *fd, &arcn->sb.st_mtim, |
||
1063 |
&arcn->sb.st_atim, 1); |
||
1064 |
|||
1065 |
14 |
(void)close(*fd); |
|
1066 |
14 |
*fd = -1; |
|
1067 |
} |
||
1068 |
|||
1069 |
/* |
||
1070 |
* set_crc() |
||
1071 |
* read a file to calculate its crc. This is a real drag. Archive formats |
||
1072 |
* that have this, end up reading the file twice (we have to write the |
||
1073 |
* header WITH the crc before writing the file contents. Oh well... |
||
1074 |
* Return: |
||
1075 |
* 0 if was able to calculate the crc, -1 otherwise |
||
1076 |
*/ |
||
1077 |
|||
1078 |
int |
||
1079 |
set_crc(ARCHD *arcn, int fd) |
||
1080 |
{ |
||
1081 |
int i; |
||
1082 |
int res; |
||
1083 |
off_t cpcnt = 0L; |
||
1084 |
u_long size; |
||
1085 |
u_int32_t crc = 0; |
||
1086 |
char tbuf[FILEBLK]; |
||
1087 |
struct stat sb; |
||
1088 |
|||
1089 |
if (fd < 0) { |
||
1090 |
/* |
||
1091 |
* hmm, no fd, should never happen. well no crc then. |
||
1092 |
*/ |
||
1093 |
arcn->crc = 0L; |
||
1094 |
return(0); |
||
1095 |
} |
||
1096 |
|||
1097 |
if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf)) |
||
1098 |
size = (u_long)sizeof(tbuf); |
||
1099 |
|||
1100 |
/* |
||
1101 |
* read all the bytes we think that there are in the file. If the user |
||
1102 |
* is trying to archive an active file, forget this file. |
||
1103 |
*/ |
||
1104 |
for (;;) { |
||
1105 |
if ((res = read(fd, tbuf, size)) <= 0) |
||
1106 |
break; |
||
1107 |
cpcnt += res; |
||
1108 |
for (i = 0; i < res; ++i) |
||
1109 |
crc += (tbuf[i] & 0xff); |
||
1110 |
} |
||
1111 |
|||
1112 |
/* |
||
1113 |
* safety check. we want to avoid archiving files that are active as |
||
1114 |
* they can create inconsistent archive copies. |
||
1115 |
*/ |
||
1116 |
if (cpcnt != arcn->sb.st_size) |
||
1117 |
paxwarn(1, "File changed size %s", arcn->org_name); |
||
1118 |
else if (fstat(fd, &sb) < 0) |
||
1119 |
syswarn(1, errno, "Failed stat on %s", arcn->org_name); |
||
1120 |
else if (timespeccmp(&arcn->sb.st_mtim, &sb.st_mtim, !=)) |
||
1121 |
paxwarn(1, "File %s was modified during read", arcn->org_name); |
||
1122 |
else if (lseek(fd, (off_t)0L, SEEK_SET) < 0) |
||
1123 |
syswarn(1, errno, "File rewind failed on: %s", arcn->org_name); |
||
1124 |
else { |
||
1125 |
arcn->crc = crc; |
||
1126 |
return(0); |
||
1127 |
} |
||
1128 |
return(-1); |
||
1129 |
} |
Generated by: GCOVR (Version 3.3) |