Changeset 680 for trunk/lib


Ignore:
Timestamp:
Aug 2, 2008, 10:07:13 PM (13 years ago)
Author:
Peter Johansson
Message:

refs #342 and #343. I think this fixes the issue but need to test it (and perhaps add tests) to be sure. I changed rmdirhier to also work on non-dirs. The name, rmdirhier, is perhaps not so descriptive anymore but I prefered changing that function rather than duplicating imilar code elsewhere. I also changed the behavior of rmdirhier in that links are no longer followed but only removed, i.e., a symlink pointing to a dir is only deleted (without deleting the daughter nodes).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/rmdirhier.cc

    r519 r680  
    6464  void rmdirhier__(const std::string& dir)
    6565  {
     66    struct stat buf;
     67    // using lstat because links should not be treated as dirs
     68    lstat(dir.c_str(),&buf);
     69    // check if dir - if not delete the node
     70    if (!(buf.st_mode & S_IFDIR)) {
     71      // Make sure file is removable before removing it
     72      chmod(dir.c_str(),S_IWRITE);
     73      if (unlink(dir.c_str()))
     74        throw FileDeleteError(dir);
     75      return;
     76    }
    6677    if (chdir(dir.c_str()))
    6778      throw BadDirectory(dir);
     
    7384      throw DirectoryOpenError(dir);
    7485    while ((entry=readdir(dp)) != NULL) {
    75       struct stat buf;
    7686      if ((std::string(entry->d_name) == ".") ||
    7787          (std::string(entry->d_name) == ".."))
    7888        continue;
    79       stat(entry->d_name,&buf);
    80       if (buf.st_mode & S_IFDIR)
    81         rmdirhier__(entry->d_name);      // sub-directory
    82       else {
    83         // Make sure file is removable before removing it
    84         chmod(entry->d_name,S_IWRITE);
    85         if (unlink(entry->d_name))
    86           throw FileDeleteError(entry->d_name);
    87       }
     89      rmdirhier__(entry->d_name);
    8890    }
    8991    closedir(dp);
Note: See TracChangeset for help on using the changeset viewer.