]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Now that exceptions are allowed, handle gracefully the case where a Buffer temp direc...
[lyx.git] / src / support / FileName.cpp
index 1b4a545f3f088e7d01974dfc737feef9862efd41..298ef969fcce60a56ee5e6563bd3a2d29b0d631f 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>
@@ -28,6 +29,7 @@
 #include <QTime>
 
 #include <boost/assert.hpp>
+#include <boost/scoped_array.hpp>
 
 #include <map>
 #include <sstream>
 # include <windows.h>
 #endif
 
-#include <fcntl.h>
 #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;
 
@@ -120,6 +140,12 @@ bool FileName::empty() const
 }
 
 
+bool FileName::isAbsolute() const
+{
+       return d->fi.isAbsolute();
+}
+
+
 string FileName::absFilename() const
 {
        return fromqstr(d->fi.absoluteFilePath());
@@ -173,11 +199,6 @@ bool FileName::moveTo(FileName const & name) const
 
 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 "
@@ -241,13 +262,15 @@ bool FileName::isReadableDirectory() const
 
 string FileName::onlyFileName() const
 {
-       return support::onlyFilename(absFilename());
+       return fromqstr(d->fi.fileName());
 }
 
 
 FileName FileName::onlyPath() const
 {
-       return FileName(support::onlyPath(absFilename()));
+       FileName path;
+       path.d->fi.setFile(d->fi.filePath());
+       return path;
 }
 
 
@@ -267,7 +290,7 @@ bool FileName::isDirWritable() const
 {
        LYXERR(Debug::FILES, "isDirWriteable: " << *this);
 
-       FileName const tmpfl(tempName(*this, "lyxwritetest"));
+       FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest");
 
        if (tmpfl.empty())
                return false;
@@ -310,9 +333,77 @@ FileNameList FileName::dirList(string const & ext) const
 }
 
 
-FileName FileName::tempName(FileName const & dir, 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(string const & 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<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();
+}
+
+
+FileName FileName::getcwd()
 {
-       return support::tempName(dir, mask);
+       return FileName(".");
 }
 
 
@@ -333,7 +424,7 @@ extern unsigned long sum(char const * file);
 unsigned long FileName::checksum() const
 {
        if (!exists()) {
-               LYXERR0("File \"" << absFilename() << "\" does not exist!");
+               //LYXERR0("File \"" << absFilename() << "\" does not exist!");
                return 0;
        }
        // a directory may be passed here so we need to test it. (bug 3622)
@@ -763,8 +854,9 @@ DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
 
 void DocFileName::set(string const & name, string const & buffer_path)
 {
-       save_abs_path_ = absolutePath(name);
-       FileName::set(save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename());
+       FileName::set(name);
+       if (!isAbsolute())
+               FileName::set(makeAbsPath(name, buffer_path).absFilename());
        zipped_valid_ = false;
 }