From 110c50944f08b0e52313c57d4f10a5f1d0923227 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Jan 2009 12:37:16 +0000 Subject: [PATCH] added wxPATH_RMDIR_FULL/RECURSIVE wxFileName::Rmdir() flags (closes #10422) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58385 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/filename.h | 18 +++++++--- interface/wx/filename.h | 41 ++++++++++++++++++++-- src/common/filename.cpp | 78 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 125 insertions(+), 12 deletions(-) diff --git a/include/wx/filename.h b/include/wx/filename.h index 515ff9be12..7a7593c6d6 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -87,12 +87,19 @@ enum wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator }; -// MkDir flags +// Mkdir flags enum { wxPATH_MKDIR_FULL = 0x0001 // create directories recursively }; +// Rmdir flags +enum +{ + wxPATH_RMDIR_FULL = 0x0001, // delete with subdirectories if empty + wxPATH_RMDIR_RECURSIVE = 0x0002 // delete all recursively (dangerous!) +}; + #if wxUSE_LONGLONG // error code of wxFileName::GetSize() extern WXDLLIMPEXP_DATA_BASE(const wxULongLong) wxInvalidSize; @@ -309,11 +316,12 @@ public: #endif // wxUSE_FFILE // directory creation and removal. - bool Mkdir( int perm = wxS_DIR_DEFAULT, int flags = 0); - static bool Mkdir( const wxString &dir, int perm = wxS_DIR_DEFAULT, int flags = 0 ); + bool Mkdir(int perm = wxS_DIR_DEFAULT, int flags = 0); + static bool Mkdir(const wxString &dir, int perm = wxS_DIR_DEFAULT, + int flags = 0); - bool Rmdir(); - static bool Rmdir( const wxString &dir ); + bool Rmdir(int flags = 0); + static bool Rmdir(const wxString &dir, int flags = 0); // operations on the path diff --git a/interface/wx/filename.h b/interface/wx/filename.h index 97fd9b6822..fcec353020 100644 --- a/interface/wx/filename.h +++ b/interface/wx/filename.h @@ -53,6 +53,24 @@ enum wxPathNormalize wxPATH_NORM_ALL = 0x00ff & ~wxPATH_NORM_CASE }; +/** + Flags for wxFileName::Rmdir(). + */ +enum +{ + /// Delete the specified directory and its subdirectories if they are empty. + wxPATH_RMDIR_FULL = 1, + + /** + Delete the specified directory and all the files and subdirectories in it + recursively. + + This flag is obviously @b dangerous and should be used with care and + after asking the user for confirmation. + */ + wxPATH_RMDIR_RECURSIVE = 2 +}; + /** The return value of wxFileName::GetSize() in case of error. */ @@ -982,13 +1000,31 @@ public: /** Deletes the specified directory from the file system. + + @param flags + Can contain one of wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE. By + default contains neither so the directory will not be removed + unless it is empty. + + @return Returns @true if the directory was successfully deleted, @false + otherwise. */ - bool Rmdir(); + bool Rmdir(int flags = 0); /** Deletes the specified directory from the file system. + + @param dir + The directory to delete + @param flags + Can contain one of wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE. By + default contains neither so the directory will not be removed + unless it is empty. + + @return Returns @true if the directory was successfully deleted, @false + otherwise. */ - static bool Rmdir(const wxString& dir); + static bool Rmdir(const wxString& dir, int flags = 0); /** Compares the filename using the rules of this platform. @@ -1140,4 +1176,3 @@ public: */ wxFileName& operator=(const wxString& filename); }; - diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 4e21ea1449..618fdac037 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -83,6 +83,7 @@ #include "wx/tokenzr.h" #include "wx/config.h" // for wxExpandEnvVars #include "wx/dynlib.h" +#include "wx/dir.h" #if defined(__WIN32__) && defined(__MINGW32__) #include "wx/msw/gccpriv.h" @@ -1123,14 +1124,83 @@ bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) return ::wxMkdir( dir, perm ); } -bool wxFileName::Rmdir() +bool wxFileName::Rmdir(int flags) { - return wxFileName::Rmdir( GetPath() ); + return wxFileName::Rmdir( GetPath(), flags ); } -bool wxFileName::Rmdir( const wxString &dir ) +bool wxFileName::Rmdir(const wxString& dir, int flags) { - return ::wxRmdir( dir ); +#ifdef __WXMSW__ + if ( flags & wxPATH_RMDIR_RECURSIVE ) + { + // SHFileOperation needs double null termination string + // but without separator at the end of the path + wxString path(dir); + if ( path.Last() == wxFILE_SEP_PATH ) + path.RemoveLast(); + path += _T('\0'); + + SHFILEOPSTRUCT fileop; + wxZeroMemory(fileop); + fileop.wFunc = FO_DELETE; + fileop.pFrom = path.fn_str(); + fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; + #ifndef __WXWINCE__ + // FOF_NOERRORUI is not defined in WinCE + fileop.fFlags |= FOF_NOERRORUI; + #endif + + int ret = SHFileOperation(&fileop); + if ( ret != 0 ) + { + // SHFileOperation may return non-Win32 error codes, so the error + // message can be incorrect + wxLogApiError(_T("SHFileOperation"), ret); + return false; + } + + return true; + } + else if ( flags & wxPATH_RMDIR_FULL ) +#else // !__WXMSW__ + if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE +#endif // !__WXMSW__ + { + wxString path(dir); + if ( path.Last() != wxFILE_SEP_PATH ) + path += wxFILE_SEP_PATH; + + wxDir d(path); + + if ( !d.IsOpened() ) + return false; + + wxString filename; + + // first delete all subdirectories + bool cont = d.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN); + while ( cont ) + { + wxFileName::Rmdir(path + filename, flags); + cont = d.GetNext(&filename); + } + +#ifndef __WXMSW__ + if ( flags & wxPATH_RMDIR_RECURSIVE ) + { + // delete all files too + cont = d.GetFirst(&filename, "", wxDIR_FILES | wxDIR_HIDDEN); + while ( cont ) + { + ::wxRemoveFile(path + filename); + cont = d.GetNext(&filename); + } + } +#endif // !__WXMSW__ + } + + return ::wxRmdir(dir); } // ---------------------------------------------------------------------------- -- 2.45.2