llvm.org GIT mirror llvm / e5c9cb5
For PR797: Remove exceptions from the Path::create*OnDisk methods. Update their users to handle error messages via arguments and result codes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29840 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 14 years ago
7 changed file(s) with 79 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
423423 /// This method attempts to make the file referenced by the Path object
424424 /// available for reading so that the canRead() method will return true.
425425 /// @brief Make the file readable;
426 bool makeReadableOnDisk(std::string* ErrMsg);
426 bool makeReadableOnDisk(std::string* ErrMsg = 0);
427427
428428 /// This method attempts to make the file referenced by the Path object
429429 /// available for writing so that the canWrite() method will return true.
430430 /// @brief Make the file writable;
431 bool makeWriteableOnDisk(std::string* ErrMsg);
431 bool makeWriteableOnDisk(std::string* ErrMsg = 0);
432432
433433 /// This method attempts to make the file referenced by the Path object
434434 /// available for execution so that the canExecute() method will return
435435 /// true.
436436 /// @brief Make the file readable;
437 bool makeExecutableOnDisk(std::string* ErrMsg);
437 bool makeExecutableOnDisk(std::string* ErrMsg = 0);
438438
439439 /// This method allows the last modified time stamp and permission bits
440440 /// to be set on the disk object referenced by the Path.
451451 /// intermediate directories, as needed. If \p create_parents is false,
452452 /// then only the final directory component of the Path name will be
453453 /// created. The created directory will have no entries.
454 /// @returns false if the Path does not reference a directory, true
455 /// otherwise.
456 /// @param create_parents Determines whether non-existent directory
457 /// components other than the last one (the "parents") are created or not.
458 /// @throws std::string if an error occurs.
454 /// @returns true if the directory could not be created, false otherwise
459455 /// @brief Create the directory this Path refers to.
460 bool createDirectoryOnDisk( bool create_parents = false );
456 bool createDirectoryOnDisk(
457 bool create_parents = false, ///< Determines whether non-existent
458 ///< directory components other than the last one (the "parents")
459 ///< are created or not.
460 std::string* ErrMsg = 0 ///< Optional place to put error messages.
461 );
461462
462463 /// This method attempts to create a file in the file system with the same
463464 /// name as the Path object. The intermediate directories must all exist
464465 /// at the time this method is called. Use createDirectoriesOnDisk to
465466 /// accomplish that. The created file will be empty upon return from this
466467 /// function.
467 /// @returns false if the Path does not reference a file, true otherwise.
468 /// @throws std::string if an error occurs.
468 /// @returns true if the file could not be created, false otherwise.
469469 /// @brief Create the file this Path refers to.
470 bool createFileOnDisk();
470 bool createFileOnDisk(
471 std::string* ErrMsg = 0 ///< Optional place to put error messages.
472 );
471473
472474 /// This is like createFile except that it creates a temporary file. A
473475 /// unique temporary file name is generated based on the contents of
475477 /// file is created. Note that this will both change the Path object
476478 /// *and* create the corresponding file. This function will ensure that
477479 /// the newly generated temporary file name is unique in the file system.
478 /// @param reuse_current When set to true, this parameter indicates that
479 /// if the current file name does not exist then it will be used without
480 /// modification.
481 /// @returns true if successful, false if the file couldn't be created.
482 /// @throws std::string if there is a hard error creating the temp file
483 /// name.
480 /// @returns true if the file couldn't be created, false otherwise.
484481 /// @brief Create a unique temporary file
485 bool createTemporaryFileOnDisk(bool reuse_current = false);
482 bool createTemporaryFileOnDisk(
483 bool reuse_current = false, ///< When set to true, this parameter
484 ///< indicates that if the current file name does not exist then
485 ///< it will be used without modification.
486 std::string* ErrMsg = 0 ///< Optional place to put error messages
487 );
486488
487489 /// This method renames the file referenced by \p this as \p newName. The
488490 /// file referenced by \p this must exist. The file referenced by
388388
389389 // Create a temporary file to store the archive in
390390 sys::Path TmpArchive = archPath;
391 TmpArchive.createTemporaryFileOnDisk();
391 if (TmpArchive.createTemporaryFileOnDisk(error))
392 return false;
392393
393394 // Make sure the temporary gets removed if we crash
394395 sys::RemoveFileOnSignal(TmpArchive);
451452 // Open another temporary file in order to avoid invalidating the
452453 // mmapped data
453454 sys::Path FinalFilePath = archPath;
454 FinalFilePath.createTemporaryFileOnDisk();
455 if (FinalFilePath.createTemporaryFileOnDisk(error))
456 return false;
455457 sys::RemoveFileOnSignal(FinalFilePath);
456458
457459 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
388388
389389 // Create a temporary file to store the archive in
390390 sys::Path TmpArchive = archPath;
391 TmpArchive.createTemporaryFileOnDisk();
391 if (TmpArchive.createTemporaryFileOnDisk(error))
392 return false;
392393
393394 // Make sure the temporary gets removed if we crash
394395 sys::RemoveFileOnSignal(TmpArchive);
451452 // Open another temporary file in order to avoid invalidating the
452453 // mmapped data
453454 sys::Path FinalFilePath = archPath;
454 FinalFilePath.createTemporaryFileOnDisk();
455 if (FinalFilePath.createTemporaryFileOnDisk(error))
456 return false;
455457 sys::RemoveFileOnSignal(FinalFilePath);
456458
457459 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
516516 }
517517
518518 bool
519 Path::createDirectoryOnDisk( bool create_parents) {
519 Path::createDirectoryOnDisk( bool create_parents, std::string* ErrMsg ) {
520520 // Get a writeable copy of the path name
521521 char pathname[MAXPATHLEN];
522522 path.copy(pathname,MAXPATHLEN);
539539 while ( next != 0 ) {
540540 *next = 0;
541541 if (0 != access(pathname, F_OK | R_OK | W_OK))
542 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
543 ThrowErrno(std::string(pathname) + ": can't create directory");
542 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
543 MakeErrMsg(ErrMsg,
544 std::string(pathname) + ": can't create directory");
545 return true;
546 }
544547 char* save = next;
545548 next = strchr(next+1,'/');
546549 *save = '/';
548551 }
549552
550553 if (0 != access(pathname, F_OK | R_OK))
551 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
552 ThrowErrno(std::string(pathname) + ": can't create directory");
553 return true;
554 }
555
556 bool
557 Path::createFileOnDisk() {
554 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) {
555 MakeErrMsg(ErrMsg, std::string(pathname) + ": can't create directory");
556 return true;
557 }
558 return false;
559 }
560
561 bool
562 Path::createFileOnDisk(std::string* ErrMsg) {
558563 // Create the file
559564 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
560 if (fd < 0)
561 ThrowErrno(path + ": can't create file");
565 if (fd < 0) {
566 MakeErrMsg(ErrMsg, path + ": can't create file");
567 return true;
568 }
562569 ::close(fd);
563
564 return true;
565 }
566
567 bool
568 Path::createTemporaryFileOnDisk(bool reuse_current) {
570 return false;
571 }
572
573 bool
574 Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
569575 // Make this into a unique file name
570576 makeUnique( reuse_current );
571577
572578 // create the file
573 int outFile = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
574 if (outFile != -1) {
575 ::close(outFile);
576 return true;
577 }
579 int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
580 if (fd < 0) {
581 MakeErrMsg(ErrMsg, path + ": can't create temporary file");
582 return true;
583 }
584 ::close(fd);
578585 return false;
579586 }
580587
424424 }
425425
426426 // doExtract - Implement the 'x' operation. This function extracts files back to
427 // the file system, making sure to uncompress any that were compressed.
428 void doExtract() {
427 // the file system, making sure to uncompress any that were compressed
428 bool doExtract(std::string* ErrMsg) {
429429 buildPaths(false);
430430 unsigned countDown = Count;
431431 for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end();
437437 if (I->hasPath()) {
438438 sys::Path dirs(I->getPath());
439439 dirs.eraseComponent();
440 dirs.createDirectoryOnDisk(/*create_parents=*/true);
440 if (dirs.createDirectoryOnDisk(/*create_parents=*/true, ErrMsg))
441 return true;
441442 }
442443
443444 // Open up a file stream for writing
463464 I->getPath().setStatusInfoOnDisk(I->getFileStatus());
464465 }
465466 }
467 return false;
466468 }
467469
468470 // doDelete - Implement the delete operation. This function deletes zero or more
710712 std::auto_ptr AutoArchive(TheArchive);
711713
712714 // Perform the operation
715 std::string ErrMsg;
713716 switch (Operation) {
714717 case Print: doPrint(); break;
715718 case Delete: doDelete(); break;
717720 case QuickAppend: /* FALL THROUGH */
718721 case ReplaceOrInsert: doReplaceOrInsert(); break;
719722 case DisplayTable: doDisplayTable(); break;
720 case Extract: doExtract(); break;
723 case Extract:
724 if (doExtract(&ErrMsg)) {
725 std::cerr << argv[0] << ": " << ErrMsg << "\n";
726 return 1;
727 }
728 break;
721729 case NoOperation:
722730 std::cerr << argv[0] << ": No operation was selected.\n";
723731 break;
491491 }
492492 // Get the program arguments
493493 sys::Path tmp_output("opt_result");
494 if (!tmp_output.createTemporaryFileOnDisk()) {
495 return PrintAndReturn(
496 "Can't create temporary file for post-link optimization");
494 std::string ErrMsg;
495 if (tmp_output.createTemporaryFileOnDisk(&ErrMsg)) {
496 return PrintAndReturn(ErrMsg);
497497 }
498498 const char* args[4];
499499 args[0] = I->c_str();
500500 args[1] = RealBytecodeOutput.c_str();
501501 args[2] = tmp_output.c_str();
502502 args[3] = 0;
503 std::string ErrMsg;
504503 if (0 == sys::Program::ExecuteAndWait(prog, args, 0,0,0, &ErrMsg)) {
505504 if (tmp_output.isBytecodeFile()) {
506505 sys::Path target(RealBytecodeOutput);
294294 }
295295
296296 sys::Path tmpAsmFilePath("/tmp/");
297 tmpAsmFilePath.createTemporaryFileOnDisk();
297 std::string ErrMsg;
298 if (tmpAsmFilePath.createTemporaryFileOnDisk(&ErrMsg)) {
299 std::cerr << "lto: " << ErrMsg << "\n";
300 return;
301 }
298302 sys::RemoveFileOnSignal(tmpAsmFilePath);
299303
300304 std::ofstream asmFile(tmpAsmFilePath.c_str(), io_mode);