X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bf7f7793e9c6e2fc3fcb8297ab7b3b56e3e9eea2..c6a6bbbf637a5a580b7ab182483d27522f5e3189:/src/common/filename.cpp diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 64b49c4910..3c5764a6bb 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -175,9 +175,12 @@ public: if ( m_hFile == INVALID_HANDLE_VALUE ) { - wxLogSysError(_("Failed to open '%s' for %s"), - filename.c_str(), - mode == Read ? _("reading") : _("writing")); + if ( mode == Read ) + wxLogSysError(_("Failed to open '%s' for reading"), + filename.c_str()); + else + wxLogSysError(_("Failed to open '%s' for writing"), + filename.c_str()); } } @@ -284,6 +287,17 @@ static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) return path; } +// return true if the format used is the DOS/Windows one and the string looks +// like a UNC path +static bool IsUNCPath(const wxString& path, wxPathFormat format) +{ + return format == wxPATH_DOS && + path.length() >= 4 && // "\\a" can't be a UNC path + path[0u] == wxFILE_SEP_PATH_DOS && + path[1u] == wxFILE_SEP_PATH_DOS && + path[2u] != wxFILE_SEP_PATH_DOS; +} + // ============================================================================ // implementation // ============================================================================ @@ -307,9 +321,28 @@ void wxFileName::Assign(const wxString& volume, const wxString& name, const wxString& ext, bool hasExt, - wxPathFormat format ) + wxPathFormat format) { - SetPath( path, format ); + // we should ignore paths which look like UNC shares because we already + // have the volume here and the UNC notation (\\server\path) is only valid + // for paths which don't start with a volume, so prevent SetPath() from + // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path + // + // note also that this is a rather ugly way to do what we want (passing + // some kind of flag telling to ignore UNC paths to SetPath() would be + // better) but this is the safest thing to do to avoid breaking backwards + // compatibility in 2.8 + if ( IsUNCPath(path, format) ) + { + // remove one of the 2 leading backslashes to ensure that it's not + // recognized as an UNC path by SetPath() + wxString pathNonUNC(path, 1, wxString::npos); + SetPath(pathNonUNC, format); + } + else // no UNC complications + { + SetPath(path, format); + } m_volume = volume; m_ext = ext; @@ -427,7 +460,7 @@ void wxFileName::Assign(const wxString& fullpathOrig, // always recognize fullpath as directory, even if it doesn't end with a // slash wxString fullpath = fullpathOrig; - if ( !wxEndsWithPathSeparator(fullpath) ) + if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) ) { fullpath += GetPathSeparator(format); } @@ -619,7 +652,7 @@ static int wxOpenWithDeleteOnClose(const wxString& filename) HANDLE h = ::CreateFile(filename, access, 0, NULL, disposition, attributes, NULL); - return wxOpenOSFHandle(h, 0); + return wxOpenOSFHandle(h, wxO_BINARY); } #endif // wxOpenOSFHandle @@ -649,7 +682,7 @@ static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose) return file->Open(path, _T("w+b")); #else // wx_fdopen int fd = wxTempOpen(path, deleteOnClose); - if (fd != -1) + if (fd == -1) return false; file->Attach(wx_fdopen(fd, "w+b")); return file->IsOpened(); @@ -1793,7 +1826,7 @@ wxString wxFileName::GetLongPath() const wxString pathOut, path = GetFullPath(); -#if defined(__WIN32__) && !defined(__WXMICROWIN__) +#if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__) #if wxUSE_DYNAMIC_LOADER typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); @@ -1944,23 +1977,18 @@ wxFileName::SplitVolume(const wxString& fullpathWithVolume, wxString fullpath = fullpathWithVolume; // special Windows UNC paths hack: transform \\share\path into share:path - if ( format == wxPATH_DOS ) + if ( IsUNCPath(fullpath, format) ) { - if ( fullpath.length() >= 4 && - fullpath[0u] == wxFILE_SEP_PATH_DOS && - fullpath[1u] == wxFILE_SEP_PATH_DOS ) - { - fullpath.erase(0, 2); + fullpath.erase(0, 2); - size_t posFirstSlash = - fullpath.find_first_of(GetPathTerminators(format)); - if ( posFirstSlash != wxString::npos ) - { - fullpath[posFirstSlash] = wxFILE_SEP_DSK; + size_t posFirstSlash = + fullpath.find_first_of(GetPathTerminators(format)); + if ( posFirstSlash != wxString::npos ) + { + fullpath[posFirstSlash] = wxFILE_SEP_DSK; - // UNC paths are always absolute, right? (FIXME) - fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); - } + // UNC paths are always absolute, right? (FIXME) + fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); } } @@ -2216,7 +2244,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, // not 9x bool ok; FILETIME ftAccess, ftCreate, ftWrite; - if ( IsDir() ) + if ( IsDir() ) { // implemented in msw/dir.cpp extern bool wxGetDirectoryTimes(const wxString& dirname, @@ -2255,6 +2283,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, return true; } #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) + // no need to test for IsDir() here wxStructStat stBuf; if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 ) { @@ -2302,14 +2331,11 @@ wxULongLong wxFileName::GetSize(const wxString &filename) DWORD lpFileSizeHigh; DWORD ret = GetFileSize(f, &lpFileSizeHigh); - if (ret == INVALID_FILE_SIZE) + if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR ) return wxInvalidSize; - // compose the low-order and high-order byte sizes - return wxULongLong(ret | (lpFileSizeHigh << sizeof(WORD)*2)); - -#else // ! __WIN32__ - + return wxULongLong(lpFileSizeHigh, ret); +#else // ! __WIN32__ wxStructStat st; #ifndef wxNEED_WX_UNISTD_H if (wxStat( filename.fn_str() , &st) != 0)