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
This commit is contained in:
Abdelrazak Younes 2008-07-23 05:17:31 +00:00
parent b9dd28b101
commit 8195925175
5 changed files with 7 additions and 72 deletions

View File

@ -422,10 +422,6 @@ void CacheItem::Impl::convertToDisplayFormat()
FileName const to_file_base = FileName::tempName("CacheItem"); FileName const to_file_base = FileName::tempName("CacheItem");
remove_loaded_file_ = true; 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 // Connect a signal to this->imageConverted and pass this signal to
// the graphics converter so that we can load the modified file // the graphics converter so that we can load the modified file
// on completion of the conversion process. // on completion of the conversion process.

View File

@ -295,7 +295,6 @@ static void build_script(FileName const & from_file,
static int counter = 0; static int counter = 0;
string const tmp = "gconvert" + convert<string>(counter++); string const tmp = "gconvert" + convert<string>(counter++);
FileName const to_base = FileName::tempName(tmp); FileName const to_base = FileName::tempName(tmp);
to_base.removeFile();
// Create a copy of the file in case the original name contains // Create a copy of the file in case the original name contains
// problematic characters like ' or ". We can work around that problem // problematic characters like ' or ". We can work around that problem

View File

@ -69,8 +69,6 @@ namespace external {
TempName::TempName() TempName::TempName()
{ {
FileName const tempname = FileName::tempName("lyxext"); FileName const tempname = FileName::tempName("lyxext");
// FIXME: This is unsafe
tempname.removeFile();
// must have an extension for the converter code to work correctly. // must have an extension for the converter code to work correctly.
tempname_ = FileName(tempname.absFilename() + ".tmp"); tempname_ = FileName(tempname.absFilename() + ".tmp");
} }

View File

@ -26,6 +26,7 @@
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QList> #include <QList>
#include <QTemporaryFile>
#include <QTime> #include <QTime>
#include "support/lassert.h" #include "support/lassert.h"
@ -308,14 +309,8 @@ bool FileName::isWritable() const
bool FileName::isDirWritable() const bool FileName::isDirWritable() const
{ {
LYXERR(Debug::FILES, "isDirWriteable: " << *this); LYXERR(Debug::FILES, "isDirWriteable: " << *this);
FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest"); FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest");
return !tmpfl.empty();
if (tmpfl.empty())
return false;
tmpfl.removeFile();
return true;
} }
@ -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 FileName::tempName(string const & mask)
{ {
FileName tmp_name(mask); FileName tmp_name(mask);
@ -387,33 +356,11 @@ FileName FileName::tempName(string const & mask)
else else
tmpfl = package().temp_dir().absFilename() + "/" + mask; tmpfl = package().temp_dir().absFilename() + "/" + mask;
#if defined (HAVE_GETPID) QTemporaryFile qt_tmp(toqstr(tmpfl));
tmpfl += convert<string>(getpid()); if (qt_tmp.open()) {
#elif defined (HAVE__GETPID) tmp_name.d->fi.setFile(qt_tmp.fileName());
tmpfl += convert<string>(_getpid()); LYXERR(Debug::FILES, "Temporary file `" << tmp_name << "' created.");
#else return tmp_name;
# 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);
} }
LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file."); LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
return FileName(); return FileName();

View File

@ -323,11 +323,6 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
string const tmp_dir = tempdir.absFilename() + "/" + mask; string const tmp_dir = tempdir.absFilename() + "/" + mask;
FileName const tmpfl = FileName::tempName(tmp_dir); 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)) { if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
LYXERR0("LyX could not create the temporary directory '" << tmp_dir LYXERR0("LyX could not create the temporary directory '" << tmp_dir