From d34dfbe0b77691a697134bf992ffb67e20302b1b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 23 May 2023 09:43:39 +0200 Subject: [PATCH] ENH: dummy fileOperation: placeholder for interfaces taking a reference STYLE: align readOnProc/writeOnProc naming within masterUncollated --- .../test/fileHandler-dummy/Make/files | 3 + .../test/fileHandler-dummy/Make/options | 2 + .../Test-fileHandler-dummy.C | 79 ++++ src/OpenFOAM/Make/files | 1 + .../collatedFileOperation.H | 1 + .../dummyFileOperation/dummyFileOperation.C | 385 ++++++++++++++++++ .../dummyFileOperation/dummyFileOperation.H | 290 +++++++++++++ .../fileOperation/fileOperation.H | 15 + .../fileOperation/fileOperationNew.C | 15 + .../masterUncollatedFileOperation.C | 90 ++-- .../masterUncollatedFileOperation.H | 7 +- .../hostUncollatedFileOperation.H | 1 + .../uncollatedFileOperation.C | 4 +- .../uncollatedFileOperation.H | 2 +- 14 files changed, 849 insertions(+), 46 deletions(-) create mode 100644 applications/test/fileHandler-dummy/Make/files create mode 100644 applications/test/fileHandler-dummy/Make/options create mode 100644 applications/test/fileHandler-dummy/Test-fileHandler-dummy.C create mode 100644 src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.C create mode 100644 src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.H diff --git a/applications/test/fileHandler-dummy/Make/files b/applications/test/fileHandler-dummy/Make/files new file mode 100644 index 0000000000..5f84ed028f --- /dev/null +++ b/applications/test/fileHandler-dummy/Make/files @@ -0,0 +1,3 @@ +Test-fileHandler-dummy.C + +EXE = $(FOAM_USER_APPBIN)/Test-fileHandler-dummy diff --git a/applications/test/fileHandler-dummy/Make/options b/applications/test/fileHandler-dummy/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/fileHandler-dummy/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/fileHandler-dummy/Test-fileHandler-dummy.C b/applications/test/fileHandler-dummy/Test-fileHandler-dummy.C new file mode 100644 index 0000000000..fd55fb991a --- /dev/null +++ b/applications/test/fileHandler-dummy/Test-fileHandler-dummy.C @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2023 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Application + Test-fileHandler-dummy + +Description + Simple test of dummy fileOperation + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "fileName.H" +#include "fileOperation.H" +#include "Switch.H" + +using namespace Foam; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noCheckProcessorDirectories(); + + argList::addBoolOption + ( + "force", + "Force use of dummy handler (and provoke NotImplemented)" + ); + + #include "setRootCase.H" + + const bool optForce = args.found("force"); + + const auto& dummy = fileOperation::null(); + + Info<< "default handler: " << fileHandler() << endl; + Info<< "dummy handler: " << dummy() << endl; + + Switch hasFile(Switch::INVALID); + + if (optForce || (dummy && dummy().good())) + { + hasFile = dummy().isFile("foo"); + } + + Info<< "check file: " << hasFile << endl; + + Info<< "\nEnd\n" << endl; + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index b4918de003..cb0be37821 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -22,6 +22,7 @@ $(fileOps)/fileOperation/fileOperationBroadcast.C $(fileOps)/fileOperation/fileOperationNew.C $(fileOps)/fileOperation/fileOperationRanks.C $(fileOps)/fileOperationInitialise/fileOperationInitialise.C +$(fileOps)/dummyFileOperation/dummyFileOperation.C $(fileOps)/uncollatedFileOperation/uncollatedFileOperation.C $(fileOps)/masterUncollatedFileOperation/masterUncollatedFileOperation.C $(fileOps)/collatedFileOperation/collatedFileOperation.C diff --git a/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.H b/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.H index d2c9ad9888..92ce70c71a 100644 --- a/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.H +++ b/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.H @@ -176,6 +176,7 @@ public: Class collatedFileOperationInitialise Declaration \*---------------------------------------------------------------------------*/ +//! Internal class only class collatedFileOperationInitialise : public masterUncollatedFileOperationInitialise diff --git a/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.C b/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.C new file mode 100644 index 0000000000..d7da952169 --- /dev/null +++ b/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.C @@ -0,0 +1,385 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2023 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "dummyFileOperation.H" +#include "dummyISstream.H" +#include "Fstream.H" +#include "objectRegistry.H" + +/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ + +namespace Foam +{ +namespace fileOperations +{ + defineTypeName(dummyFileOperation); + +// No runtime selection +} +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::fileOperations::dummyFileOperation::dummyFileOperation +( + bool verbose +) +: + fileOperation + ( + // Use COMM_SELF for now, but COMM_NULL is probably more appropriate + Tuple2 + ( + UPstream::selfComm, + fileOperation::getGlobalIORanks() + ) + ) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::fileOperations::dummyFileOperation::~dummyFileOperation() +{} + + +// * * * * * * * * * * * * * Filesystem Operations * * * * * * * * * * * * * // + +bool Foam::fileOperations::dummyFileOperation::mkDir +( + const fileName& dir, + mode_t mode +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::chMod +( + const fileName& fName, + mode_t mode +) const +{ + NotImplemented; + return false; +} + + +mode_t Foam::fileOperations::dummyFileOperation::mode +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return mode_t(0); +} + + +Foam::fileName::Type Foam::fileOperations::dummyFileOperation::type +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return fileName::Type::UNDEFINED; +} + + +bool Foam::fileOperations::dummyFileOperation::exists +( + const fileName& fName, + const bool checkGzip, + const bool followLink +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::isDir +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::isFile +( + const fileName& fName, + const bool checkGzip, + const bool followLink +) const +{ + NotImplemented; + return false; +} + + +off_t Foam::fileOperations::dummyFileOperation::fileSize +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return off_t(0); +} + + +time_t Foam::fileOperations::dummyFileOperation::lastModified +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return time_t(0); +} + + +double Foam::fileOperations::dummyFileOperation::highResLastModified +( + const fileName& fName, + const bool followLink +) const +{ + NotImplemented; + return 0; +} + + + +Foam::fileNameList Foam::fileOperations::dummyFileOperation::readDir +( + const fileName& dir, + const fileName::Type type, + const bool filtergz, + const bool followLink +) const +{ + NotImplemented; + return fileNameList(); +} + + +bool Foam::fileOperations::dummyFileOperation::cp +( + const fileName& src, + const fileName& dst, + const bool followLink +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::ln +( + const fileName& src, + const fileName& dst +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::mv +( + const fileName& src, + const fileName& dst, + const bool followLink +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::mvBak +( + const fileName& fName, + const std::string& ext +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::rm +( + const fileName& fName +) const +{ + NotImplemented; + return false; +} + + +bool Foam::fileOperations::dummyFileOperation::rmDir +( + const fileName& dir, + const bool silent, + const bool emptyOnly +) const +{ + NotImplemented; + return false; +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::fileName Foam::fileOperations::dummyFileOperation::filePath +( + const bool checkGlobal, + const IOobject& io, + const word& typeName, + const bool search +) const +{ + NotImplemented; + return fileName(); +} + + +Foam::fileName Foam::fileOperations::dummyFileOperation::dirPath +( + const bool checkGlobal, + const IOobject& io, + const bool search +) const +{ + NotImplemented; + return fileName(); +} + + +Foam::fileNameList Foam::fileOperations::dummyFileOperation::readObjects +( + const objectRegistry& db, + const fileName& instance, + const fileName& local, + word& newInstance +) const +{ + NotImplemented; + return fileNameList(); +} + + +bool Foam::fileOperations::dummyFileOperation::readHeader +( + IOobject& io, + const fileName& fName, + const word& typeName +) const +{ + NotImplemented; + io.resetHeader(); + return false; +} + + +Foam::autoPtr +Foam::fileOperations::dummyFileOperation::readStream +( + regIOobject& io, + const fileName& fName, + const word& typeName, + const bool readOnProc +) const +{ + NotImplemented; + return autoPtr(new dummyISstream()); +} + + +bool Foam::fileOperations::dummyFileOperation::read +( + regIOobject& io, + const bool masterOnly, + const IOstreamOption::streamFormat format, + const word& typeName +) const +{ + NotImplemented; + return false; +} + + +Foam::autoPtr +Foam::fileOperations::dummyFileOperation::NewIFstream +( + const fileName& filePath +) const +{ + NotImplemented; + return autoPtr(new dummyISstream()); +} + + +Foam::autoPtr +Foam::fileOperations::dummyFileOperation::NewOFstream +( + const fileName& pathName, + IOstreamOption streamOpt, + const bool writeOnProc +) const +{ + NotImplemented; + return autoPtr(new OFstream(nullptr)); // ie, /dev/null +} + + +Foam::autoPtr +Foam::fileOperations::dummyFileOperation::NewOFstream +( + IOstreamOption::atomicType atomic, + const fileName& pathName, + IOstreamOption streamOpt, + const bool writeOnProc +) const +{ + NotImplemented; + return autoPtr(new OFstream(nullptr)); // ie, /dev/null +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.H b/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.H new file mode 100644 index 0000000000..5195fe2eea --- /dev/null +++ b/src/OpenFOAM/global/fileOperations/dummyFileOperation/dummyFileOperation.H @@ -0,0 +1,290 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2023 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::fileOperations::dummyFileOperation + +Description + Dummy fileOperation, to be used as a placeholder for interfaces + taking a reference to a fileOperation. + Will mostly behave like a no-op, but at the moment no guarantees + of any particular behaviour other than good() returning false. + +\*---------------------------------------------------------------------------*/ + +#ifndef Foam_fileOperations_dummyFileOperation_H +#define Foam_fileOperations_dummyFileOperation_H + +#include "fileOperation.H" +#include "OSspecific.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace fileOperations +{ + +/*---------------------------------------------------------------------------*\ + Class dummyFileOperation Declaration +\*---------------------------------------------------------------------------*/ + +class dummyFileOperation +: + public fileOperation +{ +public: + + //- Runtime type information + TypeNameNoDebug("dummy"); + + + // Constructors + + //- Default construct + explicit dummyFileOperation(bool verbose = false); + + + //- Destructor + virtual ~dummyFileOperation(); + + + // Member Functions + + //- This fileOperation is not really valid for anything. + virtual bool good() const { return false; } + + //- No managed communicator + virtual void storeComm() const {} + + + // OSSpecific equivalents + + //- Make directory + virtual bool mkDir(const fileName&, mode_t=0777) const; + + //- Set the file mode + virtual bool chMod(const fileName&, const mode_t) const; + + //- Return the file mode + virtual mode_t mode + ( + const fileName&, + const bool followLink = true + ) const; + + //- Return the file type: DIRECTORY, FILE or SYMLINK + virtual fileName::Type type + ( + const fileName&, + const bool followLink = true + ) const; + + //- Does the name exist (as DIRECTORY or FILE) in the file system? + // Optionally enable/disable check for gzip file. + virtual bool exists + ( + const fileName&, + const bool checkGzip=true, + const bool followLink = true + ) const; + + //- Does the name exist as a DIRECTORY in the file system? + virtual bool isDir + ( + const fileName&, + const bool followLink = true + ) const; + + //- Does the name exist as a FILE in the file system? + // Optionally enable/disable check for gzip file. + virtual bool isFile + ( + const fileName&, + const bool checkGzip=true, + const bool followLink = true + ) const; + + //- Return size of file + virtual off_t fileSize + ( + const fileName&, + const bool followLink = true + ) const; + + //- Return time of last file modification + virtual time_t lastModified + ( + const fileName&, + const bool followLink = true + ) const; + + //- Return time of last file modification + virtual double highResLastModified + ( + const fileName&, + const bool followLink = true + ) const; + + //- Read a directory and return the entries as a string list + virtual fileNameList readDir + ( + const fileName&, + const fileName::Type=fileName::FILE, + const bool filtergz=true, + const bool followLink = true + ) const; + + //- Copy, recursively if necessary, the source to the destination + virtual bool cp + ( + const fileName& src, + const fileName& dst, + const bool followLink = true + ) const; + + //- Create a softlink. dst should not exist. Returns true if + // successful. + virtual bool ln(const fileName& src, const fileName& dst) const; + + //- Rename src to dst + virtual bool mv + ( + const fileName& src, + const fileName& dst, + const bool followLink = false + ) const; + + //- Rename to a corresponding backup file + // If the backup file already exists, attempt with + // "01" .. "99" suffix + virtual bool mvBak + ( + const fileName&, + const std::string& ext = "bak" + ) const; + + //- Remove a file, returning true if successful otherwise false + virtual bool rm(const fileName&) const; + + //- Remove a directory and its contents + // \param dir the directory to remove + // \param silent do not report missing directory + // \param emptyOnly only remove empty directories (recursive) + virtual bool rmDir + ( + const fileName& dir, + const bool silent = false, + const bool emptyOnly = false + ) const; + + + // (reg)IOobject functionality + + //- Search for an object. checkGlobal + virtual fileName filePath + ( + const bool checkGlobal, + const IOobject& io, + const word& typeName, + const bool search + ) const; + + //- Search for a directory. checkGlobal + virtual fileName dirPath + ( + const bool checkGlobal, + const IOobject& io, + const bool search + ) const; + + //- Search directory for objects. Used in IOobjectList. + virtual fileNameList readObjects + ( + const objectRegistry& db, + const fileName& instance, + const fileName& local, + word& newInstance + ) const; + + //- Read object header from supplied file + virtual bool readHeader + ( + IOobject&, + const fileName&, + const word& typeName + ) const; + + //- Reads header for regIOobject and returns an ISstream + //- to read the contents. + virtual autoPtr readStream + ( + regIOobject&, + const fileName&, + const word& typeName, + const bool readOnProc = true + ) const; + + //- Top-level read + virtual bool read + ( + regIOobject&, + const bool masterOnly, + const IOstreamOption::streamFormat format, + const word& typeName + ) const; + + //- Generate an ISstream that reads a file + virtual autoPtr NewIFstream(const fileName&) const; + + //- Generate an OSstream that writes a file + virtual autoPtr NewOFstream + ( + const fileName& pathname, + IOstreamOption streamOpt = IOstreamOption(), + const bool writeOnProc = true + ) const; + + //- Generate an OSstream that writes a file + virtual autoPtr NewOFstream + ( + IOstreamOption::atomicType, + const fileName& pathname, + IOstreamOption streamOpt = IOstreamOption(), + const bool writeOnProc = true + ) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace fileOperations +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H index 245eef2586..c7350b7a99 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H @@ -143,6 +143,14 @@ public: typedef IntRange procRangeType; +private: + + // Private Static Data + + //- Storage of the dummy file handler (demand-driven) + static refPtr dummyHandlerPtr_; + + protected: // Protected Static Data @@ -272,6 +280,9 @@ public: //- The currently active file handler. Avoid accessing directly static refPtr fileHandlerPtr_; + //- Reference to a dummy file handler. + static refPtr null(); + // Constructors @@ -501,6 +512,10 @@ public: // Member Functions + //- True if the fileOperation can be considered valid. + //- At the moment, primarily used to detect the dummy fileOperation. + virtual bool good() const { return true; } + //- Transfer ownership of communicator to this fileOperation. //- Use with caution virtual void storeComm() const = 0; diff --git a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperationNew.C b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperationNew.C index f836be8534..b24f38870f 100644 --- a/src/OpenFOAM/global/fileOperations/fileOperation/fileOperationNew.C +++ b/src/OpenFOAM/global/fileOperations/fileOperation/fileOperationNew.C @@ -26,15 +26,30 @@ License \*---------------------------------------------------------------------------*/ #include "fileOperation.H" +#include "dummyFileOperation.H" #include "uncollatedFileOperation.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +Foam::refPtr Foam::fileOperation::dummyHandlerPtr_; + Foam::refPtr Foam::fileOperation::fileHandlerPtr_; // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // +Foam::refPtr Foam::fileOperation::null() +{ + if (!dummyHandlerPtr_) + { + // verbose = false + dummyHandlerPtr_.reset(new fileOperations::dummyFileOperation(false)); + } + + return dummyHandlerPtr_; +} + + const Foam::fileOperation& Foam::fileOperation::fileHandler() { if (!fileOperation::fileHandlerPtr_) diff --git a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C index 314cd76cf7..700b44aa8c 100644 --- a/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C @@ -428,10 +428,12 @@ Foam::fileOperations::masterUncollatedFileOperation::localObjectPath void Foam::fileOperations::masterUncollatedFileOperation::readAndSend ( const fileName& filePath, - const labelUList& procs, + const labelUList& recvProcs, PstreamBuffers& pBufs ) { + if (recvProcs.empty()) return; + IFstream ifs(filePath, IOstreamOption::BINARY); if (!ifs.good()) @@ -462,7 +464,7 @@ void Foam::fileOperations::masterUncollatedFileOperation::readAndSend std::istreambuf_iterator() ); - for (const label proci : procs) + for (const label proci : recvProcs) { UOPstream os(proci, pBufs); os.write(buf.data(), buf.length()); @@ -483,7 +485,7 @@ void Foam::fileOperations::masterUncollatedFileOperation::readAndSend List buf(static_cast