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