#endif
#ifndef WX_PRECOMP
+ #ifdef __WXMSW__
+ #include "wx/msw/wrapwin.h" // For GetShort/LongPathName
+ #endif
#include "wx/dynarray.h"
#include "wx/intl.h"
#include "wx/log.h"
- #include "wx/file.h"
#include "wx/utils.h"
#endif
#include "wx/filename.h"
+#include "wx/private/filename.h"
#include "wx/tokenzr.h"
#include "wx/config.h" // for wxExpandEnvVars
-#include "wx/file.h"
#include "wx/dynlib.h"
-// For GetShort/LongPathName
-#ifdef __WIN32__
-#include "wx/msw/wrapwin.h"
-#if defined(__MINGW32__)
-#include "wx/msw/gccpriv.h"
-#endif
+#if defined(__WIN32__) && defined(__MINGW32__)
+ #include "wx/msw/gccpriv.h"
#endif
#ifdef __WXWINCE__
return ::wxGetHomeDir();
}
-#if wxUSE_FILE
-void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
+// ----------------------------------------------------------------------------
+// CreateTempFileName
+// ----------------------------------------------------------------------------
+
+#if wxUSE_FILE || wxUSE_FFILE
+
+
+#if !defined wx_fdopen && defined HAVE_FDOPEN
+ #define wx_fdopen fdopen
+#endif
+
+// NB: GetTempFileName() under Windows creates the file, so using
+// O_EXCL there would fail
+#ifdef __WINDOWS__
+ #define wxOPEN_EXCL 0
+#else
+ #define wxOPEN_EXCL O_EXCL
+#endif
+
+
+#ifdef wxOpenOSFHandle
+#define WX_HAVE_DELETE_ON_CLOSE
+// On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags.
+//
+static int wxOpenWithDeleteOnClose(const wxString& filename)
{
- wxString tempname = CreateTempFileName(prefix, fileTemp);
- if ( tempname.empty() )
- {
- // error, failed to get temp file name
- Clear();
- }
- else // ok
- {
- Assign(tempname);
- }
+ DWORD access = GENERIC_READ | GENERIC_WRITE;
+
+ DWORD disposition = OPEN_ALWAYS;
+
+ DWORD attributes = FILE_ATTRIBUTE_TEMPORARY |
+ FILE_FLAG_DELETE_ON_CLOSE;
+
+ HANDLE h = ::CreateFile(filename, access, 0, NULL,
+ disposition, attributes, NULL);
+
+ return wxOpenOSFHandle(h, wxO_BINARY);
}
+#endif // wxOpenOSFHandle
-/* static */
-wxString
-wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
+
+// Helper to open the file
+//
+static int wxTempOpen(const wxString& path, bool *deleteOnClose)
{
+#ifdef WX_HAVE_DELETE_ON_CLOSE
+ if (*deleteOnClose)
+ return wxOpenWithDeleteOnClose(path);
+#endif
+
+ *deleteOnClose = false;
+
+ return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600);
+}
+
+
+#if wxUSE_FFILE
+// Helper to open the file and attach it to the wxFFile
+//
+static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose)
+{
+#ifndef wx_fdopen
+ *deleteOnClose = false;
+ return file->Open(path, _T("w+b"));
+#else // wx_fdopen
+ int fd = wxTempOpen(path, deleteOnClose);
+ if (fd == -1)
+ return false;
+ file->Attach(wx_fdopen(fd, "w+b"));
+ return file->IsOpened();
+#endif // wx_fdopen
+}
+#endif // wxUSE_FFILE
+
+
+#if !wxUSE_FILE
+ #define WXFILEARGS(x, y) y
+#elif !wxUSE_FFILE
+ #define WXFILEARGS(x, y) x
+#else
+ #define WXFILEARGS(x, y) x, y
+#endif
+
+
+// Implementation of wxFileName::CreateTempFileName().
+//
+static wxString wxCreateTempImpl(
+ const wxString& prefix,
+ WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp),
+ bool *deleteOnClose = NULL)
+{
+#if wxUSE_FILE && wxUSE_FFILE
+ wxASSERT(fileTemp == NULL || ffileTemp == NULL);
+#endif
wxString path, dir, name;
+ bool wantDeleteOnClose = false;
+
+ if (deleteOnClose)
+ {
+ // set the result to false initially
+ wantDeleteOnClose = *deleteOnClose;
+ *deleteOnClose = false;
+ }
+ else
+ {
+ // easier if it alwasys points to something
+ deleteOnClose = &wantDeleteOnClose;
+ }
// use the directory specified by the prefix
- SplitPath(prefix, &dir, &name, NULL /* extension */);
+ wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */);
if (dir.empty())
{
- dir = wxGetenv(_T("TMPDIR"));
- if (dir.empty())
- {
- dir = wxGetenv(_T("TMP"));
- if (dir.empty())
- {
- dir = wxGetenv(_T("TEMP"));
- }
- }
+ dir = wxFileName::GetTempDir();
}
#if defined(__WXWINCE__)
- if (dir.empty())
- {
- // FIXME. Create \temp dir?
- if (DirExists(wxT("\\temp")))
- dir = wxT("\\temp");
- }
path = dir + wxT("\\") + name;
int i = 1;
- while (FileExists(path))
+ while (wxFileName::FileExists(path))
{
path = dir + wxT("\\") + name ;
path << i;
}
#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
-
- if ( dir.empty() )
- {
- if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
- {
- wxLogLastError(_T("GetTempPath"));
- }
-
- if ( dir.empty() )
- {
- // GetTempFileName() fails if we pass it an empty string
- dir = _T('.');
- }
- }
- else // we have a dir to create the file in
- {
- // ensure we use only the back slashes as GetTempFileName(), unlike all
- // the other APIs, is picky and doesn't accept the forward ones
- dir.Replace(_T("/"), _T("\\"));
- }
-
if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
{
wxLogLastError(_T("GetTempFileName"));
}
#else // !Windows
- if ( dir.empty() )
- {
- // default
-#if defined(__DOS__) || defined(__OS2__)
- dir = _T(".");
-#elif defined(__WXMAC__)
- dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
-#else
- dir = _T("/tmp");
-#endif
- }
-
path = dir;
if ( !wxEndsWithPathSeparator(dir) &&
{
path = wxConvFile.cMB2WX( (const char*) buf );
+ #if wxUSE_FILE
// avoid leaking the fd
if ( fileTemp )
{
fileTemp->Attach(fdTemp);
}
else
+ #endif
+
+ #if wxUSE_FFILE
+ if ( ffileTemp )
+ {
+ #ifdef wx_fdopen
+ ffileTemp->Attach(wx_fdopen(fdTemp, "r+b"));
+ #else
+ ffileTemp->Open(path, _T("r+b"));
+ close(fdTemp);
+ #endif
+ }
+ else
+ #endif
+
{
close(fdTemp);
}
path += _T("XXXXXX");
wxCharBuffer buf = wxConvFile.cWX2MB( path );
- if ( !mktemp( (const char*) buf ) )
+ if ( !mktemp( (char*)(const char*) buf ) )
{
path.clear();
}
{
// 3 hex digits is enough for numTries == 1000 < 4096
pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
- if ( !FileExists(pathTry) )
+ if ( !wxFileName::FileExists(pathTry) )
{
break;
}
{
wxLogSysError(_("Failed to create a temporary file name"));
}
- else if ( fileTemp && !fileTemp->IsOpened() )
+ else
{
+ bool ok = true;
+
// open the file - of course, there is a race condition here, this is
// why we always prefer using mkstemp()...
- //
- // NB: GetTempFileName() under Windows creates the file, so using
- // write_excl there would fail
- if ( !fileTemp->Open(path,
-#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
- wxFile::write,
-#else
- wxFile::write_excl,
-#endif
- wxS_IRUSR | wxS_IWUSR) )
+ #if wxUSE_FILE
+ if ( fileTemp && !fileTemp->IsOpened() )
+ {
+ *deleteOnClose = wantDeleteOnClose;
+ int fd = wxTempOpen(path, deleteOnClose);
+ if (fd != -1)
+ fileTemp->Attach(fd);
+ else
+ ok = false;
+ }
+ #endif
+
+ #if wxUSE_FFILE
+ if ( ffileTemp && !ffileTemp->IsOpened() )
+ {
+ *deleteOnClose = wantDeleteOnClose;
+ ok = wxTempOpen(ffileTemp, path, deleteOnClose);
+ }
+ #endif
+
+ if ( !ok )
{
// FIXME: If !ok here should we loop and try again with another
// file name? That is the standard recourse if open(O_EXCL)
return path;
}
+
+static bool wxCreateTempImpl(
+ const wxString& prefix,
+ WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp),
+ wxString *name)
+{
+ bool deleteOnClose = true;
+
+ *name = wxCreateTempImpl(prefix,
+ WXFILEARGS(fileTemp, ffileTemp),
+ &deleteOnClose);
+
+ bool ok = !name->empty();
+
+ if (deleteOnClose)
+ name->clear();
+#ifdef __UNIX__
+ else if (ok && wxRemoveFile(*name))
+ name->clear();
+#endif
+
+ return ok;
+}
+
+
+static void wxAssignTempImpl(
+ wxFileName *fn,
+ const wxString& prefix,
+ WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp))
+{
+ wxString tempname;
+ tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp));
+
+ if ( tempname.empty() )
+ {
+ // error, failed to get temp file name
+ fn->Clear();
+ }
+ else // ok
+ {
+ fn->Assign(tempname);
+ }
+}
+
+
+void wxFileName::AssignTempFileName(const wxString& prefix)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL));
+}
+
+/* static */
+wxString wxFileName::CreateTempFileName(const wxString& prefix)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL));
+}
+
+#endif // wxUSE_FILE || wxUSE_FFILE
+
+
+#if wxUSE_FILE
+
+wxString wxCreateTempFileName(const wxString& prefix,
+ wxFile *fileTemp,
+ bool *deleteOnClose)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose);
+}
+
+bool wxCreateTempFile(const wxString& prefix,
+ wxFile *fileTemp,
+ wxString *name)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name);
+}
+
+void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL));
+}
+
+/* static */
+wxString
+wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
+{
+ return wxCreateTempFileName(prefix, fileTemp);
+}
+
#endif // wxUSE_FILE
+
+#if wxUSE_FFILE
+
+wxString wxCreateTempFileName(const wxString& prefix,
+ wxFFile *fileTemp,
+ bool *deleteOnClose)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose);
+}
+
+bool wxCreateTempFile(const wxString& prefix,
+ wxFFile *fileTemp,
+ wxString *name)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name);
+
+}
+
+void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp));
+}
+
+/* static */
+wxString
+wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp)
+{
+ return wxCreateTempFileName(prefix, fileTemp);
+}
+
+#endif // wxUSE_FFILE
+
+
// ----------------------------------------------------------------------------
// directory operations
// ----------------------------------------------------------------------------
+wxString wxFileName::GetTempDir()
+{
+ wxString dir;
+ dir = wxGetenv(_T("TMPDIR"));
+ if (dir.empty())
+ {
+ dir = wxGetenv(_T("TMP"));
+ if (dir.empty())
+ {
+ dir = wxGetenv(_T("TEMP"));
+ }
+ }
+
+#if defined(__WXWINCE__)
+ if (dir.empty())
+ {
+ // FIXME. Create \temp dir?
+ if (DirExists(wxT("\\temp")))
+ dir = wxT("\\temp");
+ }
+#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
+
+ if ( dir.empty() )
+ {
+ if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempPath"));
+ }
+
+ if ( dir.empty() )
+ {
+ // GetTempFileName() fails if we pass it an empty string
+ dir = _T('.');
+ }
+ }
+#else // !Windows
+
+ if ( dir.empty() )
+ {
+ // default
+#if defined(__DOS__) || defined(__OS2__)
+ dir = _T(".");
+#elif defined(__WXMAC__)
+ dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
+#else
+ dir = _T("/tmp");
+#endif
+ }
+#endif
+
+ return dir;
+}
+
bool wxFileName::Mkdir( int perm, int flags )
{
return wxFileName::Mkdir(GetPath(), perm, flags);
format = GetFormat(format);
- // make the path absolute
+ // set up the directory to use for making the path absolute later
if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
{
if ( cwd.empty() )
{
curDir.AssignDir(cwd);
}
-
- // the path may be not absolute because it doesn't have the volume name
- // but in this case we shouldn't modify the directory components of it
- // but just set the current volume
- if ( !HasVolume() && curDir.HasVolume() )
- {
- SetVolume(curDir.GetVolume());
-
- if ( !m_relative )
- {
- // yes, it was the case - we don't need curDir then
- curDir.Clear();
- }
- }
}
// handle ~ stuff under Unix only
wxString dir = dirs[0u];
if ( !dir.empty() && dir[0u] == _T('~') )
{
+ // to make the path absolute use the home directory
curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
+ // if we are expanding the tilde, then this path
+ // *should* be already relative (since we checked for
+ // the tilde only in the first char of the first dir);
+ // if m_relative==false, it's because it was initialized
+ // from a string which started with /~; in that case
+ // we reach this point but then need m_relative=true
+ // for relative->absolute expansion later
+ m_relative = true;
+
dirs.RemoveAt(0u);
}
}
// transform relative path into abs one
if ( curDir.IsOk() )
{
- wxArrayString dirsNew = curDir.GetDirs();
- size_t count = dirs.GetCount();
- for ( size_t n = 0; n < count; n++ )
+ // this path may be relative because it doesn't have the volume name
+ // and still have m_relative=true; in this case we shouldn't modify
+ // our directory components but just set the current volume
+ if ( !HasVolume() && curDir.HasVolume() )
+ {
+ SetVolume(curDir.GetVolume());
+
+ if ( !m_relative )
{
- dirsNew.Add(dirs[n]);
+ // yes, it was the case - we don't need curDir then
+ curDir.Clear();
+ }
}
- dirs = dirsNew;
+ // finally, prepend curDir to the dirs array
+ wxArrayString dirsNew = curDir.GetDirs();
+ WX_PREPEND_ARRAY(dirs, dirsNew);
+
+ // if we used e.g. tilde expansion previously and wxGetUserHome didn't
+ // return for some reason an absolute path, then curDir maybe not be absolute!
+ if ( curDir.IsAbsolute(format) )
+ {
+ // we have prepended an absolute path and thus we are now an absolute
+ // file name too
+ m_relative = false;
+ }
+ // else if (flags & wxPATH_NORM_ABSOLUTE):
+ // should we warn the user that we didn't manage to make the path absolute?
}
// now deal with ".", ".." and the rest
m_ext.MakeLower();
}
- // we do have the path now
- //
- // NB: need to do this before (maybe) calling Assign() below
- m_relative = false;
-
#if defined(__WIN32__)
if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
{
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);
// not 9x
bool ok;
FILETIME ftAccess, ftCreate, ftWrite;
- if ( IsDir() )
+ if ( IsDir() )
{
// implemented in msw/dir.cpp
extern bool wxGetDirectoryTimes(const wxString& dirname,
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 )
{