From c9b121803c3ad85c2dbb160f67a630e262bcedf5 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 16 Jul 2010 15:45:24 +0000 Subject: [PATCH] add new files git-svn-id: svn://localhost/ardour2/branches/3.0@7427 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/pbd/clear_dir.cc | 70 ++++++++++++++++++++++++++++++++++++++++ libs/pbd/pbd/clear_dir.h | 12 +++++++ 2 files changed, 82 insertions(+) create mode 100644 libs/pbd/clear_dir.cc create mode 100644 libs/pbd/pbd/clear_dir.h diff --git a/libs/pbd/clear_dir.cc b/libs/pbd/clear_dir.cc new file mode 100644 index 0000000000..1966605af6 --- /dev/null +++ b/libs/pbd/clear_dir.cc @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include + +#include "pbd/error.h" +#include "pbd/compose.h" +#include "pbd/clear_dir.h" + +#include "i18n.h" + +using namespace PBD; +using namespace std; + +int +PBD::clear_directory (const string& dir, size_t* size, vector* paths) +{ + struct dirent* dentry; + struct stat statbuf; + DIR* dead; + int ret = 0; + + if ((dead = ::opendir (dir.c_str())) == 0) { + return -1; + } + + while ((dentry = ::readdir (dead)) != 0) { + + /* avoid '.' and '..' */ + + if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') || + (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) { + continue; + } + + string fullpath; + + fullpath = dir; + fullpath += '/'; + fullpath += dentry->d_name; + + if (::stat (fullpath.c_str(), &statbuf)) { + continue; + } + + if (!S_ISREG (statbuf.st_mode)) { + continue; + } + + if (::unlink (fullpath.c_str())) { + error << string_compose (_("cannot remove file %1 (%2)"), fullpath, strerror (errno)) + << endmsg; + ret = 1; + } + + if (paths) { + paths->push_back (dentry->d_name); + } + + if (size) { + *size += statbuf.st_size; + } + } + + ::closedir (dead); + + return ret; +} diff --git a/libs/pbd/pbd/clear_dir.h b/libs/pbd/pbd/clear_dir.h new file mode 100644 index 0000000000..690a48d9c8 --- /dev/null +++ b/libs/pbd/pbd/clear_dir.h @@ -0,0 +1,12 @@ +#ifndef __pbd_clear_dir_h__ +#define __pbd_clear_dir_h__ + +#include +#include +#include + +namespace PBD { + int clear_directory (const std::string&, size_t* = 0, std::vector* = 0); +} + +#endif /* __pbd_clear_dir_h__ */