source: trunk/lib/rmdirhier.cc @ 745

Last change on this file since 745 was 693, checked in by Jari Häkkinen, 13 years ago

Fixes #339. Change to GPLv3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: rmdirhier.cc 693 2008-09-11 20:42:56Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 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
25#include <dirent.h>
26#include <fcntl.h>
27#include <iostream>
28#include <sys/stat.h>
29#include <unistd.h>
30
31
32namespace theplu {
33namespace svndigest {
34
35  void rmdirhier(const std::string& path)
36  {
37    int fd=open(".",O_RDONLY);  // remember "original" cwd
38    void rmdirhier__(const std::string&);
39
40    try {
41      rmdirhier__(path);
42    }
43    catch(const BadDirectory& x) {
44      std::cerr << "Invalid directory: " << x.what() << std::endl;
45    }
46    catch(const DirectoryOpenError& x) {
47      std::cerr << "Error opening directory: " << x.what() << std::endl;
48    }
49    catch(const FileDeleteError& x) {
50      std::cerr << "Error deleting file: " << x.what() << std::endl;
51    }
52    catch(const DirectoryDeleteError& x) {
53      std::cerr << "Error deleting directory: " << x.what() << std::endl;
54    }
55    catch(const DirectoryError& x) {
56      std::cerr << "Directory error:" << x.what() << std::endl;
57    }
58
59    fchdir(fd); // return to "original" cwd
60  }
61
62
63  void rmdirhier__(const std::string& dir)
64  {
65    struct stat buf;
66    // using lstat because links should not be treated as dirs
67    lstat(dir.c_str(),&buf);
68    // check if dir - if not delete the node
69    if (!(buf.st_mode & S_IFDIR)) {
70      // Make sure file is removable before removing it
71      chmod(dir.c_str(),S_IWRITE);
72      if (unlink(dir.c_str()))
73        throw FileDeleteError(dir);
74      return;
75    }
76    if (chdir(dir.c_str()))
77      throw BadDirectory(dir);
78
79   // Delete any remaining directory entries
80    DIR* dp;
81    struct dirent *entry;
82    if (!(dp=opendir(".")))
83      throw DirectoryOpenError(dir);
84    while ((entry=readdir(dp)) != NULL) {
85      if ((std::string(entry->d_name) == ".") ||
86          (std::string(entry->d_name) == ".."))
87        continue;
88      rmdirhier__(entry->d_name);
89    }
90    closedir(dp);
91
92    // Remove the directory from its parent
93    chdir("..");
94    if (rmdir(dir.c_str()))
95      throw DirectoryDeleteError(dir);
96  }
97
98}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.