#include "support/FileName.h"
#include "support/FileNameList.h"
-#include "support/convert.h"
#include "support/debug.h"
#include "support/filetools.h"
#include "support/lassert.h"
#include <cerrno>
#include <fcntl.h>
-#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
-
// Three implementations of checksum(), depending on having mmap support or not.
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
#define SUM_WITH_MMAP
#endif // SUM_WITH_MMAP
using namespace std;
+using namespace lyx::support;
// OK, this is ugly, but it is the only workaround I found to compile
// with gcc (any version) on a system which uses a non-GNU toolchain.
Private(string const & abs_filename) : fi(toqstr(abs_filename))
{
+ name = fromqstr(fi.absoluteFilePath());
fi.setCaching(fi.exists() ? true : false);
}
///
inline void refresh()
{
-// There seems to be a bug in Qt >= 4.2.0, at least, that causes problems with
+// There seems to be a bug in Qt >= 4.2.0 and < 4.5.0, that causes problems with
// QFileInfo::refresh() on *nix. So we recreate the object in that case.
-// FIXME: When Trolltech fixes the bug, we will have to replace 0x999999 below
-// with the actual working minimum version.
-#if defined(_WIN32) || (QT_VERSION >= 0x999999)
+#if defined(_WIN32) || (QT_VERSION >= 0x040500)
fi.refresh();
#else
fi = QFileInfo(fi.absoluteFilePath());
Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
}
+ /// The absolute file name in UTF-8 encoding.
+ std::string name;
///
QFileInfo fi;
};
FileName::FileName(string const & abs_filename)
: d(abs_filename.empty() ? new Private : new Private(abs_filename))
{
+ //LYXERR(Debug::FILES, "FileName(" << abs_filename << ')');
+ LASSERT(empty() || isAbsolute(d->name), /**/);
}
FileName::FileName(FileName const & rhs) : d(new Private)
{
+ d->name = rhs.d->name;
d->fi = rhs.d->fi;
}
FileName & FileName::operator=(FileName const & rhs)
{
+ if (&rhs == this)
+ return *this;
+ d->name = rhs.d->name;
d->fi = rhs.d->fi;
return *this;
}
bool FileName::empty() const
{
- return d->fi.absoluteFilePath().isEmpty();
+ return d->name.empty();
}
-bool FileName::isAbsolute() const
+bool FileName::isAbsolute(string const & name)
{
- return d->fi.isAbsolute();
+ QFileInfo fi(toqstr(name));
+ return fi.isAbsolute();
}
-string FileName::absFilename() const
+string FileName::absFileName() const
{
- return fromqstr(d->fi.absoluteFilePath());
+ return d->name;
+}
+
+
+string FileName::realPath() const
+{
+ return os::real_path(absFileName());
}
void FileName::set(string const & name)
{
d->fi.setFile(toqstr(name));
+ d->name = fromqstr(d->fi.absoluteFilePath());
+ //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
+ LASSERT(empty() || isAbsolute(d->name), /**/);
}
d->fi.setFile(rhs.d->fi.filePath() + toqstr(suffix));
else
d->fi.setFile(QDir(rhs.d->fi.absoluteFilePath()), toqstr(suffix));
+ d->name = fromqstr(d->fi.absoluteFilePath());
+ //LYXERR(Debug::FILES, "FileName::set(" << d->name << ')');
+ LASSERT(empty() || isAbsolute(d->name), /**/);
}
void FileName::erase()
{
+ d->name.clear();
d->fi = QFileInfo();
}
string FileName::toFilesystemEncoding() const
{
- // FIXME: This doesn't work on Windows for non ascii file names with Qt < 4.4.
- // Provided that Windows package uses Qt4.4, this isn't a problem.
+ // This doesn't work on Windows for non ascii file names.
QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
return string(encoded.begin(), encoded.end());
}
+string FileName::toSafeFilesystemEncoding(os::file_access how) const
+{
+ // This will work on Windows for non ascii file names.
+ QString const safe_path =
+ toqstr(os::safe_internal_path(absFileName(), how));
+ QByteArray const encoded = QFile::encodeName(safe_path);
+ return string(encoded.begin(), encoded.end());
+}
+
+
FileName FileName::fromFilesystemEncoding(string const & name)
{
QByteArray const encoded(name.c_str(), name.length());
bool FileName::exists() const
{
- return d->fi.exists();
+ return !empty() && d->fi.exists();
}
bool FileName::isSymLink() const
{
- return d->fi.isSymLink();
+ return !empty() && d->fi.isSymLink();
}
bool FileName::isFileEmpty() const
{
+ LASSERT(!empty(), return true);
return d->fi.size() == 0;
}
bool FileName::isDirectory() const
{
- return d->fi.isDir();
+ return !empty() && d->fi.isDir();
}
bool FileName::isReadOnly() const
{
+ LASSERT(!empty(), return true);
return d->fi.isReadable() && !d->fi.isWritable();
}
bool FileName::isReadableDirectory() const
{
- return d->fi.isDir() && d->fi.isReadable();
+ return isDirectory() && d->fi.isReadable();
}
string FileName::onlyFileNameWithoutExt() const
{
- return fromqstr(d->fi.baseName());
+ return fromqstr(d->fi.completeBaseName());
}
string FileName::extension() const
{
- return fromqstr(d->fi.suffix());
+ return fromqstr(d->fi.suffix());
}
FileName FileName::onlyPath() const
{
FileName path;
+ if (empty())
+ return path;
path.d->fi.setFile(d->fi.path());
+ path.d->name = fromqstr(path.d->fi.absoluteFilePath());
return path;
}
bool FileName::isReadableFile() const
{
- return d->fi.isFile() && d->fi.isReadable();
+ return !empty() && d->fi.isFile() && d->fi.isReadable();
}
bool FileName::isWritable() const
{
- return d->fi.isWritable();
+ return !empty() && d->fi.isWritable();
}
bool FileName::isDirWritable() const
{
- LASSERT(d->fi.isDir(), return false);
+ LASSERT(isDirectory(), return false);
QFileInfo tmp(QDir(d->fi.absoluteFilePath()), "lyxwritetest");
QTemporaryFile qt_tmp(tmp.absoluteFilePath());
if (qt_tmp.open()) {
return dirlist;
}
- QDir dir = d->fi.absoluteDir();
+ // If the directory is specified without a trailing '/', absoluteDir()
+ // would return the parent dir, so we must use absoluteFilePath() here.
+ QDir dir = d->fi.absoluteFilePath();
if (!ext.empty()) {
QString filter;
FileName FileName::getcwd()
{
- return FileName(".");
+ // return makeAbsPath("."); would create an infinite loop
+ QFileInfo fi(".");
+ return FileName(fromqstr(fi.absoluteFilePath()));
}
}
+void FileName::refresh() const
+{
+ d->refresh();
+}
+
+
time_t FileName::lastModified() const
{
// QFileInfo caches information about the file. So, in case this file has
}
+unsigned long checksum_ifstream_fallback(char const * file)
+{
+ unsigned long result = 0;
+ //LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
+ ifstream ifs(file, ios_base::in | ios_base::binary);
+ if (!ifs)
+ return result;
+
+ istreambuf_iterator<char> beg(ifs);
+ istreambuf_iterator<char> end;
+ boost::crc_32_type crc;
+ crc = for_each(beg, end, crc);
+ result = crc.checksum();
+ return result;
+}
+
unsigned long FileName::checksum() const
{
unsigned long result = 0;
if (!exists()) {
- //LYXERR0("File \"" << absFilename() << "\" does not exist!");
+ //LYXERR0("File \"" << absFileName() << "\" does not exist!");
return result;
}
// a directory may be passed here so we need to test it. (bug 3622)
if (isDirectory()) {
- LYXERR0('"' << absFilename() << "\" is a directory!");
+ LYXERR0('"' << absFileName() << "\" is a directory!");
return result;
}
#if QT_VERSION >= 0x999999
// First version of checksum uses Qt4.4 mmap support.
// FIXME: This code is not ready with Qt4.4.2,
- // see http://bugzilla.lyx.org/show_bug.cgi?id=5293
+ // see http://www.lyx.org/trac/ticket/5293
// FIXME: should we check if the MapExtension extension is supported?
// see QAbstractFileEngine::supportsExtension() and
// QAbstractFileEngine::MapExtension)
#else // QT_VERSION
- string const encoded = toFilesystemEncoding();
+ string const encoded = toSafeFilesystemEncoding();
char const * file = encoded.c_str();
#ifdef SUM_WITH_MMAP
return result;
struct stat info;
- fstat(fd, &info);
+ if (fstat(fd, &info)){
+ // fstat fails on samba shares (bug 5891)
+ close(fd);
+ return checksum_ifstream_fallback(file);
+ }
void * mm = mmap(0, info.st_size, PROT_READ,
MAP_PRIVATE, fd, 0);
close(fd);
#else // no SUM_WITH_MMAP
-
- //LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
- ifstream ifs(file, ios_base::in | ios_base::binary);
- if (!ifs)
- return result;
-
- istreambuf_iterator<char> beg(ifs);
- istreambuf_iterator<char> end;
- boost::crc_32_type crc;
- crc = for_each(beg, end, crc);
- result = crc.checksum();
-
+ result = checksum_ifstream_fallback(file);
#endif // SUM_WITH_MMAP
#endif // QT_VERSION
- LYXERR(Debug::FILES, "Checksumming \"" << absFilename() << "\" "
+ LYXERR(Debug::FILES, "Checksumming \"" << absFileName() << "\" "
<< result << " lasted " << t.elapsed() << " ms.");
return result;
}
bool FileName::removeFile() const
{
bool const success = QFile::remove(d->fi.absoluteFilePath());
+ d->refresh();
if (!success && exists())
LYXERR0("Could not delete file " << *this);
return success;
bool FileName::createPath() const
{
- LASSERT(!empty(), /**/);
+ LASSERT(!empty(), return false);
LYXERR(Debug::FILES, "creating path '" << *this << "'.");
if (isDirectory())
return false;
docstring FileName::displayName(int threshold) const
{
- return makeDisplayPath(absFilename(), threshold);
+ return makeDisplayPath(absFileName(), threshold);
}
void FileName::changeExtension(string const & extension)
{
// FIXME: use Qt native methods...
- string const oldname = absFilename();
+ string const oldname = absFileName();
string::size_type const last_slash = oldname.rfind('/');
string::size_type last_dot = oldname.rfind('.');
if (last_dot < last_slash && last_slash != string::npos)
format = "fits";
}
+ // Dia knows also compressed form
+ if ((format == "gzip") && (!compare_ascii_no_case(extension(), "dia")))
+ format="dia";
+
if (!format.empty()) {
LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
return format;
}
-bool operator==(FileName const & l, FileName const & r)
+// Note: According to Qt, QFileInfo::operator== is undefined when
+// both files do not exist (Qt4.5 gives true for all non-existent
+// files, while Qt4.4 compares the filenames).
+// see:
+// http://www.qtsoftware.com/developer/task-tracker/
+// index_html?id=248471&method=entry.
+bool equivalent(FileName const & l, FileName const & r)
{
// FIXME: In future use Qt.
// Qt 4.4: We need to solve this warning from Qt documentation:
// * Long and short file names that refer to the same file on Windows are
// treated as if they referred to different files.
// This is supposed to be fixed for Qt5.
- FileName const lhs(os::internal_path(l.absFilename()));
- FileName const rhs(os::internal_path(r.absFilename()));
+ FileName const lhs(os::internal_path(l.absFileName()));
+ FileName const rhs(os::internal_path(r.absFileName()));
if (lhs.empty())
// QFileInfo::operator==() returns false if the two QFileInfo are empty.
lhs.d->refresh();
rhs.d->refresh();
-
+
if (!lhs.d->fi.isSymLink() && !rhs.d->fi.isSymLink()) {
// Qt already checks if the filesystem is case sensitive or not.
- return lhs.d->fi == rhs.d->fi;
+ // see note above why the extra check with fileName is needed.
+ return lhs.d->fi == rhs.d->fi
+ && lhs.d->fi.fileName() == rhs.d->fi.fileName();
}
// FIXME: When/if QFileInfo support symlink comparison, remove this code.
QFileInfo fi2(rhs.d->fi);
if (fi2.isSymLink())
fi2 = QFileInfo(fi2.symLinkTarget());
- return fi1 == fi2;
+ // see note above why the extra check with fileName is needed.
+ return fi1 == fi2 && fi1.fileName() == fi2.fileName();
+}
+
+
+bool operator==(FileName const & lhs, FileName const & rhs)
+{
+ return os::isFilesystemCaseSensitive()
+ ? lhs.absFileName() == rhs.absFileName()
+ : !QString::compare(toqstr(lhs.absFileName()),
+ toqstr(rhs.absFileName()), Qt::CaseInsensitive);
}
bool operator<(FileName const & lhs, FileName const & rhs)
{
- return lhs.absFilename() < rhs.absFilename();
+ return lhs.absFileName() < rhs.absFileName();
}
bool operator>(FileName const & lhs, FileName const & rhs)
{
- return lhs.absFilename() > rhs.absFilename();
+ return lhs.absFileName() > rhs.absFileName();
}
ostream & operator<<(ostream & os, FileName const & filename)
{
- return os << filename.absFilename();
+ return os << filename.absFileName();
}
void DocFileName::set(string const & name, string const & buffer_path)
{
- FileName::set(name);
- bool const nameIsAbsolute = isAbsolute();
- save_abs_path_ = nameIsAbsolute;
- if (!nameIsAbsolute)
- FileName::set(makeAbsPath(name, buffer_path).absFilename());
+ save_abs_path_ = isAbsolute(name);
+ if (save_abs_path_)
+ FileName::set(name);
+ else
+ FileName::set(makeAbsPath(name, buffer_path).absFileName());
zipped_valid_ = false;
}
}
-string DocFileName::relFilename(string const & path) const
+string DocFileName::relFileName(string const & path) const
{
// FIXME UNICODE
return to_utf8(relPath(path));
}
-string DocFileName::outputFilename(string const & path) const
+string DocFileName::outputFileName(string const & path) const
{
- return save_abs_path_ ? absFilename() : relFilename(path);
+ return save_abs_path_ ? absFileName() : relFileName(path);
}
-string DocFileName::mangledFilename(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.
typedef map<string, string> MangledMap;
static MangledMap mangledNames;
- MangledMap::const_iterator const it = mangledNames.find(absFilename());
+ MangledMap::const_iterator const it = mangledNames.find(absFileName());
if (it != mangledNames.end())
return (*it).second;
- string const name = absFilename();
+ string const name = absFileName();
// Now the real work
string mname = os::internal_path(name);
// Remove the extension.
// in the name.
static string const keep = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "+,-0123456789;=";
+ "+-0123456789;=";
string::size_type pos = 0;
while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
mname[pos++] = '_';
}
}
- mangledNames[absFilename()] = mname;
+ mangledNames[absFileName()] = mname;
return mname;
}
}
-string DocFileName::unzippedFilename() const
+string DocFileName::unzippedFileName() const
{
- return unzippedFileName(absFilename());
+ return support::unzippedFileName(absFileName());
}