1 | // $Id: rmdirhier.cc 1213 2010-10-09 14:47:10Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen |
---|
5 | Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2010 Peter Johansson |
---|
7 | |
---|
8 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
9 | |
---|
10 | svndigest is free software; you can redistribute it and/or modify it |
---|
11 | under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 3 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | svndigest is distributed in the hope that it will be useful, but |
---|
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "rmdirhier.h" |
---|
25 | #include "utility.h" |
---|
26 | |
---|
27 | #include <cstdio> |
---|
28 | #include <cstring> |
---|
29 | #include <dirent.h> |
---|
30 | #include <cerrno> |
---|
31 | #include <fcntl.h> |
---|
32 | #include <iostream> |
---|
33 | #include <sys/stat.h> |
---|
34 | #include <unistd.h> |
---|
35 | |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace svndigest { |
---|
39 | |
---|
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) |
---|
70 | { |
---|
71 | struct stat buf; |
---|
72 | // using lstat because links should not be treated as dirs |
---|
73 | lstat(dir, &buf); |
---|
74 | // check if dir - if not delete the node |
---|
75 | if (!(buf.st_mode & S_IFDIR)) { |
---|
76 | // Make sure file is removable before removing it |
---|
77 | chmod(dir.c_str(),S_IWRITE); |
---|
78 | if (remove(dir.c_str())) |
---|
79 | throw FileDeleteError(concatenate_path(pwd(),dir)); |
---|
80 | return; |
---|
81 | } |
---|
82 | if (::chdir(dir.c_str())) |
---|
83 | throw BadDirectory(concatenate_path(pwd(),dir)); |
---|
84 | |
---|
85 | // 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) == "..")) |
---|
93 | continue; |
---|
94 | rmdirhier__(entry->d_name); |
---|
95 | } |
---|
96 | closedir(dp); |
---|
97 | |
---|
98 | // Remove the directory from its parent |
---|
99 | chdir(".."); |
---|
100 | if (remove(dir.c_str())) |
---|
101 | throw DirectoryDeleteError(concatenate_path(pwd(),dir)); |
---|
102 | } |
---|
103 | |
---|
104 | }} // of namespace svndigest and namespace theplu |
---|