source: branches/0.6-stable/lib/rmdirhier.cc

Last change on this file was 731, checked in by Peter Johansson, 13 years ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: rmdirhier.cc 731 2008-12-15 19:03:04Z 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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "rmdirhier.h"
26
27#include <dirent.h>
28#include <fcntl.h>
29#include <iostream>
30#include <sys/stat.h>
31#include <unistd.h>
32
33
34namespace theplu {
35namespace svndigest {
36
37  void rmdirhier(const std::string& path)
38  {
39    int fd=open(".",O_RDONLY);  // remember "original" cwd
40    void rmdirhier__(const std::string&);
41
42    try {
43      rmdirhier__(path);
44    }
45    catch(const BadDirectory& x) {
46      std::cerr << "Invalid directory: " << x.what() << std::endl;
47    }
48    catch(const DirectoryOpenError& x) {
49      std::cerr << "Error opening directory: " << x.what() << std::endl;
50    }
51    catch(const FileDeleteError& x) {
52      std::cerr << "Error deleting file: " << x.what() << std::endl;
53    }
54    catch(const DirectoryDeleteError& x) {
55      std::cerr << "Error deleting directory: " << x.what() << std::endl;
56    }
57    catch(const DirectoryError& x) {
58      std::cerr << "Directory error:" << x.what() << std::endl;
59    }
60
61    fchdir(fd); // return to "original" cwd
62  }
63
64
65  void rmdirhier__(const std::string& dir)
66  {
67    struct stat buf;
68    // using lstat because links should not be treated as dirs
69    lstat(dir.c_str(),&buf);
70    // check if dir - if not delete the node
71    if (!(buf.st_mode & S_IFDIR)) {
72      // Make sure file is removable before removing it
73      chmod(dir.c_str(),S_IWRITE);
74      if (unlink(dir.c_str()))
75        throw FileDeleteError(dir);
76      return;
77    }
78    if (chdir(dir.c_str()))
79      throw BadDirectory(dir);
80
81   // Delete any remaining directory entries
82    DIR* dp;
83    struct dirent *entry;
84    if (!(dp=opendir(".")))
85      throw DirectoryOpenError(dir);
86    while ((entry=readdir(dp)) != NULL) {
87      if ((std::string(entry->d_name) == ".") ||
88          (std::string(entry->d_name) == ".."))
89        continue;
90      rmdirhier__(entry->d_name);
91    }
92    closedir(dp);
93
94    // Remove the directory from its parent
95    chdir("..");
96    if (rmdir(dir.c_str()))
97      throw DirectoryDeleteError(dir);
98  }
99
100}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.