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  //- Filtering of processorsDDD directories (in parallel):
154  // - >0 : only accept processorsDDD (default)
155  // - 0 : accept anything (e.g. when detecting nprocs from
156  // directory naming)
157  // - -1 : use nProcs(UPstream::worldComm)
158  static int nProcsFilter_;
159 
160 
161 protected:
162 
163  // Protected Static Data
164 
165  //- Cache level (eg, for caching time directories). Default: 1
166  static int cacheLevel_;
167 
168 
169  // Protected Data
170 
171  //- Communicator to use
172  mutable label comm_;
174  //- Overall number of processors.
175  // Used to synthesise processor directory naming:
176  // - parallel: UPstream::nProcs(UPstream::commWorld())
177  // - non-parallel: detected from processor dir naming ('processorsNN')
178  label nProcs_;
179 
180  //- Distributed roots (parallel run)
181  mutable bool distributed_;
182 
183  //- The list of IO ranks (global ranks)
184  // Primarily for additional bookkeeping in non-parallel
185  const labelList ioRanks_;
186 
187  //- Detected processors directories
189 
190  //- File-change monitor for all registered files
191  mutable std::unique_ptr<fileMonitor> monitorPtr_;
192 
193 
194  // Protected Member Functions
196  //- Get or create fileMonitor singleton
197  fileMonitor& monitor() const;
198 
199  //- Merge two times
200  static void mergeTimes
201  (
202  const instantList& extraTimes,
203  const word& constantName,
204  instantList& times
205  );
206 
207  //- Helper: check for file (isFile) or directory (!isFile)
208  static bool isFileOrDir(const bool isFile, const fileName&);
209 
210  //- Lookup name of processorsDDD using cache.
211  // \return empty fileName if not found.
213  (
214  const fileName& objectPath,
215  const bool syncPar
216  ) const;
217 
218  //- Lookup name of processorsDDD using cache.
219  // \note To be called on all processors
220  // \return empty fileName if not found.
222  (
223  const fileName& objectPath
224  ) const;
225 
226  //- Does IOobject exist?
227  //- Is either a directory (empty name()) or a file
228  bool exists(IOobject& io) const;
229 
230 
231  //- Is proci a master rank in the communicator (in parallel)
232  //- or a master rank in the IO ranks (non-parallel)
233  bool isIOrank(const label proci) const;
234 
235  //- Helper: output which ranks are IO
236  void printRanks() const;
237 
238 
239  //- Construction helper: check for locally allocated communicator
240  static inline label getManagedComm(const label communicator)
241  {
242  return
243  (
244  (
245  communicator < 0
246  || communicator == UPstream::commGlobal()
247  || communicator == UPstream::commSelf()
248  || communicator == UPstream::commWorld()
249  )
250  ? -1
251  : communicator
252  );
253  }
254 
255 
256 private:
257 
258  // Private Member Functions
259 
260  //- Low-level implementation for subsetting 'clone' operation
261  static autoPtr<fileOperation> New_impl
262  (
263  const fileOperation& origHandler,
265  const labelUList& subProcs,
266  bool verbose
267  );
268 
269 
270 public:
271 
272  // Static Data
273 
274  //- Return the processors directory name (usually "processors")
275  static word processorsBaseDir;
276 
277  //- Name of the default fileHandler
278  static word defaultFileHandler;
279 
280 
281  // Public Data Types
282 
283  //- Runtime type information
284  TypeName("fileOperation");
285 
286 
287  //- The currently active file handler. Avoid accessing directly
289 
290  //- Reference to a dummy file handler.
291  static refPtr<fileOperation> null();
292 
293 
294  // Constructors
295 
296  //- Construct from communicator,
297  //- optionally with specified io-ranks and/or distributed roots
298  explicit fileOperation
299  (
300  const label comm,
302  const bool distributedRoots = false
303  );
304 
305  //- Construct from communicator with specified io-ranks
306  explicit fileOperation
307  (
308  const Tuple2<label, labelList>& commAndIORanks,
309  const bool distributedRoots = false
310  );
311 
312 
313  // Declare run-time constructor selection table
314 
316  (
317  autoPtr,
319  word,
320  (
321  bool verbose
322  ),
323  (verbose)
324  );
327  (
328  autoPtr,
330  comm,
331  (
332  const Tuple2<label, labelList>& commAndIORanks,
333  const bool distributedRoots,
334  bool verbose
335  ),
336  (commAndIORanks, distributedRoots, verbose)
337  );
338 
340  // Selectors
341 
342  //- Select fileHandler-type.
343  //- Uses defaultFileHandler if the handlerType is empty.
345  (
346  const word& handlerType,
347  bool verbose = false
348  );
349 
350  //- Select fileHandler-type.
351  //- Uses defaultFileHandler if the handlerType is empty.
353  (
354  const word& handlerType,
355  const Tuple2<label, labelList>& commAndIORanks,
356  const bool distributedRoots,
357  bool verbose = false
358  );
359 
360 
361  //- Replicate the given fileHandler properties with
362  //- a subset of (global) ranks.
363  // Always includes rank 0 and constrained by the global numProcs
364  // Returns nullptr if myProcNo is not involved.
366  (
368  const fileOperation& origHandler,
370  const boolUList& useProc,
371  bool verbose = false
372  );
373 
374  //- Replicate the given fileHandler properties with
375  //- a subset of (global) ranks.
376  // Always includes rank 0 and constrained by the global numProcs
377  // Returns nullptr if myProcNo is not involved.
379  (
381  const fileOperation& origHandler,
383  const bitSet& useProc,
384  bool verbose = false
385  );
386 
387 
388  //- Destructor
389  virtual ~fileOperation() = default;
390 
391 
392  // Factory Methods, Singleton-type Functions
393 
394  //- The commonly used uncollatedFileOperation
396 
397  //- Return the current file handler.
398  //- Will create the default file handler if necessary.
399  static const fileOperation& fileHandler();
400 
401  //- Delete current file handler.
402  // \returns the old handler.
403  // Should have [[nodiscard]], but gcc ignores void casting.
404  static refPtr<fileOperation> fileHandler(std::nullptr_t);
405 
406  //- Replace the current file handler.
407  // The following are considered no-ops:
408  // - an empty/invalid newHandler does \b not delete, use a literal
409  // \c nullptr (std::nullptr_t) for that
410  // - if new handler and current handler are identical (same pointer).
411  // .
412  // \returns the old handler (on change), nullptr otherwise
413  // Should have [[nodiscard]], but gcc ignores void casting.
415  (
416  refPtr<fileOperation>& newHandler
417  );
418 
419  //- Replace the current file handler.
420  // The following are considered no-ops:
421  // - an empty/invalid newHandler does \b not delete, use a literal
422  // \c nullptr (std::nullptr_t) for that
423  // - if new handler and current handler are identical (same pointer).
424  // .
425  // \returns the old handler (on change), nullptr otherwise
426  // Should have [[nodiscard]], but gcc ignores void casting.
428  (
429  refPtr<fileOperation>&& newHandler
430  );
431 
432  //- Replace the current file handler.
433  // The following are considered no-ops:
434  // - an empty/invalid newHandler does \b not delete, use a literal
435  // \c nullptr (std::nullptr_t) for that
436  // - if new handler and current handler are identical (same pointer).
437  // .
438  // \returns the old handler (on change), nullptr otherwise
439  // Should have [[nodiscard]], but gcc ignores void casting.
441  (
442  autoPtr<fileOperation>&& newHandler
443  );
444 
445 
446  // Static Functions
447 
448  //- Return cache level
449  static int cacheLevel() noexcept
450  {
451  return cacheLevel_;
452  }
453 
454  //- Set cache level (0 = off). \return the previous value
455  static int cacheLevel(int level) noexcept
456  {
457  int old(cacheLevel_);
458  cacheLevel_ = level;
459  return old;
460  }
461 
462  //- Return collated 'processorsDDD' filtering
463  static int nProcsFilter() noexcept
464  {
465  return nProcsFilter_;
466  }
467 
468  //- Set collated 'processorsDDD' filtering (0 = off).
469  // \return the previous value
470  static int nProcsFilter(int level) noexcept
471  {
472  int old(nProcsFilter_);
473  nProcsFilter_ = level;
474  return old;
475  }
476 
477  //- Sort directory entries according to time value,
478  // with "constant" appearing first (if it exists)
479  static instantList sortTimes
480  (
481  const fileNameList& dirEntries,
482  const word& constantName = "constant"
483  );
484 
485  //- True if the file names are identical. False on an empty list
486  static bool uniformFile(const fileNameList& names);
487 
488  //- True if the file name is identical on all ranks
489  static bool uniformFile(const label comm, const fileName& name);
490 
491 
492  // Member Functions
493 
494  // Characteristics
495 
496  //- Communicator to use
497  label comm() const noexcept
498  {
499  return comm_;
500  }
501 
502  //- Set communicator to use [mutable]. Negative values are a no-op.
503  // \return old value
504  label comm(label communicator) const noexcept
505  {
506  label old(comm_);
507  if (communicator >= 0) comm_ = communicator;
508  return old;
509  }
510 
511  //- Distributed roots (parallel run)
512  bool distributed() const noexcept
513  {
514  return distributed_;
515  }
516 
517  //- Set distributed roots on/off [mutable]
518  // \return old value
519  bool distributed(bool on) const noexcept
520  {
521  bool old(distributed_);
522  distributed_ = on;
523  return old;
524  }
525 
526  //- The list of IO ranks (global ranks)
527  // Primarily for additional bookkeeping in non-parallel
528  const labelList& ioRanks() const noexcept { return ioRanks_; }
529 
530  //- Return info proxy,
531  //- used to print information to a stream
532  InfoProxy<fileOperation> info() const noexcept { return *this; }
533 
534 
535  // Member Functions
537  //- True if the fileOperation can be considered valid.
538  //- At the moment, primarily used to detect the dummy fileOperation.
539  virtual bool good() const { return true; }
540 
541  //- Transfer ownership of communicator to this fileOperation.
542  //- Use with caution
543  virtual void storeComm() const = 0;
545 
546  // OSSpecific equivalents
547 
548  //- Make directory
549  virtual bool mkDir(const fileName&, mode_t=0777) const = 0;
550 
551  //- Set the file mode
552  virtual bool chMod(const fileName&, const mode_t) const = 0;
553 
554  //- Return the file mode
555  virtual mode_t mode
556  (
557  const fileName&,
558  const bool followLink = true
559  ) const = 0;
560 
561  //- Return the file type: DIRECTORY, FILE or SYMLINK
562  virtual fileName::Type type
563  (
564  const fileName&,
565  const bool followLink = true
566  ) const = 0;
567 
568  //- Does the name exist (as DIRECTORY or FILE) in the file system?
569  // Optionally enable/disable check for gzip file.
570  virtual bool exists
571  (
572  const fileName&,
573  const bool checkGzip = true,
574  const bool followLink = true
575  ) const = 0;
576 
577  //- Does the name exist as a DIRECTORY in the file system?
578  virtual bool isDir
579  (
580  const fileName&,
581  const bool followLink = true
582  ) const = 0;
583 
584  //- Does the name exist as a FILE in the file system?
585  // Optionally enable/disable check for gzip file.
586  virtual bool isFile
587  (
588  const fileName&,
589  const bool checkGzip = true,
590  const bool followLink = true
591  ) const = 0;
592 
593  //- Return size of file
594  virtual off_t fileSize
595  (
596  const fileName&,
597  const bool followLink = true
598  ) const = 0;
599 
600  //- Return time of last file modification
601  virtual time_t lastModified
602  (
603  const fileName&,
604  const bool followLink = true
605  ) const = 0;
606 
607  //- Return time of last file modification
608  virtual double highResLastModified
609  (
610  const fileName&,
611  const bool followLink = true
612  ) const = 0;
613 
614  //- Read a directory and return the entries as a string list
615  virtual fileNameList readDir
616  (
617  const fileName&,
619  const bool filtergz=true,
620  const bool followLink = true
621  ) const = 0;
622 
623  //- Copy, recursively if necessary, the source to the destination
624  virtual bool cp
625  (
626  const fileName& src,
627  const fileName& dst,
628  const bool followLink = true
629  ) const = 0;
631  //- Create a softlink. dst should not exist. Returns true if
632  // successful.
633  virtual bool ln(const fileName& src, const fileName& dst) const = 0;
634 
635  //- Rename src to dst
636  virtual bool mv
637  (
638  const fileName& src,
639  const fileName& dst,
640  const bool followLink = false
641  ) const = 0;
643  //- Rename to a corresponding backup file
644  // If the backup file already exists, attempt with
645  // "01" .. "99" suffix
646  virtual bool mvBak
647  (
648  const fileName&,
649  const std::string& ext = "bak"
650  ) const = 0;
651 
652  //- Remove a file, returning true if successful otherwise false
653  virtual bool rm(const fileName&) const = 0;
654 
655  //- Remove a directory and its contents
656  // \param dir the directory to remove
657  // \param silent do not report missing directory
658  // \param emptyOnly only remove empty directories (recursive)
659  virtual bool rmDir
660  (
661  const fileName& dir,
662  const bool silent = false,
663  const bool emptyOnly = false
664  ) const = 0;
665 
666  //- Read dir/file (recursively if necessary) on master of the
667  //- communicator, send and write contents to all 'writeOnProc'
668  //- processors with local file name
669  // \param comm the communicator for broadcasting
670  // \param writeOnProc write on the processor
671  // \param src the source file/directory
672  // \param dst the target file/directory.
673  // If empty, treat as being identical to the src.
674  virtual bool broadcastCopy
675  (
676  const label comm,
677  const bool writeOnProc,
678  const fileName& src,
679  const fileName& dst
680  // always recreates links
681  ) const;
682 
683 
684  // (reg)IOobject functionality
685 
686  //- Generate disk file name for object. Opposite of filePath.
687  // Optional wanted typeName.
688  virtual fileName objectPath
689  (
690  const IOobject& io,
691  const word& typeName
692  ) const;
693 
694  //- Search for an object. checkGlobal : also check undecomposed case
695  // Optional wanted typeName.
696  virtual fileName filePath
697  (
698  const bool checkGlobal,
699  const IOobject&,
700  const word& typeName,
701  const bool search = true
702  ) const = 0;
703 
704  //- Search for a directory. checkGlobal : also check undecomposed
705  // case
706  virtual fileName dirPath
707  (
708  const bool checkGlobal,
709  const IOobject& io,
710  const bool search = true
711  ) const = 0;
712 
713  //- Search directory for objects. Used in IOobjectList.
714  virtual fileNameList readObjects
715  (
716  const objectRegistry& db,
717  const fileName& instance,
718  const fileName& local,
719  word& newInstance
720  ) const;
721 
722  //- Read object header from supplied file
723  virtual bool readHeader
724  (
725  IOobject&,
726  const fileName&,
727  const word& typeName
728  ) const = 0;
729 
730  //- Reads header for regIOobject and returns an ISstream
731  // to read the contents.
733  (
734  regIOobject&,
735  const fileName&,
736  const word& typeName,
737  const bool readOnProc = true
738  ) const = 0;
739 
740  //- Top-level read
741  virtual bool read
742  (
743  regIOobject&,
744  const bool masterOnly,
746  const word& typeName
747  ) const = 0;
748 
749  //- Writes a regIOobject (so header, contents and divider).
750  // Returns success state. Default action is to write to
751  // the objectPath using writeData. If !writeOnProc the
752  // file does not need to be written (this is used e.g. to
753  // suppress empty local lagrangian data)
754  virtual bool writeObject
755  (
756  const regIOobject& io,
757  IOstreamOption streamOpt = IOstreamOption(),
758  const bool writeOnProc = true
759  ) const;
760 
761 
762  // Filename (not IOobject) operations
763 
764  //- Search for a file or directory.
765  //- Use IOobject version in preference
766  virtual fileName filePath
767  (
768  const fileName&,
769  const bool checkGzip = true,
770  const bool followLink = true
771  ) const;
772 
773  //- Generate an ISstream that reads a file
774  virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
775 
776  //- Generate an OSstream that writes a file
778  (
779  const fileName& pathname,
780  IOstreamOption streamOpt = IOstreamOption(),
781  const bool writeOnProc = true
782  ) const = 0;
783 
784  //- Generate an OSstream that writes a file
786  (
788  const fileName& pathname,
789  IOstreamOption streamOpt = IOstreamOption(),
790  const bool writeOnProc = true
791  ) const = 0;
792 
793 
794  // File modification checking
795 
796  //- Add watching of a file. Returns handle
797  virtual label addWatch(const fileName&) const;
798 
799  //- Remove watch on a file (using handle)
800  virtual bool removeWatch(const label) const;
801 
802  //- Find index (or -1) of file in list of handles
803  virtual label findWatch
804  (
805  const labelList& watchIndices,
806  const fileName&
807  ) const;
808 
809  //- Helper: add watches for list of regIOobjects
810  virtual void addWatches(regIOobject&, const fileNameList&) const;
811 
812  //- Get name of file being watched (using handle)
813  virtual fileName getFile(const label) const;
814 
815  //- Update state of all files
816  virtual void updateStates
817  (
818  const bool masterOnly,
819  const bool syncPar
820  ) const;
821 
822  //- Get current state of file (using handle)
823  virtual fileMonitor::fileState getState(const label) const;
824 
825  //- Set current state of file (using handle) to unmodified
826  virtual void setUnmodified(const label) const;
827 
828 
829  // Other
830 
831  //- Actual name of processors dir
832  //- (for use in mode PROCOBJECT, PROCINSTANCE)
833  virtual word processorsDir(const IOobject& io) const
834  {
835  return processorsBaseDir;
836  }
837 
838  //- Actual name of processors dir
839  //- (for use in mode PROCOBJECT, PROCINSTANCE)
840  virtual word processorsDir(const fileName&) const
841  {
842  return processorsBaseDir;
843  }
844 
845  //- Overall number of processors,
846  //- from UPstream::nProcs() or detected from directories/results.
847  label nProcs() const noexcept { return nProcs_; }
848 
849  //- Set number of processor directories/results.
850  // Used to cache format of e.g. processorsDDD.
851  // Returns old number of processors.
852  // Only used in decomposePar
853  label nProcs(const label numProcs) noexcept
854  {
855  label old(nProcs_);
856  nProcs_ = numProcs;
857  return old;
858  }
859 
860  //- Get number of processor directories/results.
861  // Used for e.g. reconstructPar, argList checking
862  virtual label nProcs
863  (
864  const fileName& dir,
865  const fileName& local = "",
867  const label wantedNProcs = 0
868  ) const;
869 
870  //- Get sorted list of times
871  virtual instantList findTimes(const fileName&, const word&) const;
872 
873  //- Find instance where IOobject is.
874  // FatalError if it cannot be found and readOpt is
875  // (MUST_READ or MUST_READ_IF_MODIFIED).
876  // Otherwise it returns the stopInstance.
877  virtual IOobject findInstance
878  (
879  const IOobject& io,
880  const scalar startValue,
881  const word& stopInstance
882  ) const;
883 
884  //- Callback for time change
885  virtual void setTime(const Time&) const
886  {}
887 
888  //- Forcibly wait until all output done. Flush any cached data
889  virtual void flush() const;
890 
891  //- Forcibly parallel sync
892  virtual void sync();
893 
894  //- Generate path (like io.path) from root+casename with any
895  // 'processorXXX' replaced by procDir (usually 'processsors')
896  fileName processorsCasePath
897  (
898  const IOobject& io,
899  const word& procDir
900  ) const;
901 
902  //- Generate path (like io.path) with provided instance and any
903  // 'processorXXX' replaced by procDir (usually 'processsors')
904  fileName processorsPath
905  (
906  const IOobject& io,
907  const word& instance,
908  const word& procDir
909  ) const;
910 
911  //- Operating on fileName: replace processorXXX with procDir
912  fileName processorsPath(const fileName&, const word& procDir) const;
913 
914  //- Split objectPath into part before 'processor' and part after.
915  //
916  // Returns -1 or processor number and optionally number
917  // of processors. Use with care.
918  // - path/"processor"+Foam::name(proci)/local reconstructs input
919  // - path/"processors"+Foam::name(nProcs)/local reconstructs
920  // collated processors equivalence
921  static label splitProcessorPath
922  (
923  const fileName& objectPath,
924  fileName& path,
925  fileName& procDir,
926  fileName& local,
928  label& nProcs
929  );
930 
931  //- Detect processor number from '/aa/bb/processorDDD/cc'
932  static label detectProcessorPath(const fileName& objPath);
933 
934 
935  // Rank selection/sub-selection
936 
937  //- Get (contiguous) range/bounds of ranks addressed
938  //- within the given main io-ranks.
939  static labelRange subRanks(const labelUList& mainIOranks);
940 
941  //- Get list of global IO master ranks based on the hostname.
942  //- It is assumed that each host range is contiguous.
944 
945  //- Get list of global IO ranks from FOAM_IORANKS env variable.
946  //- If set, these correspond to the IO master ranks.
947  static labelList getGlobalIORanks();
948 
949 
950  // Housekeeping
951 
952  //- Same as nProcs
953  label setNProcs(label numProcs) { return nProcs(numProcs); }
954 };
955 
956 
957 //- Read pathType as an integer value
958 inline Istream& operator>>(Istream& is, fileOperation::pathType& b)
959 {
960  int val(0);
961  is >> val;
962 
963  b = static_cast<fileOperation::pathType>(val);
964  return is;
965 }
966 
967 //- Write pathType as an integer value
968 inline Ostream& operator<<(Ostream& os, const fileOperation::pathType b)
969 {
970  os << static_cast<int>(b);
971  return os;
972 }
973 
974 
975 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
976 
977 //- Return the current file handler
978 //- (will create default file handler if necessary).
979 //- Forwards to fileOperation::handler()
980 inline const fileOperation& fileHandler()
981 {
983 }
984 
985 //- Delete current file handler - forwards to fileOperation::handler()
986 // Should have [[nodiscard]], but gcc ignores void casting.
987 inline refPtr<fileOperation> fileHandler(std::nullptr_t)
988 {
989  return fileOperation::fileHandler(nullptr);
990 }
991 
992 //- Replace the current file handler - forwards to fileOperation::handler().
993 // \note legacy behaviour, so returns autoPtr instead of refPtr!
994 autoPtr<fileOperation> fileHandler(autoPtr<fileOperation>&& newHandler);
995 
996 
997 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
998 
999 } // End namespace Foam
1000 
1001 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1002 
1003 #endif
1004 
1005 // ************************************************************************* //
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.
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:72
io.objectPath() exists
virtual fileMonitor::fileState getState(const label) const
Get current state of file (using handle)
static int nProcsFilter() noexcept
Return collated &#39;processorsDDD&#39; filtering.
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: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 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:70
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:424
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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:84
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:108
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:56
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:90
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:429
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:77
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:66
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:642
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)
List< instant > instantList
List of instants.
Definition: instantList.H:41
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:172
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:81
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:419