X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1b4bff8262c70299a1c9f20548ec3043209616dc..a66bf817a6ba833ad47cefc9fa8948ef8045a59d:/src/common/filename.cpp?ds=sidebyside diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 79e2cae278..44b47f0719 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -33,7 +33,7 @@ http://msdn.microsoft.com/en-us/library/aa365248(VS.85).aspx. - wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file + wxPATH_MAC: Mac OS 8/9 only, not used any longer, absolute file names have the form volume:dir1:...:dirN:filename and the relative file names are either @@ -77,7 +77,7 @@ #endif #ifndef WX_PRECOMP - #ifdef __WXMSW__ + #ifdef __WINDOWS__ #include "wx/msw/wrapwin.h" // For GetShort/LongPathName #endif #include "wx/dynarray.h" @@ -93,13 +93,16 @@ #include "wx/config.h" // for wxExpandEnvVars #include "wx/dynlib.h" #include "wx/dir.h" +#include "wx/longlong.h" #if defined(__WIN32__) && defined(__MINGW32__) #include "wx/msw/gccpriv.h" #endif -#ifdef __WXMSW__ -#include "wx/msw/private.h" +#ifdef __WINDOWS__ + #include "wx/msw/private.h" + #include // for CLSID_ShellLink + #include "wx/msw/missing.h" #endif #if defined(__WXMAC__) @@ -118,19 +121,6 @@ #include #endif -#ifdef __MWERKS__ -#ifdef __MACH__ -#include -#include -#include -#include -#else -#include -#include -#include -#endif -#endif - #ifdef __WATCOMC__ #include #include @@ -242,49 +232,30 @@ private: #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) -// convert between wxDateTime and FILETIME which is a 64-bit value representing -// the number of 100-nanosecond intervals since January 1, 1601. +// Convert between wxDateTime and FILETIME which is a 64-bit value representing +// the number of 100-nanosecond intervals since January 1, 1601 UTC. +// +// This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch. +static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000); static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) { - FILETIME ftcopy = ft; - FILETIME ftLocal; - if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) ) - { - wxLogLastError(wxT("FileTimeToLocalFileTime")); - } + wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); + t /= 10000; // Convert hundreds of nanoseconds to milliseconds. + t -= EPOCH_OFFSET_IN_MSEC; - SYSTEMTIME st; - if ( !::FileTimeToSystemTime(&ftLocal, &st) ) - { - wxLogLastError(wxT("FileTimeToSystemTime")); - } - - dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear, - st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); + *dt = wxDateTime(t); } static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) { - SYSTEMTIME st; - st.wDay = dt.GetDay(); - st.wMonth = (WORD)(dt.GetMonth() + 1); - st.wYear = (WORD)dt.GetYear(); - st.wHour = dt.GetHour(); - st.wMinute = dt.GetMinute(); - st.wSecond = dt.GetSecond(); - st.wMilliseconds = dt.GetMillisecond(); - - FILETIME ftLocal; - if ( !::SystemTimeToFileTime(&st, &ftLocal) ) - { - wxLogLastError(wxT("SystemTimeToFileTime")); - } + // Undo the conversions above. + wxLongLong t(dt.GetValue()); + t += EPOCH_OFFSET_IN_MSEC; + t *= 10000; - if ( !::LocalFileTimeToFileTime(&ftLocal, ft) ) - { - wxLogLastError(wxT("LocalFileTimeToFileTime")); - } + ft->dwHighDateTime = t.GetHi(); + ft->dwLowDateTime = t.GetLo(); } #endif // wxUSE_DATETIME && __WIN32__ @@ -345,6 +316,56 @@ static bool IsUNCPath(const wxString& path, wxPathFormat format) !IsDOSPathSep(path[2u]); } +#ifndef __WIN32__ + +// Under Unix-ish systems (basically everything except Windows) we may work +// either with the file itself or its target if it's a symbolic link and we +// should dereference it, as determined by wxFileName::ShouldFollowLink() and +// the absence of the wxFILE_EXISTS_NO_FOLLOW flag. StatAny() can be used to +// stat the appropriate file with an extra twist that it also works when there +// is no wxFileName object at all, as is the case in static methods. + +// Private implementation, don't call directly, use one of the overloads below. +bool DoStatAny(wxStructStat& st, wxString path, bool dereference) +{ + // We need to remove any trailing slashes from the path because they could + // interfere with the symlink following decision: even if we use lstat(), + // it would still follow the symlink if we pass it a path with a slash at + // the end because the symlink resolution would happen while following the + // path and not for the last path element itself. + + while ( wxEndsWithPathSeparator(path) ) + { + const size_t posLast = path.length() - 1; + if ( !posLast ) + { + // Don't turn "/" into empty string. + break; + } + + path.erase(posLast); + } + + int ret = dereference ? wxStat(path, &st) : wxLstat(path, &st); + return ret == 0; +} + +// Overloads to use for a case when we don't have wxFileName object and when we +// do have one. +inline +bool StatAny(wxStructStat& st, const wxString& path, int flags) +{ + return DoStatAny(st, path, !(flags & wxFILE_EXISTS_NO_FOLLOW)); +} + +inline +bool StatAny(wxStructStat& st, const wxFileName& fn) +{ + return DoStatAny(st, fn.GetFullPath(), fn.ShouldFollowLink()); +} + +#endif // !__WIN32__ + // ---------------------------------------------------------------------------- // private constants // ---------------------------------------------------------------------------- @@ -370,6 +391,7 @@ void wxFileName::Assign( const wxFileName &filepath ) m_ext = filepath.GetExt(); m_relative = filepath.m_relative; m_hasExt = filepath.m_hasExt; + m_dontFollowLinks = filepath.m_dontFollowLinks; } void wxFileName::Assign(const wxString& volume, @@ -568,17 +590,19 @@ void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) void wxFileName::Clear() { - m_dirs.Clear(); - - m_volume = - m_name = - m_ext = wxEmptyString; + m_dirs.clear(); + m_volume.clear(); + m_name.clear(); + m_ext.clear(); // we don't have any absolute path for now m_relative = true; // nor any extension m_hasExt = false; + + // follow symlinks by default + m_dontFollowLinks = false; } /* static */ @@ -602,16 +626,6 @@ wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format) namespace { -// Flags for wxFileSystemObjectExists() asking it to check for: -enum -{ - wxFileSystemObject_File = 1, // file existence - wxFileSystemObject_Dir = 2, // directory existence - wxFileSystemObject_Other = 4, // existence of something else, e.g. - // device, socket, FIFO under Unix - wxFileSystemObject_Any = 7 // existence of anything at all -}; - #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) void RemoveTrailingSeparatorsFromPath(wxString& strPath) @@ -636,12 +650,14 @@ void RemoveTrailingSeparatorsFromPath(wxString& strPath) #endif // __WINDOWS__ || __OS2__ -bool wxFileSystemObjectExists(const wxString& path, int flags) +bool +wxFileSystemObjectExists(const wxString& path, int flags) { + // Should the existence of file/directory with this name be accepted, i.e. // result in the true return value from this function? - const bool acceptFile = (flags & wxFileSystemObject_File) != 0; - const bool acceptDir = (flags & wxFileSystemObject_Dir) != 0; + const bool acceptFile = (flags & wxFILE_EXISTS_REGULAR) != 0; + const bool acceptDir = (flags & wxFILE_EXISTS_DIR) != 0; wxString strPath(path); @@ -655,7 +671,7 @@ bool wxFileSystemObjectExists(const wxString& path, int flags) // we must use GetFileAttributes() instead of the ANSI C functions because // it can cope with network (UNC) paths unlike them - DWORD ret = ::GetFileAttributes(path.t_str()); + DWORD ret = ::GetFileAttributes(strPath.t_str()); if ( ret == INVALID_FILE_ATTRIBUTES ) return false; @@ -689,22 +705,35 @@ bool wxFileSystemObjectExists(const wxString& path, int flags) // We consider that the path must exist if we get a sharing violation for // it but we don't know what is it in this case. if ( rc == ERROR_SHARING_VIOLATION ) - return flags & wxFileSystemObject_Other; + return flags & wxFILE_EXISTS_ANY; // Any other error (usually ERROR_PATH_NOT_FOUND), means there is nothing // there. return false; #else // Non-MSW, non-OS/2 wxStructStat st; - if ( wxStat(strPath, &st) != 0 ) + if ( !StatAny(st, strPath, flags) ) return false; if ( S_ISREG(st.st_mode) ) return acceptFile; if ( S_ISDIR(st.st_mode) ) return acceptDir; + if ( S_ISLNK(st.st_mode) ) + { + // Take care to not test for "!= 0" here as this would erroneously + // return true if only wxFILE_EXISTS_NO_FOLLOW, which is part of + // wxFILE_EXISTS_SYMLINK, is set too. + return (flags & wxFILE_EXISTS_SYMLINK) == wxFILE_EXISTS_SYMLINK; + } + if ( S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ) + return (flags & wxFILE_EXISTS_DEVICE) != 0; + if ( S_ISFIFO(st.st_mode) ) + return (flags & wxFILE_EXISTS_FIFO) != 0; + if ( S_ISSOCK(st.st_mode) ) + return (flags & wxFILE_EXISTS_SOCKET) != 0; - return flags & wxFileSystemObject_Other; + return flags & wxFILE_EXISTS_ANY; #endif // Platforms } @@ -712,30 +741,50 @@ bool wxFileSystemObjectExists(const wxString& path, int flags) bool wxFileName::FileExists() const { - return wxFileName::FileExists( GetFullPath() ); + int flags = wxFILE_EXISTS_REGULAR; + if ( !ShouldFollowLink() ) + flags |= wxFILE_EXISTS_NO_FOLLOW; + + return wxFileSystemObjectExists(GetFullPath(), flags); } /* static */ bool wxFileName::FileExists( const wxString &filePath ) { - return wxFileSystemObjectExists(filePath, wxFileSystemObject_File); + return wxFileSystemObjectExists(filePath, wxFILE_EXISTS_REGULAR); } bool wxFileName::DirExists() const { - return wxFileName::DirExists( GetPath() ); + int flags = wxFILE_EXISTS_DIR; + if ( !ShouldFollowLink() ) + flags |= wxFILE_EXISTS_NO_FOLLOW; + + return Exists(GetPath(), flags); } /* static */ bool wxFileName::DirExists( const wxString &dirPath ) { - return wxFileSystemObjectExists(dirPath, wxFileSystemObject_Dir); + return wxFileSystemObjectExists(dirPath, wxFILE_EXISTS_DIR); +} + +bool wxFileName::Exists(int flags) const +{ + // Notice that wxFILE_EXISTS_NO_FOLLOW may be specified in the flags even + // if our DontFollowLink() hadn't been called and we do honour it then. But + // if the user took the care of calling DontFollowLink(), it is always + // taken into account. + if ( !ShouldFollowLink() ) + flags |= wxFILE_EXISTS_NO_FOLLOW; + + return wxFileSystemObjectExists(GetFullPath(), flags); } /* static */ -bool wxFileName::Exists(const wxString& path) +bool wxFileName::Exists(const wxString& path, int flags) { - return wxFileSystemObjectExists(path, wxFileSystemObject_Any); + return wxFileSystemObjectExists(path, flags); } // ---------------------------------------------------------------------------- @@ -825,7 +874,7 @@ static int wxOpenWithDeleteOnClose(const wxString& filename) DWORD attributes = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE; - HANDLE h = ::CreateFile(filename.fn_str(), access, 0, NULL, + HANDLE h = ::CreateFile(filename.t_str(), access, 0, NULL, disposition, attributes, NULL); return wxOpenOSFHandle(h, wxO_BINARY); @@ -1001,7 +1050,7 @@ static wxString wxCreateTempImpl( } #else // !HAVE_MKTEMP (includes __DOS__) // generate the unique file name ourselves - #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) ) + #if !defined(__DOS__) path << (unsigned int)getpid(); #endif @@ -1318,7 +1367,7 @@ bool wxFileName::Rmdir(int flags) const bool wxFileName::Rmdir(const wxString& dir, int flags) { -#ifdef __WXMSW__ +#ifdef __WINDOWS__ if ( flags & wxPATH_RMDIR_RECURSIVE ) { // SHFileOperation needs double null termination string @@ -1331,11 +1380,7 @@ bool wxFileName::Rmdir(const wxString& dir, int flags) SHFILEOPSTRUCT fileop; wxZeroMemory(fileop); fileop.wFunc = FO_DELETE; - #if defined(__CYGWIN__) && defined(wxUSE_UNICODE) - fileop.pFrom = path.wc_str(); - #else - fileop.pFrom = path.fn_str(); - #endif + fileop.pFrom = path.t_str(); fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; #ifndef __WXWINCE__ // FOF_NOERRORUI is not defined in WinCE @@ -1354,10 +1399,24 @@ bool wxFileName::Rmdir(const wxString& dir, int flags) return true; } else if ( flags & wxPATH_RMDIR_FULL ) -#else // !__WXMSW__ +#else // !__WINDOWS__ if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE -#endif // !__WXMSW__ +#endif // !__WINDOWS__ { +#ifndef __WINDOWS__ + if ( flags & wxPATH_RMDIR_RECURSIVE ) + { + // When deleting the tree recursively, we are supposed to delete + // this directory itself even when it is a symlink -- but without + // following it. Do it here as wxRmdir() would simply follow if + // called for a symlink. + if ( wxFileName::Exists(dir, wxFILE_EXISTS_SYMLINK) ) + { + return wxRemoveFile(dir); + } + } +#endif // !__WINDOWS__ + wxString path(dir); if ( path.Last() != wxFILE_SEP_PATH ) path += wxFILE_SEP_PATH; @@ -1369,26 +1428,32 @@ bool wxFileName::Rmdir(const wxString& dir, int flags) wxString filename; - // first delete all subdirectories - bool cont = d.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN); + // First delete all subdirectories: notice that we don't follow + // symbolic links, potentially leading outside this directory, to avoid + // unpleasant surprises. + bool cont = d.GetFirst(&filename, wxString(), + wxDIR_DIRS | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); while ( cont ) { wxFileName::Rmdir(path + filename, flags); cont = d.GetNext(&filename); } -#ifndef __WXMSW__ +#ifndef __WINDOWS__ if ( flags & wxPATH_RMDIR_RECURSIVE ) { - // delete all files too - cont = d.GetFirst(&filename, "", wxDIR_FILES | wxDIR_HIDDEN); + // Delete all files too and, for the same reasons as above, don't + // follow symlinks which could refer to the files outside of this + // directory and just delete the symlinks themselves. + cont = d.GetFirst(&filename, wxString(), + wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); while ( cont ) { ::wxRemoveFile(path + filename); cont = d.GetNext(&filename); } } -#endif // !__WXMSW__ +#endif // !__WINDOWS__ } return ::wxRmdir(dir); @@ -1620,13 +1685,6 @@ bool wxFileName::ReplaceHomeDir(wxPathFormat format) // quotation marks." #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE -// The following lines are necessary under WinCE -// #include "wx/msw/private.h" -// #include -#include -#if defined(__WXWINCE__) -#include -#endif bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targetFilename, @@ -1796,8 +1854,7 @@ bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const #if defined(__UNIX__) wxStructStat st1, st2; - if ( wxStat(fn1.GetFullPath(), &st1) == 0 && - wxStat(fn2.GetFullPath(), &st2) == 0 ) + if ( StatAny(st1, fn1) && StatAny(st2, fn2) ) { if ( st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev ) return true; @@ -1811,7 +1868,7 @@ bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const /* static */ bool wxFileName::IsCaseSensitive( wxPathFormat format ) { - // only Unix filenames are truely case-sensitive + // only Unix filenames are truly case-sensitive return GetFormat(format) == wxPATH_UNIX; } @@ -1836,7 +1893,7 @@ wxString wxFileName::GetForbiddenChars(wxPathFormat format) case wxPATH_MAC: // On a Mac even names with * and ? are allowed (Tested with OS // 9.2.1 and OS X 10.2.5) - strForbiddenChars = wxEmptyString; + strForbiddenChars.clear(); break; case wxPATH_DOS: @@ -1958,10 +2015,12 @@ wxFileName::IsMSWUniqueVolumeNamePath(const wxString& path, wxPathFormat format) return true; } -void wxFileName::AppendDir( const wxString& dir ) +bool wxFileName::AppendDir( const wxString& dir ) { - if ( IsValidDirComponent(dir) ) - m_dirs.Add( dir ); + if (!IsValidDirComponent(dir)) + return false; + m_dirs.Add(dir); + return true; } void wxFileName::PrependDir( const wxString& dir ) @@ -1969,10 +2028,12 @@ void wxFileName::PrependDir( const wxString& dir ) InsertDir(0, dir); } -void wxFileName::InsertDir(size_t before, const wxString& dir) +bool wxFileName::InsertDir(size_t before, const wxString& dir) { - if ( IsValidDirComponent(dir) ) - m_dirs.Insert(dir, before); + if (!IsValidDirComponent(dir)) + return false; + m_dirs.Insert(dir, before); + return true; } void wxFileName::RemoveDir(size_t pos) @@ -2122,7 +2183,7 @@ wxString wxFileName::GetShortPath() const { wxString path(GetFullPath()); -#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) +#if defined(__WINDOWS__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) DWORD sz = ::GetShortPathName(path.t_str(), NULL, 0); if ( sz != 0 ) { @@ -2214,7 +2275,7 @@ wxString wxFileName::GetLongPath() const GetVolumeSeparator(wxPATH_DOS) + GetPathSeparator(wxPATH_DOS); else - pathOut = wxEmptyString; + pathOut.clear(); wxArrayString dirs = GetDirs(); dirs.Add(GetFullName()); @@ -2272,7 +2333,7 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format ) { if (format == wxPATH_NATIVE) { -#if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__) +#if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__) format = wxPATH_DOS; #elif defined(__VMS) format = wxPATH_VMS; @@ -2664,7 +2725,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) // no need to test for IsDir() here wxStructStat stBuf; - if ( wxStat( GetFullPath(), &stBuf) == 0 ) + if ( StatAny(stBuf, *this) ) { // Android defines st_*time fields as unsigned long, but time_t as long, // hence the static_casts.