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 | Copyright (C) 2017 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 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 namespace Foam
43 {
44 
45 class IOobject;
46 class regIOobject;
47 class objectRegistry;
48 class Time;
49 
50 /*---------------------------------------------------------------------------*\
51  Class fileOperation Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class fileOperation
55 {
56 protected:
57 
58  // Protected data
59 
60  //- file-change monitor for all registered files
62 
63 
64  // Protected Member Functions
65 
66  fileMonitor& monitor() const;
67 
68  //- Sort directory entries according to time value
69  static instantList sortTimes(const fileNameList&, const word&);
70 
71  //- Helper: check for file (isFile) or directory (!isFile)
72  static bool isFileOrDir(const bool isFile, const fileName&);
73 
74 public:
75 
76  // Static data
77 
78  //- Return the processors directory name (usually "processors")
79  static word processorsDir;
80 
81  //- Default fileHandler
82  static word defaultFileHandler;
83 
84 
85  // Public data types
86 
87  //- Enumeration for the location of an IOobject
88  enum pathType
89  {
90  NOTFOUND, // not found
91  ABSOLUTE, // instance is absolute directory
92  OBJECT, // objectPath exists
93  PROCESSORSOBJECT, // objectPath exists in processors/
94  PARENTOBJECT, // parent of object path
95  FINDINSTANCE, // file found in time directory
96  PROCESSORSFINDINSTANCE // as above but in processors/
97  };
98 
99 
100  //- Runtime type information
101  TypeName("fileOperation");
102 
103 
104  //- Static fileOperation
106 
107 
108  // Constructors
109 
110  //- Construct null
111  fileOperation();
112 
113 
114  // Declare run-time constructor selection table
115 
117  (
118  autoPtr,
120  word,
121  (
122  const bool verbose
123  ),
124  (verbose)
125  );
126 
127 
128  // Selectors
129 
130  //- Select type
131  static autoPtr<fileOperation> New(const word& type, const bool verbose);
132 
133 
134  //- Destructor
135  virtual ~fileOperation();
136 
137 
138  // Member Functions
139 
140  // OSSpecific equivalents
141 
142  //- Make directory
143  virtual bool mkDir(const fileName&, mode_t=0777) const = 0;
144 
145  //- Set the file mode
146  virtual bool chMod(const fileName&, const mode_t) const = 0;
147 
148  //- Return the file mode
149  virtual mode_t mode
150  (
151  const fileName&,
152  const bool followLink = true
153  ) const = 0;
154 
155  //- Return the file type: DIRECTORY, FILE or LINK
156  virtual fileName::Type type
157  (
158  const fileName&,
159  const bool followLink = true
160  ) const = 0;
161 
162  //- Does the name exist (as DIRECTORY or FILE) in the file system?
163  // Optionally enable/disable check for gzip file.
164  virtual bool exists
165  (
166  const fileName&,
167  const bool checkGzip=true,
168  const bool followLink = true
169  ) const = 0;
170 
171  //- Does the name exist as a DIRECTORY in the file system?
172  virtual bool isDir
173  (
174  const fileName&,
175  const bool followLink = true
176  ) const = 0;
177 
178  //- Does the name exist as a FILE in the file system?
179  // Optionally enable/disable check for gzip file.
180  virtual bool isFile
181  (
182  const fileName&,
183  const bool checkGzip=true,
184  const bool followLink = true
185  ) const = 0;
186 
187  //- Return size of file
188  virtual off_t fileSize
189  (
190  const fileName&,
191  const bool followLink = true
192  ) const = 0;
193 
194  //- Return time of last file modification
195  virtual time_t lastModified
196  (
197  const fileName&,
198  const bool followLink = true
199  ) const = 0;
200 
201  //- Return time of last file modification
202  virtual double highResLastModified
203  (
204  const fileName&,
205  const bool followLink = true
206  ) const = 0;
207 
208  //- Read a directory and return the entries as a string list
209  virtual fileNameList readDir
210  (
211  const fileName&,
213  const bool filtergz=true,
214  const bool followLink = true
215  ) const = 0;
216 
217  //- Copy, recursively if necessary, the source to the destination
218  virtual bool cp
219  (
220  const fileName& src,
221  const fileName& dst,
222  const bool followLink = true
223  ) const = 0;
224 
225  //- Create a softlink. dst should not exist. Returns true if
226  // successful.
227  virtual bool ln(const fileName& src, const fileName& dst) const = 0;
228 
229  //- Rename src to dst
230  virtual bool mv
231  (
232  const fileName& src,
233  const fileName& dst,
234  const bool followLink = false
235  ) const = 0;
236 
237  //- Rename to a corresponding backup file
238  // If the backup file already exists, attempt with
239  // "01" .. "99" suffix
240  virtual bool mvBak
241  (
242  const fileName&,
243  const std::string& ext = "bak"
244  ) const = 0;
245 
246  //- Remove a file, returning true if successful otherwise false
247  virtual bool rm(const fileName&) const = 0;
248 
249  //- Remove a dirctory and its contents
250  virtual bool rmDir(const fileName&) const = 0;
251 
252 // //- Open a shared library. Return handle to library. Print error
253 // // message if library cannot be loaded (check = true)
254 // virtual void* dlOpen
255 // (
256 // const fileName& lib,
257 // const bool check = true
258 // ) const = 0;
259 
260 
261  // (reg)IOobject functinality
262 
263  //- Generate disk file name for object. Opposite of filePath.
264  // Optional wanted typeName.
265  virtual fileName objectPath
266  (
267  const IOobject& io,
268  const word& typeName
269  ) const;
270 
271  //- Search for an object. checkGlobal : also check undecomposed case
272  // Optional wanted typeName.
273  virtual fileName filePath
274  (
275  const bool checkGlobal,
276  const IOobject&,
277  const word& typeName
278  ) const = 0;
279 
280  //- Search for a directory. checkGlobal : also check undecomposed
281  // case
282  virtual fileName dirPath
283  (
284  const bool checkGlobal,
285  const IOobject&
286  ) const = 0;
287 
288  //- Search directory for objects. Used in IOobjectList.
289  virtual fileNameList readObjects
290  (
291  const objectRegistry& db,
292  const fileName& instance,
293  const fileName& local,
294  word& newInstance
295  ) const;
296 
297  //- Read object header from supplied file
298  virtual bool readHeader
299  (
300  IOobject&,
301  const fileName&,
302  const word& typeName
303  ) const = 0;
304 
305  //- Reads header for regIOobject and returns an ISstream
306  // to read the contents.
308  (
309  regIOobject&,
310  const fileName&,
311  const word& typeName,
312  const bool valid = true
313  ) const = 0;
314 
315  //- Top-level read
316  virtual bool read
317  (
318  regIOobject&,
319  const bool masterOnly,
321  const word& typeName
322  ) const = 0;
323 
324  //- Writes a regIOobject (so header, contents and divider).
325  // Returns success state. Default action is to write to
326  // the objectPath using writeData. If !valid the
327  // file does not need to be written (this is used e.g. to
328  // suppress empty local lagrangian data)
329  virtual bool writeObject
330  (
331  const regIOobject&,
335  const bool valid = true
336  ) const;
337 
338 
339  // Filename (not IOobject) operations
340 
341  //- Search for a file or directory. Use IOobject version in
342  // preference
343  virtual fileName filePath(const fileName&) const;
344 
345  //- Generate an ISstream that reads a file
346  virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
347 
348  //- Generate an Ostream that writes a file
350  (
351  const fileName& pathname,
355  const bool valid = true
356  ) const = 0;
357 
358 
359  // File modification checking
360 
361  //- Add watching of a file. Returns handle
362  virtual label addWatch(const fileName&) const;
363 
364  //- Remove watch on a file (using handle)
365  virtual bool removeWatch(const label) const;
366 
367  //- Find index (or -1) of file in list of handles
368  virtual label findWatch
369  (
370  const labelList& watchIndices,
371  const fileName&
372  ) const;
373 
374  //- Helper: add watches for list of regIOobjects
375  virtual void addWatches(regIOobject&, const fileNameList&) const;
376 
377  //- Get name of file being watched (using handle)
378  virtual fileName getFile(const label) const;
379 
380  //- Update state of all files
381  virtual void updateStates
382  (
383  const bool masterOnly,
384  const bool syncPar
385  ) const;
386 
387  //- Get current state of file (using handle)
388  virtual fileMonitor::fileState getState(const label) const;
389 
390  //- Set current state of file (using handle) to unmodified
391  virtual void setUnmodified(const label) const;
392 
393 
394  // Other
395 
396  //- Get number of processor directories/results. Used for e.g.
397  // reconstructPar, argList checking
398  virtual label nProcs
399  (
400  const fileName& dir,
401  const fileName& local = ""
402  ) const;
403 
404  //- Get sorted list of times
405  virtual instantList findTimes(const fileName&, const word&) const;
406 
407  //- Callback for time change
408  virtual void setTime(const Time&) const
409  {}
410 };
411 
412 
413 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
414 // Global declarations: defined in fileOperation.C
415 
416 //- Get current file handler
417 const fileOperation& fileHandler();
418 
419 //- Reset file handler
421 
422 
423 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
424 
425 } // End namespace Foam
426 
427 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
428 
429 #endif
430 
431 // ************************************************************************* //
pathType
Enumeration for the location of an IOobject.
Definition: fileOperation.H:87
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
Definition: fileOperation.H:60
virtual bool rmDir(const fileName &) const =0
Remove a dirctory and its contents.
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
fileOperation()
Construct null.
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.
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.
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"))
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?
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.
virtual bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist (as DIRECTORY or FILE) in the file system?
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 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.
virtual bool cp(const fileName &src, const fileName &dst, const bool followLink=true) const =0
Copy, recursively if necessary, the source to the destination.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static word processorsDir
Return the processors directory name (usually "processors")
Definition: fileOperation.H:78
fileMonitor & monitor() const
Definition: fileOperation.C:80
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.
Definition: fileOperation.H:81
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.
Definition: fileOperation.C:98
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
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.
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
virtual fileName objectPath(const IOobject &io, const word &typeName) const
Generate disk file name for object. Opposite of filePath.
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
Namespace for OpenFOAM.