From 44b62d54c61e4a0989fcc0f24eecfe0bd883d72b Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Fri, 4 Jan 2002 13:06:28 +0000
Subject: [PATCH] always make the name of the file to replace absolute before
 using it

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13351 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/tempfile.tex | 18 ++++++++++++------
 src/common/file.cpp        | 20 +++++++++++++++++---
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/docs/latex/wx/tempfile.tex b/docs/latex/wx/tempfile.tex
index 01a31ffdb2..1bb2f1bf9d 100644
--- a/docs/latex/wx/tempfile.tex
+++ b/docs/latex/wx/tempfile.tex
@@ -65,25 +65,31 @@ Associates wxTempFile with the file to be replaced and opens it. You should use
 
 \func{bool}{Open}{\param{const wxString\& }{strName}}
 
-Open the temporary file (strName is the name of file to be replaced), returns
-TRUE on success, FALSE if an error occurred.
+Open the temporary file, returns {\tt TRUE} on success, {\tt FALSE} if an error
+occurred.
+
+{\it strName} is the name of file to be replaced. The temporary file is always
+created in the directory where {\it strName} is. In particular, if 
+{\it strName} doesn't include the path, it is created in the current directory
+and the program should have write access to it for the function to succeed.
 
 \membersection{wxTempFile::IsOpened}\label{wxtempfileisopened}
+
 \constfunc{bool}{IsOpened}{\void}
 
-Returns TRUE if the file was successfully opened.
+Returns {\tt TRUE} if the file was successfully opened.
 
 \membersection{wxTempFile::Write}\label{wxtempfilewrite}
 
 \func{bool}{Write}{\param{const void }{*p}, \param{size\_t }{n}}
 
-Write to the file, return TRUE on success, FALSE on failure.
+Write to the file, return {\tt TRUE} on success, {\tt FALSE} on failure.
 
 \membersection{wxTempFile::Write}\label{wxtempfilewrites}
 
 \func{bool}{Write}{\param{const wxString\& }{str}, \param{wxMBConv&}{ conv = wxConvLibc}}
 
-Write to the file, return TRUE on success, FALSE on failure.
+Write to the file, return {\tt TRUE} on success, {\tt FALSE} on failure.
 
 The second argument is only meaningful in Unicode build of wxWindows when
 {\it conv} is used to convert {\it str} to multibyte representation.
@@ -93,7 +99,7 @@ The second argument is only meaningful in Unicode build of wxWindows when
 \func{bool}{Commit}{\void}
 
 Validate changes: deletes the old file of name m\_strName and renames the new
-file to the old name. Returns TRUE if both actions succeeded. If FALSE is
+file to the old name. Returns {\tt TRUE} if both actions succeeded. If {\tt FALSE} is
 returned it may unfortunately mean two quite different things: either that
 either the old file couldn't be deleted or that the new file couldn't be renamed
 to the old name.
diff --git a/src/common/file.cpp b/src/common/file.cpp
index e7834e082a..bbce6d8de1 100644
--- a/src/common/file.cpp
+++ b/src/common/file.cpp
@@ -469,6 +469,7 @@ bool wxFile::Eof() const
 // ----------------------------------------------------------------------------
 // construction
 // ----------------------------------------------------------------------------
+
 wxTempFile::wxTempFile(const wxString& strName)
 {
     Open(strName);
@@ -476,9 +477,22 @@ wxTempFile::wxTempFile(const wxString& strName)
 
 bool wxTempFile::Open(const wxString& strName)
 {
-    m_strName = strName;
+    // we must have an absolute filename because otherwise CreateTempFileName()
+    // would create the temp file in $TMP (i.e. the system standard location
+    // for the temp files) which might be on another volume/drive/mount and
+    // wxRename()ing it later to m_strName from Commit() would then fail
+    //
+    // with the absolute filename, the temp file is created in the same
+    // directory as this one which ensures that wxRename() may work later
+    wxFileName fn(strName);
+    if ( !fn.IsAbsolute() )
+    {
+        fn.Normalize(wxPATH_NORM_ABSOLUTE);
+    }
+
+    m_strName = fn.GetFullPath();
 
-    m_strTemp = wxFileName::CreateTempFileName(strName, &m_file);
+    m_strTemp = wxFileName::CreateTempFileName(m_strName, &m_file);
 
     if ( m_strTemp.empty() )
     {
@@ -491,7 +505,7 @@ bool wxTempFile::Open(const wxString& strName)
     mode_t mode;
 
     wxStructStat st;
-    if ( stat(strName.fn_str(), &st) == 0 )
+    if ( stat(m_strName.fn_str(), &st) == 0 )
     {
         mode = st.st_mode;
     }
-- 
2.50.0

