From 8482e4bdb9928fe9982ac214aee345b19894b39b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 3 Oct 2004 22:12:58 +0000 Subject: [PATCH 1/1] added wxUmaskChanger class and wxCHANGE_UMASK macro and use them instead of duplicating the same umask-setting code in several places git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29629 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/function.tex | 15 ++++++++++++++- include/wx/filefn.h | 35 +++++++++++++++++++++++++++++++++++ src/common/fileconf.cpp | 25 +++---------------------- src/common/filefn.cpp | 9 +-------- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index aee339bb42..2efa51c3d5 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -31,6 +31,7 @@ the corresponding topic. \helpref{wxBITMAP}{wxbitmapmacro}\\ \helpref{wxBeginBusyCursor}{wxbeginbusycursor}\\ \helpref{wxBell}{wxbell}\\ +\helpref{wxCHANGE\_UMASK}{wxchangeumask}\\ \helpref{wxCHECK}{wxcheck}\\ \helpref{wxCHECK2\_MSG}{wxcheck2msg}\\ \helpref{wxCHECK2}{wxcheck2}\\ @@ -890,7 +891,7 @@ threads. \wxheading{Include files} - + \wxheading{See also} @@ -1032,6 +1033,18 @@ Converts a Unix to a DOS filename by replacing forward slashes with backslashes. +\membersection{wxCHANGE\_UMASK}\label{wxchangeumask} + +\func{}{wxCHANGE\_UMASK}{\param{int }{mask}} + +Under Unix this macro changes the current process umask to the given value, +unless it is equal to $-1$ in which case nothing is done, and restores it to +the original value on scope exit. It works by declaring a variable which sets +umask to \arg{mask} in its constructor and restores it in its destructor. + +Under other platforms this macro expands to nothing. + + \membersection{::wxConcatFiles}\label{wxconcatfiles} \func{bool}{wxConcatFiles}{\param{const wxString\& }{file1}, \param{const wxString\& }{file2}, diff --git a/include/wx/filefn.h b/include/wx/filefn.h index 24c64f8f79..1b102609ef 100644 --- a/include/wx/filefn.h +++ b/include/wx/filefn.h @@ -549,6 +549,41 @@ WXDLLIMPEXP_BASE int wxParseCommonDialogsFilter(const wxString& wildCard, wxArra // classes // ---------------------------------------------------------------------------- +#ifdef __UNIX__ + +// set umask to the given value in ctor and reset it to the old one in dtor +class WXDLLIMPEXP_BASE wxUmaskChanger +{ +public: + // change the umask to the given one if it is not -1: this allows to write + // the same code whether you really want to change umask or not, as is in + // wxFileConfig::Flush() for example + wxUmaskChanger(int umaskNew) + { + m_umaskOld = umaskNew == -1 ? -1 : umask((mode_t)umaskNew); + } + + ~wxUmaskChanger() + { + if ( m_umaskOld != -1 ) + umask((mode_t)m_umaskOld); + } + +private: + int m_umaskOld; +}; + +// this macro expands to an "anonymous" wxUmaskChanger object under Unix and +// nothing elsewhere +#define wxCHANGE_UMASK(m) wxUmaskChanger wxMAKE_UNIQUE_NAME(umaskChanger_)(m) + +#else // !__UNIX__ + +#define wxCHANGE_UMASK(m) + +#endif // __UNIX__/!__UNIX__ + + // Path searching class WXDLLIMPEXP_BASE wxPathList : public wxStringList { diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 6215e1fe37..ac4e2a53ea 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -39,6 +39,7 @@ #include "wx/memtext.h" #include "wx/config.h" #include "wx/fileconf.h" +#include "wx/filefn.h" #if wxUSE_STREAMS #include "wx/stream.h" @@ -61,12 +62,6 @@ #include #include -// headers needed for umask() -#ifdef __UNIX__ - #include - #include -#endif // __UNIX__ - // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -476,7 +471,7 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName, #if wxUSE_STREAMS wxFileConfig::wxFileConfig(wxInputStream &inStream, wxMBConv& conv) - : m_conv(conv) + : m_conv(conv) { // always local_file when this constructor is called (?) SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE); @@ -965,14 +960,8 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) if ( LineListIsEmpty() || !m_pRootGroup->IsDirty() || !m_strLocalFile ) return true; -#ifdef __UNIX__ // set the umask if needed - mode_t umaskOld = 0; - if ( m_umask != -1 ) - { - umaskOld = umask((mode_t)m_umask); - } -#endif // __UNIX__ + wxCHANGE_UMASK(m_umask); wxTempFile file(m_strLocalFile); @@ -1016,14 +1005,6 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) } #endif // __WXMAC__ -#ifdef __UNIX__ - // restore the old umask if we changed it - if ( m_umask != -1 ) - { - (void)umask(umaskOld); - } -#endif // __UNIX__ - return ret; } diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index eaefdbca40..0e3f2a552e 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -1021,11 +1021,9 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) 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__ + wxCHANGE_UMASK(0); // create file2 with the same permissions than file1 and open it for // writing @@ -1034,11 +1032,6 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) if ( !fileOut.Create(file2, overwrite, 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; -- 2.45.2