xref: /csrg-svn/usr.bin/ar/replace.c (revision 68964)
146003Sbostic /*-
266645Spendry  * Copyright (c) 1990, 1993, 1994
361906Sbostic  *	The Regents of the University of California.  All rights reserved.
446003Sbostic  *
546003Sbostic  * This code is derived from software contributed to Berkeley by
646003Sbostic  * Hugh Smith at The University of Guelph.
746003Sbostic  *
846003Sbostic  * %sccs.include.redist.c%
946003Sbostic  */
1046003Sbostic 
1146003Sbostic #ifndef lint
12*68964Sbostic static char sccsid[] = "@(#)replace.c	8.4 (Berkeley) 04/27/95";
1346003Sbostic #endif /* not lint */
1446003Sbostic 
1546003Sbostic #include <sys/param.h>
1646003Sbostic #include <sys/stat.h>
1766567Spendry 
1866567Spendry #include <ar.h>
1966567Spendry #include <dirent.h>
2066567Spendry #include <err.h>
2146003Sbostic #include <fcntl.h>
2246003Sbostic #include <stdio.h>
2347233Sbostic #include <string.h>
2466567Spendry #include <unistd.h>
2566567Spendry 
2646003Sbostic #include "archive.h"
2747233Sbostic #include "extern.h"
2846003Sbostic 
2946003Sbostic /*
3046003Sbostic  * replace --
3146003Sbostic  *	Replace or add named members to archive.  Entries already in the
3246003Sbostic  *	archive are swapped in place.  Others are added before or after
3346003Sbostic  *	the key entry, based on the a, b and i options.  If the u option
3446003Sbostic  *	is specified, modification dates select for replacement.
3546003Sbostic  */
3666567Spendry int
replace(argv)3746003Sbostic replace(argv)
3846003Sbostic 	char **argv;
3946003Sbostic {
4066567Spendry 	char *file;
4166567Spendry 	int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
4246003Sbostic 	struct stat sb;
4346003Sbostic 	CF cf;
4446003Sbostic 	off_t size, tsize;
4546003Sbostic 
4666567Spendry 	errflg = 0;
4746003Sbostic 	/*
4846003Sbostic 	 * If doesn't exist, simply append to the archive.  There's
4946003Sbostic 	 * a race here, but it's pretty short, and not worth fixing.
5046003Sbostic 	 */
5146003Sbostic 	exists = !stat(archive, &sb);
5246003Sbostic 	afd = open_archive(O_CREAT|O_RDWR);
5346003Sbostic 
5446003Sbostic 	if (!exists) {
5546003Sbostic 		tfd1 = -1;
5646003Sbostic 		tfd2 = tmp();
5746003Sbostic 		goto append;
5846003Sbostic 	}
5946003Sbostic 
6046003Sbostic 	tfd1 = tmp();			/* Files before key file. */
6146003Sbostic 	tfd2 = tmp();			/* Files after key file. */
6246003Sbostic 
6346003Sbostic 	/*
6446003Sbostic 	 * Break archive into two parts -- entries before and after the key
6546003Sbostic 	 * entry.  If positioning before the key, place the key at the
6646003Sbostic 	 * beginning of the after key entries and if positioning after the
6746003Sbostic 	 * key, place the key at the end of the before key entries.  Put it
6846003Sbostic 	 * all back together at the end.
6946003Sbostic 	 */
7046009Sbostic 	mods = (options & (AR_A|AR_B));
7147446Sbostic 	for (curfd = tfd1; get_arobj(afd);) {
7247233Sbostic 		if (*argv && (file = files(argv))) {
7346003Sbostic 			if ((sfd = open(file, O_RDONLY)) < 0) {
7466567Spendry 				errflg = 1;
7566567Spendry 				warn("%s", file);
7646003Sbostic 				goto useold;
7746003Sbostic 			}
7846003Sbostic 			(void)fstat(sfd, &sb);
79*68964Sbostic 			if (options & AR_U && sb.st_mtime <= chdr.date) {
80*68964Sbostic 				(void)close(sfd);
8146003Sbostic 				goto useold;
82*68964Sbostic 			}
8346003Sbostic 
8446003Sbostic 			if (options & AR_V)
8547214Sbostic 			     (void)printf("r - %s\n", file);
8646003Sbostic 
8747205Sbostic 			/* Read from disk, write to an archive; pad on write */
8846003Sbostic 			SETCF(sfd, file, curfd, tname, WPAD);
8947240Sbostic 			put_arobj(&cf, &sb);
9046003Sbostic 			(void)close(sfd);
9147240Sbostic 			skip_arobj(afd);
9246003Sbostic 			continue;
9346003Sbostic 		}
9446003Sbostic 
9546003Sbostic 		if (mods && compare(posname)) {
9646003Sbostic 			mods = 0;
9746003Sbostic 			if (options & AR_B)
9846003Sbostic 				curfd = tfd2;
9947205Sbostic 			/* Read and write to an archive; pad on both. */
10046003Sbostic 			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
10147240Sbostic 			put_arobj(&cf, (struct stat *)NULL);
10246003Sbostic 			if (options & AR_A)
10346003Sbostic 				curfd = tfd2;
10446003Sbostic 		} else {
10547205Sbostic 			/* Read and write to an archive; pad on both. */
10646003Sbostic useold:			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
10747240Sbostic 			put_arobj(&cf, (struct stat *)NULL);
10846003Sbostic 		}
10946003Sbostic 	}
11046003Sbostic 
11146003Sbostic 	if (mods) {
11266567Spendry 		warnx("%s: archive member not found", posarg);
11346003Sbostic                 close_archive(afd);
11466567Spendry                 return (1);
11546003Sbostic         }
11646003Sbostic 
11746003Sbostic 	/* Append any left-over arguments to the end of the after file. */
11846003Sbostic append:	while (file = *argv++) {
11946003Sbostic 		if (options & AR_V)
12047214Sbostic 			(void)printf("a - %s\n", file);
12146003Sbostic 		if ((sfd = open(file, O_RDONLY)) < 0) {
12266567Spendry 			errflg = 1;
12366567Spendry 			warn("%s", file);
12446003Sbostic 			continue;
12546003Sbostic 		}
12646003Sbostic 		(void)fstat(sfd, &sb);
12747205Sbostic 		/* Read from disk, write to an archive; pad on write. */
12846009Sbostic 		SETCF(sfd, file,
12946009Sbostic 		    options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
13047240Sbostic 		put_arobj(&cf, &sb);
13146003Sbostic 		(void)close(sfd);
13246003Sbostic 	}
13346003Sbostic 
13446003Sbostic 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
13546003Sbostic 
13647205Sbostic 	SETCF(tfd1, tname, afd, archive, NOPAD);
13746003Sbostic 	if (tfd1 != -1) {
13846003Sbostic 		tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
13946003Sbostic 		(void)lseek(tfd1, (off_t)0, SEEK_SET);
14047240Sbostic 		copy_ar(&cf, size);
14146003Sbostic 	} else
14246003Sbostic 		tsize = 0;
14346003Sbostic 
14446003Sbostic 	tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
14546003Sbostic 	(void)lseek(tfd2, (off_t)0, SEEK_SET);
14646003Sbostic 	cf.rfd = tfd2;
14747240Sbostic 	copy_ar(&cf, size);
14846003Sbostic 
14946003Sbostic 	(void)ftruncate(afd, tsize + SARMAG);
15046003Sbostic 	close_archive(afd);
15166567Spendry 	return (errflg);
15246003Sbostic }
153