From 8195925175605a47d55f53a4d313ccf48a6b29d1 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Wed, 23 Jul 2008 05:17:31 +0000 Subject: [PATCH] Replace home made temp file creation with safer (and cleaner) Qt' solution. Should fix http://bugzilla.lyx.org/show_bug.cgi?id=4693 git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25822 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/GraphicsCacheItem.cpp | 4 -- src/graphics/GraphicsConverter.cpp | 1 - src/insets/InsetExternal.cpp | 2 - src/support/FileName.cpp | 67 ++++-------------------------- src/support/filetools.cpp | 5 --- 5 files changed, 7 insertions(+), 72 deletions(-) diff --git a/src/graphics/GraphicsCacheItem.cpp b/src/graphics/GraphicsCacheItem.cpp index 0859b44649..ff83f6f7d2 100644 --- a/src/graphics/GraphicsCacheItem.cpp +++ b/src/graphics/GraphicsCacheItem.cpp @@ -422,10 +422,6 @@ void CacheItem::Impl::convertToDisplayFormat() FileName const to_file_base = FileName::tempName("CacheItem"); remove_loaded_file_ = true; - // Remove the temp file, we only want the name... - // FIXME: This is unsafe! - to_file_base.removeFile(); - // Connect a signal to this->imageConverted and pass this signal to // the graphics converter so that we can load the modified file // on completion of the conversion process. diff --git a/src/graphics/GraphicsConverter.cpp b/src/graphics/GraphicsConverter.cpp index 4433f82b0f..6242ed91cb 100644 --- a/src/graphics/GraphicsConverter.cpp +++ b/src/graphics/GraphicsConverter.cpp @@ -295,7 +295,6 @@ static void build_script(FileName const & from_file, static int counter = 0; string const tmp = "gconvert" + convert(counter++); FileName const to_base = FileName::tempName(tmp); - to_base.removeFile(); // Create a copy of the file in case the original name contains // problematic characters like ' or ". We can work around that problem diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 0001b3775f..1fafc96a6e 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -69,8 +69,6 @@ namespace external { TempName::TempName() { FileName const tempname = FileName::tempName("lyxext"); - // FIXME: This is unsafe - tempname.removeFile(); // must have an extension for the converter code to work correctly. tempname_ = FileName(tempname.absFilename() + ".tmp"); } diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index ecd703b043..bd1aac9aab 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "support/lassert.h" @@ -308,14 +309,8 @@ bool FileName::isWritable() const bool FileName::isDirWritable() const { LYXERR(Debug::FILES, "isDirWriteable: " << *this); - FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest"); - - if (tmpfl.empty()) - return false; - - tmpfl.removeFile(); - return true; + return !tmpfl.empty(); } @@ -352,32 +347,6 @@ FileNameList FileName::dirList(string const & ext) const } -static int make_tempfile(char * templ) -{ -#if defined(HAVE_MKSTEMP) - return ::mkstemp(templ); -#elif defined(HAVE_MKTEMP) - // This probably just barely works... - ::mktemp(templ); -# if defined (HAVE_OPEN) -# if (!defined S_IRUSR) -# define S_IRUSR S_IREAD -# define S_IWUSR S_IWRITE -# endif - return ::open(templ, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); -# elif defined (HAVE__OPEN) - return ::_open(templ, - _O_RDWR | _O_CREAT | _O_EXCL, - _S_IREAD | _S_IWRITE); -# else -# error No open() function. -# endif -#else -#error FIX FIX FIX -#endif -} - - FileName FileName::tempName(string const & mask) { FileName tmp_name(mask); @@ -387,33 +356,11 @@ FileName FileName::tempName(string const & mask) else tmpfl = package().temp_dir().absFilename() + "/" + mask; -#if defined (HAVE_GETPID) - tmpfl += convert(getpid()); -#elif defined (HAVE__GETPID) - tmpfl += convert(_getpid()); -#else -# error No getpid() function -#endif - tmpfl += "XXXXXX"; - - // The supposedly safe mkstemp version - // FIXME: why not using std::string directly? - boost::scoped_array tmpl(new char[tmpfl.length() + 1]); // + 1 for '\0' - tmpfl.copy(tmpl.get(), string::npos); - tmpl[tmpfl.length()] = '\0'; // terminator - - int const tmpf = make_tempfile(tmpl.get()); - if (tmpf != -1) { - string const t(to_utf8(from_filesystem8bit(tmpl.get()))); -#if defined (HAVE_CLOSE) - ::close(tmpf); -#elif defined (HAVE__CLOSE) - ::_close(tmpf); -#else -# error No x() function. -#endif - LYXERR(Debug::FILES, "Temporary file `" << t << "' created."); - return FileName(t); + QTemporaryFile qt_tmp(toqstr(tmpfl)); + if (qt_tmp.open()) { + tmp_name.d->fi.setFile(qt_tmp.fileName()); + LYXERR(Debug::FILES, "Temporary file `" << tmp_name << "' created."); + return tmp_name; } LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file."); return FileName(); diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index aa9d99da0e..eb982a2f8a 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -323,11 +323,6 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask) string const tmp_dir = tempdir.absFilename() + "/" + mask; FileName const tmpfl = FileName::tempName(tmp_dir); - // FileName::tempName actually creates a file to make sure that it - // stays unique. So we have to delete it before we can create - // a dir with the same name. Note also that we are not thread - // safe because of the gap between unlink and mkdir. (Lgb) - tmpfl.removeFile(); if (tmpfl.empty() || !tmpfl.createDirectory(0700)) { LYXERR0("LyX could not create the temporary directory '" << tmp_dir