source: trunk/lib/rmdirhier.cc @ 834

Last change on this file since 834 was 823, checked in by Jari Häkkinen, 12 years ago

Added includes needed on SuSE Linux.

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