#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 = 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());
}
-string FileName::absFilename() const
+string FileName::absFileName() const
{
return d->name;
}
string FileName::realPath() const
{
- return os::real_path(toFilesystemEncoding());
+ return os::real_path(absFileName());
}
void FileName::set(string const & name)
{
- d->name = name;
d->fi.setFile(toqstr(name));
+ d->name = fromqstr(d->fi.absoluteFilePath());
//LYXERR(Debug::FILES, "FileName::set(" << name << ')');
LASSERT(empty() || isAbsolute(d->name), /**/);
}
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());
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;
}
+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;
}
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)
// * 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.
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);
+ ? 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();
}
if (save_abs_path_)
FileName::set(name);
else
- FileName::set(makeAbsPath(name, buffer_path).absFilename());
+ 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());
}