source: trunk/lib/rmdirhier.cc @ 815

Last change on this file since 815 was 810, checked in by Peter Johansson, 12 years ago

fixes #389

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: rmdirhier.cc 810 2009-08-06 03:49:16Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen
5  Copyright (C) 2007, 2008 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "rmdirhier.h"
24#include "utility.h"
25
26#include <dirent.h>
27#include <cerrno>
28#include <fcntl.h>
29#include <iostream>
30#include <sys/stat.h>
31#include <unistd.h>
32
33
34namespace theplu {
35namespace svndigest {
36
37  void rmdirhier(const std::string& path)
38  {
39    int fd=open(".",O_RDONLY);  // remember "original" cwd
40    void rmdirhier__(const std::string&);
41
42    try {
43      rmdirhier__(path);
44    }
45    catch(const BadDirectory& x) {
46      std::cerr << "svndigest: " << x.what() << "\n" 
47                << strerror(errno) << std::endl;
48    }
49    catch(const DirectoryOpenError& x) {
50      std::cerr << "svndigest: cannot open directory " << x.what() << "\n" 
51                << strerror(errno) << "\n";
52    }
53    catch(const FileDeleteError& x) {
54      std::cerr << "svndigest: cannot delete file " << x.what() << "\n"
55                << strerror(errno) << "\n";
56    }
57    catch(const DirectoryDeleteError& x) {
58      std::cerr << "svndigest: cannot delete directory " << x.what() << "\n"
59                << strerror(errno) << "\n";
60    }
61
62    fchdir(fd); // return to "original" cwd
63  }
64
65
66  void rmdirhier__(const std::string& dir)
67  {
68    struct stat buf;
69    // using lstat because links should not be treated as dirs
70    lstat(dir.c_str(),&buf);
71    // check if dir - if not delete the node
72    if (!(buf.st_mode & S_IFDIR)) {
73      // Make sure file is removable before removing it
74      chmod(dir.c_str(),S_IWRITE);
75      if (unlink(dir.c_str()))
76        throw FileDeleteError(concatenate_path(pwd(),dir));
77      return;
78    }
79    if (chdir(dir.c_str()))
80      throw BadDirectory(concatenate_path(pwd(),dir));
81
82   // Delete any remaining directory entries
83    DIR* dp;
84    struct dirent *entry;
85    if (!(dp=opendir(".")))
86      throw DirectoryOpenError(concatenate_path(pwd(),dir));
87    while ((entry=readdir(dp)) != NULL) {
88      if ((std::string(entry->d_name) == ".") ||
89          (std::string(entry->d_name) == ".."))
90        continue;
91      rmdirhier__(entry->d_name);
92    }
93    closedir(dp);
94
95    // Remove the directory from its parent
96    chdir("..");
97    if (rmdir(dir.c_str()))
98      throw DirectoryDeleteError(concatenate_path(pwd(),dir));
99  }
100
101}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.