uncollatedFileOperation.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017 OpenFOAM Foundation
9  Copyright (C) 2020-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::fileOperations::uncollatedFileOperation
29 
30 Description
31  fileOperation that assumes file operations are local.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef Foam_fileOperations_uncollatedFileOperation_H
36 #define Foam_fileOperations_uncollatedFileOperation_H
37 
38 #include "fileOperation.H"
39 #include "OSspecific.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 namespace fileOperations
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class uncollatedFileOperation Declaration
50 \*---------------------------------------------------------------------------*/
51 
53 :
54  public fileOperation
55 {
56  // Private Data
57 
58  //- Communicator allocated/managed by us
59  mutable label managedComm_;
60 
61 
62  // Private Member Functions
63 
64  //- Any initialisation steps after constructing
65  void init(bool verbose);
66 
67 
68 protected:
69 
70  // Protected Member Functions
71 
72  //- Search for an object.
73  // checkGlobal : also check undecomposed case
74  // isFile : true:check for file false:check for directory
76  (
77  const bool checkGlobal,
78  const bool isFile,
79  const IOobject& io,
80  const bool search
81  ) const;
82 
83  //- Lookup name of processorsDDD using cache.
84  // \note Do not use any parallel synchronisation
85  // \return empty fileName if not found.
87  (
88  const fileName&
89  ) const;
90 
91 
92 public:
93 
94  //- Runtime type information
95  TypeName("uncollated");
96 
97 
98  // Constructors
99 
100  //- Default construct
101  explicit uncollatedFileOperation(bool verbose = false);
102 
103  //- Construct from communicator with specified io-ranks
104  explicit uncollatedFileOperation
105  (
106  const Tuple2<label, labelList>& commAndIORanks,
107  const bool distributedRoots,
108  bool verbose = false
109  );
110 
111 
112  //- Destructor
113  virtual ~uncollatedFileOperation();
114 
115 
116  // Member Functions
117 
118  //- Transfer ownership of communicator to this fileOperation.
119  //- Use with caution
120  virtual void storeComm() const;
121 
122 
123  // OSSpecific equivalents
124 
125  //- Make directory
126  virtual bool mkDir(const fileName&, mode_t=0777) const;
127 
128  //- Set the file mode
129  virtual bool chMod(const fileName&, const mode_t) const;
130 
131  //- Return the file mode
132  virtual mode_t mode
133  (
134  const fileName&,
135  const bool followLink = true
136  ) const;
137 
138  //- Return the file type: DIRECTORY, FILE or SYMLINK
139  virtual fileName::Type type
140  (
141  const fileName&,
142  const bool followLink = true
143  ) const;
144 
145  //- Does the name exist (as DIRECTORY or FILE) in the file system?
146  // Optionally enable/disable check for gzip file.
147  virtual bool exists
148  (
149  const fileName&,
150  const bool checkGzip=true,
151  const bool followLink = true
152  ) const;
153 
154  //- Does the name exist as a DIRECTORY in the file system?
155  virtual bool isDir
156  (
157  const fileName&,
158  const bool followLink = true
159  ) const;
160 
161  //- Does the name exist as a FILE in the file system?
162  // Optionally enable/disable check for gzip file.
163  virtual bool isFile
164  (
165  const fileName&,
166  const bool checkGzip=true,
167  const bool followLink = true
168  ) const;
169 
170  //- Return size of file
171  virtual off_t fileSize
172  (
173  const fileName&,
174  const bool followLink = true
175  ) const;
176 
177  //- Return time of last file modification
178  virtual time_t lastModified
179  (
180  const fileName&,
181  const bool followLink = true
182  ) const;
183 
184  //- Return time of last file modification
185  virtual double highResLastModified
186  (
187  const fileName&,
188  const bool followLink = true
189  ) const;
190 
191  //- Read a directory and return the entries as a string list
192  virtual fileNameList readDir
193  (
194  const fileName&,
196  const bool filtergz=true,
197  const bool followLink = true
198  ) const;
199 
200  //- Copy, recursively if necessary, the source to the destination
201  virtual bool cp
202  (
203  const fileName& src,
204  const fileName& dst,
205  const bool followLink = true
206  ) const;
207 
208  //- Create a softlink. dst should not exist. Returns true if
209  // successful.
210  virtual bool ln(const fileName& src, const fileName& dst) const;
211 
212  //- Rename src to dst
213  virtual bool mv
214  (
215  const fileName& src,
216  const fileName& dst,
217  const bool followLink = false
218  ) const;
219 
220  //- Rename to a corresponding backup file
221  // If the backup file already exists, attempt with
222  // "01" .. "99" suffix
223  virtual bool mvBak
224  (
225  const fileName&,
226  const std::string& ext = "bak"
227  ) const;
228 
229  //- Remove a file, returning true if successful otherwise false
230  virtual bool rm(const fileName&) const;
231 
232  //- Remove a directory and its contents
233  // \param dir the directory to remove
234  // \param silent do not report missing directory
235  // \param emptyOnly only remove empty directories (recursive)
236  virtual bool rmDir
237  (
238  const fileName& dir,
239  const bool silent = false,
240  const bool emptyOnly = false
241  ) const;
242 
243 
244  // (reg)IOobject functionality
245 
246  //- Search for an object. checkGlobal : also check undecomposed case
247  virtual fileName filePath
248  (
249  const bool checkGlobal,
250  const IOobject& io,
251  const word& typeName,
252  const bool search
253  ) const;
254 
255  //- Search for a directory. checkGlobal : also check undecomposed
256  // case
257  virtual fileName dirPath
258  (
259  const bool checkGlobal,
260  const IOobject& io,
261  const bool search
262  ) const;
263 
264  //- Search directory for objects. Used in IOobjectList.
265  virtual fileNameList readObjects
266  (
267  const objectRegistry& db,
268  const fileName& instance,
269  const fileName& local,
270  word& newInstance
271  ) const;
272 
273  //- Read object header from supplied file
274  virtual bool readHeader
275  (
276  IOobject&,
277  const fileName&,
278  const word& typeName
279  ) const;
280 
281  //- Reads header for regIOobject and returns an ISstream
282  // to read the contents.
284  (
285  regIOobject&,
286  const fileName&,
287  const word& typeName,
288  const bool readOnProc = true
289  ) const;
290 
291  //- Top-level read
292  virtual bool read
293  (
294  regIOobject&,
295  const bool masterOnly,
297  const word& typeName
298  ) const;
299 
300  //- Generate an ISstream that reads a file
301  virtual autoPtr<ISstream> NewIFstream(const fileName&) const;
302 
303  //- Generate an OSstream that writes a file
305  (
306  const fileName& pathname,
307  IOstreamOption streamOpt = IOstreamOption(),
308  const bool writeOnProc = true
309  ) const;
310 
311  //- Generate an OSstream that writes a file
313  (
315  const fileName& pathname,
316  IOstreamOption streamOpt = IOstreamOption(),
317  const bool writeOnProc = true
318  ) const;
319 };
320 
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 } // End namespace fileOperations
325 } // End namespace Foam
326 
327 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328 
329 #endif
330 
331 // ************************************************************************* //
virtual bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true) const
Does the name exist (as DIRECTORY or FILE) in the file system?
A class for handling file names.
Definition: fileName.H:71
virtual bool mv(const fileName &src, const fileName &dst, const bool followLink=false) const
Rename src to dst.
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
virtual bool chMod(const fileName &, const mode_t) const
Set the file mode.
virtual fileName filePath(const bool checkGlobal, const IOobject &io, const word &typeName, const bool search) const
Search for an object. checkGlobal : also check undecomposed case.
uncollatedFileOperation(bool verbose=false)
Default construct.
virtual bool read(regIOobject &, const bool masterOnly, const IOstreamOption::streamFormat format, const word &typeName) const
Top-level read.
virtual mode_t mode(const fileName &, const bool followLink=true) const
Return the file mode.
fileName filePathInfo(const bool checkGlobal, const bool isFile, const IOobject &io, const bool search) const
Search for an object.
virtual bool readHeader(IOobject &, const fileName &, const word &typeName) const
Read object header from supplied file.
virtual bool rmDir(const fileName &dir, const bool silent=false, const bool emptyOnly=false) const
Remove a directory and its contents.
virtual double highResLastModified(const fileName &, const bool followLink=true) const
Return time of last file modification.
A simple container for options an IOstream can normally have.
virtual bool mkDir(const fileName &, mode_t=0777) const
Make directory.
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
An encapsulation of filesystem-related operations.
virtual bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true) const
Does the name exist as a FILE in the file system?
fileOperation that assumes file operations are local.
TypeName("uncollated")
Runtime type information.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
atomicType
Atomic operations (output)
virtual fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true, const bool followLink=true) const
Read a directory and return the entries as a string list.
virtual bool isDir(const fileName &, const bool followLink=true) const
Does the name exist as a DIRECTORY in the file system?
virtual autoPtr< ISstream > NewIFstream(const fileName &) const
Generate an ISstream that reads a file.
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool cp(const fileName &src, const fileName &dst, const bool followLink=true) const
Copy, recursively if necessary, the source to the destination.
A regular file.
Definition: fileName.H:83
virtual refPtr< dirIndexList > lookupProcessorsPath(const fileName &) const
Lookup name of processorsDDD using cache.
virtual off_t fileSize(const fileName &, const bool followLink=true) const
Return size of file.
bool local
Definition: EEqn.H:20
virtual time_t lastModified(const fileName &, const bool followLink=true) const
Return time of last file modification.
virtual bool rm(const fileName &) const
Remove a file, returning true if successful otherwise false.
virtual bool ln(const fileName &src, const fileName &dst) const
Create a softlink. dst should not exist. Returns true if.
virtual fileName dirPath(const bool checkGlobal, const IOobject &io, const bool search) const
Search for a directory. checkGlobal : also check undecomposed.
word format(conversionProperties.get< word >("format"))
virtual void storeComm() const
Transfer ownership of communicator to this fileOperation. Use with caution.
virtual bool mvBak(const fileName &, const std::string &ext="bak") const
Rename to a corresponding backup file.
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
streamFormat
Data format (ascii | binary)
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:640
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
virtual autoPtr< OSstream > NewOFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const
Generate an OSstream that writes a file.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
virtual fileName::Type type(const fileName &, const bool followLink=true) const
Return the file type: DIRECTORY, FILE or SYMLINK.
Type
Enumerations to handle directory entry types.
Definition: fileName.H:80
Namespace for OpenFOAM.
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool readOnProc=true) const
Reads header for regIOobject and returns an ISstream.