3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "support/FileName.h"
14 #include "support/FileNameList.h"
16 #include "support/convert.h"
17 #include "support/debug.h"
18 #include "support/filetools.h"
19 #include "support/lassert.h"
20 #include "support/lstrings.h"
21 #include "support/qstring_helpers.h"
22 #include "support/os.h"
23 #include "support/Package.h"
24 #include "support/qstring_helpers.h"
31 #include <QTemporaryFile>
34 #include <boost/crc.hpp>
35 #include <boost/scoped_array.hpp>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
63 #if defined(HAVE_MKSTEMP) && ! defined(HAVE_DECL_MKSTEMP)
64 extern "C" int mkstemp(char *);
67 #if !defined(HAVE_MKSTEMP) && defined(HAVE_MKTEMP)
71 # ifdef HAVE_PROCESS_H
76 // Three implementations of checksum(), depending on having mmap support or not.
77 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
80 #endif // SUM_WITH_MMAP
84 // OK, this is ugly, but it is the only workaround I found to compile
85 // with gcc (any version) on a system which uses a non-GNU toolchain.
86 // The problem is that gcc uses a weak symbol for a particular
87 // instantiation and that the system linker usually does not
88 // understand those weak symbols (seen on HP-UX, tru64, AIX and
89 // others). Thus we force an explicit instanciation of this particular
91 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
96 /////////////////////////////////////////////////////////////////////
100 /////////////////////////////////////////////////////////////////////
102 struct FileName::Private
106 Private(string const & abs_filename) : fi(toqstr(abs_filename))
108 fi.setCaching(fi.exists() ? true : false);
111 inline void refresh()
113 // There seems to be a bug in Qt >= 4.2.0, at least, that causes problems with
114 // QFileInfo::refresh() on *nix. So we recreate the object in that case.
115 // FIXME: When Trolltech fixes the bug, we will have to replace 0x999999 below
116 // with the actual working minimum version.
117 #if defined(_WIN32) || (QT_VERSION >= 0x999999)
120 fi = QFileInfo(fi.absoluteFilePath());
126 bool isFilesystemEqual(QString const & lhs, QString const & rhs)
128 return QString::compare(lhs, rhs, os::isFilesystemCaseSensitive() ?
129 Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
136 /////////////////////////////////////////////////////////////////////
140 /////////////////////////////////////////////////////////////////////
143 FileName::FileName() : d(new Private)
148 FileName::FileName(string const & abs_filename)
149 : d(abs_filename.empty() ? new Private : new Private(abs_filename))
154 FileName::~FileName()
160 FileName::FileName(FileName const & rhs) : d(new Private)
166 FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private)
172 FileName & FileName::operator=(FileName const & rhs)
179 bool FileName::empty() const
181 return d->fi.absoluteFilePath().isEmpty();
185 bool FileName::isAbsolute() const
187 return d->fi.isAbsolute();
191 string FileName::absFilename() const
193 return fromqstr(d->fi.absoluteFilePath());
197 string FileName::realPath() const
199 return os::real_path(toFilesystemEncoding());
203 void FileName::set(string const & name)
205 d->fi.setFile(toqstr(name));
209 void FileName::set(FileName const & rhs, string const & suffix)
211 if (!rhs.d->fi.isDir())
212 d->fi.setFile(rhs.d->fi.filePath() + toqstr(suffix));
214 d->fi.setFile(QDir(rhs.d->fi.absoluteFilePath()), toqstr(suffix));
218 void FileName::erase()
224 bool FileName::copyTo(FileName const & name) const
226 LYXERR(Debug::FILES, "Copying " << name);
227 QFile::remove(name.d->fi.absoluteFilePath());
228 bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
230 LYXERR0("FileName::copyTo(): Could not copy file "
231 << *this << " to " << name);
236 bool FileName::renameTo(FileName const & name) const
238 bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
240 LYXERR0("Could not rename file " << *this << " to " << name);
245 bool FileName::moveTo(FileName const & name) const
247 QFile::remove(name.d->fi.absoluteFilePath());
249 bool success = QFile::rename(d->fi.absoluteFilePath(),
250 name.d->fi.absoluteFilePath());
252 LYXERR0("Could not move file " << *this << " to " << name);
257 bool FileName::changePermission(unsigned long int mode) const
259 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
260 if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
261 LYXERR0("File " << *this << ": cannot change permission to "
270 string FileName::toFilesystemEncoding() const
272 // FIXME: This doesn't work on Windows for non ascii file names with Qt < 4.4.
273 // Provided that Windows package uses Qt4.4, this isn't a problem.
274 QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
275 return string(encoded.begin(), encoded.end());
279 FileName FileName::fromFilesystemEncoding(string const & name)
281 QByteArray const encoded(name.c_str(), name.length());
282 return FileName(fromqstr(QFile::decodeName(encoded)));
286 bool FileName::exists() const
288 return d->fi.exists();
292 bool FileName::isSymLink() const
294 return d->fi.isSymLink();
298 bool FileName::isFileEmpty() const
300 return d->fi.size() == 0;
304 bool FileName::isDirectory() const
306 return d->fi.isDir();
310 bool FileName::isReadOnly() const
312 return d->fi.isReadable() && !d->fi.isWritable();
316 bool FileName::isReadableDirectory() const
318 return d->fi.isDir() && d->fi.isReadable();
322 string FileName::onlyFileName() const
324 return fromqstr(d->fi.fileName());
328 string FileName::onlyFileNameWithoutExt() const
330 return fromqstr(d->fi.completeBaseName());
334 string FileName::extension() const
336 return fromqstr(d->fi.suffix());
340 bool FileName::hasExtension(const string & ext)
342 return Private::isFilesystemEqual(d->fi.suffix(), toqstr(ext));
346 FileName FileName::onlyPath() const
349 path.d->fi.setFile(d->fi.path());
354 bool FileName::isReadableFile() const
356 return d->fi.isFile() && d->fi.isReadable();
360 bool FileName::isWritable() const
362 return d->fi.isWritable();
366 bool FileName::isDirWritable() const
368 LASSERT(d->fi.isDir(), return false);
369 QFileInfo tmp(QDir(d->fi.absoluteFilePath()), "lyxwritetest");
370 QTemporaryFile qt_tmp(tmp.absoluteFilePath());
372 LYXERR(Debug::FILES, "Directory " << *this << " is writable");
375 LYXERR(Debug::FILES, "Directory " << *this << " is not writable");
380 FileNameList FileName::dirList(string const & ext) const
382 FileNameList dirlist;
383 if (!isDirectory()) {
384 LYXERR0("Directory '" << *this << "' does not exist!");
388 QDir dir = d->fi.absoluteDir();
393 case '.': filter = "*" + toqstr(ext); break;
394 case '*': filter = toqstr(ext); break;
395 default: filter = "*." + toqstr(ext);
397 dir.setNameFilters(QStringList(filter));
398 LYXERR(Debug::FILES, "filtering on extension "
399 << fromqstr(filter) << " is requested.");
402 QFileInfoList list = dir.entryInfoList();
403 for (int i = 0; i != list.size(); ++i) {
404 FileName fi(fromqstr(list.at(i).absoluteFilePath()));
405 dirlist.push_back(fi);
406 LYXERR(Debug::FILES, "found file " << fi);
413 static string createTempFile(QString const & mask)
415 QTemporaryFile qt_tmp(mask);
417 string const temp_file = fromqstr(qt_tmp.fileName());
418 LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
421 LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
422 << qt_tmp.fileTemplate());
427 FileName FileName::tempName(FileName const & temp_dir, string const & mask)
429 QFileInfo tmp_fi(QDir(temp_dir.d->fi.absoluteFilePath()), toqstr(mask));
430 LYXERR(Debug::FILES, "Temporary file in " << tmp_fi.absoluteFilePath());
431 return FileName(createTempFile(tmp_fi.absoluteFilePath()));
435 FileName FileName::tempName(string const & mask)
437 return tempName(package().temp_dir(), mask);
441 FileName FileName::getcwd()
443 return FileName(".");
447 FileName FileName::tempPath()
449 return FileName(os::internal_path(fromqstr(QDir::tempPath())));
453 time_t FileName::lastModified() const
455 // QFileInfo caches information about the file. So, in case this file has
456 // been touched between the object creation and now, we refresh the file
459 return d->fi.lastModified().toTime_t();
463 bool FileName::chdir() const
465 return QDir::setCurrent(d->fi.absoluteFilePath());
469 unsigned long FileName::checksum() const
471 unsigned long result = 0;
474 //LYXERR0("File \"" << absFilename() << "\" does not exist!");
477 // a directory may be passed here so we need to test it. (bug 3622)
479 LYXERR0('"' << absFilename() << "\" is a directory!");
483 // This is used in the debug output at the end of the method.
485 if (lyxerr.debugging(Debug::FILES))
488 #if QT_VERSION >= 0x999999
489 // First version of checksum uses Qt4.4 mmap support.
490 // FIXME: This code is not ready with Qt4.4.2,
491 // see http://bugzilla.lyx.org/show_bug.cgi?id=5293
492 // FIXME: should we check if the MapExtension extension is supported?
493 // see QAbstractFileEngine::supportsExtension() and
494 // QAbstractFileEngine::MapExtension)
495 QFile qf(fi.filePath());
496 if (!qf.open(QIODevice::ReadOnly))
498 qint64 size = fi.size();
499 uchar * ubeg = qf.map(0, size);
500 uchar * uend = ubeg + size;
501 boost::crc_32_type ucrc;
502 ucrc.process_block(ubeg, uend);
505 result = ucrc.checksum();
509 string const encoded = toFilesystemEncoding();
510 char const * file = encoded.c_str();
513 //LYXERR(Debug::FILES, "using mmap (lightning fast)");
515 int fd = open(file, O_RDONLY);
522 void * mm = mmap(0, info.st_size, PROT_READ,
524 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
525 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
530 char * beg = static_cast<char*>(mm);
531 char * end = beg + info.st_size;
533 boost::crc_32_type crc;
534 crc.process_block(beg, end);
535 result = crc.checksum();
537 munmap(mm, info.st_size);
540 #else // no SUM_WITH_MMAP
542 //LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
543 ifstream ifs(file, ios_base::in | ios_base::binary);
547 istreambuf_iterator<char> beg(ifs);
548 istreambuf_iterator<char> end;
549 boost::crc_32_type crc;
550 crc = for_each(beg, end, crc);
551 result = crc.checksum();
553 #endif // SUM_WITH_MMAP
556 LYXERR(Debug::FILES, "Checksumming \"" << absFilename() << "\" "
557 << result << " lasted " << t.elapsed() << " ms.");
562 bool FileName::removeFile() const
564 bool const success = QFile::remove(d->fi.absoluteFilePath());
566 if (!success && exists())
567 LYXERR0("Could not delete file " << *this);
572 static bool rmdir(QFileInfo const & fi)
574 QDir dir(fi.absoluteFilePath());
575 QFileInfoList list = dir.entryInfoList();
577 for (int i = 0; i != list.size(); ++i) {
578 if (list.at(i).fileName() == ".")
580 if (list.at(i).fileName() == "..")
583 if (list.at(i).isDir()) {
584 LYXERR(Debug::FILES, "Removing dir "
585 << fromqstr(list.at(i).absoluteFilePath()));
586 removed = rmdir(list.at(i));
589 LYXERR(Debug::FILES, "Removing file "
590 << fromqstr(list.at(i).absoluteFilePath()));
591 removed = dir.remove(list.at(i).fileName());
595 LYXERR0("Could not delete "
596 << fromqstr(list.at(i).absoluteFilePath()));
599 QDir parent = fi.absolutePath();
600 success &= parent.rmdir(fi.fileName());
605 bool FileName::destroyDirectory() const
607 bool const success = rmdir(d->fi);
609 LYXERR0("Could not delete " << *this);
615 // Only used in non Win32 platforms
616 static int mymkdir(char const * pathname, unsigned long int mode)
618 // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
620 # if MKDIR_TAKES_ONE_ARG
622 return ::mkdir(pathname);
623 // FIXME: "Permissions of created directories are ignored on this system."
626 return ::mkdir(pathname, mode_t(mode));
628 #elif defined(_WIN32)
630 return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
631 // FIXME: "Permissions of created directories are ignored on this system."
633 return ::_mkdir(pathname);
634 // FIXME: "Permissions of created directories are ignored on this system."
636 # error "Don't know how to create a directory on this system."
642 bool FileName::createDirectory(int permission) const
644 LASSERT(!empty(), return false);
646 // FIXME: "Permissions of created directories are ignored on this system."
649 return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
654 bool FileName::createPath() const
656 LASSERT(!empty(), /**/);
657 LYXERR(Debug::FILES, "creating path '" << *this << "'.");
662 bool success = dir.mkpath(d->fi.absoluteFilePath());
664 LYXERR0("Cannot create path '" << *this << "'!");
669 docstring const FileName::absoluteFilePath() const
671 return qstring_to_ucs4(d->fi.absoluteFilePath());
675 docstring FileName::displayName(int threshold) const
677 return makeDisplayPath(absFilename(), threshold);
681 docstring FileName::fileContents(string const & encoding) const
683 if (!isReadableFile()) {
684 LYXERR0("File '" << *this << "' is not redable!");
688 QFile file(d->fi.absoluteFilePath());
689 if (!file.open(QIODevice::ReadOnly)) {
690 LYXERR0("File '" << *this
691 << "' could not be opened in read only mode!");
694 QByteArray contents = file.readAll();
697 if (contents.isEmpty()) {
698 LYXERR(Debug::FILES, "File '" << *this
699 << "' is either empty or some error happened while reading it.");
704 if (encoding.empty() || encoding == "UTF-8")
705 s = QString::fromUtf8(contents.data());
706 else if (encoding == "ascii")
707 s = QString::fromAscii(contents.data());
708 else if (encoding == "local8bit")
709 s = QString::fromLocal8Bit(contents.data());
710 else if (encoding == "latin1")
711 s = QString::fromLatin1(contents.data());
713 return qstring_to_ucs4(s);
717 void FileName::changeExtension(string const & extension)
719 // FIXME: use Qt native methods...
720 string const oldname = absFilename();
721 string::size_type const last_slash = oldname.rfind('/');
722 string::size_type last_dot = oldname.rfind('.');
723 if (last_dot < last_slash && last_slash != string::npos)
724 last_dot = string::npos;
727 // Make sure the extension starts with a dot
728 if (!extension.empty() && extension[0] != '.')
729 ext= '.' + extension;
733 set(oldname.substr(0, last_dot) + ext);
737 string FileName::guessFormatFromContents() const
739 // the different filetypes and what they contain in one of the first lines
740 // (dots are any characters). (Herbert 20020131)
743 // EPS %!PS-Adobe-3.0 EPSF...
750 // PBM P1... or P4 (B/W)
751 // PGM P2... or P5 (Grayscale)
752 // PPM P3... or P6 (color)
753 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
754 // SGI \001\332... (decimal 474)
756 // TIFF II... or MM...
758 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
759 // ...static char *...
760 // XWD \000\000\000\151 (0x00006900) decimal 105
762 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
763 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
764 // Z \037\235 UNIX compress
767 if (empty() || !isReadableFile())
770 ifstream ifs(toFilesystemEncoding().c_str());
772 // Couldn't open file...
776 static string const gzipStamp = "\037\213";
779 static string const zipStamp = "PK";
782 static string const compressStamp = "\037\235";
784 // Maximum strings to read
785 int const max_count = 50;
790 bool firstLine = true;
791 while ((count++ < max_count) && format.empty()) {
793 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
794 << "\tFile type not recognised before EOF!");
799 string const stamp = str.substr(0, 2);
800 if (firstLine && str.size() >= 2) {
801 // at first we check for a zipped file, because this
802 // information is saved in the first bytes of the file!
803 // also some graphic formats which save the information
804 // in the first line, too.
805 if (prefixIs(str, gzipStamp)) {
808 } else if (stamp == zipStamp) {
811 } else if (stamp == compressStamp) {
815 } else if (stamp == "BM") {
818 } else if (stamp == "\001\332") {
822 // Don't need to use str.at(0), str.at(1) because
823 // we already know that str.size() >= 2
824 } else if (str[0] == 'P') {
840 } else if ((stamp == "II") || (stamp == "MM")) {
843 } else if (prefixIs(str,"%TGIF")) {
846 } else if (prefixIs(str,"#FIG")) {
849 } else if (prefixIs(str,"GIF")) {
852 } else if (str.size() > 3) {
853 int const c = ((str[0] << 24) & (str[1] << 16) &
854 (str[2] << 8) & str[3]);
865 else if (contains(str,"EPSF"))
866 // dummy, if we have wrong file description like
867 // %!PS-Adobe-2.0EPSF"
870 else if (contains(str, "Grace"))
873 else if (contains(str, "JFIF"))
876 else if (contains(str, "%PDF"))
879 else if (contains(str, "PNG"))
882 else if (contains(str, "%!PS-Adobe")) {
885 if (contains(str,"EPSF"))
891 else if (contains(str, "_bits[]"))
894 else if (contains(str, "XPM") || contains(str, "static char *"))
897 else if (contains(str, "BITPIX"))
901 // Dia knows also compressed form
902 if ((format == "gzip") && (!compare_ascii_no_case(extension(), "dia")))
905 if (!format.empty()) {
906 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
910 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
911 << "\tCouldn't find a known format!");
916 bool FileName::isZippedFile() const
918 string const type = guessFormatFromContents();
919 return contains("gzip zip compress", type) && !type.empty();
923 docstring const FileName::relPath(string const & path) const
926 return makeRelPath(absoluteFilePath(), from_utf8(path));
930 bool operator==(FileName const & lhs, FileName const & rhs)
932 // Avoid unnecessary checks below
933 if (lhs.empty() || rhs.empty())
934 return lhs.empty() && rhs.empty();
936 // Firstly, compare the filenames.
937 if (QString::compare(toqstr(lhs.absFilename()),
938 toqstr(rhs.absFilename()),
939 os::isFilesystemCaseSensitive() ?
940 Qt::CaseSensitive : Qt::CaseInsensitive) == 0) {
944 // They don't match, so check whether they point to the same file.
945 return os::isSameFile(lhs.toFilesystemEncoding(), rhs.toFilesystemEncoding());
949 bool operator!=(FileName const & lhs, FileName const & rhs)
951 return !(operator==(lhs, rhs));
955 bool operator<(FileName const & lhs, FileName const & rhs)
957 return lhs.absFilename() < rhs.absFilename();
961 bool operator>(FileName const & lhs, FileName const & rhs)
963 return lhs.absFilename() > rhs.absFilename();
967 ostream & operator<<(ostream & os, FileName const & filename)
969 return os << filename.absFilename();
973 /////////////////////////////////////////////////////////////////////
977 /////////////////////////////////////////////////////////////////////
980 DocFileName::DocFileName()
981 : save_abs_path_(true)
985 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
986 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
990 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
991 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
995 void DocFileName::set(string const & name, string const & buffer_path)
998 bool const nameIsAbsolute = isAbsolute();
999 save_abs_path_ = nameIsAbsolute;
1000 if (!nameIsAbsolute)
1001 FileName::set(makeAbsPath(name, buffer_path).absFilename());
1002 zipped_valid_ = false;
1006 void DocFileName::erase()
1009 zipped_valid_ = false;
1013 string DocFileName::relFilename(string const & path) const
1016 return to_utf8(relPath(path));
1020 string DocFileName::outputFilename(string const & path) const
1022 return save_abs_path_ ? absFilename() : relFilename(path);
1026 string DocFileName::mangledFilename(string const & dir) const
1028 // We need to make sure that every DocFileName instance for a given
1029 // filename returns the same mangled name.
1030 typedef map<string, string> MangledMap;
1031 static MangledMap mangledNames;
1032 MangledMap::const_iterator const it = mangledNames.find(absFilename());
1033 if (it != mangledNames.end())
1034 return (*it).second;
1036 string const name = absFilename();
1037 // Now the real work
1038 string mname = os::internal_path(name);
1039 // Remove the extension.
1040 mname = support::changeExtension(name, string());
1041 // The mangled name must be a valid LaTeX name.
1042 // The list of characters to keep is probably over-restrictive,
1043 // but it is not really a problem.
1044 // Apart from non-ASCII characters, at least the following characters
1045 // are forbidden: '/', '.', ' ', and ':'.
1046 // On windows it is not possible to create files with '<', '>' or '?'
1048 static string const keep = "abcdefghijklmnopqrstuvwxyz"
1049 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1051 string::size_type pos = 0;
1052 while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
1054 // Add the extension back on
1055 mname = support::changeExtension(mname, getExtension(name));
1057 // Prepend a counter to the filename. This is necessary to make
1058 // the mangled name unique.
1059 static int counter = 0;
1061 s << counter++ << mname;
1064 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
1065 // is longer than about 160 characters. MiKTeX's pdflatex
1066 // is even pickier. A maximum length of 100 has been proven to work.
1067 // If dir.size() > max length, all bets are off for YAP. We truncate
1068 // the filename nevertheless, keeping a minimum of 10 chars.
1070 string::size_type max_length = max(100 - ((int)dir.size() + 1), 10);
1072 // If the mangled file name is too long, hack it to fit.
1073 // We know we're guaranteed to have a unique file name because
1075 if (mname.size() > max_length) {
1076 int const half = (int(max_length) / 2) - 2;
1078 mname = mname.substr(0, half) + "___" +
1079 mname.substr(mname.size() - half);
1083 mangledNames[absFilename()] = mname;
1088 bool DocFileName::isZipped() const
1090 if (!zipped_valid_) {
1091 zipped_ = isZippedFile();
1092 zipped_valid_ = true;
1098 string DocFileName::unzippedFilename() const
1100 return unzippedFileName(absFilename());
1104 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
1106 return static_cast<FileName const &>(lhs)
1107 == static_cast<FileName const &>(rhs)
1108 && lhs.saveAbsPath() == rhs.saveAbsPath();
1112 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
1114 return !(lhs == rhs);
1117 } // namespace support