lib/rmdirhier.cc

Code
Comments
Other
Rev Date Author Line
49 14 Jan 06 jari 1 // $Id$
49 14 Jan 06 jari 2
84 13 Mar 06 jari 3 /*
978 12 Dec 09 peter 4   Copyright (C) 2006 Jari Häkkinen
1635 30 Mar 23 peter 5   Copyright (C) 2007 Peter Johansson
1635 30 Mar 23 peter 6   Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson
1635 30 Mar 23 peter 7   Copyright (C) 2010, 2011, 2023 Peter Johansson
84 13 Mar 06 jari 8
687 04 Aug 08 peter 9   This file is part of svndigest, http://dev.thep.lu.se/svndigest
84 13 Mar 06 jari 10
149 12 Aug 06 jari 11   svndigest is free software; you can redistribute it and/or modify it
84 13 Mar 06 jari 12   under the terms of the GNU General Public License as published by
693 11 Sep 08 jari 13   the Free Software Foundation; either version 3 of the License, or
84 13 Mar 06 jari 14   (at your option) any later version.
84 13 Mar 06 jari 15
149 12 Aug 06 jari 16   svndigest is distributed in the hope that it will be useful, but
84 13 Mar 06 jari 17   WITHOUT ANY WARRANTY; without even the implied warranty of
149 12 Aug 06 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84 13 Mar 06 jari 19   General Public License for more details.
84 13 Mar 06 jari 20
84 13 Mar 06 jari 21   You should have received a copy of the GNU General Public License
693 11 Sep 08 jari 22   along with svndigest. If not, see <http://www.gnu.org/licenses/>.
84 13 Mar 06 jari 23 */
84 13 Mar 06 jari 24
1619 12 Mar 23 peter 25 #include <config.h>
1619 12 Mar 23 peter 26
49 14 Jan 06 jari 27 #include "rmdirhier.h"
1428 17 Dec 11 peter 28 #include "DirectoryUtil.h"
809 06 Aug 09 peter 29 #include "utility.h"
49 14 Jan 06 jari 30
1673 26 Aug 23 peter 31 #include <yat/utility/Exception.h>
1675 26 Aug 23 peter 32 #include <yat/utility/utility.h>
1428 17 Dec 11 peter 33
1213 09 Oct 10 peter 34 #include <cstdio>
823 25 Sep 09 jari 35 #include <cstring>
49 14 Jan 06 jari 36 #include <dirent.h>
49 14 Jan 06 jari 37 #include <fcntl.h>
49 14 Jan 06 jari 38 #include <iostream>
49 14 Jan 06 jari 39 #include <sys/stat.h>
49 14 Jan 06 jari 40 #include <unistd.h>
49 14 Jan 06 jari 41
49 14 Jan 06 jari 42
49 14 Jan 06 jari 43 namespace theplu {
149 12 Aug 06 jari 44 namespace svndigest {
49 14 Jan 06 jari 45
1428 17 Dec 11 peter 46   void rmdirhier(const std::string& dir)
49 14 Jan 06 jari 47   {
687 04 Aug 08 peter 48     struct stat buf;
687 04 Aug 08 peter 49     // using lstat because links should not be treated as dirs
1186 27 Aug 10 peter 50     lstat(dir, &buf);
1445 20 Dec 11 peter 51     // check if dir
1445 20 Dec 11 peter 52     if ((buf.st_mode & S_IFDIR)) {
1445 20 Dec 11 peter 53       // Delete sub-nodes
1445 20 Dec 11 peter 54       DirectoryUtil du(dir);
1445 20 Dec 11 peter 55       for (DirectoryUtil::const_iterator node=du.begin();node!=du.end();++node){
1675 26 Aug 23 peter 56         if ((yat::utility::basename(node->path())!=".") &&
1675 26 Aug 23 peter 57             (yat::utility::basename(node->path())!=".."))
1445 20 Dec 11 peter 58           rmdirhier(node->path());
1445 20 Dec 11 peter 59       }
687 04 Aug 08 peter 60     }
1445 20 Dec 11 peter 61     // Make sure file is removable before removing it
1675 26 Aug 23 peter 62     yat::utility::chmod(dir,S_IWRITE);
1675 26 Aug 23 peter 63     yat::utility::remove(dir);
49 14 Jan 06 jari 64   }
49 14 Jan 06 jari 65
149 12 Aug 06 jari 66 }} // of namespace svndigest and namespace theplu