1 | // $Id: rmdirhier.cc 682 2008-08-02 20:19:43Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "rmdirhier.h" |
---|
25 | |
---|
26 | #include <dirent.h> |
---|
27 | #include <fcntl.h> |
---|
28 | #include <iostream> |
---|
29 | #include <sys/stat.h> |
---|
30 | #include <unistd.h> |
---|
31 | |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace svndigest { |
---|
35 | |
---|
36 | void rmdirhier(const std::string& path) |
---|
37 | { |
---|
38 | int fd=open(".",O_RDONLY); // remember "original" cwd |
---|
39 | void rmdirhier__(const std::string&); |
---|
40 | |
---|
41 | try { |
---|
42 | rmdirhier__(path); |
---|
43 | } |
---|
44 | catch(const BadDirectory& x) { |
---|
45 | std::cerr << "Invalid directory: " << x.what() << std::endl; |
---|
46 | } |
---|
47 | catch(const DirectoryOpenError& x) { |
---|
48 | std::cerr << "Error opening directory: " << x.what() << std::endl; |
---|
49 | } |
---|
50 | catch(const FileDeleteError& x) { |
---|
51 | std::cerr << "Error deleting file: " << x.what() << std::endl; |
---|
52 | } |
---|
53 | catch(const DirectoryDeleteError& x) { |
---|
54 | std::cerr << "Error deleting directory: " << x.what() << std::endl; |
---|
55 | } |
---|
56 | catch(const DirectoryError& x) { |
---|
57 | std::cerr << "Directory error:" << x.what() << std::endl; |
---|
58 | } |
---|
59 | |
---|
60 | fchdir(fd); // return to "original" cwd |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void rmdirhier__(const std::string& dir) |
---|
65 | { |
---|
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 | } |
---|
77 | if (chdir(dir.c_str())) |
---|
78 | throw BadDirectory(dir); |
---|
79 | |
---|
80 | // Delete any remaining directory entries |
---|
81 | DIR* dp; |
---|
82 | struct dirent *entry; |
---|
83 | if (!(dp=opendir("."))) |
---|
84 | throw DirectoryOpenError(dir); |
---|
85 | while ((entry=readdir(dp)) != NULL) { |
---|
86 | if ((std::string(entry->d_name) == ".") || |
---|
87 | (std::string(entry->d_name) == "..")) |
---|
88 | continue; |
---|
89 | rmdirhier__(entry->d_name); |
---|
90 | } |
---|
91 | closedir(dp); |
---|
92 | |
---|
93 | // Remove the directory from its parent |
---|
94 | chdir(".."); |
---|
95 | if (rmdir(dir.c_str())) |
---|
96 | throw DirectoryDeleteError(dir); |
---|
97 | } |
---|
98 | |
---|
99 | }} // of namespace svndigest and namespace theplu |
---|