]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Transfer tempName() implementation to FileName.
[lyx.git] / src / support / FileName.cpp
index 32422b81468efd2bc1ac538ed138e43c5b3229ac..1abdd5ff042adbb96919740c450804ac0c247b25 100644 (file)
 #include "support/FileName.h"
 #include "support/FileNameList.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
+#include "support/Package.h"
 #include "support/qstring_helpers.h"
 
 #include <QDateTime>
 #include <QFile>
 #include <QFileInfo>
 #include <QList>
+#include <QTime>
 
 #include <boost/assert.hpp>
+#include <boost/scoped_array.hpp>
 
 #include <map>
 #include <sstream>
 #ifdef HAVE_SYS_STAT_H
 # include <sys/stat.h>
 #endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+#ifdef HAVE_DIRECT_H
+# include <direct.h>
+#endif
+#ifdef _WIN32
+# include <windows.h>
+#endif
+
 #include <cerrno>
 #include <fcntl.h>
 
 
-using std::map;
-using std::string;
-using std::ifstream;
-using std::ostringstream;
-using std::endl;
+#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;
 
 namespace lyx {
 namespace support {
@@ -64,7 +91,9 @@ struct FileName::Private
        Private() {}
 
        Private(string const & abs_filename) : fi(toqstr(abs_filename))
-       {}
+       {
+               fi.setCaching(fi.exists() ? true : false);
+       }
        ///
        QFileInfo fi;
 };
@@ -131,10 +160,9 @@ void FileName::erase()
 }
 
 
-bool FileName::copyTo(FileName const & name, bool overwrite) const
+bool FileName::copyTo(FileName const & name) const
 {
-       if (overwrite)
-               QFile::remove(name.d->fi.absoluteFilePath());
+       QFile::remove(name.d->fi.absoluteFilePath());
        bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
        if (!success)
                lyxerr << "FileName::copyTo(): Could not copy file "
@@ -143,6 +171,45 @@ bool FileName::copyTo(FileName const & name, bool overwrite) const
 }
 
 
+bool FileName::renameTo(FileName const & name) const
+{
+       bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       if (!success)
+               LYXERR0("Could not rename file " << *this << " to " << name);
+       return success;
+}
+
+
+bool FileName::moveTo(FileName const & name) const
+{
+       QFile::remove(name.d->fi.absoluteFilePath());
+
+       bool success = QFile::rename(d->fi.absoluteFilePath(),
+               name.d->fi.absoluteFilePath());
+       if (!success)
+               LYXERR0("Could not move file " << *this << " to " << name);
+       return success;
+}
+
+
+bool FileName::changePermission(unsigned long int mode) const
+{
+       if (!isWritable()) {
+               LYXERR0("File " << *this << " is not writable!");
+               return false;
+       }
+
+#if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
+       if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
+               LYXERR0("File " << *this << ": cannot change permission to "
+                       << mode << ".");
+               return false;
+       }
+#endif
+       return true;
+}
+
+
 string FileName::toFilesystemEncoding() const
 {
        QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
@@ -193,7 +260,7 @@ bool FileName::isReadableDirectory() const
 }
 
 
-std::string FileName::onlyFileName() const
+string FileName::onlyFileName() const
 {
        return support::onlyFilename(absFilename());
 }
@@ -221,7 +288,7 @@ bool FileName::isDirWritable() const
 {
        LYXERR(Debug::FILES, "isDirWriteable: " << *this);
 
-       FileName const tmpfl(tempName(*this, "lyxwritetest"));
+       FileName const tmpfl = FileName::tempName(*this, "lyxwritetest");
 
        if (tmpfl.empty())
                return false;
@@ -231,7 +298,7 @@ bool FileName::isDirWritable() const
 }
 
 
-FileNameList FileName::dirList(std::string const & ext) const
+FileNameList FileName::dirList(string const & ext) const
 {
        FileNameList dirlist;
        if (!isDirectory()) {
@@ -264,13 +331,71 @@ FileNameList FileName::dirList(std::string const & ext) const
 }
 
 
-FileName FileName::tempName(FileName const & dir, std::string const & mask)
+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)
 {
-       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();
 }
 
 
