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