1 | // $Id: rmdirhier.cc 84 2006-03-13 22:04:34Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen |
---|
5 | |
---|
6 | This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat |
---|
7 | |
---|
8 | svnstat 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 | svnstat 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 svnstat { |
---|
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 | if (chdir(dir.c_str())) |
---|
67 | throw BadDirectory(dir); |
---|
68 | |
---|
69 | // Delete any remaining directory entries |
---|
70 | DIR* dp; |
---|
71 | struct dirent *entry; |
---|
72 | if (!(dp=opendir("."))) |
---|
73 | throw DirectoryOpenError(dir); |
---|
74 | while ((entry=readdir(dp)) != NULL) { |
---|
75 | struct stat buf; |
---|
76 | if ((std::string(entry->d_name) == ".") || |
---|
77 | (std::string(entry->d_name) == "..")) |
---|
78 | 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 | } |
---|
88 | } |
---|
89 | closedir(dp); |
---|
90 | |
---|
91 | // Remove the directory from its parent |
---|
92 | chdir(".."); |
---|
93 | if (rmdir(dir.c_str())) |
---|
94 | throw DirectoryDeleteError(dir); |
---|
95 | } |
---|
96 | |
---|
97 | }} // of namespace svnstat and namespace theplu |
---|