From: Abdelrazak Younes Date: Sun, 16 Dec 2007 09:52:36 +0000 (+0000) Subject: Simplify FileName::tempName(). X-Git-Tag: 1.6.10~6909 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=86506a80eb29dd5c1f47e11c0e3ef59d0d334a2b;p=features.git Simplify FileName::tempName(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22163 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/Buffer.cpp b/src/Buffer.cpp index f0787ab4ba..c54a987486 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1922,7 +1922,7 @@ int AutoSaveBuffer::generateChild() // anyway. bool failed = false; - FileName const tmp_ret = FileName::tempName(FileName(), "lyxauto"); + FileName const tmp_ret = FileName::tempName("lyxauto"); if (!tmp_ret.empty()) { buffer_.writeFile(tmp_ret); // assume successful write of tmp_ret diff --git a/src/LyXVC.cpp b/src/LyXVC.cpp index 4518a8076c..ff903417fd 100644 --- a/src/LyXVC.cpp +++ b/src/LyXVC.cpp @@ -217,7 +217,7 @@ string const LyXVC::getLogFile() const if (!vcs) return string(); - FileName const tmpf = FileName::tempName(FileName(), "lyxvclog"); + FileName const tmpf = FileName::tempName("lyxvclog"); if (tmpf.empty()) { LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf); return string(); diff --git a/src/graphics/GraphicsCacheItem.cpp b/src/graphics/GraphicsCacheItem.cpp index f9fbce598b..140ed5ccad 100644 --- a/src/graphics/GraphicsCacheItem.cpp +++ b/src/graphics/GraphicsCacheItem.cpp @@ -372,7 +372,7 @@ void CacheItem::Impl::convertToDisplayFormat() FileName filename; zipped_ = filename_.isZippedFile(); if (zipped_) { - unzipped_filename_ = FileName::tempName(FileName(), + unzipped_filename_ = FileName::tempName( filename_.toFilesystemEncoding()); if (unzipped_filename_.empty()) { setStatus(ErrorConverting); @@ -416,7 +416,7 @@ void CacheItem::Impl::convertToDisplayFormat() // Add some stuff to create a uniquely named temporary file. // This file is deleted in loadImage after it is loaded into memory. - FileName const to_file_base = FileName::tempName(FileName(), "CacheItem"); + FileName const to_file_base = FileName::tempName("CacheItem"); remove_loaded_file_ = true; // Remove the temp file, we only want the name... diff --git a/src/graphics/GraphicsConverter.cpp b/src/graphics/GraphicsConverter.cpp index 2898f624f4..f9847d8cdb 100644 --- a/src/graphics/GraphicsConverter.cpp +++ b/src/graphics/GraphicsConverter.cpp @@ -293,7 +293,7 @@ static void build_script(FileName const & from_file, // Remember to remove the temp file because we only want the name... static int counter = 0; string const tmp = "gconvert" + convert(counter++); - FileName const to_base = FileName::tempName(FileName(), tmp); + FileName const to_base = FileName::tempName(tmp); to_base.removeFile(); // Create a copy of the file in case the original name contains diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index a58af2a068..f7f5595ceb 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -65,7 +65,7 @@ namespace external { TempName::TempName() { - FileName const tempname = FileName::tempName(FileName(), "lyxext"); + FileName const tempname = FileName::tempName("lyxext"); // FIXME: This is unsafe tempname.removeFile(); // must have an extension for the converter code to work correctly. diff --git a/src/mathed/MathExtern.cpp b/src/mathed/MathExtern.cpp index 1a9938dbe2..c6447cc68f 100644 --- a/src/mathed/MathExtern.cpp +++ b/src/mathed/MathExtern.cpp @@ -1020,7 +1020,7 @@ namespace { { // In order to avoid parsing problems with command interpreters // we pass input data through a file - FileName const cas_tmpfile = FileName::tempName(FileName(), "casinput"); + FileName const cas_tmpfile = FileName::tempName("casinput"); if (cas_tmpfile.empty()) { lyxerr << "Warning: cannot create temporary file." << endl; diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 1abdd5ff04..f3e73a8bc4 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -288,7 +288,7 @@ bool FileName::isDirWritable() const { LYXERR(Debug::FILES, "isDirWriteable: " << *this); - FileName const tmpfl = FileName::tempName(*this, "lyxwritetest"); + FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest"); if (tmpfl.empty()) return false; @@ -357,11 +357,15 @@ static int make_tempfile(char * templ) } -FileName FileName::tempName(FileName const & dir, string const & mask) +FileName FileName::tempName(string const & mask) { - string const tmpdir = dir.empty() ? - package().temp_dir().absFilename() : dir.absFilename(); - string tmpfl = to_filesystem8bit(from_utf8(addName(tmpdir, mask))); + FileName tmp_name(mask); + string tmpfl; + if (tmp_name.d->fi.isAbsolute()) + tmpfl = mask; + else + tmpfl = package().temp_dir().absFilename() + "/" + mask; + #if defined (HAVE_GETPID) tmpfl += convert(getpid()); #elif defined (HAVE__GETPID) diff --git a/src/support/FileName.h b/src/support/FileName.h index dd3932a2dd..1231916004 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -148,10 +148,11 @@ public: bool isZippedFile() const; static FileName fromFilesystemEncoding(std::string const & name); - /// (securely) create a temporary file in the given dir with the given mask - /// \p mask must be in filesystem encoding - static FileName tempName(FileName const & dir = FileName(), - std::string const & mask = empty_string()); + /// (securely) create a temporary file with the given mask. + /// \p mask must be in filesystem encoding, if it contains a + /// relative path, the template file will be created in the global + /// temporary directory as given by 'package().temp_dir()'. + static FileName tempName(std::string const & mask = empty_string()); /// filename without path std::string onlyFileName() const; diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index f86fe835f6..52b7bbadbb 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -322,7 +322,8 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask) LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n" << "createTmpDir: mask=`" << mask << '\''); - FileName const tmpfl = FileName::tempName(tempdir, mask); + FileName const tmpfl = FileName::tempName(tempdir.absFilename() + + "/" + mask); // 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