fileOperation.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::fileOperation
29 
30 Description
31  An encapsulation of filesystem-related operations.
32 
33  Several of the file handlers can be configured to use specific ranks for
34  IO operations. These can either be defined from the command-line
35  \c -ioRanks option, or via the \c FOAM_ENV environment variable.
36  In either case, the list of IO ranks shall always include the value
37  \b 0 (master rank). The ranks may be specified as a plain list, or as an
38  OpenFOAM list. The special value \em host can be used to specify selection
39  based on hostname.
40 
41  For example,
42  \verbatim
43  FOAM_IORANKS='0 4 8' decomposePar -fileHandler hostCollated
44  FOAM_IORANKS='0,4,8' decomposePar -fileHandler hostCollated
45  FOAM_IORANKS='(0 4 8)' decomposePar -fileHandler hostCollated
46  \endverbatim
47  will generate
48  \verbatim
49  processors12_0-3/ : containing data for processors 0 to 3
50  processors12_4-7/ : containing data for processors 4 to 7
51  processors12_8-11/ : containing data for processors 8 to 11
52  \endverbatim
53 
54  The value \em host can be used to specify a single IO rank for each
55  host. For example,
56  \verbatim
57  decomposePar -fileHandler collated -ioRanks host
58  decomposePar -fileHandler hostCollated
59  \endverbatim
60 
61 Environment
62  - \c FOAM_ENV : list of io-ranks as plain space or comma separated
63  list or as an OpenFOAM formatted list. Eg, '(0 4 8)'
64 
65 Namespace
66  Foam::fileOperations
67 
68 Description
69  Namespace for implementations of a fileOperation
70 
71 \*---------------------------------------------------------------------------*/
72 
73 #ifndef Foam_fileOperation_H
74 #define Foam_fileOperation_H
75 
76 #include "ISstream.H"
77 #include "Ostream.H"
78 #include "UPstream.H"
79 #include "fileMonitor.H"
80 #include "fileNameList.H"
81 #include "instantList.H"
82 #include "refPtr.H"
83 #include "bitSet.H"
84 #include "Enum.H"
85 #include "Tuple2.H"
86 #include "InfoProxy.H"
87 
88 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89 
90 namespace Foam
91 {
92 
93 // Forward Declarations
94 class fileOperation;
95 class objectRegistry;
96 class regIOobject;
97 class IOobject;
98 class Time;
99 
100 Ostream& operator<<(Ostream& os, const InfoProxy<fileOperation>& info);
101 
102 /*---------------------------------------------------------------------------*\
103  Class fileOperation Declaration
104 \*---------------------------------------------------------------------------*/
105 
106 class fileOperation
107 {
108 public:
110  // Public Data Types
112  //- Enumeration for the location of an IOobject
113  enum pathType : int
114  {
115  NOTFOUND = 0,
116  ABSOLUTE,
117  OBJECT,
119 
120  // NOTE: increasing precedence (uncollated, collated, rank-collated)
121 
130  FINDINSTANCE,
133  PROCINSTANCE
134  };
135  static const Enum<pathType> pathTypeNames_;
137  //- Augment fileName with pathType and local offset
140 
141  //- For addressing a range of processors,
142  //- identical to UPstream::rangeType
144 
145 
146 private:
147 
148  // Private Static Data
149 
150  //- Storage of the dummy file handler (demand-driven)
151  static refPtr<fileOperation> dummyHandlerPtr_;
152 
153 
154 protected:
155 
156  // Protected Static Data
157 
158  //- Cache level (eg, for caching time directories). Default: 1
159  static int cacheLevel_;
160 
161 
162  // Protected Data
164  //- Communicator to use
165  mutable label comm_;
166 
167  //- Overall number of processors.
168  // Used to synthesise processor directory naming:
169  // - parallel: UPstream::nProcs(UPstream::commWorld())
170  // - non-parallel: detected from processor dir naming ('processorsNN')
171  label nProcs_;
172 
173  //- Distributed roots (parallel run)
174  mutable bool distributed_;
175 
176  //- The list of IO ranks (global ranks)
177  // Primarily for additional bookkeeping in non-parallel
178  const labelList ioRanks_;
179 
180  //- Detected processors directories
182 
183  //- File-change monitor for all registered files
184  mutable std::unique_ptr<fileMonitor> monitorPtr_;
186 
187  // Protected Member Functions
188 
189  //- Get or create fileMonitor singleton
190  fileMonitor& monitor() const;
191 
192  //- Merge two times
193  static void mergeTimes
194  (
195  const instantList& extraTimes,
196  const word& constantName,
197  instantList& times
198  );
199 
200  //- Helper: check for file (isFile) or directory (!isFile)
201  static bool isFileOrDir(const bool isFile, const fileName&);
203  //- Lookup name of processorsDDD using cache.
204  // \return empty fileName if not found.
206  (
207  const fileName& objectPath,
208  const bool syncPar
209  ) const;
210 
211  //- Lookup name of processorsDDD using cache.
212  // \note To be called on all processors
213  // \return empty fileName if not found.
215  (
216  const fileName& objectPath
217  ) const;
218 
219  //- Does IOobject exist?
220  //- Is either a directory (empty name()) or a file
221  bool exists(IOobject& io) const;
222 
223 
224  //- Is proci a master rank in the communicator (in parallel)
225  //- or a master rank in the IO ranks (non-parallel)
226  bool isIOrank(const label proci) const;
227 
228  //- Helper: output which ranks are IO
229  void printRanks() const;
230 
231 
232  //- Construction helper: check for locally allocated communicator
233  static inline label getManagedComm(const label communicator)
234  {
235  return
236  (
237  (
238  communicator < 0
239  || communicator == UPstream::commGlobal()
240  || communicator == UPstream::commSelf()
241  || communicator == UPstream::commWorld()
242  )
243  ? -1
244  : communicator
245  );
246  }
247 
248 
249 private:
250 
251  // Private Member Functions
252 
253  //- Low-level implementation for subsetting 'clone' operation
254  static autoPtr<fileOperation> New_impl
255  (
256  const fileOperation& origHandler,
258  const labelUList& subProcs,
259  bool verbose
260  );
261 
262 
263 public:
264 
265  // Static Data
266 
267  //- Return the processors directory name (usually "processors")
268  static word processorsBaseDir;
269 
270  //- Name of the default fileHandler
272 
273 
274  // Public Data Types
275 
276  //- Runtime type information
277  TypeName("fileOperation");
278 
279 
280  //- The currently active file handler. Avoid accessing directly
282 
283  //- Reference to a dummy file handler.
284  static refPtr<fileOperation> null();
285 
286 
287  // Constructors
288 
289  //- Construct from communicator,
290  //- optionally with specified io-ranks and/or distributed roots
291  explicit fileOperation
292  (
293  const label comm,
295  const bool distributedRoots = false
296  );
297 
298  //- Construct from communicator with specified io-ranks
299  explicit fileOperation
300  (
301  const Tuple2<label, labelList>& commAndIORanks,
302  const bool distributedRoots = false
303  );
304 
305 
306  // Declare run-time constructor selection table
307 
309  (
312  word,
313  (
314  bool verbose
315  ),
316  (verbose)
317  );
318 
320  (
321  autoPtr,
323  comm,
324  (
325  const Tuple2<label, labelList>& commAndIORanks,
326  const bool distributedRoots,
327  bool verbose
328  ),
329  (commAndIORanks, distributedRoots, verbose)
330  );
331 
332 
333  // Selectors
334 
335  //- Select fileHandler-type.
336  //- Uses defaultFileHandler if the handlerType is empty.
338  (
339  const word& handlerType,
340  bool verbose = false
341  );
342 
343  //- Select fileHandler-type.
344  //- Uses defaultFileHandler if the handlerType is empty.
346  (
347  const word& handlerType,
348  const Tuple2<label, labelList>& commAndIORanks,
349  const bool distributedRoots,
350  bool verbose = false
351  );
352 
353 
354  //- Replicate the given fileHandler properties with
355  //- a subset of (global) ranks.
356  // Always includes rank 0 and constrained by the global numProcs
357  // Returns nullptr if myProcNo is not involved.
359  (
361  const fileOperation& origHandler,
363  const boolUList& useProc,
364  bool verbose = false
365  );
366 
367  //- Replicate the given fileHandler properties with
368  //- a subset of (global) ranks.
369  // Always includes rank 0 and constrained by the global numProcs
370  // Returns nullptr if myProcNo is not involved.
372  (
374  const fileOperation& origHandler,
376  const bitSet& useProc,
377  bool verbose = false
378  );
379 
380 
381  //- Destructor
382  virtual ~fileOperation() = default;
383 
384 
385  // Factory Methods, Singleton-type Functions
386 
387  //- The commonly used uncollatedFileOperation
389 
390  //- Return the current file handler.
391  //- Will create the default file handler if necessary.
392  static const fileOperation& fileHandler();
393 
394  //- Delete current file handler.
395  // \returns the old handler.
396  // Should have [[nodiscard]], but gcc ignores void casting.
397  static refPtr<fileOperation> fileHandler(std::nullptr_t);
398 
399  //- Replace the current file handler.
400  // The following are considered no-ops:
401  // - an empty/invalid newHandler does \b not delete, use a literal
402  // \c nullptr (std::nullptr_t) for that
403  // - if new handler and current handler are identical (same pointer).
404  // .
405  // \returns the old handler (on change), nullptr otherwise
406  // Should have [[nodiscard]], but gcc ignores void casting.
408  (
409  refPtr<fileOperation>& newHandler
410  );
411 
412  //- Replace the current file handler.
413  // The following are considered no-ops:
414  // - an empty/invalid newHandler does \b not delete, use a literal
415  // \c nullptr (std::nullptr_t) for that
416  // - if new handler and current handler are identical (same pointer).
417  // .
418  // \returns the old handler (on change), nullptr otherwise
419  // Should have [[nodiscard]], but gcc ignores void casting.
421  (
422  refPtr<fileOperation>&& newHandler
423  );
424 
425  //- Replace the current file handler.
426  // The following are considered no-ops:
427  // - an empty/invalid newHandler does \b not delete, use a literal
428  // \c nullptr (std::nullptr_t) for that
429  // - if new handler and current handler are identical (same pointer).
430  // .
431  // \returns the old handler (on change), nullptr otherwise
432  // Should have [[nodiscard]], but gcc ignores void casting.
434  (
435  autoPtr<fileOperation>&& newHandler
436  );
437 
438 
439  // Static Functions
440 
441  //- Return cache level
442  static int cacheLevel() noexcept
443  {
444  return cacheLevel_;
445  }
446 
447  //- Set cache level (0 = off). \return the previous value
448  static int cacheLevel(int level) noexcept
449  {
450  int old(cacheLevel_);
451  cacheLevel_ = level;
452  return old;
453  }
454 
455  //- Sort directory entries according to time value,
456  // with "constant" appearing first (if it exists)
457  static instantList sortTimes
458  (
459  const fileNameList& dirEntries,
460  const word& constantName = "constant"
461  );
462 
463  //- True if the file names are identical. False on an empty list
464  static bool uniformFile(const fileNameList& names);
465 
466  //- True if the file name is identical on all ranks
467  static bool uniformFile(const label comm, const fileName& name);
468 
469 
470  // Member Functions
471 
472  // Characteristics
473 
474  //- Communicator to use
475  label comm() const noexcept
476  {
477  return comm_;
478  }
479 
480  //- Set communicator to use [mutable]. Negative values are a no-op.
481  // \return old value
482  label comm(label communicator) const noexcept
483  {
484  label old(comm_);
485  if (communicator >= 0) comm_ = communicator;
486  return old;
487  }
488 
489  //- Distributed roots (parallel run)
490  bool distributed() const noexcept
491  {
492  return distributed_;
493  }
494 
495  //- Set distributed roots on/off [mutable]
496  // \return old value
497  bool distributed(bool on) const noexcept
498  {
499  bool old(distributed_);
500  distributed_ = on;
501  return old;
502  }
503 
504  //- The list of IO ranks (global ranks)
505  // Primarily for additional bookkeeping in non-parallel
506  const labelList& ioRanks() const noexcept { return ioRanks_; }
507 
508  //- Return info proxy,
509  //- used to print information to a stream
510  InfoProxy<fileOperation> info() const noexcept { return *this; }
511 
512 
513  // Member Functions
514 
515  //- True if the fileOperation can be considered valid.
516  //- At the moment, primarily used to detect the dummy fileOperation.
517  virtual bool good() const { return true; }
518 
519  //- Transfer ownership of communicator to this fileOperation.
520  //- Use with caution
521  virtual void storeComm() const = 0;
522 
523 
524  // OSSpecific equivalents
525 
526  //- Make directory
527  virtual bool mkDir(const fileName&, mode_t=0777) const = 0;
528 
529  //- Set the file mode
530  virtual bool chMod(const fileName&, const mode_t) const = 0;
531 
532  //- Return the file mode
533  virtual mode_t mode
534  (
535  const fileName&,
536  const bool followLink = true
537  ) const = 0;
538 
539  //- Return the file type: DIRECTORY, FILE or SYMLINK
540  virtual fileName::Type type
541  (
542  const fileName&,
543  const bool followLink = true
544  ) const = 0;
545 
546  //- Does the name exist (as DIRECTORY or FILE) in the file system?
547  // Optionally enable/disable check for gzip file.
548  virtual bool exists
549  (
550  const fileName&,
551  const bool checkGzip = true,
552  const bool followLink = true
553  ) const = 0;
554 
555  //- Does the name exist as a DIRECTORY in the file system?
556  virtual bool isDir
557  (
558  const fileName&,
559  const bool followLink = true
560  ) const = 0;
561 
562  //- Does the name exist as a FILE in the file system?
563  // Optionally enable/disable check for gzip file.
564  virtual bool isFile
565  (
566  const fileName&,
567  const bool checkGzip = true,
568  const bool followLink = true
569  ) const = 0;
571  //- Return size of file
572  virtual off_t fileSize
573  (
574  const fileName&,
575  const bool followLink = true
576  ) const = 0;
577 
578  //- Return time of last file modification
579  virtual time_t lastModified
580  (
581  const fileName&,
582  const bool followLink = true
583  ) const = 0;
584 
585  //- Return time of last file modification
586  virtual double highResLastModified
587  (
588  const fileName&,
589  const bool followLink = true
590  ) const = 0;
591 
592  //- Read a directory and return the entries as a string list
593  virtual fileNameList readDir
594  (
595  const fileName&,
597  const bool filtergz=true,
598  const bool followLink = true
599  ) const = 0;
601  //- Copy, recursively if necessary, the source to the destination
602  virtual bool cp
603  (
604  const fileName& src,
605  const fileName& dst,
606  const bool followLink = true
607  ) const = 0;
608 
609  //- Create a softlink. dst should not exist. Returns true if
610  // successful.
611  virtual bool ln(const fileName& src, const fileName& dst) const = 0;
613  //- Rename src to dst
614  virtual bool mv
615  (
616  const fileName& src,
617  const fileName& dst,
618  const bool followLink = false
619  ) const = 0;
620 
621  //- Rename to a corresponding backup file
622  // If the backup file already exists, attempt with
623  // "01" .. "99" suffix
624  virtual bool mvBak
625  (
626  const fileName&,
627  const std::string& ext = "bak"
628  ) const = 0;
629 
630  //- Remove a file, returning true if successful otherwise false
631  virtual bool rm(const fileName&) const = 0;
632 
633  //- Remove a directory and its contents
634  // \param dir the directory to remove
635  // \param silent do not report missing directory
636  // \param emptyOnly only remove empty directories (recursive)
637  virtual bool rmDir
638  (
639  const fileName& dir,
640  const bool silent = false,
641  const bool emptyOnly = false
642  ) const = 0;
643 
644  //- Read dir/file (recursively if necessary) on master of the
645  //- communicator, send and write contents to all 'writeOnProc'
646  //- processors with local file name
647  // \param comm the communicator for broadcasting
648  // \param writeOnProc write on the processor
649  // \param src the source file/directory
650  // \param dst the target file/directory.
651  // If empty, treat as being identical to the src.
652  virtual bool broadcastCopy
653  (
654  const label comm,
655  const bool writeOnProc,
656  const fileName& src,
657  const fileName& dst
658  // always recreates links
659  ) const;
660 
661 
662  // (reg)IOobject functionality
663 
664  //- Generate disk file name for object. Opposite of filePath.
665  // Optional wanted typeName.
666  virtual fileName objectPath
667  (
668  const IOobject& io,
669  const word& typeName
670  ) const;
671 
672  //- Search for an object. checkGlobal : also check undecomposed case
673  // Optional wanted typeName.
674  virtual fileName filePath
675  (
676  const bool checkGlobal,
677  const IOobject&,
678  const word& typeName,
679  const bool search = true
680  ) const = 0;
681 
682  //- Search for a directory. checkGlobal : also check undecomposed
683  // case
684  virtual fileName dirPath
685  (
686  const bool checkGlobal,
687  const IOobject& io,
688  const bool search = true
689  ) const = 0;
690 
691  //- Search directory for objects. Used in IOobjectList.
692  virtual fileNameList readObjects
693  (
694  const objectRegistry& db,
695  const fileName& instance,
696  const fileName& local,
697  word& newInstance
698  ) const;
699 
700  //- Read object header from supplied file
701  virtual bool readHeader
702  (
703  IOobject&,
704  const fileName&,
705  const word& typeName
706  ) const = 0;
707 
708  //- Reads header for regIOobject and returns an ISstream
709  // to read the contents.
711  (
712  regIOobject&,
713  const fileName&,
714  const word& typeName,
715  const bool readOnProc = true
716  ) const = 0;
717 
718  //- Top-level read
719  virtual bool read
720  (
721  regIOobject&,
722  const bool masterOnly,
724  const word& typeName
725  ) const = 0;
726 
727  //- Writes a regIOobject (so header, contents and divider).
728  // Returns success state. Default action is to write to
729  // the objectPath using writeData. If !writeOnProc the
730  // file does not need to be written (this is used e.g. to
731  // suppress empty local lagrangian data)
732  virtual bool writeObject
733  (
734  const regIOobject& io,
735  IOstreamOption streamOpt = IOstreamOption(),
736  const bool writeOnProc = true
737  ) const;
738 
739 
740  // Filename (not IOobject) operations
741 
742  //- Search for a file or directory.
743  //- Use IOobject version in preference
744  virtual fileName filePath
745  (
746  const fileName&,
747  const bool checkGzip = true,
748  const bool followLink = true
749  ) const;
750 
751  //- Generate an ISstream that reads a file
752  virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
753 
754  //- Generate an OSstream that writes a file
756  (
757  const fileName& pathname,
758  IOstreamOption streamOpt = IOstreamOption(),
759  const bool writeOnProc = true
760  ) const = 0;
761 
762  //- Generate an OSstream that writes a file
764  (
766  const fileName& pathname,
767  IOstreamOption streamOpt = IOstreamOption(),
768  const bool writeOnProc = true
769  ) const = 0;
770 
771 
772  // File modification checking
773 
774  //- Add watching of a file. Returns handle
775  virtual label addWatch(const fileName&) const;
776 
777  //- Remove watch on a file (using handle)
778  virtual bool removeWatch(const label) const;
779 
780  //- Find index (or -1) of file in list of handles
781  virtual label findWatch
782  (
783  const labelList& watchIndices,
784  const fileName&
785  ) const;
786 
787  //- Helper: add watches for list of regIOobjects
788  virtual void addWatches(regIOobject&, const fileNameList&) const;
789 
790  //- Get name of file being watched (using handle)
791  virtual fileName getFile(const label) const;
792 
793  //- Update state of all files
794  virtual void updateStates
795  (
796  const bool masterOnly,
797  const bool syncPar
798  ) const;
799 
800  //- Get current state of file (using handle)
801  virtual fileMonitor::fileState getState(const label) const;
802 
803  //- Set current state of file (using handle) to unmodified
804  virtual void setUnmodified(const label) const;
805 
806 
807  // Other
808 
809  //- Actual name of processors dir
810  //- (for use in mode PROCOBJECT, PROCINSTANCE)
811  virtual word processorsDir(const IOobject& io) const
812  {
813  return processorsBaseDir;
814  }
815 
816  //- Actual name of processors dir
817  //- (for use in mode PROCOBJECT, PROCINSTANCE)
818  virtual word processorsDir(const fileName&) const
819  {
820  return processorsBaseDir;
821  }
822 
823  //- Overall number of processors,
824  //- from UPstream::nProcs() or detected from directories/results.
825  label nProcs() const noexcept { return nProcs_; }
826 
827  //- Set number of processor directories/results.
828  // Used to cache format of e.g. processorsDDD.
829  // Returns old number of processors.
830  // Only used in decomposePar
831  label nProcs(const label numProcs) noexcept
832  {
833  label old(nProcs_);
834  nProcs_ = numProcs;
835  return old;
836  }
837 
838  //- Get number of processor directories/results.
839  // Used for e.g. reconstructPar, argList checking
840  virtual label nProcs
841  (
842  const fileName& dir,
843  const fileName& local = ""
844  ) const;
845 
846  //- Get sorted list of times
847  virtual instantList findTimes(const fileName&, const word&) const;
848 
849  //- Find instance where IOobject is.
850  // FatalError if it cannot be found and readOpt is
851  // (MUST_READ or MUST_READ_IF_MODIFIED).
852  // Otherwise it returns the stopInstance.
853  virtual IOobject findInstance
854  (
855  const IOobject& io,
856  const scalar startValue,
857  const word& stopInstance
858  ) const;
859 
860  //- Callback for time change
861  virtual void setTime(const Time&) const
862  {}
863 
864  //- Forcibly wait until all output done. Flush any cached data
865  virtual void flush() const;
866 
867  //- Forcibly parallel sync
868  virtual void sync();
869 
870  //- Generate path (like io.path) from root+casename with any
871  // 'processorXXX' replaced by procDir (usually 'processsors')
872  fileName processorsCasePath
873  (
874  const IOobject& io,
875  const word& procDir
876  ) const;
877 
878  //- Generate path (like io.path) with provided instance and any
879  // 'processorXXX' replaced by procDir (usually 'processsors')
880  fileName processorsPath
881  (
882  const IOobject& io,
883  const word& instance,
884  const word& procDir
885  ) const;
886 
887  //- Operating on fileName: replace processorXXX with procDir
888  fileName processorsPath(const fileName&, const word& procDir) const;
889 
890  //- Split objectPath into part before 'processor' and part after.
891  //
892  // Returns -1 or processor number and optionally number
893  // of processors. Use with care.
894  // - path/"processor"+Foam::name(proci)/local reconstructs input
895  // - path/"processors"+Foam::name(nProcs)/local reconstructs
896  // collated processors equivalence
897  static label splitProcessorPath
898  (
899  const fileName& objectPath,
900  fileName& path,
901  fileName& procDir,
902  fileName& local,
904  label& nProcs
905  );
906 
907  //- Detect processor number from '/aa/bb/processorDDD/cc'
908  static label detectProcessorPath(const fileName& objPath);
909 
910 
911  // Rank selection/sub-selection
912 
913  //- Get (contiguous) range/bounds of ranks addressed
914  //- within the given main io-ranks.
915  static labelRange subRanks(const labelUList& mainIOranks);
916 
917  //- Get list of global IO master ranks based on the hostname.
918  //- It is assumed that each host range is contiguous.
920 
921  //- Get list of global IO ranks from FOAM_IORANKS env variable.
922  //- If set, these correspond to the IO master ranks.
923  static labelList getGlobalIORanks();
924 
925 
926  // Housekeeping
927 
928  //- Same as nProcs
929  label setNProcs(label numProcs) { return nProcs(numProcs); }
930 };
931 
932 
933 //- Read pathType as an integer value
934 inline Istream& operator>>(Istream& is, fileOperation::pathType& b)
935 {
936  int val(0);
937  is >> val;
938 
939  b = static_cast<fileOperation::pathType>(val);
940  return is;
941 }
942 
943 //- Write pathType as an integer value
944 inline Ostream& operator<<(Ostream& os, const fileOperation::pathType b)
945 {
946  os << static_cast<int>(b);
947  return os;
948 }
949 
950 
951 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
952 
953 //- Return the current file handler
954 //- (will create default file handler if necessary).
955 //- Forwards to fileOperation::handler()
956 inline const fileOperation& fileHandler()
957 {
959 }
960 
961 //- Delete current file handler - forwards to fileOperation::handler()
962 // Should have [[nodiscard]], but gcc ignores void casting.
963 inline refPtr<fileOperation> fileHandler(std::nullptr_t)
964 {
965  return fileOperation::fileHandler(nullptr);
966 }
967 
968 //- Replace the current file handler - forwards to fileOperation::handler().
969 // \note legacy behaviour, so returns autoPtr instead of refPtr!
970 autoPtr<fileOperation> fileHandler(autoPtr<fileOperation>&& newHandler);
971 
972 
973 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
974 
975 } // End namespace Foam
976 
977 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
978 
979 #endif
980 
981 // ************************************************************************* //
virtual bool rm(const fileName &) const =0
Remove a file, returning true if successful otherwise false.
virtual autoPtr< ISstream > readStream(regIOobject &, const fileName &, const word &typeName, const bool readOnProc=true) const =0
Reads header for regIOobject and returns an ISstream.
static const Enum< pathType > pathTypeNames_
static label detectProcessorPath(const fileName &objPath)
Detect processor number from &#39;/aa/bb/processorDDD/cc&#39;.
virtual refPtr< dirIndexList > lookupProcessorsPath(const fileName &objectPath) const
Lookup name of processorsDDD using cache.
List< instant > instantList
List of instants.
Definition: instantList.H:41
static refPtr< fileOperation > fileHandlerPtr_
The currently active file handler. Avoid accessing directly.
virtual fileName dirPath(const bool checkGlobal, const IOobject &io, const bool search=true) const =0
Search for a directory. checkGlobal : also check undecomposed.
List< dirIndex > dirIndexList
virtual double highResLastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
A class for handling file names.
Definition: fileName.H:71
io.objectPath() exists
virtual fileMonitor::fileState getState(const label) const
Get current state of file (using handle)
as PROCUNCOLLATED but with instance
virtual bool chMod(const fileName &, const mode_t) const =0
Set the file mode.
Checking for changes to files.
Definition: fileMonitor.H:62
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 dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
label nProcs() const noexcept
Overall number of processors, from UPstream::nProcs() or detected from directories/results.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
static labelList getGlobalHostIORanks()
Get list of global IO master ranks based on the hostname. It is assumed that each host range is conti...
fileName processorsPath(const IOobject &io, const word &instance, const word &procDir) const
Generate path (like io.path) with provided instance and any.
virtual bool mvBak(const fileName &, const std::string &ext="bak") const =0
Rename to a corresponding backup file.
An interval of (signed) integers defined by a start and a size.
Definition: IntRange.H:59
fileState
Enumeration defining the file state.
Definition: fileMonitor.H:71
virtual off_t fileSize(const fileName &, const bool followLink=true) const =0
Return size of file.
pathType
Enumeration for the location of an IOobject.
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 a file.
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
virtual void storeComm() const =0
Transfer ownership of communicator to this fileOperation. Use with caution.
virtual bool mv(const fileName &src, const fileName &dst, const bool followLink=false) const =0
Rename src to dst.
file found in time directory
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
static constexpr label commSelf() noexcept
Communicator within the current rank only.
Definition: UPstream.H:426
A simple container for options an IOstream can normally have.
static refPtr< fileOperation > null()
Reference to a dummy file handler.
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?
static word processorsBaseDir
Return the processors directory name (usually "processors")
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
An encapsulation of filesystem-related operations.
as PROCOBJECT but with instance
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
virtual mode_t mode(const fileName &, const bool followLink=true) const =0
Return the file mode.
label nProcs_
Overall number of processors.
IntRange< int > procRangeType
For addressing a range of processors, identical to UPstream::rangeType.
virtual fileName::Type type(const fileName &, const bool followLink=true) const =0
Return the file type: DIRECTORY, FILE or SYMLINK.
refPtr< dirIndexList > lookupAndCacheProcessorsPath(const fileName &objectPath, const bool syncPar) const
Lookup name of processorsDDD using cache.
constexpr const char *const group
Group name for atomic constants.
virtual void updateStates(const bool masterOnly, const bool syncPar) const
Update state of all files.
Tuple2< fileName, Tuple2< pathType, int > > dirIndex
Augment fileName with pathType and local offset.
atomicType
Atomic operations (output)
static label splitProcessorPath(const fileName &objectPath, fileName &path, fileName &procDir, fileName &local, procRangeType &group, label &nProcs)
Split objectPath into part before &#39;processor&#39; and part after.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
instance is absolute directory
virtual fileName filePath(const bool checkGlobal, const IOobject &, const word &typeName, const bool search=true) const =0
Search for an object. checkGlobal : also check undecomposed case.
virtual bool broadcastCopy(const label comm, const bool writeOnProc, const fileName &src, const fileName &dst) const
Read dir/file (recursively if necessary) on master of the communicator, send and write contents to al...
virtual ~fileOperation()=default
Destructor.
virtual bool ln(const fileName &src, const fileName &dst) const =0
Create a softlink. dst should not exist. Returns true if.
objectPath exists in &#39;processorsNN&#39;
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
objectPath exists in &#39;processorN&#39;
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.
virtual instantList findTimes(const fileName &, const word &) const
Get sorted list of times.
Istream & operator>>(Istream &, directionInfo &)
A regular file.
Definition: fileName.H:83
virtual bool writeObject(const regIOobject &io, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const
Writes a regIOobject (so header, contents and divider).
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.
static instantList sortTimes(const fileNameList &dirEntries, const word &constantName="constant")
Sort directory entries according to time value,.
bool local
Definition: EEqn.H:20
virtual label addWatch(const fileName &) const
Add watching of a file. Returns handle.
as PROCBASEOBJECT but with instance
objectPath exists in &#39;processorsNN_first-last&#39;
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
virtual bool good() const
True if the fileOperation can be considered valid. At the moment, primarily used to detect the dummy ...
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.
static label getManagedComm(const label communicator)
Construction helper: check for locally allocated communicator.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
virtual void sync()
Forcibly parallel sync.
static int cacheLevel() noexcept
Return cache level.
OBJstream os(runTime.globalPath()/outputName)
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
static const UList< label > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:45
fileMonitor & monitor() const
Get or create fileMonitor singleton.
HashTable< dirIndexList > procsDirs_
Detected processors directories.
static autoPtr< fileOperation > New(const word &handlerType, bool verbose=false)
Select fileHandler-type. Uses defaultFileHandler if the handlerType is empty.
fileName processorsCasePath(const IOobject &io, const word &procDir) const
Generate path (like io.path) from root+casename with any.
static bool uniformFile(const fileNameList &names)
True if the file names are identical. False on an empty list.
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds)
Definition: UPstream.H:431
static labelRange subRanks(const labelUList &mainIOranks)
Get (contiguous) range/bounds of ranks addressed within the given main io-ranks.
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
virtual bool rmDir(const fileName &dir, const bool silent=false, const bool emptyOnly=false) const =0
Remove a directory and its contents.
static bool isFileOrDir(const bool isFile, const fileName &)
Helper: check for file (isFile) or directory (!isFile)
word format(conversionProperties.get< word >("format"))
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
void printRanks() const
Helper: output which ranks are IO.
virtual void setTime(const Time &) const
Callback for time change.
static word defaultFileHandler
Name of the default fileHandler.
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
virtual bool isDir(const fileName &, const bool followLink=true) const =0
Does the name exist as a DIRECTORY in the file system?
label comm() const noexcept
Communicator to use.
InfoProxy< fileOperation > info() const noexcept
Return info proxy, used to print information to a stream.
static int cacheLevel_
Cache level (eg, for caching time directories). Default: 1.
virtual time_t lastModified(const fileName &, const bool followLink=true) const =0
Return time of last file modification.
const labelList ioRanks_
The list of IO ranks (global ranks)
bool distributed() const noexcept
Distributed roots (parallel run)
streamFormat
Data format (ascii | binary)
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
std::unique_ptr< fileMonitor > monitorPtr_
File-change monitor for all registered files.
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:640
fileOperation(const label comm, const labelUList &ioRanks=labelUList::null(), const bool distributedRoots=false)
Construct from communicator, optionally with specified io-ranks and/or distributed roots...
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 =0
Generate an OSstream that writes a file.
bool isIOrank(const label proci) const
Is proci a master rank in the communicator (in parallel) or a master rank in the IO ranks (non-parall...
static labelList getGlobalIORanks()
Get list of global IO ranks from FOAM_IORANKS env variable. If set, these correspond to the IO master...
static const fileOperation & fileHandler()
Return the current file handler. Will create the default file handler if necessary.
List< label > labelList
A List of labels.
Definition: List.H:62
TypeName("fileOperation")
Runtime type information.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
virtual label findWatch(const labelList &watchIndices, const fileName &) const
Find index (or -1) of file in list of handles.
List< fileName > fileNameList
List of fileName.
Definition: fileNameList.H:32
const labelList & ioRanks() const noexcept
The list of IO ranks (global ranks)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
virtual fileName objectPath(const IOobject &io, const word &typeName) const
Generate disk file name for object. Opposite of filePath.
declareRunTimeSelectionTable(autoPtr, fileOperation, word,(bool verbose),(verbose))
bool distributed_
Distributed roots (parallel run)
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 directory entry types.
Definition: fileName.H:80
virtual bool read(regIOobject &, const bool masterOnly, const IOstreamOption::streamFormat format, const word &typeName) const =0
Top-level read.
virtual word processorsDir(const IOobject &io) const
Actual name of processors dir (for use in mode PROCOBJECT, PROCINSTANCE)
label comm_
Communicator to use.
Namespace for OpenFOAM.
label setNProcs(label numProcs)
Same as nProcs.
static autoPtr< fileOperation > NewUncollated()
The commonly used uncollatedFileOperation.
static constexpr label commGlobal() noexcept
Communicator for all ranks, irrespective of any local worlds.
Definition: UPstream.H:421