1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. 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 8.1 (Berkeley) 06/06/93"; 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 err = 0; 51 /* 52 * If doesn't exist, simply append to the archive. There's 53 * a race here, but it's pretty short, and not worth fixing. 54 */ 55 exists = !stat(archive, &sb); 56 afd = open_archive(O_CREAT|O_RDWR); 57 58 if (!exists) { 59 tfd1 = -1; 60 tfd2 = tmp(); 61 goto append; 62 } 63 64 tfd1 = tmp(); /* Files before key file. */ 65 tfd2 = tmp(); /* Files after key file. */ 66 67 /* 68 * Break archive into two parts -- entries before and after the key 69 * entry. If positioning before the key, place the key at the 70 * beginning of the after key entries and if positioning after the 71 * key, place the key at the end of the before key entries. Put it 72 * all back together at the end. 73 */ 74 mods = (options & (AR_A|AR_B)); 75 for (curfd = tfd1; get_arobj(afd);) { 76 if (*argv && (file = files(argv))) { 77 if ((sfd = open(file, O_RDONLY)) < 0) { 78 err = 1; 79 (void)fprintf(stderr, "ar: %s: %s.\n", 80 file, strerror(errno)); 81 goto useold; 82 } 83 (void)fstat(sfd, &sb); 84 if (options & AR_U && sb.st_mtime <= chdr.date) 85 goto useold; 86 87 if (options & AR_V) 88 (void)printf("r - %s\n", file); 89 90 /* Read from disk, write to an archive; pad on write */ 91 SETCF(sfd, file, curfd, tname, WPAD); 92 put_arobj(&cf, &sb); 93 (void)close(sfd); 94 skip_arobj(afd); 95 continue; 96 } 97 98 if (mods && compare(posname)) { 99 mods = 0; 100 if (options & AR_B) 101 curfd = tfd2; 102 /* Read and write to an archive; pad on both. */ 103 SETCF(afd, archive, curfd, tname, RPAD|WPAD); 104 put_arobj(&cf, (struct stat *)NULL); 105 if (options & AR_A) 106 curfd = tfd2; 107 } else { 108 /* Read and write to an archive; pad on both. */ 109 useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD); 110 put_arobj(&cf, (struct stat *)NULL); 111 } 112 } 113 114 if (mods) { 115 (void)fprintf(stderr, "ar: %s: archive member not found.\n", 116 posarg); 117 close_archive(afd); 118 return(1); 119 } 120 121 /* Append any left-over arguments to the end of the after file. */ 122 append: while (file = *argv++) { 123 if (options & AR_V) 124 (void)printf("a - %s\n", file); 125 if ((sfd = open(file, O_RDONLY)) < 0) { 126 err = 1; 127 (void)fprintf(stderr, "ar: %s: %s.\n", 128 file, strerror(errno)); 129 continue; 130 } 131 (void)fstat(sfd, &sb); 132 /* Read from disk, write to an archive; pad on write. */ 133 SETCF(sfd, file, 134 options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD); 135 put_arobj(&cf, &sb); 136 (void)close(sfd); 137 } 138 139 (void)lseek(afd, (off_t)SARMAG, SEEK_SET); 140 141 SETCF(tfd1, tname, afd, archive, NOPAD); 142 if (tfd1 != -1) { 143 tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); 144 (void)lseek(tfd1, (off_t)0, SEEK_SET); 145 copy_ar(&cf, size); 146 } else 147 tsize = 0; 148 149 tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); 150 (void)lseek(tfd2, (off_t)0, SEEK_SET); 151 cf.rfd = tfd2; 152 copy_ar(&cf, size); 153 154 (void)ftruncate(afd, tsize + SARMAG); 155 close_archive(afd); 156 return(err); 157 } 158