llvm.org GIT mirror llvm / 0c33231
Modify Path::eraseFromDisk to not throw an exception. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29400 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 14 years ago
6 changed file(s) with 58 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
4242 : Filename(filename), DeleteIt(deleteIt) {}
4343
4444 ~FileRemover() {
45 if (DeleteIt)
46 try {
47 Filename.eraseFromDisk();
48 } catch (...) {} // Ignore problems deleting the file.
45 if (DeleteIt) {
46 // Ignore problems deleting the file.
47 Filename.eraseFromDisk();
48 }
4949 }
5050
5151 /// releaseFile - Take ownership of the file away from the FileRemover so it
521521 /// \p destroy_contents parameter is ignored.
522522 /// @param destroy_contents Indicates whether the contents of a destroyed
523523 /// directory should also be destroyed (recursively).
524 /// @returns true if the file/directory was destroyed, false if the path
525 /// refers to something that is neither a file nor a directory.
526 /// @throws std::string if there is an error.
524 /// @returns false if the file/directory was destroyed, true on error.
527525 /// @brief Removes the file or directory from the filesystem.
528 bool eraseFromDisk(bool destroy_contents = false) const;
529
526 bool eraseFromDisk(bool destroy_contents = false,
527 std::string *Err = 0) const;
530528 /// @}
531529 /// @name Data
532530 /// @{
533531 private:
534 mutable std::string path; ///< Storage for the path name.
532 mutable std::string path; ///< Storage for the path name.
535533
536534 /// @}
537535 };
395395 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
396396
397397 // Check for errors opening or creating archive file.
398 if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
398 if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
399399 if (TmpArchive.exists())
400400 TmpArchive.eraseFromDisk();
401401 if (error)
414414
415415 // Loop over all member files, and write them out. Note that this also
416416 // builds the symbol table, symTab.
417 for ( MembersList::iterator I = begin(), E = end(); I != E; ++I) {
418 if (!writeMember(*I,ArchiveFile,CreateSymbolTable,
419 TruncateNames,Compress,error))
420 {
417 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
418 if (!writeMember(*I, ArchiveFile, CreateSymbolTable,
419 TruncateNames, Compress, error)) {
421420 if (TmpArchive.exists())
422421 TmpArchive.eraseFromDisk();
423422 ArchiveFile.close();
447446 sys::RemoveFileOnSignal(FinalFilePath);
448447
449448 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
450 if ( !FinalFile.is_open() || FinalFile.bad() ) {
449 if (!FinalFile.is_open() || FinalFile.bad()) {
451450 if (TmpArchive.exists())
452451 TmpArchive.eraseFromDisk();
453452 if (error)
395395 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
396396
397397 // Check for errors opening or creating archive file.
398 if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
398 if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
399399 if (TmpArchive.exists())
400400 TmpArchive.eraseFromDisk();
401401 if (error)
414414
415415 // Loop over all member files, and write them out. Note that this also
416416 // builds the symbol table, symTab.
417 for ( MembersList::iterator I = begin(), E = end(); I != E; ++I) {
418 if (!writeMember(*I,ArchiveFile,CreateSymbolTable,
419 TruncateNames,Compress,error))
420 {
417 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
418 if (!writeMember(*I, ArchiveFile, CreateSymbolTable,
419 TruncateNames, Compress, error)) {
421420 if (TmpArchive.exists())
422421 TmpArchive.eraseFromDisk();
423422 ArchiveFile.close();
447446 sys::RemoveFileOnSignal(FinalFilePath);
448447
449448 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
450 if ( !FinalFile.is_open() || FinalFile.bad() ) {
449 if (!FinalFile.is_open() || FinalFile.bad()) {
451450 if (TmpArchive.exists())
452451 TmpArchive.eraseFromDisk();
453452 if (error)
603603 }
604604
605605 bool
606 Path::eraseFromDisk(bool remove_contents) const {
607 // Make sure we're dealing with a directory
606 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
607 // Make sure we're dealing with a directory.
608608 if (isFile()) {
609 if (0 != unlink(path.c_str()))
610 ThrowErrno(path + ": can't destroy file");
611 } else if (isDirectory()) {
612 if (remove_contents) {
613 // Recursively descend the directory to remove its content
614 std::string cmd("/bin/rm -rf ");
615 cmd += path;
616 system(cmd.c_str());
617 } else {
618 // Otherwise, try to just remove the one directory
619 char pathname[MAXPATHLEN];
620 path.copy(pathname,MAXPATHLEN);
621 int lastchar = path.length() - 1 ;
622 if (pathname[lastchar] == '/')
623 pathname[lastchar] = 0;
624 else
625 pathname[lastchar+1] = 0;
626 if ( 0 != rmdir(pathname))
627 ThrowErrno(std::string(pathname) + ": can't destroy directory");
628 }
629 }
609 if (unlink(path.c_str()) != 0)
610 return GetErrno(path + ": can't destroy file", ErrStr);
611 return false;
612 }
613
614 if (!isDirectory()) {
615 if (ErrStr) *ErrStr = "not a file or directory";
616 return true;
617 }
618 if (remove_contents) {
619 // Recursively descend the directory to remove its contents.
620 std::string cmd = "/bin/rm -rf " + path;
621 system(cmd.c_str());
622 return false;
623 }
624
625 // Otherwise, try to just remove the one directory.
626 char pathname[MAXPATHLEN];
627 path.copy(pathname, MAXPATHLEN);
628 int lastchar = path.length() - 1 ;
629 if (pathname[lastchar] == '/')
630 pathname[lastchar] = 0;
630631 else
631 return false;
632 return true;
632 pathname[lastchar+1] = 0;
633
634 if (rmdir(pathname) != 0)
635 return GetErrno(std::string(pathname) + ": can't destroy directory",
636 ErrStr);
637 return false;
633638 }
634639
635640 bool
570570 }
571571
572572 bool
573 Path::eraseFromDisk(bool remove_contents) const {
573 Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
574574 if (isFile()) {
575575 DWORD attr = GetFileAttributes(path.c_str());
576576
577577 // If it doesn't exist, we're done.
578578 if (attr == INVALID_FILE_ATTRIBUTES)
579 return true;
579 return false;
580580
581581 // Read-only files cannot be deleted on Windows. Must remove the read-only
582582 // attribute first.
583583 if (attr & FILE_ATTRIBUTE_READONLY) {
584584 if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
585 ThrowError(path + ": Can't destroy file: ");
585 return GetError(path + ": Can't destroy file: ", ErrStr);
586586 }
587587
588588 if (!DeleteFile(path.c_str()))
591591 } else if (isDirectory()) {
592592 // If it doesn't exist, we're done.
593593 if (!exists())
594 return true;
594 return false;
595595
596596 char *pathname = reinterpret_cast(_alloca(path.length()+3));
597597 int lastchar = path.length() - 1 ;
628628 FindClose(h);
629629 if (err != ERROR_NO_MORE_FILES) {
630630 SetLastError(err);
631 ThrowError(path + ": Can't read directory: ");
631 return GetError(path + ": Can't read directory: ", ErrStr);
632632 }
633633
634634 for (std::vector::iterator I = list.begin(); I != list.end();
638638 }
639639 } else {
640640 if (GetLastError() != ERROR_FILE_NOT_FOUND)
641 ThrowError(path + ": Can't read directory: ");
641 return GetError(path + ": Can't read directory: ", ErrStr);
642642 }
643643 }
644644
645645 pathname[lastchar] = 0;
646646 if (!RemoveDirectory(pathname))
647 ThrowError(std::string(pathname) + ": Can't destroy directory: ");
648 return true;
647 return GetError(std::string(pathname) + ": Can't destroy directory: ",
648 ErrStr);
649 return false;
649650 } else {
650651 // It appears the path doesn't exist.
651 return false;
652 return true;
652653 }
653654 }
654655
788789
789790 }
790791 }
791
792