]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
add debug function which prints the callstack to stderr, disabled by default
[lyx.git] / src / support / FileName.cpp
index 96aa4da93feaca12e28cc7a2943ed8b377c9886e..a16278fce4d21f720be487e6dad7913b728cb146 100644 (file)
@@ -183,7 +183,7 @@ bool FileName::isAbsolute(string const & name)
 }
 
 
-string FileName::absFilename() const
+string FileName::absFileName() const
 {
        return d->name;
 }
@@ -191,7 +191,7 @@ string FileName::absFilename() const
 
 string FileName::realPath() const
 {
-       return os::real_path(absFilename());
+       return os::real_path(absFileName());
 }
 
 
@@ -281,7 +281,7 @@ 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));
+               toqstr(os::safe_internal_path(absFileName(), how));
        QByteArray const encoded = QFile::encodeName(safe_path);
        return string(encoded.begin(), encoded.end());
 }
@@ -492,17 +492,33 @@ bool FileName::chdir() const
 }
 
 
+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;
        }
 
@@ -543,7 +559,11 @@ unsigned long FileName::checksum() const
                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);
@@ -564,22 +584,11 @@ unsigned long FileName::checksum() const
        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;
 }
@@ -700,7 +709,7 @@ docstring const FileName::absoluteFilePath() const
 
 docstring FileName::displayName(int threshold) const
 {
-       return makeDisplayPath(absFilename(), threshold);
+       return makeDisplayPath(absFileName(), threshold);
 }
 
 
@@ -743,7 +752,7 @@ docstring FileName::fileContents(string const & encoding) const
 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)
@@ -966,8 +975,8 @@ bool equivalent(FileName const & l, FileName const & r)
        // * 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.
@@ -1002,9 +1011,9 @@ bool equivalent(FileName const & l, FileName const & r)
 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);
 }
 
 
@@ -1016,19 +1025,19 @@ bool operator!=(FileName const & lhs, FileName const & rhs)
 
 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();
 }
 
 
@@ -1060,7 +1069,7 @@ void DocFileName::set(string const & name, string const & buffer_path)
        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;
 }
 
@@ -1072,30 +1081,30 @@ void DocFileName::erase()
 }
 
 
-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.
@@ -1109,7 +1118,7 @@ string DocFileName::mangledFilename(string const & dir) const
        // 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++] = '_';
@@ -1142,7 +1151,7 @@ string DocFileName::mangledFilename(string const & dir) const
                }
        }
 
-       mangledNames[absFilename()] = mname;
+       mangledNames[absFileName()] = mname;
        return mname;
 }
 
@@ -1157,9 +1166,9 @@ bool DocFileName::isZipped() const
 }
 
 
-string DocFileName::unzippedFilename() const
+string DocFileName::unzippedFileName() const
 {
-       return unzippedFileName(absFilename());
+       return support::unzippedFileName(absFileName());
 }