fileOperation.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::fileOperation
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #ifndef fileOperation_H
30 #define fileOperation_H
31 
32 #include "ISstream.H"
33 #include "Ostream.H"
34 #include "autoPtr.H"
35 #include "fileNameList.H"
36 #include "instantList.H"
37 #include "fileMonitor.H"
38 #include "labelList.H"
39 #include "Switch.H"
40 #include "tmpNrc.H"
41 #include "NamedEnum.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 class IOobject;
49 class regIOobject;
50 class objectRegistry;
51 class Time;
52 
53 /*---------------------------------------------------------------------------*\
54  Class fileOperation Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class fileOperation
58 {
59 public:
60 
61  //- Enumeration for the location of an IOobject
62  enum pathType
63  {
64  NOTFOUND, // not found
65  ABSOLUTE, // instance is absolute directory
66  OBJECT, // io.objectPath() exists
67  WRITEOBJECT, // write path exists
68  PROCUNCOLLATED, // objectPath exists in processor0
69  PROCBASEOBJECT, // objectPath exists in specified, constant
70  // processorsDir (usually 'processorsDDD')
71  PROCOBJECT, // objectPath exists in locally differing
72  // processorsDir (e.g. 'processorsDDD_0-1')
73  PARENTOBJECT, // parent of object path
74  FINDINSTANCE, // file found in time directory
75  PROCUNCOLLATEDINSTANCE, // as PROCUNCOLLATED but with instance
76  PROCBASEINSTANCE, // as PROCBASEOBJECT but with instance
77  PROCINSTANCE // as PROCOBJECT but with instance
78  };
79 
81 
82  //- Description of processor directory naming:
83  // - processor directory naming
84  // - whether directory contains a range
85  // (so differs on different processors)
86  // - index in range
88 
90 
91 
92 protected:
93 
94  // Protected data
95 
96  //- Communicator to use
97  const label comm_;
98 
99  //- Detected processors directories
101 
102  //- file-change monitor for all registered files
104 
105 
106  // Protected Member Functions
107 
108  fileMonitor& monitor() const;
109 
110  //- Sort directory entries according to time value
111  static instantList sortTimes(const fileNameList&, const word&);
112 
113  //- Merge two times
114  static void mergeTimes
115  (
116  const instantList& extraTimes,
117  const word& constantName,
118  instantList& times
119  );
120 
121  //- Helper: check for file (isFile) or directory (!isFile)
122  static bool isFileOrDir(const bool isFile, const fileName&);
123 
124  //- Detect presence of processorsDDD
125  void cacheProcessorsPath(const fileName& fName) const;
126 
127  //- Lookup name of processorsDDD using cache. Return empty fileName
128  // if not found
130 
131  //- Does ioobject exist. Is either a directory (empty name()) or
132  // a file
133  bool exists(IOobject& io) const;
134 
135 
136 public:
137 
138  // Static data
139 
140  //- Return the processors directory name (usually "processors")
141  static word processorsBaseDir;
142 
143  //- Default fileHandler
144  static word defaultFileHandler;
145 
146 
147  // Public data types
148 
149  //- Runtime type information
150  TypeName("fileOperation");
151 
152 
153  //- Static fileOperation
155 
156 
157  // Constructors
158 
159  //- Construct null
160  fileOperation(const label comm);
161 
162 
163  // Declare run-time constructor selection table
164 
166  (
167  autoPtr,
169  word,
170  (
171  const bool verbose
172  ),
173  (verbose)
174  );
175 
176 
177  // Selectors
178 
179  //- Select type
180  static autoPtr<fileOperation> New(const word& type, const bool verbose);
181 
182 
183  //- Destructor
184  virtual ~fileOperation();
185 
186 
187  // Member Functions
188 
189  // OSSpecific equivalents
190 
191  //- Make directory
192  virtual bool mkDir(const fileName&, mode_t=0777) const = 0;
193 
194  //- Set the file mode
195  virtual bool chMod(const fileName&, const mode_t) const = 0;
196 
197  //- Return the file mode
198  virtual mode_t mode
199  (
200  const fileName&,
201  const bool followLink = true
202  ) const = 0;
203 
204  //- Return the file type: DIRECTORY, FILE or LINK
205  virtual fileName::Type type
206  (
207  const fileName&,
208  const bool followLink = true
209  ) const = 0;
210 
211  //- Does the name exist (as DIRECTORY or FILE) in the file system?
212  // Optionally enable/disable check for gzip file.
213  virtual bool exists
214  (
215  const fileName&,
216  const bool checkGzip=true,
217  const bool followLink = true
218  ) const = 0;
219 
220  //- Does the name exist as a DIRECTORY in the file system?
221  virtual bool isDir
222  (
223  const fileName&,
224  const bool followLink = true
225  ) const = 0;
226 
227  //- Does the name exist as a FILE in the file system?
228  // Optionally enable/disable check for gzip file.
229  virtual bool isFile
230  (
231  const fileName&,
232  const bool checkGzip=true,
233  const bool followLink = true
234  ) const = 0;
235 
236  //- Return size of file
237  virtual off_t fileSize
238  (
239  const fileName&,
240  const bool followLink = true
241  ) const = 0;
242 
243  //- Return time of last file modification
244  virtual time_t lastModified
245  (
246  const fileName&,
247  const bool followLink = true
248  ) const = 0;
249 
250  //- Return time of last file modification
251  virtual double highResLastModified
252  (
253  const fileName&,
254  const bool followLink = true
255  ) const = 0;
256 
257  //- Read a directory and return the entries as a string list
258  virtual fileNameList readDir
259  (
260  const fileName&,
262  const bool filtergz=true,
263  const bool followLink = true
264  ) const = 0;
265 
266  //- Copy, recursively if necessary, the source to the destination
267  virtual bool cp
268  (
269  const fileName& src,
270  const fileName& dst,
271  const bool followLink = true
272  ) const = 0;
273 
274  //- Create a softlink. dst should not exist. Returns true if
275  // successful.
276  virtual bool ln(const fileName& src, const fileName& dst) const = 0;
277 
278  //- Rename src to dst
279  virtual bool mv
280  (
281  const fileName& src,
282  const fileName& dst,
283  const bool followLink = false
284  ) const = 0;
285 
286  //- Rename to a corresponding backup file
287  // If the backup file already exists, attempt with
288  // "01" .. "99" suffix
289  virtual bool mvBak
290  (
291  const fileName&,
292  const std::string& ext = "bak"
293  ) const = 0;
294 
295  //- Remove a file, returning true if successful otherwise false
296  virtual bool rm(const fileName&) const = 0;
297 
298  //- Remove a directory and its contents
299  virtual bool rmDir(const fileName&) const = 0;
300 
301  // //- Open a shared library. Return handle to library. Print error
302  // // message if library cannot be loaded (check = true)
303  // virtual void* dlOpen
304  // (
305  // const fileName& lib,
306  // const bool check = true
307  // ) const = 0;
308 
309 
310  // (reg)IOobject functinality
311 
312  //- Generate disk file name for object. Opposite of filePath.
313  // Optional wanted typeName.
314  virtual fileName objectPath
315  (
316  const IOobject& io,
317  const word& typeName
318  ) const;
319 
320  //- Search for an object. checkGlobal : also check undecomposed case
321  // Optional wanted typeName.
322  virtual fileName filePath
323  (
324  const bool checkGlobal,
325  const IOobject&,
326  const word& typeName
327  ) const = 0;
328 
329  //- Search for a directory. checkGlobal : also check undecomposed
330  // case
331  virtual fileName dirPath
332  (
333  const bool checkGlobal,
334  const IOobject&
335  ) const = 0;
336 
337  //- Search directory for objects. Used in IOobjectList.
338  virtual fileNameList readObjects
339  (
340  const objectRegistry& db,
341  const fileName& instance,
342  const fileName& local,
343  word& newInstance
344  ) const;
345 
346  //- Read object header from supplied file
347  virtual bool readHeader
348  (
349  IOobject&,
350  const fileName&,
351  const word& typeName
352  ) const = 0;
353 
354  //- Reads header for regIOobject and returns an ISstream
355  // to read the contents.
357  (
358  regIOobject&,
359  const fileName&,
360  const word& typeName,
361  const bool valid = true
362  ) const = 0;
363 
364  //- Top-level read
365  virtual bool read
366  (
367  regIOobject&,
368  const bool masterOnly,
370  const word& typeName
371  ) const = 0;
372 
373  //- Writes a regIOobject (so header, contents and divider).
374  // Returns success state. Default action is to write to
375  // the objectPath using writeData. If !valid the
376  // file does not need to be written (this is used e.g. to
377  // suppress empty local lagrangian data)
378  virtual bool writeObject
379  (
380  const regIOobject&,
384  const bool valid = true
385  ) const;
386 
387 
388  // Filename (not IOobject) operations
389 
390  //- Search for a file or directory. Use IOobject version in
391  // preference
392  virtual fileName filePath(const fileName&) const;
393 
394  //- Generate an ISstream that reads a file
395  virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
396 
397  //- Generate an Ostream that writes a file
399  (
400  const fileName& pathname,
404  const bool valid = true
405  ) const = 0;
406 
407 
408  // File modification checking
409 
410  //- Add watching of a file. Returns handle
411  virtual label addWatch(const fileName&) const;
412 
413  //- Remove watch on a file (using handle)
414  virtual bool removeWatch(const label) const;
415 
416  //- Find index (or -1) of file in list of handles
417  virtual label findWatch
418  (
419  const labelList& watchIndices,
420  const fileName&
421  ) const;
422 
423  //- Helper: add watches for list of regIOobjects
424  virtual void addWatches(regIOobject&, const fileNameList&) const;
425 
426  //- Get name of file being watched (using handle)
427  virtual fileName getFile(const label) const;
428 
429  //- Update state of all files
430  virtual void updateStates
431  (
432  const bool masterOnly,
433  const bool syncPar
434  ) const;
435 
436  //- Get current state of file (using handle)
437  virtual fileMonitor::fileState getState(const label) const;
438 
439  //- Set current state of file (using handle) to unmodified
440  virtual void setUnmodified(const label) const;
441 
442 
443  // Other
444 
445  //- Actual name of processors dir (for use in mode PROCOBJECT,
446  // PROCINSTANCE)
447  virtual word processorsDir(const IOobject& io) const
448  {
449  return processorsBaseDir;
450  }
451 
452  //- Actual name of processors dir (for use in mode PROCOBJECT,
453  // PROCINSTANCE)
454  virtual word processorsDir(const fileName&) const
455  {
456  return processorsBaseDir;
457  }
458 
459  //- Set number of processor directories/results. Only used in
460  // decomposePar
461  virtual void setNProcs(const label nProcs);
462 
463  //- Get number of processor directories/results. Used for e.g.
464  // reconstructPar, argList checking
465  virtual label nProcs
466  (
467  const fileName& dir,
468  const fileName& local = ""
469  ) const;
470 
471  //- Get sorted list of times
472  virtual instantList findTimes(const fileName&, const word&) const;
473 
474  //- Find instance where IOobject is. Fails if cannot be found
475  // and readOpt() is MUST_READ/MUST_READ_IF_MODIFIED. Otherwise
476  // returns stopInstance.
477  virtual IOobject findInstance
478  (
479  const IOobject& io,
480  const scalar startValue,
481  const word& stopInstance
482  ) const;
483 
484  //- Callback for time change
485  virtual void setTime(const Time&) const
486  {}
487 
488  //- Forcibly wait until all output done. Flush any cached data
489  virtual void flush() const;
490 
491  //- Generate path (like io.path) from root+casename with any
492  // 'processorXXX' replaced by procDir (usually 'processsors')
494  (
495  const IOobject&,
496  const word& procDir
497  ) const;
498 
499  //- Generate path (like io.path) with provided instance and any
500  // 'processorXXX' replaced by procDir (usually 'processsors')
502  (
503  const IOobject&,
504  const word& instance,
505  const word& procDir
506  ) const;
507 
508  //- Operating on fileName: replace processorXXX with procDir
509  fileName processorsPath(const fileName&, const word& procDir) const;
510 
511  //- Split fileName into part before 'processor' and part after.
512  // Returns -1 or processor number and optionally number
513  // of processors. Use with care.
514  // - path/"processor"+Foam::name(proci)/local reconstructs input
515  // - path/"processors"+Foam::name(nProcs)/local reconstructs
516  // collated processors equivalence
518  (
519  const fileName&,
520  fileName& path,
521  fileName& procDir,
522  fileName& local,
523  label& groupStart,
524  label& groupSize,
525  label& nProcs
526  );
527 
528  //- Detect processor number from '/aa/bb/processorDDD/cc'
529  static label detectProcessorPath(const fileName&);
530 };
531 
532 
533 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
534 // Global declarations: defined in fileOperation.C
535 
536 //- Get current file handler
537 const fileOperation& fileHandler();
538 
539 //- Reset file handler
541 
542 //- Recursively search the given directory for the file
543 // returning the path relative to the directory or
544 // fileName::null if not found
545 fileName search(const word& file, const fileName& directory);
546 
547 //- Copy all the files from the source to the target directory
548 void cpFiles(const fileName& srcDir, const fileName& targetDir);
549 
550 
551 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
552 
553 } // End namespace Foam
554 
555 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
556 
557 #endif
558 
559 // ************************************************************************* //
pathType
Enumeration for the location of an IOobject.
Definition: fileOperation.H:61
virtual bool writeObject(const regIOobject &, IOstream::streamFormat format=IOstream::ASCII, IOstream::versionNumber version=IOstream::currentVersion, IOstream::compressionType compression=IOstream::UNCOMPRESSED, const bool valid=true) const
Writes a regIOobject (so header, contents and divider).
virtual bool rm(const fileName &) const =0
Remove a file, returning true if successful otherwise false.
autoPtr< fileMonitor > monitorPtr_
file-change monitor for all registered files
virtual bool rmDir(const fileName &) const =0
Remove a directory and its contents.
List< dirIndex > dirIndexList
Definition: fileOperation.H:88
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
virtual double highResLastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
A class for handling file names.
Definition: fileName.H:69
virtual fileMonitor::fileState getState(const label) const
Get current state of file (using handle)
virtual bool chMod(const fileName &, const mode_t) const =0
Set the file mode.
Checking for changes to files.
Definition: fileMonitor.H:61
virtual fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true, const bool followLink=true) const =0
Read a directory and return the entries as a string list.
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:66
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
void cpFiles(const fileName &srcDir, const fileName &targetDir)
Copy all the files from the source to the target directory.
virtual bool mvBak(const fileName &, const std::string &ext="bak") const =0
Rename to a corresponding backup file.
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:69
virtual off_t fileSize(const fileName &, const bool followLink=true) const =0
Return size of file.
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
virtual bool mkDir(const fileName &, mode_t=0777) const =0
Make directory.
bool exists(IOobject &io) const
Does ioobject exist. Is either a directory (empty name()) or.
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
virtual bool mv(const fileName &src, const fileName &dst, const bool followLink=false) const =0
Rename src to dst.
virtual ~fileOperation()
Destructor.
word format(conversionProperties.lookup("format"))
Tuple2< fileName, Tuple2< pathType, label > > dirIndex
Description of processor directory naming:
Definition: fileOperation.H:86
tmpNrc< dirIndexList > lookupProcessorsPath(const fileName &) const
Lookup name of processorsDDD using cache. Return empty fileName.
virtual bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist as a FILE in the file system?
fileName processorsPath(const IOobject &, const word &instance, const word &procDir) const
Generate path (like io.path) with provided instance and any.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
declareRunTimeSelectionTable(autoPtr, fileOperation, word,(const bool verbose),(verbose))
virtual mode_t mode(const fileName &, const bool followLink=true) const =0
Return the file mode.
virtual fileName::Type type(const fileName &, const bool followLink=true) const =0
Return the file type: DIRECTORY, FILE or LINK.
static autoPtr< fileOperation > New(const word &type, const bool verbose)
Select type.
void cacheProcessorsPath(const fileName &fName) const
Detect presence of processorsDDD.
virtual void updateStates(const bool masterOnly, const bool syncPar) const
Update state of all files.
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool valid=true) const =0
Reads header for regIOobject and returns an ISstream.
virtual bool ln(const fileName &src, const fileName &dst) const =0
Create a softlink. dst should not exist. Returns true if.
A class for handling words, derived from string.
Definition: word.H:59
virtual fileName getFile(const label) const
Get name of file being watched (using handle)
virtual IOobject findInstance(const IOobject &io, const scalar startValue, const word &stopInstance) const
Find instance where IOobject is. Fails if cannot be found.
virtual instantList findTimes(const fileName &, const word &) const
Get sorted list of times.
virtual autoPtr< Ostream > NewOFstream(const fileName &pathname, IOstream::streamFormat format=IOstream::ASCII, IOstream::versionNumber version=IOstream::currentVersion, IOstream::compressionType compression=IOstream::UNCOMPRESSED, const bool valid=true) const =0
Generate an Ostream that writes a file.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
virtual autoPtr< ISstream > NewIFstream(const fileName &) const =0
Generate an ISstream that reads a file.
virtual void addWatches(regIOobject &, const fileNameList &) const
Helper: add watches for list of regIOobjects.
virtual fileName filePath(const bool checkGlobal, const IOobject &, const word &typeName) const =0
Search for an object. checkGlobal : also check undecomposed case.
const fileOperation & fileHandler()
Get current file handler.
virtual label addWatch(const fileName &) const
Add watching of a file. Returns handle.
An STL-conforming hash table.
Definition: HashTable.H:62
virtual bool cp(const fileName &src, const fileName &dst, const bool followLink=true) const =0
Copy, recursively if necessary, the source to the destination.
static void mergeTimes(const instantList &extraTimes, const word &constantName, instantList &times)
Merge two times.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static word processorsBaseDir
Return the processors directory name (usually "processors")
fileName processorsCasePath(const IOobject &, const word &procDir) const
Generate path (like io.path) from root+casename with any.
fileOperation(const label comm)
Construct null.
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
fileMonitor & monitor() const
Definition: fileOperation.C:86
HashTable< dirIndexList > procsDirs_
Detected processors directories.
Definition: fileOperation.H:99
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:52
static bool isFileOrDir(const bool isFile, const fileName &)
Helper: check for file (isFile) or directory (!isFile)
static autoPtr< fileOperation > fileHandlerPtr_
Static fileOperation.
virtual label nProcs(const fileName &dir, const fileName &local="") const
Get number of processor directories/results. Used for e.g.
virtual void setTime(const Time &) const
Callback for time change.
static word defaultFileHandler
Default fileHandler.
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
virtual bool isDir(const fileName &, const bool followLink=true) const =0
Does the name exist as a DIRECTORY in the file system?
virtual time_t lastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
static instantList sortTimes(const fileNameList &, const word &)
Sort directory entries according to time value.
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
const label comm_
Communicator to use.
Definition: fileOperation.H:96
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
virtual bool read(regIOobject &, const bool masterOnly, const IOstream::streamFormat format, const word &typeName) const =0
Top-level read.
static label detectProcessorPath(const fileName &)
Detect processor number from &#39;/aa/bb/processorDDD/cc&#39;.
TypeName("fileOperation")
Runtime type information.
Registry of regIOobjects.
virtual label findWatch(const labelList &watchIndices, const fileName &) const
Find index (or -1) of file in list of handles.
virtual fileName dirPath(const bool checkGlobal, const IOobject &) const =0
Search for a directory. checkGlobal : also check undecomposed.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
static const NamedEnum< pathType, 12 > pathTypeNames_
Definition: fileOperation.H:79
virtual fileName objectPath(const IOobject &io, const word &typeName) const
Generate disk file name for object. Opposite of filePath.
static label splitProcessorPath(const fileName &, fileName &path, fileName &procDir, fileName &local, label &groupStart, label &groupSize, label &nProcs)
Split fileName into part before &#39;processor&#39; and part after.
virtual void setNProcs(const label nProcs)
Set number of processor directories/results. Only used in.
virtual bool removeWatch(const label) const
Remove watch on a file (using handle)
virtual bool readHeader(IOobject &, const fileName &, const word &typeName) const =0
Read object header from supplied file.
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
virtual word processorsDir(const IOobject &io) const
Actual name of processors dir (for use in mode PROCOBJECT,.
Namespace for OpenFOAM.