]> git.lyx.org Git - lyx.git/commitdiff
Replace home made temp file creation with safer (and cleaner) Qt' solution. Should...
authorAbdelrazak Younes <younes@lyx.org>
Wed, 23 Jul 2008 05:17:31 +0000 (05:17 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Wed, 23 Jul 2008 05:17:31 +0000 (05:17 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25822 a592a061-630c-0410-9148-cb99ea01b6c8

src/graphics/GraphicsCacheItem.cpp
src/graphics/GraphicsConverter.cpp
src/insets/InsetExternal.cpp
src/support/FileName.cpp
src/support/filetools.cpp

index 0859b446497b55ff1c3dd5b83e6706233e5a19cf..ff83f6f7d23ccb96b3a4fd9dc19c3ce789cf035d 100644 (file)
@@ -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.
index 4433f82b0f14aeef7fc37a876ed9869a9e380025..6242ed91cb8e4b14e436cffbe4665ff5afc0a559 100644 (file)
@@ -295,7 +295,6 @@ static void build_script(FileName const & from_file,
        static int counter = 0;
        string const tmp = "gconvert" + convert<string>(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
index 0001b3775f4d8f7741a0565fa78e2a5faf2f5c48..1fafc96a6e26500e353513de873a7ebf9f63969b 100644 (file)
@@ -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");
 }
index ecd703b0436087313afda54f29e3685cca51ef54..bd1aac9aaba22c8d1185c59cd9f8f5225005b8b6 100644 (file)
@@ -26,6 +26,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QList>
+#include <QTemporaryFile>
 #include <QTime>
 
 #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<string>(getpid());
-#elif defined (HAVE__GETPID)
-       tmpfl += convert<string>(_getpid());
-#else
-# error No getpid() function
-#endif
-       tmpfl += "XXXXXX";
-
-       // The supposedly safe mkstemp version
-       // FIXME: why not using std::string directly?
-       boost::scoped_array<char> 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();
index aa9d99da0edcc0200452e712a02702cd8bbbbfd3..eb982a2f8ab8af18bf64aa45c0181f71f83048a3 100644 (file)
@@ -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