-std::time_t FileName::lastModified() const
+time_t FileName::lastModified() const
 {
        return d->fi.lastModified().toTime_t();
 }
@@ -295,8 +420,15 @@ unsigned long FileName::checksum() const
                LYXERR0('"' << absFilename() << "\" is a directory!");
                return 0;
        }
-       LYXERR0("Checksumming \"" << absFilename() << "\".");
-       return sum(absFilename().c_str());
+       if (!lyxerr.debugging(Debug::FILES))
+               return sum(absFilename().c_str());
+
+       QTime t;
+       t.start();
+       unsigned long r = sum(absFilename().c_str());
+       lyxerr << "Checksumming \"" << absFilename() << "\" lasted "
+               << t.elapsed() << " ms." << endl;
+       return r;
 }
 
 
@@ -352,10 +484,50 @@ bool FileName::destroyDirectory() const
 }
 
 
+static int mymkdir(char const * pathname, unsigned long int mode)
+{
+       // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
+#if HAVE_MKDIR
+# if MKDIR_TAKES_ONE_ARG
+       // MinGW32
+       return ::mkdir(pathname);
+       // FIXME: "Permissions of created directories are ignored on this system."
+# else
+       // POSIX
+       return ::mkdir(pathname, mode_t(mode));
+# endif
+#elif defined(_WIN32)
+       // plain Windows 32
+       return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
+       // FIXME: "Permissions of created directories are ignored on this system."
+#elif HAVE__MKDIR
+       return ::_mkdir(pathname);
+       // FIXME: "Permissions of created directories are ignored on this system."
+#else
+#   error "Don't know how to create a directory on this system."
+#endif
+
+}
+
+
 bool FileName::createDirectory(int permission) const
 {
        BOOST_ASSERT(!empty());
-       return mkdir(*this, permission) == 0;
+       return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
+}
+
+
+bool FileName::createPath() const
+{
+       BOOST_ASSERT(!empty());
+       if (isDirectory())
+               return true;
+
+       QDir dir;
+       bool success = dir.mkpath(d->fi.absoluteFilePath());
+       if (!success)
+               LYXERR0("Cannot create path '" << *this << "'!");
+       return success;
 }
 
 
@@ -407,7 +579,7 @@ docstring FileName::fileContents(string const & encoding) const
 }
 
 
-void FileName::changeExtension(std::string const & extension)
+void FileName::changeExtension(string const & extension)
 {
        // FIXME: use Qt native methods...
        string const oldname = absFilename();
@@ -640,7 +812,7 @@ bool operator>(FileName const & lhs, FileName const & rhs)
 }
 
 
-std::ostream & operator<<(std::ostream & os, FileName const & filename)
+ostream & operator<<(ostream & os, FileName const & filename)
 {
        return os << filename.absFilename();
 }
@@ -696,7 +868,7 @@ string DocFileName::outputFilename(string const & path) const
 }
 
 
-string DocFileName::mangledFilename(std::string const & dir) const
+string DocFileName::mangledFilename(string const & dir) const
 {
        // We need to make sure that every DocFileName instance for a given
        // filename returns the same mangled name.
@@ -730,7 +902,7 @@ string DocFileName::mangledFilename(std::string const & dir) const
        // Prepend a counter to the filename. This is necessary to make
        // the mangled name unique.
        static int counter = 0;
-       std::ostringstream s;
+       ostringstream s;
        s << counter++ << mname;
        mname = s.str();
 
@@ -740,7 +912,7 @@ string DocFileName::mangledFilename(std::string const & dir) const
        // If dir.size() > max length, all bets are off for YAP. We truncate
        // the filename nevertheless, keeping a minimum of 10 chars.
 
-       string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
+       string::size_type max_length = max(100 - ((int)dir.size() + 1), 10);
 
        // If the mangled file name is too long, hack it to fit.
        // We know we're guaranteed to have a unique file name because