From 32f3104396bf705c0ba9eee6a851bfe1f20dd3f0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 16 Jan 2001 15:28:21 +0000 Subject: [PATCH] patch from Roland Scholz to fix the file permissions in wxCopyFile git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9112 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 7 +++--- src/common/filefn.cpp | 52 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index aa01794a8e..da680ebeb1 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -4,12 +4,13 @@ wxWindows 2 Change Log 2.4.0 ----- +2.3.0 +----- + wxBase: - wxMimeTypesManager now can create file associations too - -2.3.0 ------ +- wxCopyFile() respects the file permissions (Roland Scholz) wxMSW: diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index c6aa8b4b48..ea1417f900 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -31,7 +31,7 @@ #include "wx/utils.h" #include "wx/intl.h" -#include "wx/ffile.h" +#include "wx/file.h" #include "wx/filename.h" // there are just too many of those... @@ -72,6 +72,7 @@ #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) #include #include + #include #endif // __WINDOWS__ #endif // native Win compiler @@ -996,14 +997,50 @@ wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& fil bool wxCopyFile (const wxString& file1, const wxString& file2) { - wxFFile fileIn(file1, "rb"); + wxStructStat fbuf; + + // get permissions of file1 + if ( wxStat(file1, &fbuf) != 0 ) + { + // the file probably doesn't exist or we haven't the rights to read + // from it anyhow + wxLogSysError(_("Impossible to get permissions for file '%s'"), + file1.c_str()); + return FALSE; + } + + // open file1 for reading + wxFile fileIn(file1, wxFile::read); if ( !fileIn.IsOpened() ) return FALSE; - wxFFile fileOut(file2, "wb"); - if ( !fileOut.IsOpened() ) + // remove file2, if it exists. This is needed for creating + // file2 with the correct permissions in the next step + if ( wxFileExists(file2) && !wxRemoveFile(file2) ) + { + wxLogSysError(_("Impossible to overwrite the file '%s'"), + file2.c_str()); + return FALSE; + } + +#ifdef __UNIX__ + // reset the umask as we want to create the file with exactly the same + // permissions as the original one + mode_t oldUmask = umask( 0 ); +#endif // __UNIX__ + + // create file2 with the same permissions than file1 and open it for + // writing + wxFile fileOut; + if ( !fileOut.Create(file2, TRUE, fbuf.st_mode & 0777) ) return FALSE; +#ifdef __UNIX__ + /// restore the old umask + umask(oldUmask); +#endif // __UNIX__ + + // copy contents of file1 to file2 char buf[4096]; size_t count; for ( ;; ) @@ -1020,6 +1057,13 @@ wxCopyFile (const wxString& file1, const wxString& file2) return FALSE; } + if ( chmod(file2, fbuf.st_mode) != 0 ) + { + wxLogSysError(_("Impossible to set permissions for the file '%s'"), + file2.c_str()); + return FALSE; + } + return TRUE; }