Chris Lattner | 97f752f | 2003-12-30 07:45:46 +0000 | [diff] [blame] | 1 | //===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Tanya Lattner | d607491 | 2004-11-15 19:21:49 +0000 | [diff] [blame] | 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 10 | // Builds up (relatively) standard unix archive files (.a) containing LLVM |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 11 | // bytecode or other files. |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 4fa9fd3 | 2003-10-10 18:47:08 +0000 | [diff] [blame] | 14 | |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 16 | #include "llvm/Bytecode/Archive.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compressor.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 19 | #include "llvm/System/Signals.h" |
Reid Spencer | 86f42bd | 2004-07-04 12:20:55 +0000 | [diff] [blame] | 20 | #include <iostream> |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <iomanip> |
Duraid Madina | 6927b06 | 2005-12-28 06:56:09 +0000 | [diff] [blame] | 23 | #include <memory> |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 27 | // Option for compatibility with ASIX, not used but must allow it to be present. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 28 | static cl::opt<bool> |
| 29 | X32Option ("X32_64", cl::Hidden, |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 30 | cl::desc("Ignored option for compatibility with AIX")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 31 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 32 | // llvm-ar operation code and modifier flags. This must come first. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 34 | Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 35 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 36 | // llvm-ar remaining positional arguments. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 37 | static cl::list<std::string> |
| 38 | RestOfArgs(cl::Positional, cl::OneOrMore, |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 39 | cl::desc("[relpos] [count] <archive-file> [members]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 40 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 41 | // MoreHelp - Provide additional help output explaining the operations and |
| 42 | // modifiers of llvm-ar. This object instructs the CommandLine library |
| 43 | // to print the text of the constructor when the --help option is given. |
| 44 | static cl::extrahelp MoreHelp( |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 45 | "\nOPERATIONS:\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 46 | " d[NsS] - delete file(s) from the archive\n" |
| 47 | " m[abiSs] - move file(s) in the archive\n" |
| 48 | " p[kN] - print file(s) found in the archive\n" |
| 49 | " q[ufsS] - quick append file(s) to the archive\n" |
| 50 | " r[abfiuzRsS] - replace or insert file(s) into the archive\n" |
| 51 | " t - display contents of archive\n" |
| 52 | " x[No] - extract file(s) from the archive\n" |
| 53 | "\nMODIFIERS (operation specific):\n" |
| 54 | " [a] - put file(s) after [relpos]\n" |
| 55 | " [b] - put file(s) before [relpos] (same as [i])\n" |
| 56 | " [f] - truncate inserted file names\n" |
| 57 | " [i] - put file(s) before [relpos] (same as [b])\n" |
| 58 | " [k] - always print bytecode files (default is to skip them)\n" |
| 59 | " [N] - use instance [count] of name\n" |
| 60 | " [o] - preserve original dates\n" |
| 61 | " [P] - use full path names when matching\n" |
| 62 | " [R] - recurse through directories when inserting\n" |
| 63 | " [s] - create an archive index (cf. ranlib)\n" |
| 64 | " [S] - do not build a symbol table\n" |
| 65 | " [u] - update only files newer than archive contents\n" |
| 66 | " [z] - compress files before inserting/extracting\n" |
| 67 | "\nMODIFIERS (generic):\n" |
| 68 | " [c] - do not warn if the library had to be created\n" |
| 69 | " [v] - be verbose about actions taken\n" |
| 70 | " [V] - be *really* verbose about actions taken\n" |
| 71 | ); |
| 72 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 73 | // This enumeration delineates the kinds of operations on an archive |
| 74 | // that are permitted. |
| 75 | enum ArchiveOperation { |
| 76 | NoOperation, ///< An operation hasn't been specified |
| 77 | Print, ///< Print the contents of the archive |
| 78 | Delete, ///< Delete the specified members |
| 79 | Move, ///< Move members to end or as given by {a,b,i} modifiers |
| 80 | QuickAppend, ///< Quickly append to end of archive |
| 81 | ReplaceOrInsert, ///< Replace or Insert members |
| 82 | DisplayTable, ///< Display the table of contents |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 83 | Extract ///< Extract files back to file system |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 84 | }; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 85 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 86 | // Modifiers to follow operation to vary behavior |
| 87 | bool AddAfter = false; ///< 'a' modifier |
| 88 | bool AddBefore = false; ///< 'b' modifier |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 89 | bool Create = false; ///< 'c' modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 90 | bool TruncateNames = false; ///< 'f' modifier |
| 91 | bool InsertBefore = false; ///< 'i' modifier |
| 92 | bool DontSkipBytecode = false; ///< 'k' modifier |
| 93 | bool UseCount = false; ///< 'N' modifier |
| 94 | bool OriginalDates = false; ///< 'o' modifier |
| 95 | bool FullPath = false; ///< 'P' modifier |
| 96 | bool RecurseDirectories = false; ///< 'R' modifier |
| 97 | bool SymTable = true; ///< 's' & 'S' modifiers |
| 98 | bool OnlyUpdate = false; ///< 'u' modifier |
| 99 | bool Verbose = false; ///< 'v' modifier |
| 100 | bool ReallyVerbose = false; ///< 'V' modifier |
| 101 | bool Compression = false; ///< 'z' modifier |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 102 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 103 | // Relative Positional Argument (for insert/move). This variable holds |
| 104 | // the name of the archive member to which the 'a', 'b' or 'i' modifier |
| 105 | // refers. Only one of 'a', 'b' or 'i' can be specified so we only need |
| 106 | // one variable. |
| 107 | std::string RelPos; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 108 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 109 | // Select which of multiple entries in the archive with the same name should be |
| 110 | // used (specified with -N) for the delete and extract operations. |
| 111 | int Count = 1; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 112 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 113 | // This variable holds the name of the archive file as given on the |
| 114 | // command line. |
| 115 | std::string ArchiveName; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 116 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 117 | // This variable holds the list of member files to proecess, as given |
| 118 | // on the command line. |
| 119 | std::vector<std::string> Members; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 120 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 121 | // This variable holds the (possibly expanded) list of path objects that |
| 122 | // correspond to files we will |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 123 | std::set<sys::Path> Paths; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 124 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 125 | // The Archive object to which all the editing operations will be sent. |
| 126 | Archive* TheArchive = 0; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 127 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 128 | // getRelPos - Extract the member filename from the command line for |
| 129 | // the [relpos] argument associated with a, b, and i modifiers |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 130 | void getRelPos() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 131 | if(RestOfArgs.size() > 0) { |
| 132 | RelPos = RestOfArgs[0]; |
| 133 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 134 | } |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 135 | else |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 136 | throw "Expected [relpos] for a, b, or i modifier"; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 139 | // getCount - Extract the [count] argument associated with the N modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 140 | // from the command line and check its value. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 141 | void getCount() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 142 | if(RestOfArgs.size() > 0) { |
| 143 | Count = atoi(RestOfArgs[0].c_str()); |
| 144 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 145 | } |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 146 | else |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 147 | throw "Expected [count] value with N modifier"; |
| 148 | |
| 149 | // Non-positive counts are not allowed |
| 150 | if (Count < 1) |
| 151 | throw "Invalid [count] value (not a positive integer)"; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 154 | // getArchive - Get the archive file name from the command line |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 155 | void getArchive() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 156 | if(RestOfArgs.size() > 0) { |
| 157 | ArchiveName = RestOfArgs[0]; |
| 158 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 159 | } |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 160 | else |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 161 | throw "An archive name must be specified."; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 164 | // getMembers - Copy over remaining items in RestOfArgs to our Members vector |
| 165 | // This is just for clarity. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 166 | void getMembers() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 167 | if(RestOfArgs.size() > 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 168 | Members = std::vector<std::string>(RestOfArgs); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 171 | // parseCommandLine - Parse the command line options as presented and return the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 172 | // operation specified. Process all modifiers and check to make sure that |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 173 | // constraints on modifier/operation pairs have not been violated. |
| 174 | ArchiveOperation parseCommandLine() { |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 175 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 176 | // Keep track of number of operations. We can only specify one |
| 177 | // per execution. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 178 | unsigned NumOperations = 0; |
| 179 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 180 | // Keep track of the number of positional modifiers (a,b,i). Only |
| 181 | // one can be specified. |
| 182 | unsigned NumPositional = 0; |
| 183 | |
| 184 | // Keep track of which operation was requested |
| 185 | ArchiveOperation Operation = NoOperation; |
| 186 | |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 187 | for(unsigned i=0; i<Options.size(); ++i) { |
| 188 | switch(Options[i]) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 189 | case 'd': ++NumOperations; Operation = Delete; break; |
| 190 | case 'm': ++NumOperations; Operation = Move ; break; |
| 191 | case 'p': ++NumOperations; Operation = Print; break; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 192 | case 'r': ++NumOperations; Operation = ReplaceOrInsert; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 193 | case 't': ++NumOperations; Operation = DisplayTable; break; |
| 194 | case 'x': ++NumOperations; Operation = Extract; break; |
| 195 | case 'c': Create = true; break; |
| 196 | case 'f': TruncateNames = true; break; |
| 197 | case 'k': DontSkipBytecode = true; break; |
| 198 | case 'l': /* accepted but unused */ break; |
| 199 | case 'o': OriginalDates = true; break; |
| 200 | case 'P': FullPath = true; break; |
| 201 | case 'R': RecurseDirectories = true; break; |
| 202 | case 's': SymTable = true; break; |
| 203 | case 'S': SymTable = false; break; |
| 204 | case 'u': OnlyUpdate = true; break; |
| 205 | case 'v': Verbose = true; break; |
| 206 | case 'V': Verbose = ReallyVerbose = true; break; |
| 207 | case 'z': Compression = true; break; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 208 | case 'a': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 209 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 210 | AddAfter = true; |
| 211 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 212 | break; |
| 213 | case 'b': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 214 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 215 | AddBefore = true; |
| 216 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 217 | break; |
| 218 | case 'i': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 219 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 220 | InsertBefore = true; |
| 221 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 222 | break; |
| 223 | case 'N': |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 224 | getCount(); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 225 | UseCount = true; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 226 | break; |
| 227 | default: |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 228 | cl::PrintHelpMessage(); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 232 | // At this point, the next thing on the command line must be |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 233 | // the archive name. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 234 | getArchive(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 235 | |
| 236 | // Everything on the command line at this point is a member. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 237 | getMembers(); |
| 238 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 239 | // Perform various checks on the operation/modifier specification |
| 240 | // to make sure we are dealing with a legal request. |
| 241 | if (NumOperations == 0) |
| 242 | throw "You must specify at least one of the operations"; |
| 243 | if (NumOperations > 1) |
| 244 | throw "Only one operation may be specified"; |
| 245 | if (NumPositional > 1) |
| 246 | throw "You may only specify one of a, b, and i modifiers"; |
| 247 | if (AddAfter || AddBefore || InsertBefore) |
| 248 | if (Operation != Move && Operation != ReplaceOrInsert) |
| 249 | throw "The 'a', 'b' and 'i' modifiers can only be specified with " |
| 250 | "the 'm' or 'r' operations"; |
| 251 | if (RecurseDirectories && Operation != ReplaceOrInsert) |
| 252 | throw "The 'R' modifiers is only applicabe to the 'r' operation"; |
| 253 | if (OriginalDates && Operation != Extract) |
| 254 | throw "The 'o' modifier is only applicable to the 'x' operation"; |
| 255 | if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert) |
| 256 | throw "The 'f' modifier is only applicable to the 'q' and 'r' operations"; |
| 257 | if (OnlyUpdate && Operation != ReplaceOrInsert) |
| 258 | throw "The 'u' modifier is only applicable to the 'r' operation"; |
| 259 | if (Compression && Operation!=ReplaceOrInsert && Operation!=Extract) |
| 260 | throw "The 'z' modifier is only applicable to the 'r' and 'x' operations"; |
| 261 | if (Count > 1 && Members.size() > 1) |
| 262 | throw "Only one member name may be specified with the 'N' modifier"; |
| 263 | |
| 264 | // Return the parsed operation to the caller |
| 265 | return Operation; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 266 | } |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 267 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 268 | // recurseDirectories - Implements the "R" modifier. This function scans through |
| 269 | // the Paths vector (built by buildPaths, below) and replaces any directories it |
| 270 | // finds with all the files in that directory (recursively). It uses the |
| 271 | // sys::Path::getDirectoryContent method to perform the actual directory scans. |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 272 | std::set<sys::Path> recurseDirectories(const sys::Path& path) { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 273 | std::set<sys::Path> result; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 274 | if (RecurseDirectories) { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 275 | std::set<sys::Path> content; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 276 | path.getDirectoryContents(content); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 277 | for (std::set<sys::Path>::iterator I = content.begin(), E = content.end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 278 | I != E; ++I) { |
Chris Lattner | 8bdbb04 | 2006-08-01 18:09:46 +0000 | [diff] [blame] | 279 | // Make sure it exists and is a directory |
| 280 | sys::FileStatus Status; |
| 281 | if (!I->getFileStatus(Status)) { |
| 282 | if (Status.isDir) { |
| 283 | std::set<sys::Path> moreResults = recurseDirectories(*I); |
| 284 | result.insert(moreResults.begin(), moreResults.end()); |
| 285 | } else { |
| 286 | result.insert(*I); |
| 287 | } |
| 288 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | // buildPaths - Convert the strings in the Members vector to sys::Path objects |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 295 | // and make sure they are valid and exist exist. This check is only needed for |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 296 | // the operations that add/replace files to the archive ('q' and 'r') |
| 297 | void buildPaths(bool checkExistence = true) { |
| 298 | for (unsigned i = 0; i < Members.size(); i++) { |
| 299 | sys::Path aPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 300 | if (!aPath.set(Members[i])) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 301 | throw std::string("File member name invalid: ") + Members[i]; |
| 302 | if (checkExistence) { |
| 303 | if (!aPath.exists()) |
| 304 | throw std::string("File does not exist: ") + Members[i]; |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 305 | sys::FileStatus si; |
| 306 | std::string Err; |
| 307 | if (aPath.getFileStatus(si, &Err)) |
| 308 | throw Err; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 309 | if (si.isDir) { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 310 | std::set<sys::Path> dirpaths = recurseDirectories(aPath); |
| 311 | Paths.insert(dirpaths.begin(),dirpaths.end()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 312 | } else { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 313 | Paths.insert(aPath); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 314 | } |
| 315 | } else { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 316 | Paths.insert(aPath); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 321 | // printSymbolTable - print out the archive's symbol table. |
| 322 | void printSymbolTable() { |
| 323 | std::cout << "\nArchive Symbol Table:\n"; |
| 324 | const Archive::SymTabType& symtab = TheArchive->getSymbolTable(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 325 | for (Archive::SymTabType::const_iterator I=symtab.begin(), E=symtab.end(); |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 326 | I != E; ++I ) { |
| 327 | unsigned offset = TheArchive->getFirstFileOffset() + I->second; |
| 328 | std::cout << " " << std::setw(9) << offset << "\t" << I->first <<"\n"; |
| 329 | } |
| 330 | } |
| 331 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 332 | // doPrint - Implements the 'p' operation. This function traverses the archive |
| 333 | // looking for members that match the path list. It is careful to uncompress |
| 334 | // things that should be and to skip bytecode files unless the 'k' modifier was |
| 335 | // given. |
| 336 | void doPrint() { |
| 337 | buildPaths(false); |
| 338 | unsigned countDown = Count; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 339 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 340 | I != E; ++I ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 341 | if (Paths.empty() || |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 342 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 343 | if (countDown == 1) { |
| 344 | const char* data = reinterpret_cast<const char*>(I->getData()); |
| 345 | |
| 346 | // Skip things that don't make sense to print |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 347 | if (I->isLLVMSymbolTable() || I->isSVR4SymbolTable() || |
| 348 | I->isBSD4SymbolTable() || (!DontSkipBytecode && |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 349 | (I->isBytecode() || I->isCompressedBytecode()))) |
| 350 | continue; |
| 351 | |
| 352 | if (Verbose) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 353 | std::cout << "Printing " << I->getPath().toString() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 354 | |
| 355 | if (I->isCompressedBytecode()) |
Reid Spencer | b55bc06 | 2004-11-14 23:17:41 +0000 | [diff] [blame] | 356 | Compressor::decompressToStream(data+4,I->getSize()-4,std::cout); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 357 | else if (I->isCompressed()) { |
Reid Spencer | b55bc06 | 2004-11-14 23:17:41 +0000 | [diff] [blame] | 358 | Compressor::decompressToStream(data,I->getSize(),std::cout); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 359 | } else { |
| 360 | unsigned len = I->getSize(); |
| 361 | std::cout.write(data, len); |
| 362 | } |
| 363 | } else { |
| 364 | countDown--; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // putMode - utility function for printing out the file mode when the 't' |
| 371 | // operation is in verbose mode. |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 372 | void printMode(unsigned mode) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 373 | if (mode & 004) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 374 | std::cout << "r"; |
| 375 | else |
| 376 | std::cout << "-"; |
| 377 | if (mode & 002) |
| 378 | std::cout << "w"; |
| 379 | else |
| 380 | std::cout << "-"; |
| 381 | if (mode & 001) |
| 382 | std::cout << "x"; |
| 383 | else |
| 384 | std::cout << "-"; |
| 385 | } |
| 386 | |
| 387 | // doDisplayTable - Implement the 't' operation. This function prints out just |
| 388 | // the file names of each of the members. However, if verbose mode is requested |
| 389 | // ('v' modifier) then the file type, permission mode, user, group, size, and |
| 390 | // modification time are also printed. |
| 391 | void doDisplayTable() { |
| 392 | buildPaths(false); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 393 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 394 | I != E; ++I ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 395 | if (Paths.empty() || |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 396 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 397 | if (Verbose) { |
| 398 | // FIXME: Output should be this format: |
| 399 | // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile |
| 400 | if (I->isBytecode()) |
| 401 | std::cout << "b"; |
| 402 | else if (I->isCompressedBytecode()) |
| 403 | std::cout << "B"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 404 | else if (I->isCompressed()) |
| 405 | std::cout << "Z"; |
| 406 | else |
| 407 | std::cout << " "; |
| 408 | unsigned mode = I->getMode(); |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 409 | printMode((mode >> 6) & 007); |
| 410 | printMode((mode >> 3) & 007); |
| 411 | printMode(mode & 007); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 412 | std::cout << " " << std::setw(4) << I->getUser(); |
| 413 | std::cout << "/" << std::setw(4) << I->getGroup(); |
| 414 | std::cout << " " << std::setw(8) << I->getSize(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 415 | std::cout << " " << std::setw(20) << |
Reid Spencer | b55bc06 | 2004-11-14 23:17:41 +0000 | [diff] [blame] | 416 | I->getModTime().toString().substr(4); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 417 | std::cout << " " << I->getPath().toString() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 418 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 419 | std::cout << I->getPath().toString() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 423 | if (ReallyVerbose) |
| 424 | printSymbolTable(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // doExtract - Implement the 'x' operation. This function extracts files back to |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame^] | 428 | // the file system, making sure to uncompress any that were compressed |
| 429 | bool doExtract(std::string* ErrMsg) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 430 | buildPaths(false); |
| 431 | unsigned countDown = Count; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 432 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 433 | I != E; ++I ) { |
| 434 | if (Paths.empty() || |
| 435 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 436 | |
| 437 | // Make sure the intervening directories are created |
| 438 | if (I->hasPath()) { |
| 439 | sys::Path dirs(I->getPath()); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 440 | dirs.eraseComponent(); |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame^] | 441 | if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg)) |
| 442 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Open up a file stream for writing |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 446 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 447 | std::ios::binary; |
| 448 | std::ofstream file(I->getPath().c_str(), io_mode); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 449 | |
| 450 | // Get the data and its length |
| 451 | const char* data = reinterpret_cast<const char*>(I->getData()); |
| 452 | unsigned len = I->getSize(); |
| 453 | |
| 454 | // Write the data, making sure to uncompress things first |
| 455 | if (I->isCompressed()) { |
Reid Spencer | b55bc06 | 2004-11-14 23:17:41 +0000 | [diff] [blame] | 456 | Compressor::decompressToStream(data,len,file); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 457 | } else { |
| 458 | file.write(data,len); |
| 459 | } |
| 460 | file.close(); |
| 461 | |
| 462 | // If we're supposed to retain the original modification times, etc. do so |
| 463 | // now. |
| 464 | if (OriginalDates) |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 465 | I->getPath().setStatusInfoOnDisk(I->getFileStatus()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame^] | 468 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // doDelete - Implement the delete operation. This function deletes zero or more |
| 472 | // members from the archive. Note that if the count is specified, there should |
| 473 | // be no more than one path in the Paths list or else this algorithm breaks. |
| 474 | // That check is enforced in parseCommandLine (above). |
| 475 | void doDelete() { |
| 476 | buildPaths(false); |
| 477 | if (Paths.empty()) return; |
| 478 | unsigned countDown = Count; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 479 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 480 | I != E; ) { |
| 481 | if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) { |
| 482 | if (countDown == 1) { |
| 483 | Archive::iterator J = I; |
| 484 | ++I; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 485 | TheArchive->erase(J); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 486 | } else |
| 487 | countDown--; |
| 488 | } else { |
| 489 | ++I; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // We're done editting, reconstruct the archive. |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 494 | std::string errmsg; |
| 495 | if (!TheArchive->writeToDisk(SymTable,TruncateNames,Compression,&errmsg)) |
| 496 | throw errmsg; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 497 | if (ReallyVerbose) |
| 498 | printSymbolTable(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // doMore - Implement the move operation. This function re-arranges just the |
| 502 | // order of the archive members so that when the archive is written the move |
| 503 | // of the members is accomplished. Note the use of the RelPos variable to |
| 504 | // determine where the items should be moved to. |
| 505 | void doMove() { |
| 506 | |
| 507 | buildPaths(false); |
| 508 | |
| 509 | // By default and convention the place to move members to is the end of the |
| 510 | // archive. |
| 511 | Archive::iterator moveto_spot = TheArchive->end(); |
| 512 | |
| 513 | // However, if the relative positioning modifiers were used, we need to scan |
| 514 | // the archive to find the member in question. If we don't find it, its no |
| 515 | // crime, we just move to the end. |
| 516 | if (AddBefore || InsertBefore || AddAfter) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 517 | for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 518 | I != E; ++I ) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 519 | if (RelPos == I->getPath().toString()) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 520 | if (AddAfter) { |
| 521 | moveto_spot = I; |
| 522 | moveto_spot++; |
| 523 | } else { |
| 524 | moveto_spot = I; |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // Keep a list of the paths remaining to be moved |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 532 | std::set<sys::Path> remaining(Paths); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 533 | |
| 534 | // Scan the archive again, this time looking for the members to move to the |
| 535 | // moveto_spot. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 536 | for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 537 | I != E && !remaining.empty(); ++I ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 538 | std::set<sys::Path>::iterator found = |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 539 | std::find(remaining.begin(),remaining.end(),I->getPath()); |
| 540 | if (found != remaining.end()) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 541 | if (I != moveto_spot) |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 542 | TheArchive->splice(moveto_spot,*TheArchive,I); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 543 | remaining.erase(found); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // We're done editting, reconstruct the archive. |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 548 | std::string errmsg; |
| 549 | if (!TheArchive->writeToDisk(SymTable,TruncateNames,Compression,&errmsg)) |
| 550 | throw errmsg; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 551 | if (ReallyVerbose) |
| 552 | printSymbolTable(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | // doQuickAppend - Implements the 'q' operation. This function just |
| 556 | // indiscriminantly adds the members to the archive and rebuilds it. |
| 557 | void doQuickAppend() { |
| 558 | // Get the list of paths to append. |
| 559 | buildPaths(true); |
| 560 | if (Paths.empty()) return; |
| 561 | |
| 562 | // Append them quickly. |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 563 | for (std::set<sys::Path>::iterator PI = Paths.begin(), PE = Paths.end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 564 | PI != PE; ++PI) { |
| 565 | TheArchive->addFileBefore(*PI,TheArchive->end()); |
| 566 | } |
| 567 | |
| 568 | // We're done editting, reconstruct the archive. |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 569 | std::string errmsg; |
| 570 | if (!TheArchive->writeToDisk(SymTable,TruncateNames,Compression,&errmsg)) |
| 571 | throw errmsg; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 572 | if (ReallyVerbose) |
| 573 | printSymbolTable(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // doReplaceOrInsert - Implements the 'r' operation. This function will replace |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 577 | // any existing files or insert new ones into the archive. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 578 | void doReplaceOrInsert() { |
| 579 | |
| 580 | // Build the list of files to be added/replaced. |
| 581 | buildPaths(true); |
| 582 | if (Paths.empty()) return; |
| 583 | |
| 584 | // Keep track of the paths that remain to be inserted. |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 585 | std::set<sys::Path> remaining(Paths); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 586 | |
| 587 | // Default the insertion spot to the end of the archive |
| 588 | Archive::iterator insert_spot = TheArchive->end(); |
| 589 | |
| 590 | // Iterate over the archive contents |
| 591 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
| 592 | I != E && !remaining.empty(); ++I ) { |
| 593 | |
| 594 | // Determine if this archive member matches one of the paths we're trying |
| 595 | // to replace. |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 596 | |
| 597 | std::set<sys::Path>::iterator found = remaining.end(); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 598 | for (std::set<sys::Path>::iterator RI = remaining.begin(), |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 599 | RE = remaining.end(); RI != RE; ++RI ) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 600 | std::string compare(RI->toString()); |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 601 | if (TruncateNames && compare.length() > 15) { |
| 602 | const char* nm = compare.c_str(); |
| 603 | unsigned len = compare.length(); |
| 604 | size_t slashpos = compare.rfind('/'); |
| 605 | if (slashpos != std::string::npos) { |
| 606 | nm += slashpos + 1; |
| 607 | len -= slashpos +1; |
| 608 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 609 | if (len > 15) |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 610 | len = 15; |
| 611 | compare.assign(nm,len); |
| 612 | } |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 613 | if (compare == I->getPath().toString()) { |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 614 | found = RI; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 619 | if (found != remaining.end()) { |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 620 | sys::FileStatus si; |
| 621 | std::string Err; |
| 622 | if (found->getFileStatus(si, &Err)) |
| 623 | throw Err; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 624 | if (si.isDir) { |
| 625 | if (OnlyUpdate) { |
| 626 | // Replace the item only if it is newer. |
| 627 | if (si.modTime > I->getModTime()) |
| 628 | I->replaceWith(*found); |
| 629 | } else { |
| 630 | // Replace the item regardless of time stamp |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 631 | I->replaceWith(*found); |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 632 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 633 | } else { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 634 | // We purposefully ignore directories. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // Remove it from our "to do" list |
| 638 | remaining.erase(found); |
| 639 | } |
| 640 | |
| 641 | // Determine if this is the place where we should insert |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 642 | if ((AddBefore || InsertBefore) && (RelPos == I->getPath().toString())) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 643 | insert_spot = I; |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 644 | else if (AddAfter && (RelPos == I->getPath().toString())) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 645 | insert_spot = I; |
| 646 | insert_spot++; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // If we didn't replace all the members, some will remain and need to be |
| 651 | // inserted at the previously computed insert-spot. |
| 652 | if (!remaining.empty()) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 653 | for (std::set<sys::Path>::iterator PI = remaining.begin(), |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 654 | PE = remaining.end(); PI != PE; ++PI) { |
| 655 | TheArchive->addFileBefore(*PI,insert_spot); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // We're done editting, reconstruct the archive. |
Reid Spencer | 3039b99 | 2006-07-07 19:09:14 +0000 | [diff] [blame] | 660 | std::string errmsg; |
| 661 | if (!TheArchive->writeToDisk(SymTable,TruncateNames,Compression,&errmsg)) |
| 662 | throw errmsg; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 663 | if (ReallyVerbose) |
| 664 | printSymbolTable(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // main - main program for llvm-ar .. see comments in the code |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 668 | int main(int argc, char **argv) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 669 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 670 | // Have the command line options parsed and handle things |
| 671 | // like --help and --version. |
| 672 | cl::ParseCommandLineOptions(argc, argv, |
| 673 | " LLVM Archiver (llvm-ar)\n\n" |
| 674 | " This program archives bytecode files into single libraries\n" |
| 675 | ); |
| 676 | |
| 677 | // Print a stack trace if we signal out. |
Reid Spencer | 9de7b33 | 2004-08-29 19:28:55 +0000 | [diff] [blame] | 678 | sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | f73b4ca | 2004-02-19 20:32:12 +0000 | [diff] [blame] | 679 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 680 | int exitCode = 0; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 681 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 682 | // Make sure we don't exit with "unhandled exception". |
| 683 | try { |
| 684 | // Do our own parsing of the command line because the CommandLine utility |
| 685 | // can't handle the grouped positional parameters without a dash. |
| 686 | ArchiveOperation Operation = parseCommandLine(); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 687 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 688 | // Check the path name of the archive |
| 689 | sys::Path ArchivePath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 690 | if (!ArchivePath.set(ArchiveName)) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 691 | throw std::string("Archive name invalid: ") + ArchiveName; |
| 692 | |
| 693 | // Create or open the archive object. |
| 694 | if (!ArchivePath.exists()) { |
| 695 | // Produce a warning if we should and we're creating the archive |
| 696 | if (!Create) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 697 | std::cerr << argv[0] << ": creating " << ArchivePath.toString() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 698 | TheArchive = Archive::CreateEmpty(ArchivePath); |
| 699 | } else { |
Chris Lattner | 67c3821 | 2004-12-15 07:44:15 +0000 | [diff] [blame] | 700 | std::string Error; |
| 701 | TheArchive = Archive::OpenAndLoad(ArchivePath, &Error); |
Reid Spencer | b4ed7b0 | 2004-12-15 21:58:03 +0000 | [diff] [blame] | 702 | if (TheArchive == 0) { |
Chris Lattner | 67c3821 | 2004-12-15 07:44:15 +0000 | [diff] [blame] | 703 | std::cerr << argv[0] << ": error loading '" << ArchivePath << "': " |
| 704 | << Error << "!\n"; |
Reid Spencer | b4ed7b0 | 2004-12-15 21:58:03 +0000 | [diff] [blame] | 705 | return 1; |
| 706 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // Make sure we're not fooling ourselves. |
| 710 | assert(TheArchive && "Unable to instantiate the archive"); |
| 711 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 712 | // Make sure we clean up the archive even on failure. |
| 713 | std::auto_ptr<Archive> AutoArchive(TheArchive); |
| 714 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 715 | // Perform the operation |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame^] | 716 | std::string ErrMsg; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 717 | switch (Operation) { |
| 718 | case Print: doPrint(); break; |
| 719 | case Delete: doDelete(); break; |
| 720 | case Move: doMove(); break; |
| 721 | case QuickAppend: /* FALL THROUGH */ |
| 722 | case ReplaceOrInsert: doReplaceOrInsert(); break; |
| 723 | case DisplayTable: doDisplayTable(); break; |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame^] | 724 | case Extract: |
| 725 | if (doExtract(&ErrMsg)) { |
| 726 | std::cerr << argv[0] << ": " << ErrMsg << "\n"; |
| 727 | return 1; |
| 728 | } |
| 729 | break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 730 | case NoOperation: |
| 731 | std::cerr << argv[0] << ": No operation was selected.\n"; |
| 732 | break; |
| 733 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 734 | } catch (const char*msg) { |
| 735 | // These errors are usage errors, thrown only by the various checks in the |
| 736 | // code above. |
| 737 | std::cerr << argv[0] << ": " << msg << "\n\n"; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 738 | cl::PrintHelpMessage(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 739 | exitCode = 1; |
| 740 | } catch (const std::string& msg) { |
| 741 | // These errors are thrown by LLVM libraries (e.g. lib System) and represent |
| 742 | // a more serious error so we bump the exitCode and don't print the usage. |
| 743 | std::cerr << argv[0] << ": " << msg << "\n"; |
| 744 | exitCode = 2; |
| 745 | } catch (...) { |
| 746 | // This really shouldn't happen, but just in case .... |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 747 | std::cerr << argv[0] << ": An unexpected unknown exception occurred.\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 748 | exitCode = 3; |
| 749 | } |
| 750 | |
| 751 | // Return result code back to operating system. |
| 752 | return exitCode; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 753 | } |