source: trunk/lib/rmdirhier.cc @ 809

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

fixes #391

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: rmdirhier.cc 809 2009-08-06 01:24:17Z 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 <fcntl.h>
28#include <iostream>
29#include <sys/stat.h>
30#include <unistd.h>
31
32
33namespace theplu {
34namespace 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(concatenate_path(pwd(),dir));
75      return;
76    }
77    if (chdir(dir.c_str()))
78      throw BadDirectory(concatenate_path(pwd(),dir));
79
80   // Delete any remaining directory entries
81    DIR* dp;
82    struct dirent *entry;
83    if (!(dp=opendir(".")))
84      throw DirectoryOpenError(concatenate_path(pwd(),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(concatenate_path(pwd(),dir));
97  }
98
99}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.