1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Hugh Smith at The University of Guelph. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)replace.c 5.7 (Berkeley) 03/12/91"; 13 #endif /* not lint */ 14 15 #include <sys/param.h> 16 #include <sys/stat.h> 17 #include <fcntl.h> 18 #include <dirent.h> 19 #include <errno.h> 20 #include <unistd.h> 21 #include <ar.h> 22 #include <stdio.h> 23 #include <string.h> 24 #include "archive.h" 25 #include "extern.h" 26 27 extern CHDR chdr; /* converted header */ 28 extern char *archive; /* archive name */ 29 extern char *tname; /* temporary file "name" */ 30 31 /* 32 * replace -- 33 * Replace or add named members to archive. Entries already in the 34 * archive are swapped in place. Others are added before or after 35 * the key entry, based on the a, b and i options. If the u option 36 * is specified, modification dates select for replacement. 37 */ 38 replace(argv) 39 char **argv; 40 { 41 extern char *posarg, *posname; /* positioning file name */ 42 register char *file; 43 register int afd, curfd, mods, sfd; 44 struct stat sb; 45 CF cf; 46 off_t size, tsize; 47 int err, exists, tfd1, tfd2; 48 char *rname(); 49 50 /* 51 * If doesn't exist, simply append to the archive. There's 52 * a race here, but it's pretty short, and not worth fixing. 53 */ 54 exists = !stat(archive, &sb); 55 afd = open_archive(O_CREAT|O_RDWR); 56 57 if (!exists) { 58 tfd1 = -1; 59 tfd2 = tmp(); 60 goto append; 61 } 62 63 tfd1 = tmp(); /* Files before key file. */ 64 tfd2 = tmp(); /* Files after key file. */ 65 66 /* 67 * Break archive into two parts -- entries before and after the key 68 * entry. If positioning before the key, place the key at the 69 * beginning of the after key entries and if positioning after the 70 * key, place the key at the end of the before key entries. Put it 71 * all back together at the end. 72 */ 73 mods = (options & (AR_A|AR_B)); 74 for (err = 0, curfd = tfd1; get_arobj(afd);) { 75 if (*argv && (file = files(argv))) { 76 if ((sfd = open(file, O_RDONLY)) < 0) { 77 err = 1; 78 (void)fprintf(stderr, "ar: %s: %s.\n", 79 file, strerror(errno)); 80 goto useold; 81 } 82 (void)fstat(sfd, &sb); 83 if (options & AR_U && sb.st_mtime <= chdr.date) 84 goto useold; 85 86 if (options & AR_V) 87 (void)printf("r - %s\n", file); 88 89 /* Read from disk, write to an archive; pad on write */ 90 SETCF(sfd, file, curfd, tname, WPAD); 91 put_arobj(&cf, &sb); 92 (void)close(sfd); 93 skip_arobj(afd); 94 continue; 95 } 96 97 if (mods && compare(posname)) { 98 mods = 0; 99 if (options & AR_B) 100 curfd = tfd2; 101 /* Read and write to an archive; pad on both. */ 102 SETCF(afd, archive, curfd, tname, RPAD|WPAD); 103 put_arobj(&cf, (struct stat *)NULL); 104 if (options & AR_A) 105 curfd = tfd2; 106 } else { 107 /* Read and write to an archive; pad on both. */ 108 useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD); 109 put_arobj(&cf, (struct stat *)NULL); 110 } 111 } 112 113 if (mods) { 114 (void)fprintf(stderr, "ar: %s: archive member not found.\n", 115 posarg); 116 close_archive(afd); 117 return(1); 118 } 119 120 /* Append any left-over arguments to the end of the after file. */ 121 append: while (file = *argv++) { 122 if (options & AR_V) 123 (void)printf("a - %s\n", file); 124 if ((sfd = open(file, O_RDONLY)) < 0) { 125 err = 1; 126 (void)fprintf(stderr, "ar: %s: %s.\n", 127 file, strerror(errno)); 128 continue; 129 } 130 (void)fstat(sfd, &sb); 131 /* Read from disk, write to an archive; pad on write. */ 132 SETCF(sfd, file, 133 options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD); 134 put_arobj(&cf, &sb); 135 (void)close(sfd); 136 } 137 138 (void)lseek(afd, (off_t)SARMAG, SEEK_SET); 139 140 SETCF(tfd1, tname, afd, archive, NOPAD); 141 if (tfd1 != -1) { 142 tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); 143 (void)lseek(tfd1, (off_t)0, SEEK_SET); 144 copy_ar(&cf, size); 145 } else 146 tsize = 0; 147 148 tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); 149 (void)lseek(tfd2, (off_t)0, SEEK_SET); 150 cf.rfd = tfd2; 151 copy_ar(&cf, size); 152 153 (void)ftruncate(afd, tsize + SARMAG); 154 close_archive(afd); 155 return(err); 156 } 157