llvm.org GIT mirror llvm / 898fd7f
Revert PathV2 changes, as sys::fs::unique_file is not finished yet. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126773 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
2 changed file(s) with 43 addition(s) and 46 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 =
367 sys::fs::unique_file("%%-%%-%%-%%-" + sys::path::filename(archPath.str()),
368 ArchFD, TempArchivePath)) {
369 if (ErrMsg) *ErrMsg = ec.message();
370 return true;
371 }
363 sys::Path TmpArchive = archPath;
364 if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
365 return true;
372366
373367 // Make sure the temporary gets removed if we crash
374 sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str()));
368 sys::RemoveFileOnSignal(TmpArchive);
375369
376370 // Create archive file for output.
377 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 }
378382
379383 // If we're creating a symbol table, reset it now
380384 if (CreateSymbolTable) {
390394 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
391395 if (writeMember(*I, ArchiveFile, CreateSymbolTable,
392396 TruncateNames, Compress, ErrMsg)) {
397 TmpArchive.eraseFromDisk();
393398 ArchiveFile.close();
394 bool existed;
395 sys::fs::remove(TempArchivePath.str(), existed);
396399 return true;
397400 }
398401 }
407410 // ensure compatibility with other archivers we need to put the symbol
408411 // table first in the file. Unfortunately, this means mapping the file
409412 // we just wrote back in and copying it to the destination file.
410 SmallString<128> TempArchiveWithSymbolTablePath;
413 sys::Path FinalFilePath = archPath;
411414
412415 // Map in the archive we just wrote.
413416 {
414417 OwningPtr arch;
415 if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) {
418 if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
416419 if (ErrMsg)
417420 *ErrMsg = ec.message();
418421 return true;
421424
422425 // Open another temporary file in order to avoid invalidating the
423426 // mmapped data
424 if (error_code ec =
425 sys::fs::unique_file("%%-%%-%%-%%-" + sys::path::filename(archPath.str()),
426 ArchFD, TempArchiveWithSymbolTablePath)) {
427 if (ErrMsg) *ErrMsg = ec.message();
428 return true;
429 }
430 sys::RemoveFileOnSignal(sys::Path(TempArchiveWithSymbolTablePath.str()));
431
432 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 }
433438
434439 // Write the file magic number
435440 FinalFile << ARFILE_MAGIC;
442447 if (foreignST) {
443448 if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
444449 FinalFile.close();
445 bool existed;
446 sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
450 TmpArchive.eraseFromDisk();
447451 return true;
448452 }
449453 }
461465 } // free arch.
462466
463467 // Move the final file over top of TmpArchive
464 if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(),
465 TempArchivePath.str())) {
466 if (ErrMsg) *ErrMsg = ec.message();
467 return true;
468 }
468 if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
469 return true;
469470 }
470471
471472 // Before we replace the actual archive, we need to forget all the
473474 // this because we cannot replace an open file on Windows.
474475 cleanUpMemory();
475476
476 if (error_code ec = sys::fs::rename(TempArchivePath.str(),
477 archPath.str())) {
478 if (ErrMsg) *ErrMsg = ec.message();
479 return true;
480 }
477 if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
478 return true;
481479
482480 // Set correct read and write permissions after temporary file is moved
483481 // to final destination path.