Transfer tempName() implementation to FileName.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22161 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-12-16 07:52:38 +00:00
parent 1948c5812c
commit 4020357dd2
12 changed files with 95 additions and 141 deletions

View File

@ -615,7 +615,7 @@ bool Buffer::readString(string const & s)
Lexer lex(0, 0); Lexer lex(0, 0);
istringstream is(s); istringstream is(s);
lex.setStream(is); lex.setStream(is);
FileName const name(tempName()); FileName const name = FileName::tempName();
switch (readFile(lex, name, true)) { switch (readFile(lex, name, true)) {
case failure: case failure:
return false; return false;
@ -739,7 +739,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
// lyx2lyx would fail // lyx2lyx would fail
return wrongversion; return wrongversion;
FileName const tmpfile(tempName()); FileName const tmpfile = FileName::tempName();
if (tmpfile.empty()) { if (tmpfile.empty()) {
Alert::error(_("Conversion failed"), Alert::error(_("Conversion failed"),
bformat(_("%1$s is from a different" bformat(_("%1$s is from a different"
@ -1922,7 +1922,7 @@ int AutoSaveBuffer::generateChild()
// anyway. // anyway.
bool failed = false; bool failed = false;
FileName const tmp_ret(tempName(FileName(), "lyxauto")); FileName const tmp_ret = FileName::tempName(FileName(), "lyxauto");
if (!tmp_ret.empty()) { if (!tmp_ret.empty()) {
buffer_.writeFile(tmp_ret); buffer_.writeFile(tmp_ret);
// assume successful write of tmp_ret // assume successful write of tmp_ret

View File

@ -217,7 +217,7 @@ string const LyXVC::getLogFile() const
if (!vcs) if (!vcs)
return string(); return string();
FileName const tmpf(tempName(FileName(), "lyxvclog")); FileName const tmpf = FileName::tempName(FileName(), "lyxvclog");
if (tmpf.empty()) { if (tmpf.empty()) {
LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf); LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
return string(); return string();

View File

@ -433,7 +433,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
if (format != FORMAT) { if (format != FORMAT) {
LYXERR(Debug::TCLASS, "Converting layout file from format " LYXERR(Debug::TCLASS, "Converting layout file from format "
<< format << " to " << FORMAT); << format << " to " << FORMAT);
FileName const tempfile(tempName()); FileName const tempfile = FileName::tempName();
error = !layout2layout(filename, tempfile); error = !layout2layout(filename, tempfile);
if (!error) if (!error)
error = read(tempfile, rt); error = read(tempfile, rt);

View File

@ -372,7 +372,8 @@ void CacheItem::Impl::convertToDisplayFormat()
FileName filename; FileName filename;
zipped_ = filename_.isZippedFile(); zipped_ = filename_.isZippedFile();
if (zipped_) { if (zipped_) {
unzipped_filename_ = tempName(FileName(), filename_.toFilesystemEncoding()); unzipped_filename_ = FileName::tempName(FileName(),
filename_.toFilesystemEncoding());
if (unzipped_filename_.empty()) { if (unzipped_filename_.empty()) {
setStatus(ErrorConverting); setStatus(ErrorConverting);
LYXERR(Debug::GRAPHICS, "\tCould not create temporary file."); LYXERR(Debug::GRAPHICS, "\tCould not create temporary file.");
@ -415,7 +416,7 @@ void CacheItem::Impl::convertToDisplayFormat()
// Add some stuff to create a uniquely named temporary file. // Add some stuff to create a uniquely named temporary file.
// This file is deleted in loadImage after it is loaded into memory. // This file is deleted in loadImage after it is loaded into memory.
FileName const to_file_base(tempName(FileName(), "CacheItem")); FileName const to_file_base = FileName::tempName(FileName(), "CacheItem");
remove_loaded_file_ = true; remove_loaded_file_ = true;
// Remove the temp file, we only want the name... // Remove the temp file, we only want the name...

View File

@ -293,7 +293,7 @@ static void build_script(FileName const & from_file,
// Remember to remove the temp file because we only want the name... // Remember to remove the temp file because we only want the name...
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(tempName(FileName(), tmp)); FileName const to_base = FileName::tempName(FileName(), tmp);
to_base.removeFile(); 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

View File

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

View File

@ -1020,7 +1020,7 @@ namespace {
{ {
// In order to avoid parsing problems with command interpreters // In order to avoid parsing problems with command interpreters
// we pass input data through a file // we pass input data through a file
FileName const cas_tmpfile(tempName(FileName(), "casinput")); FileName const cas_tmpfile = FileName::tempName(FileName(), "casinput");
if (cas_tmpfile.empty()) { if (cas_tmpfile.empty()) {
lyxerr << "Warning: cannot create temporary file." lyxerr << "Warning: cannot create temporary file."
<< endl; << endl;

View File

@ -13,11 +13,13 @@
#include "support/FileName.h" #include "support/FileName.h"
#include "support/FileNameList.h" #include "support/FileNameList.h"
#include "support/convert.h"
#include "support/debug.h" #include "support/debug.h"
#include "support/filetools.h" #include "support/filetools.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/lyxlib.h" #include "support/lyxlib.h"
#include "support/os.h" #include "support/os.h"
#include "support/Package.h"
#include "support/qstring_helpers.h" #include "support/qstring_helpers.h"
#include <QDateTime> #include <QDateTime>
@ -28,6 +30,7 @@
#include <QTime> #include <QTime>
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/scoped_array.hpp>
#include <map> #include <map>
#include <sstream> #include <sstream>
@ -50,8 +53,26 @@
# include <windows.h> # include <windows.h>
#endif #endif
#include <fcntl.h>
#include <cerrno> #include <cerrno>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
extern "C" int mkstemp(char *);
#endif
#if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
# ifdef HAVE_IO_H
# include <io.h>
# endif
# ifdef HAVE_PROCESS_H
# include <process.h>
# endif
#endif
using namespace std; using namespace std;
@ -267,7 +288,7 @@ bool FileName::isDirWritable() const
{ {
LYXERR(Debug::FILES, "isDirWriteable: " << *this); LYXERR(Debug::FILES, "isDirWriteable: " << *this);
FileName const tmpfl(tempName(*this, "lyxwritetest")); FileName const tmpfl = FileName::tempName(*this, "lyxwritetest");
if (tmpfl.empty()) if (tmpfl.empty())
return false; return false;
@ -310,9 +331,67 @@ 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(FileName const & dir, string const & mask) FileName FileName::tempName(FileName const & dir, string const & mask)
{ {
return support::tempName(dir, mask); string const tmpdir = dir.empty() ?
package().temp_dir().absFilename() : dir.absFilename();
string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, 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);
}
LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file.");
return FileName();
} }

View File

@ -93,7 +93,6 @@ liblyxsupport_la_SOURCES = \
SignalSlot.cpp \ SignalSlot.cpp \
SignalSlot.h \ SignalSlot.h \
SignalSlotPrivate.h \ SignalSlotPrivate.h \
tempname.cpp \
textutils.h \ textutils.h \
Translator.h \ Translator.h \
Timeout.cpp \ Timeout.cpp \

View File

@ -322,8 +322,8 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n" LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
<< "createTmpDir: mask=`" << mask << '\''); << "createTmpDir: mask=`" << mask << '\'');
FileName const tmpfl(tempName(tempdir, mask)); FileName const tmpfl = FileName::tempName(tempdir, mask);
// lyx::tempName actually creates a file to make sure that it // FileName::tempName actually creates a file to make sure that it
// stays unique. So we have to delete it before we can create // 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 // a dir with the same name. Note also that we are not thread
// safe because of the gap between unlink and mkdir. (Lgb) // safe because of the gap between unlink and mkdir. (Lgb)

View File

@ -31,11 +31,6 @@ int kill(int pid, int sig);
/// FIXME: same here /// FIXME: same here
void abort(); void abort();
/// (securely) create a temporary file in the given dir with the given mask
/// \p mask must be in filesystem encoding
FileName const tempName(FileName const & dir = FileName(),
std::string const & mask = std::string());
/** /**
* Returns true if var is approximately equal to number with allowed error * Returns true if var is approximately equal to number with allowed error

View File

@ -1,120 +0,0 @@
/**
* \file tempname.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "support/lyxlib.h"
#include "support/convert.h"
#include "support/debug.h"
#include "support/filetools.h"
#include "support/Package.h"
#include <boost/scoped_array.hpp>
#include <cstdlib>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
extern "C" int mkstemp(char *);
#endif
#if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
# include <fcntl.h>
# ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
# endif
# ifdef HAVE_IO_H
# include <io.h>
# endif
# ifdef HAVE_PROCESS_H
# include <process.h>
# endif
#endif
using namespace std;
using boost::scoped_array;
namespace lyx {
namespace support {
namespace {
inline
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
}
} // namespace anon
FileName const tempName(FileName const & dir, string const & mask)
{
string const tmpdir = dir.empty()
? package().temp_dir().absFilename() : dir.absFilename();
string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, 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
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.");
return FileName();
}
} // namespace support
} // namespace lyx