diff --git a/applications/utilities/mesh/manipulation/setSet/setSet.C b/applications/utilities/mesh/manipulation/setSet/setSet.C index 8a2d0a16e5..86f4880a6c 100644 --- a/applications/utilities/mesh/manipulation/setSet/setSet.C +++ b/applications/utilities/mesh/manipulation/setSet/setSet.C @@ -887,7 +887,7 @@ int main(int argc, char *argv[]) Info<< "Reading commands from file " << batchFile << endl; // we cannot handle .gz files - if (!isFile(batchFile, false)) + if (!isFile(batchFile, false, false)) { FatalErrorInFunction << "Cannot open file " << batchFile << exit(FatalError); diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 2046a91393..b8392a936e 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -456,7 +456,12 @@ bool Foam::chMod(const fileName& name, const mode_t m) } -mode_t Foam::mode(const fileName& name, const bool followLink) +mode_t Foam::mode +( + const fileName& name, + const bool checkVariants, + const bool followLink +) { if (POSIX::debug) { @@ -466,7 +471,7 @@ mode_t Foam::mode(const fileName& name, const bool followLink) error::printStack(Pout); } } - fileStat fileStatus(name, followLink); + fileStat fileStatus(name, checkVariants, followLink); if (fileStatus.isValid()) { return fileStatus.status().st_mode; @@ -478,13 +483,18 @@ mode_t Foam::mode(const fileName& name, const bool followLink) } -Foam::fileName::Type Foam::type(const fileName& name, const bool followLink) +Foam::fileName::Type Foam::type +( + const fileName& name, + const bool checkVariants, + const bool followLink +) { if (POSIX::debug) { Pout<< FUNCTION_NAME << " : name:" << name << endl; } - mode_t m = mode(name, followLink); + mode_t m = mode(name, checkVariants, followLink); if (S_ISREG(m)) { @@ -508,20 +518,20 @@ Foam::fileName::Type Foam::type(const fileName& name, const bool followLink) bool Foam::exists ( const fileName& name, - const bool checkGzip, + const bool checkVariants, const bool followLink ) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip - << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " checkVariants:" + << bool(checkVariants) << " followLink:" << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - return mode(name, followLink) || isFile(name, checkGzip, followLink); + return mode(name, checkVariants, followLink); } @@ -529,51 +539,55 @@ bool Foam::isDir(const fileName& name, const bool followLink) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " followLink:" + << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - return S_ISDIR(mode(name, followLink)); + return S_ISDIR(mode(name, false, followLink)); } bool Foam::isFile ( const fileName& name, - const bool checkGzip, + const bool checkVariants, const bool followLink ) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip - << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " checkVariants:" + << bool(checkVariants) << " followLink:" << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - return - S_ISREG(mode(name, followLink)) - || (checkGzip && S_ISREG(mode(name + ".gz", followLink))) - || (checkGzip && S_ISREG(mode(name + ".orig", followLink))); + return S_ISREG(mode(name, checkVariants, followLink)); } -off_t Foam::fileSize(const fileName& name, const bool followLink) +off_t Foam::fileSize +( + const fileName& name, + const bool checkVariants, + const bool followLink +) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " checkVariants:" + << bool(checkVariants) << " followLink:" << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - fileStat fileStatus(name, followLink); + fileStat fileStatus(name, checkVariants, followLink); if (fileStatus.isValid()) { return fileStatus.status().st_size; @@ -585,17 +599,23 @@ off_t Foam::fileSize(const fileName& name, const bool followLink) } -time_t Foam::lastModified(const fileName& name, const bool followLink) +time_t Foam::lastModified +( + const fileName& name, + const bool checkVariants, + const bool followLink +) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " checkVariants:" + << bool(checkVariants) << " followLink:" << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - fileStat fileStatus(name, followLink); + fileStat fileStatus(name, checkVariants, followLink); if (fileStatus.isValid()) { return fileStatus.status().st_mtime; @@ -607,17 +627,23 @@ time_t Foam::lastModified(const fileName& name, const bool followLink) } -double Foam::highResLastModified(const fileName& name, const bool followLink) +double Foam::highResLastModified +( + const fileName& name, + const bool checkVariants, + const bool followLink +) { if (POSIX::debug) { - Pout<< FUNCTION_NAME << " : name:" << name << endl; + Pout<< FUNCTION_NAME << " : name:" << name << " checkVariants:" + << bool(checkVariants) << " followLink:" << followLink << endl; if ((POSIX::debug & 2) && !Pstream::master()) { error::printStack(Pout); } } - fileStat fileStatus(name, followLink); + fileStat fileStatus(name, checkVariants, followLink); if (fileStatus.isValid()) { return @@ -635,7 +661,7 @@ Foam::fileNameList Foam::readDir ( const fileName& directory, const fileName::Type type, - const bool filtergz, + const bool filterVariants, const bool followLink ) { @@ -700,24 +726,25 @@ Foam::fileNameList Foam::readDir ) ) { - if ((directory/fName).type(followLink) == type) + if ((directory/fName).type(false, followLink) == type) { if (nEntries >= dirEntries.size()) { dirEntries.setSize(dirEntries.size() + maxNnames); } - if (filtergz && fExt == "gz") + dirEntries[nEntries++] = fName; + + if (filterVariants) { - dirEntries[nEntries++] = fName.lessExt(); - } - else if (filtergz && fExt == "orig") - { - dirEntries[nEntries++] = fName.lessExt(); - } - else - { - dirEntries[nEntries++] = fName; + for (label i = 0; i < fileStat::nVariants_; ++ i) + { + if (fExt == fileStat::variantExts_[i]) + { + dirEntries[nEntries - 1] = fName.lessExt(); + break; + } + } } } } @@ -750,7 +777,7 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink) return false; } - const fileName::Type srcType = src.type(followLink); + const fileName::Type srcType = src.type(false, followLink); fileName destFile(dest); @@ -949,7 +976,7 @@ bool Foam::mv(const fileName& src, const fileName& dst, const bool followLink) if ( dst.type() == fileName::DIRECTORY - && src.type(followLink) != fileName::DIRECTORY + && src.type(false, followLink) != fileName::DIRECTORY ) { const fileName dstName(dst/src.name()); @@ -976,7 +1003,7 @@ bool Foam::mvBak(const fileName& src, const std::string& ext) } } - if (exists(src, false)) + if (exists(src, false, false)) { const int maxIndex = 99; char index[3]; @@ -992,7 +1019,7 @@ bool Foam::mvBak(const fileName& src, const std::string& ext) // avoid overwriting existing files, except for the last // possible index where we have no choice - if (!exists(dstName, false) || n == maxIndex) + if (!exists(dstName, false, false) || n == maxIndex) { return ::rename(src.c_str(), dstName.c_str()) == 0; } @@ -1017,15 +1044,22 @@ bool Foam::rm(const fileName& file) } } - // Try returning plain file name; if not there, try with .gz + // Try returning plain file name; if not there, try variants if (remove(file.c_str()) == 0) { return true; } - else + + for (label i = 0; i < fileStat::nVariants_; ++ i) { - return ::remove(string(file + ".gz").c_str()) == 0; + const fileName fileVar = file + "." + fileStat::variantExts_[i]; + if (::remove(string(fileVar).c_str()) == 0) + { + return true; + } } + + return false; } @@ -1064,7 +1098,7 @@ bool Foam::rmDir(const fileName& directory) { fileName path = directory/fName; - if (path.type(false) == fileName::DIRECTORY) + if (path.type(false, false) == fileName::DIRECTORY) { if (!rmDir(path)) { diff --git a/src/OSspecific/POSIX/fileStat.C b/src/OSspecific/POSIX/fileStat.C index 7f40e0637f..ffb26041ca 100644 --- a/src/OSspecific/POSIX/fileStat.C +++ b/src/OSspecific/POSIX/fileStat.C @@ -31,6 +31,13 @@ License #include #include +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const Foam::label Foam::fileStat::nVariants_ = 2; + +const char* Foam::fileStat::variantExts_[] = {"gz", "orig"}; + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::fileStat::fileStat() @@ -42,6 +49,7 @@ Foam::fileStat::fileStat() Foam::fileStat::fileStat ( const fileName& fName, + const bool checkVariants, const bool followLink, const unsigned int maxTime ) @@ -53,26 +61,22 @@ Foam::fileStat::fileStat if (!timedOut(myTimer)) { - if (followLink) + int (*getFileStatus)(const char *, struct stat *) = + followLink ? ::stat : ::lstat; + + if (getFileStatus(fName.c_str(), &status_) == 0) { - if (::stat(fName.c_str(), &status_) != 0) - { - locIsValid = false; - } - else - { - locIsValid = true; - } + locIsValid = true; } - else + else if (checkVariants) { - if (::lstat(fName.c_str(), &status_) != 0) + for (label i = 0; !locIsValid && i < nVariants_; ++ i) { - locIsValid = false; - } - else - { - locIsValid = true; + const fileName fNameVar = fName + "." + variantExts_[i]; + if (getFileStatus(fNameVar.c_str(), &status_) == 0) + { + locIsValid = true; + } } } } diff --git a/src/OSspecific/POSIX/fileStat.H b/src/OSspecific/POSIX/fileStat.H index ba089dffe0..ff32faf64a 100644 --- a/src/OSspecific/POSIX/fileStat.H +++ b/src/OSspecific/POSIX/fileStat.H @@ -74,6 +74,15 @@ class fileStat public: + // Public static data + + //- Number of file variants + static const label nVariants_; + + //- Extensions of the file variants + static const char* variantExts_[]; + + // Constructors //- Empty constructor @@ -85,6 +94,7 @@ public: fileStat ( const fileName& fName, + const bool checkVariants = true, const bool followLink = true, const unsigned int maxTime = 0 ); diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C index bb502534e3..d17cbfa1a5 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C @@ -55,7 +55,7 @@ Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) // If the file is compressed, decompress it before reading. if (!ifPtr_->good()) { - if (isFile(pathname + ".gz", false)) + if (isFile(pathname + ".gz", false, false)) { delete ifPtr_; @@ -71,7 +71,7 @@ Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) compression_ = IOstream::COMPRESSED; } } - else if (isFile(pathname + ".orig", false)) + else if (isFile(pathname + ".orig", false, false)) { delete ifPtr_; @@ -174,8 +174,8 @@ Foam::IFstream& Foam::IFstream::operator()() const { if (!good()) { - // also checks .gz file - if (isFile(pathname_, true)) + // also checks variants + if (isFile(pathname_, true, true)) { check("IFstream::operator()"); FatalIOError.exit(); diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C index 64ca1eb403..3952d44aef 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C @@ -62,7 +62,7 @@ Foam::OFstreamAllocator::OFstreamAllocator if (compression == IOstream::COMPRESSED) { // Get identically named uncompressed version out of the way - fileName::Type pathType = Foam::type(pathname, false); + fileName::Type pathType = Foam::type(pathname, false, false); if (pathType == fileName::FILE || pathType == fileName::LINK) { rm(pathname); @@ -82,12 +82,16 @@ Foam::OFstreamAllocator::OFstreamAllocator { // get identically named compressed version out of the way fileName gzPathName(pathname + ".gz"); - fileName::Type gzType = Foam::type(gzPathName, false); + fileName::Type gzType = Foam::type(gzPathName, false, false); if (gzType == fileName::FILE || gzType == fileName::LINK) { rm(gzPathName); } - if (!append && Foam::type(pathname, false) == fileName::LINK) + if + ( + !append + && Foam::type(pathname, false, false) == fileName::LINK + ) { // Disallow writing into softlink to avoid any problems with // e.g. softlinked initial fields diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C index 336b1d8871..0a8d04cf44 100644 --- a/src/OpenFOAM/db/Time/Time.C +++ b/src/OpenFOAM/db/Time/Time.C @@ -162,7 +162,7 @@ void Foam::Time::setControls() // Check if time directory exists // If not increase time precision to see if it is formatted differently. - if (!fileHandler().exists(timePath(), false)) + if (!fileHandler().exists(timePath(), false, false)) { int oldPrecision = precision_; int requiredPrecision = -1; @@ -186,7 +186,7 @@ void Foam::Time::setControls() oldTime = newTime; // Check the existence of the time directory with the new format - found = fileHandler().exists(timePath(), false); + found = fileHandler().exists(timePath(), false, false); if (found) { diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C index 1608063279..87e7fd07f2 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C @@ -157,7 +157,7 @@ bool Foam::dynamicCode::resolveTemplates if (!templateDir.empty() && isDir(templateDir)) { file = templateDir/templateName; - if (!isFile(file, false)) + if (!isFile(file, false, false)) { file.clear(); } @@ -512,7 +512,7 @@ bool Foam::dynamicCode::upToDate(const SHA1Digest& sha1) const { const fileName file = digestFile(); - if (!exists(file, false) || SHA1Digest(IFstream(file)()) != sha1) + if (!exists(file, false, false) || SHA1Digest(IFstream(file)()) != sha1) { return false; } diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H index 3f01a6e75a..79780ff319 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H @@ -198,6 +198,7 @@ public: virtual mode_t mode ( const fileName&, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -205,6 +206,7 @@ public: virtual fileName::Type type ( const fileName&, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -213,7 +215,7 @@ public: virtual bool exists ( const fileName&, - const bool checkGzip=true, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -229,7 +231,7 @@ public: virtual bool isFile ( const fileName&, - const bool checkGzip=true, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -237,6 +239,7 @@ public: virtual off_t fileSize ( const fileName&, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -244,6 +247,7 @@ public: virtual time_t lastModified ( const fileName&, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -251,6 +255,7 @@ public: virtual double highResLastModified ( const fileName&, + const bool checkVariants = true, const bool followLink = true ) const = 0; @@ -259,7 +264,7 @@ public: ( const fileName&, const fileName::Type=fileName::FILE, - const bool filtergz=true, + const bool filterVariants = true, const bool followLink = true ) const = 0; diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index e6efc0335a..d4ee8b67da 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -36,6 +36,7 @@ License #include "SubList.H" #include "unthreadedInitialise.H" #include "PackedBoolList.H" +#include "gzstream.h" /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ @@ -496,25 +497,28 @@ bool Foam::fileOperations::masterUncollatedFileOperation::uniformFile void Foam::fileOperations::masterUncollatedFileOperation::readAndSend ( const fileName& filePath, - const IOstream::compressionType cmp, const labelUList& procs, PstreamBuffers& pBufs ) { - if (cmp == IOstream::compressionType::COMPRESSED) + if (debug) + { + Pout<< FUNCTION_NAME << ": Opening " << filePath << endl; + } + + IFstream is(filePath, IOstream::streamFormat::BINARY); + + if (!is.good()) + { + FatalIOErrorInFunction(filePath) << "Cannot open file " << filePath + << exit(FatalIOError); + } + + if (isA(is.stdStream())) { if (debug) { - Pout<< "masterUncollatedFileOperation::readAndSend :" - << " Opening compressed " << filePath << endl; - } - - IFstream is(filePath, IOstream::streamFormat::BINARY); - - if (!is.good()) - { - FatalIOErrorInFunction(filePath) << "Cannot open file " << filePath - << exit(FatalIOError); + Pout<< FUNCTION_NAME << ": Reading compressed" << endl; } std::ostringstream stringStr; @@ -530,21 +534,12 @@ void Foam::fileOperations::masterUncollatedFileOperation::readAndSend else { off_t count(Foam::fileSize(filePath)); - IFstream is(filePath, IOstream::streamFormat::BINARY); - - if (!is.good()) - { - FatalIOErrorInFunction(filePath) << "Cannot open file " << filePath - << exit(FatalIOError); - } - if (debug) { - Pout<< "masterUncollatedFileOperation::readStream :" - << " From " << filePath << " reading " << label(count) - << " bytes" << endl; + Pout<< FUNCTION_NAME << " : Reading " << count << " bytes " << endl; } + List buf(static_cast