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/filetools.h"
15 #include "support/lstrings.h"
16 #include "support/os.h"
17 #include "support/qstring_helpers.h"
28 #include <boost/assert.hpp>
29 #include <boost/filesystem/config.hpp>
30 #include <boost/detail/workaround.hpp>
31 #include <boost/throw_exception.hpp>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
51 using std::ostringstream;
57 // BOOST_POSIX or BOOST_WINDOWS specify which API to use.
58 # if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX )
59 # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
60 # define BOOST_WINDOWS
66 #if defined (BOOST_WINDOWS)
67 # define WIN32_LEAN_AND_MEAN
74 static bool copy_file(std::string const & source, std::string const & target, bool noclobber)
78 int const infile = ::open(source.c_str(), O_RDONLY);
82 struct stat source_stat;
83 int const ret = ::fstat(infile, &source_stat);
89 int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC);
91 int const outfile = ::open(target.c_str(), flags, source_stat.st_mode);
98 std::size_t const buf_sz = 32768;
104 in = ::read(infile, buf, buf_sz);
107 } else if (in == 0) {
110 out = ::write(outfile, buf, in);
122 if (in == -1 || out == -1)
127 if (::CopyFile(source.c_str(), target.c_str(), noclobber) == 0) {
128 // CopyFile is probably not setting errno so this is most
141 /////////////////////////////////////////////////////////////////////
145 /////////////////////////////////////////////////////////////////////
148 FileName::FileName(string const & abs_filename)
149 : name_(abs_filename)
151 BOOST_ASSERT(empty() || absolutePath(name_));
153 BOOST_ASSERT(!contains(name_, '\\'));
158 void FileName::set(string const & name)
161 BOOST_ASSERT(absolutePath(name_));
163 BOOST_ASSERT(!contains(name_, '\\'));
168 void FileName::erase()
174 bool FileName::copyTo(FileName const & name, bool noclobber) const
177 copy_file(toFilesystemEncoding(), name.toFilesystemEncoding(), noclobber);
186 string FileName::toFilesystemEncoding() const
188 QByteArray const encoded = QFile::encodeName(toqstr(name_));
189 return string(encoded.begin(), encoded.end());
193 FileName FileName::fromFilesystemEncoding(string const & name)
195 QByteArray const encoded(name.c_str(), name.length());
196 return FileName(fromqstr(QFile::decodeName(encoded)));
200 bool FileName::exists() const
202 return QFileInfo(toqstr(name_)).exists();
206 bool FileName::isSymLink() const
208 return QFileInfo(toqstr(name_)).isSymLink();
212 bool FileName::isFileEmpty() const
214 return QFileInfo(toqstr(name_)).size() == 0;
218 bool FileName::isDirectory() const
220 return QFileInfo(toqstr(name_)).isDir();
224 bool FileName::isReadOnly() const
226 QFileInfo const fi(toqstr(name_));
227 return fi.isReadable() && !fi.isWritable();
231 bool FileName::isReadableDirectory() const
233 QFileInfo const fi(toqstr(name_));
234 return fi.isDir() && fi.isReadable();
238 std::string FileName::onlyFileName() const
240 return support::onlyFilename(absFilename());
244 FileName FileName::onlyPath() const
246 return FileName(support::onlyPath(absFilename()));
250 bool FileName::isReadableFile() const
252 QFileInfo const fi(toqstr(name_));
253 return fi.isFile() && fi.isReadable();
257 bool FileName::isWritable() const
259 QFileInfo const fi(toqstr(name_));
260 return fi.isWritable();
264 bool FileName::isDirWritable() const
266 LYXERR(Debug::FILES, "isDirWriteable: " << *this);
268 FileName const tmpfl(tempName(*this, "lyxwritetest"));
278 FileName FileName::tempName(FileName const & dir, std::string const & mask)
280 return support::tempName(dir, mask);
284 std::time_t FileName::lastModified() const
286 return QFileInfo(toqstr(name_)).lastModified().toTime_t();
290 static bool rmdir(QFileInfo const & fi)
292 QDir dir(fi.absoluteFilePath());
293 QFileInfoList list = dir.entryInfoList();
294 for (int i = 0; i < list.size(); ++i) {
295 if (list.at(i).fileName() == ".")
297 if (list.at(i).fileName() == "..")
299 if (list.at(i).isDir()) {
300 LYXERR(Debug::FILES, "Erasing dir "
301 << fromqstr(list.at(i).absoluteFilePath()) << endl);
302 if (!rmdir(list.at(i)))
306 LYXERR(Debug::FILES, "Erasing file "
307 << fromqstr(list.at(i).absoluteFilePath()) << endl);
308 dir.remove(list.at(i).fileName());
311 QDir parent = fi.absolutePath();
312 return parent.rmdir(fi.fileName());
316 bool FileName::destroyDirectory() const
318 bool const success = rmdir(QFileInfo(toqstr(name_)));
320 lyxerr << "Could not delete " << *this << "." << endl;
326 bool FileName::createDirectory(int permission) const
328 BOOST_ASSERT(!empty());
329 return mkdir(*this, permission) == 0;
333 docstring FileName::displayName(int threshold) const
335 return makeDisplayPath(absFilename(), threshold);
339 string FileName::fileContents() const
342 string const encodedname = toFilesystemEncoding();
343 ifstream ifs(encodedname.c_str());
351 lyxerr << "LyX was not able to read file '" << *this << '\'' << std::endl;
356 string FileName::guessFormatFromContents() const
358 // the different filetypes and what they contain in one of the first lines
359 // (dots are any characters). (Herbert 20020131)
362 // EPS %!PS-Adobe-3.0 EPSF...
369 // PBM P1... or P4 (B/W)
370 // PGM P2... or P5 (Grayscale)
371 // PPM P3... or P6 (color)
372 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
373 // SGI \001\332... (decimal 474)
375 // TIFF II... or MM...
377 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
378 // ...static char *...
379 // XWD \000\000\000\151 (0x00006900) decimal 105
381 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
382 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
383 // Z \037\235 UNIX compress
386 if (empty() || !isReadableFile())
389 ifstream ifs(toFilesystemEncoding().c_str());
391 // Couldn't open file...
395 static string const gzipStamp = "\037\213";
398 static string const zipStamp = "PK";
401 static string const compressStamp = "\037\235";
403 // Maximum strings to read
404 int const max_count = 50;
409 bool firstLine = true;
410 while ((count++ < max_count) && format.empty()) {
412 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
413 << "\tFile type not recognised before EOF!");
418 string const stamp = str.substr(0, 2);
419 if (firstLine && str.size() >= 2) {
420 // at first we check for a zipped file, because this
421 // information is saved in the first bytes of the file!
422 // also some graphic formats which save the information
423 // in the first line, too.
424 if (prefixIs(str, gzipStamp)) {
427 } else if (stamp == zipStamp) {
430 } else if (stamp == compressStamp) {
434 } else if (stamp == "BM") {
437 } else if (stamp == "\001\332") {
441 // Don't need to use str.at(0), str.at(1) because
442 // we already know that str.size() >= 2
443 } else if (str[0] == 'P') {
459 } else if ((stamp == "II") || (stamp == "MM")) {
462 } else if (prefixIs(str,"%TGIF")) {
465 } else if (prefixIs(str,"#FIG")) {
468 } else if (prefixIs(str,"GIF")) {
471 } else if (str.size() > 3) {
472 int const c = ((str[0] << 24) & (str[1] << 16) &
473 (str[2] << 8) & str[3]);
484 else if (contains(str,"EPSF"))
485 // dummy, if we have wrong file description like
486 // %!PS-Adobe-2.0EPSF"
489 else if (contains(str, "Grace"))
492 else if (contains(str, "JFIF"))
495 else if (contains(str, "%PDF"))
498 else if (contains(str, "PNG"))
501 else if (contains(str, "%!PS-Adobe")) {
504 if (contains(str,"EPSF"))
510 else if (contains(str, "_bits[]"))
513 else if (contains(str, "XPM") || contains(str, "static char *"))
516 else if (contains(str, "BITPIX"))
520 if (!format.empty()) {
521 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
525 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
526 << "\tCouldn't find a known format!");
531 bool FileName::isZippedFile() const
533 string const type = guessFormatFromContents();
534 return contains("gzip zip compress", type) && !type.empty();
538 bool operator==(FileName const & lhs, FileName const & rhs)
540 return lhs.absFilename() == rhs.absFilename();
544 bool operator!=(FileName const & lhs, FileName const & rhs)
546 return lhs.absFilename() != rhs.absFilename();
550 bool operator<(FileName const & lhs, FileName const & rhs)
552 return lhs.absFilename() < rhs.absFilename();
556 bool operator>(FileName const & lhs, FileName const & rhs)
558 return lhs.absFilename() > rhs.absFilename();
562 std::ostream & operator<<(std::ostream & os, FileName const & filename)
564 return os << filename.absFilename();
568 /////////////////////////////////////////////////////////////////////
572 /////////////////////////////////////////////////////////////////////
575 DocFileName::DocFileName()
576 : save_abs_path_(true)
580 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
581 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
585 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
586 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
590 void DocFileName::set(string const & name, string const & buffer_path)
592 save_abs_path_ = absolutePath(name);
593 name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
594 zipped_valid_ = false;
598 void DocFileName::erase()
601 zipped_valid_ = false;
605 string const DocFileName::relFilename(string const & path) const
608 return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
612 string const DocFileName::outputFilename(string const & path) const
615 return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
619 string const DocFileName::mangledFilename(std::string const & dir) const
621 // We need to make sure that every DocFileName instance for a given
622 // filename returns the same mangled name.
623 typedef map<string, string> MangledMap;
624 static MangledMap mangledNames;
625 MangledMap::const_iterator const it = mangledNames.find(name_);
626 if (it != mangledNames.end())
630 string mname = os::internal_path(name_);
631 // Remove the extension.
632 mname = changeExtension(name_, string());
633 // The mangled name must be a valid LaTeX name.
634 // The list of characters to keep is probably over-restrictive,
635 // but it is not really a problem.
636 // Apart from non-ASCII characters, at least the following characters
637 // are forbidden: '/', '.', ' ', and ':'.
638 // On windows it is not possible to create files with '<', '>' or '?'
640 static string const keep = "abcdefghijklmnopqrstuvwxyz"
641 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
643 string::size_type pos = 0;
644 while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
646 // Add the extension back on
647 mname = changeExtension(mname, getExtension(name_));
649 // Prepend a counter to the filename. This is necessary to make
650 // the mangled name unique.
651 static int counter = 0;
652 std::ostringstream s;
653 s << counter++ << mname;
656 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
657 // is longer than about 160 characters. MiKTeX's pdflatex
658 // is even pickier. A maximum length of 100 has been proven to work.
659 // If dir.size() > max length, all bets are off for YAP. We truncate
660 // the filename nevertheless, keeping a minimum of 10 chars.
662 string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
664 // If the mangled file name is too long, hack it to fit.
665 // We know we're guaranteed to have a unique file name because
667 if (mname.size() > max_length) {
668 int const half = (int(max_length) / 2) - 2;
670 mname = mname.substr(0, half) + "___" +
671 mname.substr(mname.size() - half);
675 mangledNames[name_] = mname;
680 bool DocFileName::isZipped() const
682 if (!zipped_valid_) {
683 zipped_ = isZippedFile();
684 zipped_valid_ = true;
690 string const DocFileName::unzippedFilename() const
692 return unzippedFileName(name_);
696 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
698 return lhs.absFilename() == rhs.absFilename()
699 && lhs.saveAbsPath() == rhs.saveAbsPath();
703 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
705 return !(lhs == rhs);
708 } // namespace support