1 //===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the Archive and ArchiveMember
11 // classes that is common to both reading and writing archives..
13 //===----------------------------------------------------------------------===//
16 #include "ArchiveInternals.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/PathV1.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/system_error.h"
28 // getMemberSize - compute the actual physical size of the file member as seen
29 // on disk. This isn't the size of member's payload. Use getSize() for that.
31 ArchiveMember::getMemberSize() const {
32 // Basically its the file size plus the header size
33 unsigned result = Size + sizeof(ArchiveMemberHeader);
35 // If it has a long filename, include the name length
36 if (hasLongFilename())
37 result += path.length() + 1;
39 // If its now odd lengthed, include the padding byte
46 // This default constructor is only use by the ilist when it creates its
47 // sentry node. We give it specific static values to make it stand out a bit.
48 ArchiveMember::ArchiveMember()
49 : parent(0), path("--invalid--"), flags(0), data(0)
51 User = sys::Process::GetCurrentUserId();
52 Group = sys::Process::GetCurrentGroupId();
55 ModTime = sys::TimeValue::now();
58 // This is the constructor that the Archive class uses when it is building or
59 // reading an archive. It just defaults a few things and ensures the parent is
60 // set for the iplist. The Archive class fills in the ArchiveMember's data.
61 // This is required because correctly setting the data may depend on other
62 // things in the Archive.
63 ArchiveMember::ArchiveMember(Archive* PAR)
64 : parent(PAR), path(), flags(0), data(0)
68 // This method allows an ArchiveMember to be replaced with the data for a
69 // different file, presumably as an update to the member. It also makes sure
70 // the flags are reset correctly.
71 bool ArchiveMember::replaceWith(StringRef newFile, std::string* ErrMsg) {
73 if (sys::fs::exists(newFile.str(), Exists) || !Exists) {
75 *ErrMsg = "Can not replace an archive member with a non-existent file";
82 // SVR4 symbol tables have an empty name
83 if (path == ARFILE_SVR4_SYMTAB_NAME)
84 flags |= SVR4SymbolTableFlag;
86 flags &= ~SVR4SymbolTableFlag;
88 // BSD4.4 symbol tables have a special name
89 if (path == ARFILE_BSD4_SYMTAB_NAME)
90 flags |= BSD4SymbolTableFlag;
92 flags &= ~BSD4SymbolTableFlag;
95 if (path == ARFILE_STRTAB_NAME)
96 flags |= StringTableFlag;
98 flags &= ~StringTableFlag;
100 // If it has a slash then it has a path
101 bool hasSlash = path.find('/') != std::string::npos;
103 flags |= HasPathFlag;
105 flags &= ~HasPathFlag;
107 // If it has a slash or its over 15 chars then its a long filename format
108 if (hasSlash || path.length() > 15)
109 flags |= HasLongFilenameFlag;
111 flags &= ~HasLongFilenameFlag;
113 // Get the signature and status info
114 const char* signature = (const char*) data;
115 SmallString<4> magic;
117 sys::fs::get_magic(path, magic.capacity(), magic);
118 signature = magic.c_str();
119 sys::PathWithStatus PWS(path);
120 const sys::FileStatus *FSinfo = PWS.getFileStatus(false, ErrMsg);
123 User = FSinfo->getUser();
124 Group = FSinfo->getGroup();
125 Mode = FSinfo->getMode();
126 ModTime = FSinfo->getTimestamp();
127 Size = FSinfo->getSize();
130 // Determine what kind of file it is.
131 if (sys::fs::identify_magic(StringRef(signature, 4)) ==
132 sys::fs::file_magic::bitcode)
133 flags |= BitcodeFlag;
135 flags &= ~BitcodeFlag;
140 // Archive constructor - this is the only constructor that gets used for the
141 // Archive class. Everything else (default,copy) is deprecated. This just
142 // initializes and maps the file into memory, if requested.
143 Archive::Archive(StringRef filename, LLVMContext &C)
144 : archPath(filename), members(), mapfile(0), base(0), strtab(),
145 firstFileOffset(0), modules(), Context(C) {}
148 Archive::mapToMemory(std::string* ErrMsg) {
149 OwningPtr<MemoryBuffer> File;
150 if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
152 *ErrMsg = ec.message();
155 mapfile = File.take();
156 base = mapfile->getBufferStart();
160 void Archive::cleanUpMemory() {
161 // Shutdown the file mapping
168 // Delete any Modules and ArchiveMember's we've allocated as a result of
169 // symbol table searches.
170 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
171 delete I->second.first;
172 delete I->second.second;
176 // Archive destructor - just clean up memory
177 Archive::~Archive() {