Changeset 1428


Ignore:
Timestamp:
Dec 17, 2011, 1:37:06 AM (9 years ago)
Author:
Peter Johansson
Message:

simplify rmdirhier using new DirectoryUtil?

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/rmdirhier.cc

    r1423 r1428  
    2323
    2424#include "rmdirhier.h"
     25#include "DirectoryUtil.h"
    2526#include "utility.h"
     27
     28#include "yat/Exception.h"
    2629
    2730#include <cstdio>
     
    3841namespace svndigest {
    3942
    40   void rmdirhier(const std::string& path)
    41   {
    42     int fd=open(".",O_RDONLY);  // remember "original" cwd
    43     void rmdirhier__(const std::string&);
    44 
    45     try {
    46       rmdirhier__(path);
    47     }
    48     catch(const BadDirectory& x) {
    49       std::cerr << "svndigest: " << x.what() << "\n"
    50                 << strerror(errno) << std::endl;
    51     }
    52     catch(const DirectoryOpenError& x) {
    53       std::cerr << "svndigest: cannot open directory " << x.what() << "\n"
    54                 << strerror(errno) << "\n";
    55     }
    56     catch(const FileDeleteError& x) {
    57       std::cerr << "svndigest: cannot delete file " << x.what() << "\n"
    58                 << strerror(errno) << "\n";
    59     }
    60     catch(const DirectoryDeleteError& x) {
    61       std::cerr << "svndigest: cannot delete directory " << x.what() << "\n"
    62                 << strerror(errno) << "\n";
    63     }
    64 
    65     fchdir(fd); // return to "original" cwd
    66   }
    67 
    68 
    69   void rmdirhier__(const std::string& dir)
     43  void rmdirhier(const std::string& dir)
    7044  {
    7145    struct stat buf;
     
    7650      // Make sure file is removable before removing it
    7751      chmod(dir.c_str(),S_IWRITE);
    78       if (::remove(dir.c_str()))
    79         throw FileDeleteError(concatenate_path(pwd(),dir));
     52      remove(dir);
    8053      return;
    8154    }
    82     if (::chdir(dir.c_str()))
    83       throw BadDirectory(concatenate_path(pwd(),dir));
    8455
    8556   // Delete any remaining directory entries
    86     DIR* dp;
    87     struct dirent *entry;
    88     if (!(dp=opendir(".")))
    89       throw DirectoryOpenError(concatenate_path(pwd(),dir));
    90     while ((entry=readdir(dp)) != NULL) {
    91       if ((std::string(entry->d_name) == ".") ||
    92           (std::string(entry->d_name) == ".."))
     57    DirectoryUtil du(dir);
     58    for (DirectoryUtil::const_iterator node=du.begin(); node!=du.end();++node){
     59      if ((file_name(node->path())==".") || (file_name(node->path())==".."))
    9360        continue;
    94       rmdirhier__(entry->d_name);
     61      rmdirhier(node->path());
    9562    }
    96     closedir(dp);
    9763
    9864    // Remove the directory from its parent
    99     chdir("..");
    100     if (::remove(dir.c_str()))
    101       throw DirectoryDeleteError(concatenate_path(pwd(),dir));
     65    remove(dir);
    10266  }
    10367
  • trunk/lib/rmdirhier.h

    r978 r1428  
    2424*/
    2525
    26 #include <stdexcept>
    2726#include <string>
    2827
    2928namespace theplu {
    3029namespace svndigest {
    31 
    32   struct DirectoryError : public std::runtime_error
    33   { inline DirectoryError(const std::string& s) : runtime_error(s) {} };
    34 
    35   struct BadDirectory : public DirectoryError
    36   { inline BadDirectory(const std::string& s) : DirectoryError(s) {} };
    37 
    38   struct DirectoryOpenError : public DirectoryError
    39   { inline DirectoryOpenError(const std::string& s) : DirectoryError(s) {} };
    40 
    41   struct FileDeleteError : public DirectoryError
    42   { FileDeleteError(const std::string& s) : DirectoryError(s) {} };
    43 
    44   struct DirectoryDeleteError : public DirectoryError
    45   { DirectoryDeleteError(const std::string& s) : DirectoryError(s) {} };
    4630
    4731  void rmdirhier(const std::string& path);
Note: See TracChangeset for help on using the changeset viewer.