Created class to simplify temp directory management
This commit is contained in:
+1
-1
@@ -90,7 +90,7 @@ trsmallut.o : smallut.cpp smallut.h
|
||||
|
||||
WIPEDIR_OBJS= trwipedir.o $(BIGLIB)
|
||||
wipedir : $(WIPEDIR_OBJS)
|
||||
$(CXX) $(ALL_CXXFLAGS) -o wipedir $(WIPEDIR_OBJS) $(LIBICONV)
|
||||
$(CXX) $(ALL_CXXFLAGS) -o wipedir $(WIPEDIR_OBJS) -L/opt/local/lib -liconv
|
||||
trwipedir.o : wipedir.cpp
|
||||
$(CXX) $(ALL_CXXFLAGS) -DTEST_WIPEDIR -c -o trwipedir.o \
|
||||
wipedir.cpp
|
||||
|
||||
+44
-17
@@ -26,22 +26,6 @@ static char rcsid[] = "@(#$Id: pathut.cpp,v 1.23 2008-11-24 15:47:40 dockes Exp
|
||||
#include <sys/param.h>
|
||||
#include <pwd.h>
|
||||
#include <math.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <stack>
|
||||
#ifndef NO_NAMESPACES
|
||||
using std::string;
|
||||
using std::list;
|
||||
using std::stack;
|
||||
#endif /* NO_NAMESPACES */
|
||||
|
||||
#include "autoconfig.h"
|
||||
#include "pathut.h"
|
||||
#include "transcode.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
// Let's include all files where statfs can be defined and hope for no
|
||||
// conflict...
|
||||
@@ -58,6 +42,21 @@ using std::stack;
|
||||
#include <sys/vfs.h>
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <stack>
|
||||
#ifndef NO_NAMESPACES
|
||||
using std::string;
|
||||
using std::list;
|
||||
using std::stack;
|
||||
#endif /* NO_NAMESPACES */
|
||||
|
||||
#include "pathut.h"
|
||||
#include "transcode.h"
|
||||
#include "wipedir.h"
|
||||
|
||||
bool fsocc(const string &path, int *pc, long *blocks)
|
||||
{
|
||||
#ifdef sun
|
||||
@@ -155,7 +154,7 @@ TempFileInternal::TempFileInternal(const string& suffix)
|
||||
int fd;
|
||||
if ((fd = mkstemp(cp)) < 0) {
|
||||
free(cp);
|
||||
m_reason = "maketmpdir: mkstemp failed\n";
|
||||
m_reason = "TempFileInternal: mkstemp failed\n";
|
||||
return;
|
||||
}
|
||||
close(fd);
|
||||
@@ -177,6 +176,34 @@ TempFileInternal::~TempFileInternal()
|
||||
unlink(m_filename.c_str());
|
||||
}
|
||||
|
||||
TempDir::TempDir()
|
||||
{
|
||||
if (!maketmpdir(m_dirname, m_reason)) {
|
||||
m_dirname.erase();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
TempDir::~TempDir()
|
||||
{
|
||||
if (!m_dirname.empty()) {
|
||||
(void)wipedir(m_dirname, true, true);
|
||||
m_dirname.erase();
|
||||
}
|
||||
}
|
||||
|
||||
bool TempDir::wipe()
|
||||
{
|
||||
if (m_dirname.empty()) {
|
||||
m_reason = "TempDir::wipe: no directory !\n";
|
||||
return false;
|
||||
}
|
||||
if (wipedir(m_dirname, false, true)) {
|
||||
m_reason = "TempDir::wipe: wipedir failed\n";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void path_catslash(string &s) {
|
||||
if (s.empty() || s[s.length() - 1] != '/')
|
||||
|
||||
@@ -87,4 +87,18 @@ private:
|
||||
|
||||
typedef RefCntr<TempFileInternal> TempFile;
|
||||
|
||||
/// Temporary directory class
|
||||
class TempDir {
|
||||
public:
|
||||
TempDir();
|
||||
~TempDir();
|
||||
const char *dirname() {return m_dirname.c_str();}
|
||||
const string &getreason() {return m_reason;}
|
||||
bool ok() {return !m_dirname.empty();}
|
||||
bool wipe();
|
||||
private:
|
||||
string m_dirname;
|
||||
string m_reason;
|
||||
};
|
||||
|
||||
#endif /* _PATHUT_H_INCLUDED_ */
|
||||
|
||||
@@ -19,8 +19,7 @@ static char rcsid[] = "@(#$Id: transcode.cpp,v 1.12 2008-09-15 08:01:29 dockes E
|
||||
*/
|
||||
|
||||
#ifndef TEST_TRANSCODE
|
||||
|
||||
#include <errno.h>
|
||||
#include "autoconfig.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@@ -28,11 +27,11 @@ static char rcsid[] = "@(#$Id: transcode.cpp,v 1.12 2008-09-15 08:01:29 dockes E
|
||||
using std::string;
|
||||
#endif /* NO_NAMESPACES */
|
||||
|
||||
#include <errno.h>
|
||||
#include <iconv.h>
|
||||
|
||||
#include "transcode.h"
|
||||
#include "debuglog.h"
|
||||
#include "autoconfig.h"
|
||||
|
||||
#ifdef RCL_ICONV_INBUF_CONST
|
||||
#define ICV_P2_TYPE const char**
|
||||
|
||||
+58
-7
@@ -37,7 +37,7 @@ using namespace std;
|
||||
#include "pathut.h"
|
||||
#include "wipedir.h"
|
||||
|
||||
int wipedir(const string& dir)
|
||||
int wipedir(const string& dir, bool selfalso, bool recurse)
|
||||
{
|
||||
struct stat st;
|
||||
int statret;
|
||||
@@ -78,7 +78,15 @@ int wipedir(const string& dir)
|
||||
goto out;
|
||||
}
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
remaining++;
|
||||
if (recurse) {
|
||||
int rr = wipedir(fn, true, true);
|
||||
if (rr == -1)
|
||||
goto out;
|
||||
else
|
||||
remaining += rr;
|
||||
} else {
|
||||
remaining++;
|
||||
}
|
||||
} else {
|
||||
if (unlink(fn.c_str()) < 0) {
|
||||
LOGERR(("wipedir: cant unlink %s, errno %d\n",
|
||||
@@ -89,6 +97,14 @@ int wipedir(const string& dir)
|
||||
}
|
||||
|
||||
ret = remaining;
|
||||
if (selfalso && ret == 0) {
|
||||
if (rmdir(dir.c_str()) < 0) {
|
||||
LOGERR(("wipedir: rmdir(%s) failed, errno %d\n",
|
||||
dir.c_str(), errno));
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
if (d)
|
||||
closedir(d);
|
||||
@@ -103,15 +119,50 @@ int wipedir(const string& dir)
|
||||
#include "wipedir.h"
|
||||
|
||||
using namespace std;
|
||||
static const char *thisprog;
|
||||
|
||||
static int op_flags;
|
||||
#define OPT_MOINS 0x1
|
||||
#define OPT_r 0x2
|
||||
#define OPT_s 0x4
|
||||
static char usage [] =
|
||||
"wipedir [-r -s] topdir\n"
|
||||
" -r : recurse\n"
|
||||
" -s : also delete topdir\n"
|
||||
;
|
||||
static void
|
||||
Usage(void)
|
||||
{
|
||||
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
||||
exit(1);
|
||||
}
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: wipedir <dir>\n");
|
||||
exit(1);
|
||||
thisprog = argv[0];
|
||||
argc--; argv++;
|
||||
|
||||
while (argc > 0 && **argv == '-') {
|
||||
(*argv)++;
|
||||
if (!(**argv))
|
||||
/* Cas du "adb - core" */
|
||||
Usage();
|
||||
while (**argv)
|
||||
switch (*(*argv)++) {
|
||||
case 'r': op_flags |= OPT_r; break;
|
||||
case 's': op_flags |= OPT_s; break;
|
||||
default: Usage(); break;
|
||||
}
|
||||
b1: argc--; argv++;
|
||||
}
|
||||
string dir = argv[1];
|
||||
int cnt = wipedir(dir);
|
||||
|
||||
if (argc != 1)
|
||||
Usage();
|
||||
|
||||
string dir = *argv++;argc--;
|
||||
|
||||
bool topalso = ((op_flags&OPT_s) != 0);
|
||||
bool recurse = ((op_flags&OPT_r) != 0);
|
||||
int cnt = wipedir(dir, topalso, recurse);
|
||||
printf("wipedir returned %d\n", cnt);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
+2
-2
@@ -21,9 +21,9 @@
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Remove all files inside directory (not recursive).
|
||||
* Remove all files inside directory.
|
||||
* @return 0 if ok, count of remaining entries (ie: subdirs), or -1 for error
|
||||
*/
|
||||
int wipedir(const std::string& dirname);
|
||||
int wipedir(const std::string& dirname, bool topalso = 0, bool recurse = 0);
|
||||
|
||||
#endif /* _FILEUT_H_INCLUDED_ */
|
||||
|
||||
Reference in New Issue
Block a user