#include "support/filetools.h"
#include "support/lassert.h"
#include "support/lstrings.h"
-#include "support/qstring_helpers.h"
+#include "support/mutex.h"
#include "support/os.h"
#include "support/Package.h"
#include "support/qstring_helpers.h"
#include <QTime>
#include <boost/crc.hpp>
-#include <boost/scoped_array.hpp>
#include <algorithm>
#include <iterator>
{
Private() {}
- Private(string const & abs_filename) : fi(toqstr(abs_filename))
+ Private(string const & abs_filename) : fi(toqstr(handleTildeName(abs_filename)))
{
name = fromqstr(fi.absoluteFilePath());
fi.setCaching(fi.exists() ? true : false);
}
///
- inline void refresh()
+ inline void refresh()
{
fi.refresh();
}
-
static
bool isFilesystemEqual(QString const & lhs, QString const & rhs)
{
Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
}
+ static
+ string const handleTildeName(string const & name)
+ {
+ return name == "~" ? Package::get_home_dir().absFileName() :
+ prefixIs(name, "~/") ? Package::get_home_dir().absFileName() + name.substr(1) :
+ name;
+ }
+
/// The absolute file name in UTF-8 encoding.
std::string name;
///
bool FileName::isAbsolute(string const & name)
{
- QFileInfo fi(toqstr(name));
+ QFileInfo fi(toqstr(Private::handleTildeName(name)));
return fi.isAbsolute();
}
void FileName::set(string const & name)
{
- d->fi.setFile(toqstr(name));
+ d->fi.setFile(toqstr(Private::handleTildeName(name)));
d->name = fromqstr(d->fi.absoluteFilePath());
//LYXERR(Debug::FILES, "FileName::set(" << name << ')');
LATTEST(empty() || isAbsolute(d->name));
bool FileName::copyTo(FileName const & name, bool keepsymlink) const
{
FileNameSet visited;
- visited.insert(*this);
return copyTo(name, keepsymlink, visited);
}
{
LYXERR(Debug::FILES, "Copying " << name << " keep symlink: " << keepsymlink);
if (keepsymlink && name.isSymLink()) {
+ visited.insert(*this);
FileName const target(fromqstr(name.d->fi.symLinkTarget()));
if (visited.find(target) != visited.end()) {
LYXERR(Debug::FILES, "Found circular symlink: " << target);
bool FileName::renameTo(FileName const & name) const
{
- LYXERR(Debug::FILES, "Renaming " << name);
+ LYXERR(Debug::FILES, "Renaming " << name << " as " << *this);
bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
if (!success)
LYXERR0("Could not rename file " << *this << " to " << name);
bool FileName::moveTo(FileName const & name) const
{
- LYXERR(Debug::FILES, "Moving " << name);
+ LYXERR(Debug::FILES, "Moving " << *this << " to " << name);
QFile::remove(name.d->fi.absoluteFilePath());
bool success = QFile::rename(d->fi.absoluteFilePath(),
<< mode << ".");
return false;
}
+#else
+ // squash warning
+ (void) mode;
#endif
return true;
}
}
+//QFileInfo caching info might fool this test if file was changed meanwhile.
+//refresh() helps, but we don't want to put it blindly here, because it might
+//trigger slowdown on networked file systems.
bool FileName::isFileEmpty() const
{
LASSERT(!empty(), return true);
}
-static string createTempFile(QString const & mask)
-{
- // FIXME: This is not safe. QTemporaryFile creates a file in open(),
- // but the file is deleted when qt_tmp goes out of scope.
- // Therefore the next call to createTempFile() may create the
- // same file again. To make this safe the QTemporaryFile object
- // needs to be kept for the whole life time of the temp file name.
- // This can be achieved by using the TempFile class.
- QTemporaryFile qt_tmp(mask + ".XXXXXXXXXXXX");
- if (qt_tmp.open()) {
- string const temp_file = fromqstr(qt_tmp.fileName());
- LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
- return temp_file;
- }
- LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
- << qt_tmp.fileTemplate());
- return string();
-}
-
-
-FileName FileName::tempName(FileName const & temp_dir, string const & mask)
-{
- QFileInfo tmp_fi(QDir(temp_dir.d->fi.absoluteFilePath()), toqstr(mask));
- LYXERR(Debug::FILES, "Temporary file in " << tmp_fi.absoluteFilePath());
- return FileName(createTempFile(tmp_fi.absoluteFilePath()));
-}
-
-
-FileName FileName::tempName(string const & mask)
-{
- return tempName(package().temp_dir(), mask);
-}
-
-
FileName FileName::getcwd()
{
// return makeAbsPath("."); would create an infinite loop
}
+bool FileName::link(FileName const & name) const
+{
+ return QFile::link(toqstr(absFileName()), toqstr(name.absFileName()));
+}
+
+
unsigned long checksum_ifstream_fallback(char const * file)
{
unsigned long result = 0;
// FIXME: This code is not ready with Qt4.4.2,
// see http://www.lyx.org/trac/ticket/5293
// FIXME: should we check if the MapExtension extension is supported?
- // see QAbstractFileEngine::supportsExtension() and
+ // see QAbstractFileEngine::supportsExtension() and
// QAbstractFileEngine::MapExtension)
QFile qf(fi.filePath());
if (!qf.open(QIODevice::ReadOnly))
continue;
bool removed;
if (list.at(i).isDir()) {
- LYXERR(Debug::FILES, "Removing dir "
+ LYXERR(Debug::FILES, "Removing dir "
<< fromqstr(list.at(i).absoluteFilePath()));
removed = rmdir(list.at(i));
}
else {
- LYXERR(Debug::FILES, "Removing file "
+ LYXERR(Debug::FILES, "Removing file "
<< fromqstr(list.at(i).absoluteFilePath()));
removed = dir.remove(list.at(i).fileName());
}
LYXERR0("Could not delete "
<< fromqstr(list.at(i).absoluteFilePath()));
}
- }
+ }
QDir parent = fi.absolutePath();
success &= parent.rmdir(fi.fileName());
return success;
// Only used in non Win32 platforms
+#ifndef Q_OS_WIN32
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
+# if HAVE_MKDIR
+# if MKDIR_TAKES_ONE_ARG
// MinGW32
return ::mkdir(pathname);
// FIXME: "Permissions of created directories are ignored on this system."
-# else
+# else
// POSIX
return ::mkdir(pathname, mode_t(mode));
-# endif
-#elif defined(_WIN32)
+# 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
+# elif HAVE__MKDIR
return ::_mkdir(pathname);
// FIXME: "Permissions of created directories are ignored on this system."
-#else
+# else
# error "Don't know how to create a directory on this system."
-#endif
-
+# endif
}
+#endif
bool FileName::createDirectory(int permission) const
LASSERT(!empty(), return false);
#ifdef Q_OS_WIN32
// FIXME: "Permissions of created directories are ignored on this system."
+ (void) permission;
return createPath();
#else
return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
string DocFileName::mangledFileName(string const & dir) const
{
- // FIXME THREAD
// Concurrent access to these variables is possible.
// We need to make sure that every DocFileName instance for a given
// filename returns the same mangled name.
typedef map<string, string> MangledMap;
static MangledMap mangledNames;
+ static Mutex mangledMutex;
+ // this locks both access to mangledNames and counter below
+ Mutex::Locker lock(&mangledMutex);
MangledMap::const_iterator const it = mangledNames.find(absFileName());
if (it != mangledNames.end())
return (*it).second;
// Add the extension back on
mname = support::changeExtension(mname, getExtension(name));
- // FIXME THREAD
// Prepend a counter to the filename. This is necessary to make
// the mangled name unique.
static int counter = 0;