llvm.org GIT mirror llvm / c850965
Revert "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@123557 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 9 years ago
3 changed file(s) with 43 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
2424
2525 namespace llvm {
2626 class MemoryBuffer;
27 class raw_ostream;
2827
2928 // Forward declare classes
3029 class Module; // From VMCore
482481 bool loadSymbolTable(std::string* ErrMessage);
483482
484483 /// @brief Write the symbol table to an ofstream.
485 void writeSymbolTable(raw_ostream& ARFile);
484 void writeSymbolTable(std::ofstream& ARFile);
486485
487486 /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
488487 /// false, otherwise true. If an error occurs and error is non-null then
491490 /// @returns true Writing member failed, \p error set to error message
492491 bool writeMember(
493492 const ArchiveMember& member, ///< The member to be written
494 raw_ostream& ARFile, ///< The file to write member onto
493 std::ofstream& ARFile, ///< The file to write member onto
495494 bool CreateSymbolTable, ///< Should symbol table be created?
496495 bool TruncateNames, ///< Should names be truncated to 11 chars?
497496 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"
2120 #include "llvm/Support/Signals.h"
2221 #include "llvm/Support/system_error.h"
2322 #include
2726
2827 // Write an integer using variable bit rate encoding. This saves a few bytes
2928 // per entry in the symbol table.
30 static inline void writeInteger(unsigned num, raw_ostream& ARFile) {
29 static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
3130 while (1) {
3231 if (num < 0x80) { // done?
3332 ARFile << (unsigned char)num;
201200 bool
202201 Archive::writeMember(
203202 const ArchiveMember& member,
204 raw_ostream& ARFile,
203 std::ofstream& ARFile,
205204 bool CreateSymbolTable,
206205 bool TruncateNames,
207206 bool ShouldCompress,
208207 std::string* ErrMsg
209208 ) {
210209
211 unsigned filepos = ARFile.tell();
210 unsigned filepos = ARFile.tellp();
212211 filepos -= 8;
213212
214213 // Get the data and its size either from the
281280 ARFile.write(data,fSize);
282281
283282 // Make sure the member is an even length
284 if ((ARFile.tell() & 1) == 1)
283 if ((ARFile.tellp() & 1) == 1)
285284 ARFile << ARFILE_PAD;
286285
287286 // Close the mapped file if it was opened
291290
292291 // Write out the LLVM symbol table as an archive member to the file.
293292 void
294 Archive::writeSymbolTable(raw_ostream& ARFile) {
293 Archive::writeSymbolTable(std::ofstream& ARFile) {
295294
296295 // Construct the symbol table's header
297296 ArchiveMemberHeader Hdr;
315314
316315 #ifndef NDEBUG
317316 // Save the starting position of the symbol tables data content.
318 unsigned startpos = ARFile.tell();
317 unsigned startpos = ARFile.tellp();
319318 #endif
320319
321320 // Write out the symbols sequentially
332331
333332 #ifndef NDEBUG
334333 // Now that we're done with the symbol table, get the ending file position
335 unsigned endpos = ARFile.tell();
334 unsigned endpos = ARFile.tellp();
336335 #endif
337336
338337 // Make sure that the amount we wrote is what we pre-computed. This is
361360 }
362361
363362 // Create a temporary file to store the archive in
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 }
363 sys::Path TmpArchive = archPath;
364 if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
365 return true;
371366
372367 // Make sure the temporary gets removed if we crash
373 sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str()));
368 sys::RemoveFileOnSignal(TmpArchive);
374369
375370 // Create archive file for output.
376 raw_fd_ostream ArchiveFile(ArchFD, true);
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 }
377382
378383 // If we're creating a symbol table, reset it now
379384 if (CreateSymbolTable) {
389394 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
390395 if (writeMember(*I, ArchiveFile, CreateSymbolTable,
391396 TruncateNames, Compress, ErrMsg)) {
397 TmpArchive.eraseFromDisk();
392398 ArchiveFile.close();
393 bool existed;
394 sys::fs::remove(TempArchivePath.str(), existed);
395399 return true;
396400 }
397401 }
406410 // ensure compatibility with other archivers we need to put the symbol
407411 // table first in the file. Unfortunately, this means mapping the file
408412 // we just wrote back in and copying it to the destination file.
409 SmallString<128> TempArchiveWithSymbolTablePath;
413 sys::Path FinalFilePath = archPath;
410414
411415 // Map in the archive we just wrote.
412416 {
413417 OwningPtr arch;
414 if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) {
418 if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
415419 if (ErrMsg)
416420 *ErrMsg = ec.message();
417421 return true;
420424
421425 // Open another temporary file in order to avoid invalidating the
422426 // mmapped data
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);
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 }
431438
432439 // Write the file magic number
433440 FinalFile << ARFILE_MAGIC;
440447 if (foreignST) {
441448 if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
442449 FinalFile.close();
443 bool existed;
444 sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
450 TmpArchive.eraseFromDisk();
445451 return true;
446452 }
447453 }
459465 } // free arch.
460466
461467 // Move the final file over top of TmpArchive
462 if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(),
463 TempArchivePath.str())) {
464 if (ErrMsg) *ErrMsg = ec.message();
465 return true;
466 }
468 if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
469 return true;
467470 }
468471
469472 // Before we replace the actual archive, we need to forget all the
471474 // this because we cannot replace an open file on Windows.
472475 cleanUpMemory();
473476
474 if (error_code ec = sys::fs::rename(TempArchivePath.str(),
475 archPath.str())) {
476 if (ErrMsg) *ErrMsg = ec.message();
477 return true;
478 }
477 if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
478 return true;
479479
480480 // Set correct read and write permissions after temporary file is moved
481481 // to final destination path.
0 ; Test linking of a bc file to an archive via llvm-ld.
1 ; XFAIL: *
21 ; PR1434
32 ; RUN: llvm-as %s -o %t.bar.bc
43 ; RUN: echo {define i32* @foo(i32 %x) \{ ret i32* @baz \} \