#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 <QList>
#include <QTime>
-#include <boost/assert.hpp>
+#include "support/lassert.h"
+#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>
+
+#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 {
}
+bool FileName::isAbsolute() const
+{
+ return d->fi.isAbsolute();
+}
+
+
string FileName::absFilename() const
{
return fromqstr(d->fi.absoluteFilePath());
void FileName::set(string const & name)
{
d->fi.setFile(toqstr(name));
- BOOST_ASSERT(d->fi.isAbsolute());
}
}
-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 "
}
-bool FileName::changePermission(unsigned long int mode) const
+bool FileName::moveTo(FileName const & name) const
{
- if (!isWritable()) {
- LYXERR0("File " << *this << " is not writable!");
- return false;
- }
+ 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 defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
- if (::chmod(file.toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
+ if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
LYXERR0("File " << *this << ": cannot change permission to "
<< mode << ".");
return false;
string FileName::onlyFileName() const
{
- return support::onlyFilename(absFilename());
+ return fromqstr(d->fi.fileName());
+}
+
+
+string FileName::onlyFileNameWithoutExt() const
+{
+ return fromqstr(d->fi.baseName());
}
FileName FileName::onlyPath() const
{
- return FileName(support::onlyPath(absFilename()));
+ FileName path;
+ path.d->fi.setFile(d->fi.path());
+ return path;
}
{
LYXERR(Debug::FILES, "isDirWriteable: " << *this);
- FileName const tmpfl(tempName(*this, "lyxwritetest"));
+ FileName const tmpfl = FileName::tempName(absFilename() + "/lyxwritetest");
if (tmpfl.empty())
return false;
}
-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(".");
}
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)
}
+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;
+ LASSERT(!empty(), /**/);
+ return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
+}
+
+
+bool FileName::createPath() const
+{
+ LASSERT(!empty(), /**/);
+ if (isDirectory())
+ return true;
+
+ QDir dir;
+ bool success = dir.mkpath(d->fi.absoluteFilePath());
+ if (!success)
+ LYXERR0("Cannot create path '" << *this << "'!");
+ return success;
}
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);
+ bool const nameIsAbsolute = isAbsolute();
+ save_abs_path_ = nameIsAbsolute;
+ if (!nameIsAbsolute)
+ FileName::set(makeAbsPath(name, buffer_path).absFilename());
zipped_valid_ = false;
}