llvm.org GIT mirror llvm / 770772e
Archive: Replace all internal uses of PathV1 with PathV2. The external API still uses PathV1. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123551 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 9 years ago
2 changed file(s) with 44 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
2424
2525 namespace llvm {
2626 class MemoryBuffer;
27 class raw_ostream;
2728
2829 // Forward declare classes
2930 class Module; // From VMCore
481482 bool loadSymbolTable(std::string* ErrMessage);
482483
483484 /// @brief Write the symbol table to an ofstream.
484 void writeSymbolTable(std::ofstream& ARFile);
485 void writeSymbolTable(raw_ostream& ARFile);
485486
486487 /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
487488 /// false, otherwise true. If an error occurs and error is non-null then
490491 /// @returns true Writing member failed, \p error set to error message
491492 bool writeMember(
492493 const ArchiveMember& member, ///< The member to be written
493 std::ofstream& ARFile, ///< The file to write member onto
494 raw_ostream& ARFile, ///< The file to write member onto
494495 bool CreateSymbolTable, ///< Should symbol table be created?
495496 bool TruncateNames, ///< Should names be truncated to 11 chars?
496497 bool ShouldCompress, ///< Should the member be compressed?
1717 #include "llvm/Support/FileSystem.h"
1818 #include "llvm/Support/MemoryBuffer.h"
1919 #include "llvm/Support/Process.h"
20 #include "llvm/Support/raw_ostream.h"
2021 #include "llvm/Support/Signals.h"
2122 #include "llvm/Support/system_error.h"
2223 #include
2627
2728 // Write an integer using variable bit rate encoding. This saves a few bytes
2829 // per entry in the symbol table.
29 static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
30 static inline void writeInteger(unsigned num, raw_ostream& ARFile) {
3031 while (1) {
3132 if (num < 0x80) { // done?
3233 ARFile << (unsigned char)num;
200201 bool
201202 Archive::writeMember(
202203 const ArchiveMember& member,
203 std::ofstream& ARFile,
204 raw_ostream& ARFile,
204205 bool CreateSymbolTable,
205206 bool TruncateNames,
206207 bool ShouldCompress,
207208 std::string* ErrMsg
208209 ) {
209210
210 unsigned filepos = ARFile.tellp();
211 unsigned filepos = ARFile.tell();
211212 filepos -= 8;
212213
213214 // Get the data and its size either from the
280281 ARFile.write(data,fSize);
281282
282283 // Make sure the member is an even length
283 if ((ARFile.tellp() & 1) == 1)
284 if ((ARFile.tell() & 1) == 1)
284285 ARFile << ARFILE_PAD;
285286
286287 // Close the mapped file if it was opened
290291
291292 // Write out the LLVM symbol table as an archive member to the file.
292293 void
293 Archive::writeSymbolTable(std::ofstream& ARFile) {
294 Archive::writeSymbolTable(raw_ostream& ARFile) {
294295
295296 // Construct the symbol table's header
296297 ArchiveMemberHeader Hdr;
314315
315316 #ifndef NDEBUG
316317 // Save the starting position of the symbol tables data content.
317 unsigned startpos = ARFile.tellp();
318 unsigned startpos = ARFile.tell();
318319 #endif
319320
320321 // Write out the symbols sequentially
331332
332333 #ifndef NDEBUG
333334 // Now that we're done with the symbol table, get the ending file position
334 unsigned endpos = ARFile.tellp();
335 unsigned endpos = ARFile.tell();
335336 #endif
336337
337338 // Make sure that the amount we wrote is what we pre-computed. This is
360361 }
361362
362363 // Create a temporary file to store the archive in
363 sys::Path TmpArchive = archPath;
364 if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
365 return true;
364 SmallString<128> TempArchivePath;
365 int ArchFD;
366 if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
367 ArchFD, TempArchivePath)) {
368 if (ErrMsg) *ErrMsg = ec.message();
369 return true;
370 }
366371
367372 // Make sure the temporary gets removed if we crash
368 sys::RemoveFileOnSignal(TmpArchive);
373 sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str()));
369374
370375 // Create archive file for output.
371 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
372 std::ios::binary;
373 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
374
375 // Check for errors opening or creating archive file.
376 if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
377 TmpArchive.eraseFromDisk();
378 if (ErrMsg)
379 *ErrMsg = "Error opening archive file: " + archPath.str();
380 return true;
381 }
376 raw_fd_ostream ArchiveFile(ArchFD, true);
382377
383378 // If we're creating a symbol table, reset it now
384379 if (CreateSymbolTable) {
394389 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
395390 if (writeMember(*I, ArchiveFile, CreateSymbolTable,
396391 TruncateNames, Compress, ErrMsg)) {
397 TmpArchive.eraseFromDisk();
398392 ArchiveFile.close();
393 bool existed;
394 sys::fs::remove(TempArchivePath.str(), existed);
399395 return true;
400396 }
401397 }
410406 // ensure compatibility with other archivers we need to put the symbol
411407 // table first in the file. Unfortunately, this means mapping the file
412408 // we just wrote back in and copying it to the destination file.
413 sys::Path FinalFilePath = archPath;
409 SmallString<128> TempArchiveWithSymbolTablePath;
414410
415411 // Map in the archive we just wrote.
416412 {
417413 OwningPtr arch;
418 if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
414 if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) {
419415 if (ErrMsg)
420416 *ErrMsg = ec.message();
421417 return true;
424420
425421 // Open another temporary file in order to avoid invalidating the
426422 // mmapped data
427 if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
428 return true;
429 sys::RemoveFileOnSignal(FinalFilePath);
430
431 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
432 if (!FinalFile.is_open() || FinalFile.bad()) {
433 TmpArchive.eraseFromDisk();
434 if (ErrMsg)
435 *ErrMsg = "Error opening archive file: " + FinalFilePath.str();
436 return true;
437 }
423 if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
424 ArchFD, TempArchiveWithSymbolTablePath)) {
425 if (ErrMsg) *ErrMsg = ec.message();
426 return true;
427 }
428 sys::RemoveFileOnSignal(sys::Path(TempArchiveWithSymbolTablePath.str()));
429
430 raw_fd_ostream FinalFile(ArchFD, true);
438431
439432 // Write the file magic number
440433 FinalFile << ARFILE_MAGIC;
447440 if (foreignST) {
448441 if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
449442 FinalFile.close();
450 TmpArchive.eraseFromDisk();
443 bool existed;
444 sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
451445 return true;
452446 }
453447 }
465459 } // free arch.
466460
467461 // Move the final file over top of TmpArchive
468 if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
469 return true;
462 if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(),
463 TempArchivePath.str())) {
464 if (ErrMsg) *ErrMsg = ec.message();
465 return true;
466 }
470467 }
471468
472469 // Before we replace the actual archive, we need to forget all the
474471 // this because we cannot replace an open file on Windows.
475472 cleanUpMemory();
476473
477 if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
478 return true;
474 if (error_code ec = sys::fs::rename(TempArchivePath.str(),
475 archPath.str())) {
476 if (ErrMsg) *ErrMsg = ec.message();
477 return true;
478 }
479479
480480 // Set correct read and write permissions after temporary file is moved
481481 // to final destination path.