OSspecific.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) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27InNamespace
28 Foam
29
30Description
31 Functions used by OpenFOAM that are specific to POSIX compliant
32 operating systems and need to be replaced or emulated on other systems.
33
34SourceFiles
35 POSIX.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_OSspecific_H
40#define Foam_OSspecific_H
41
42#include "fileNameList.H"
43#include "stringList.H"
44
45#include <sys/types.h>
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward declarations
53class CStringList;
54
55
56// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57
58//- Return the PID of this process
59pid_t pid();
60
61//- Return the parent PID of this process
62pid_t ppid();
63
64//- Return the group PID of this process
65pid_t pgid();
66
67//- True if environment variable of given name is defined.
68// Using an empty name is a no-op and always returns false.
69bool hasEnv(const std::string& envName);
70
71//- Get environment value for given envName.
72// \return empty string if environment is undefined or envName is empty.
73string getEnv(const std::string& envName);
74
75//- Set an environment variable, return true on success.
76// Using an empty name is a no-op and always returns false.
77bool setEnv(const word& name, const std::string& value, const bool overwrite);
78
79//- Deprecated(2020-05) check for existence of environment variable
80// \deprecated(2020-05) - use hasEnv() function
81FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function")
82inline bool env(const std::string& envName) { return Foam::hasEnv(envName); }
83
84//- Return the system's host name, as per hostname(1)
85string hostName();
86
87//- Deprecated(2022-01) full hostname resolution may be unreliable
88// \deprecated(2022-01) - use hostname() function without parameter
89FOAM_DEPRECATED_FOR(2022-01, "hostname() function without parameter")
90string hostName(bool full);
91
92//- Deprecated(2022-01) domain name resolution may be unreliable
93// \deprecated(2022-01) - avoid usage entirely
94FOAM_DEPRECATED_FOR(2022-01, "nothing : avoid using at all")
95string domainName();
96
97//- Return the user's login name
98string userName();
99
100//- Is the current user the administrator (root)
101bool isAdministrator();
102
103//- Return home directory path name for the current user
104fileName home();
105
106//- Return home directory path name for a particular user
107fileName home(const std::string& userName);
108
109//- The physical or logical current working directory path name.
110// The behaviour is controlled by the \c cwd optimisation Switch
111// A value of 0 corresponds to the physical value, which is identical
112// to what getcwd and pwd -P would deliver.
113// A value of 1 corresponds to the logical value, which corresponds
114// to the PWD environment value and to what pwd -L would deliver.
115fileName cwd();
116
117//- The physical or logical current working directory path name.
118fileName cwd(bool logical);
119
120//- Change current directory to the one specified and return true on success.
121// Using an empty name is a no-op and always returns false.
122bool chDir(const fileName& dir);
123
124//- Make a directory and return an error if it could not be created
125// and does not already exist.
126// Using an empty pathName is a no-op and always returns false.
127bool mkDir(const fileName& pathName, mode_t mode=0777);
128
129//- Set the file/directory mode, return true on success.
130// Using an empty name is a no-op and always returns false.
131bool chMod(const fileName& name, const mode_t mode);
132
133//- Return the file mode, normally following symbolic links
134// Using an empty name is a no-op and always returns 0.
135mode_t mode(const fileName& name, const bool followLink=true);
136
137//- Return the file type: DIRECTORY or FILE, normally following symbolic links
138// Using an empty name is a no-op and always returns UNDEFINED.
139fileName::Type type(const fileName& name, const bool followLink=true);
140
141//- Does the name exist (as DIRECTORY or FILE) in the file system?
142// Optionally enable/disable check for gzip file.
143// Using an empty name is a no-op and always returns false.
144bool exists
145(
146 const fileName& name,
147 const bool checkGzip=true,
148 const bool followLink=true
149);
150
151//- Does the name exist as a DIRECTORY in the file system?
152// Using an empty name is a no-op and always returns false.
153bool isDir(const fileName& name, const bool followLink=true);
154
155//- Does the name exist as a FILE in the file system?
156// Optionally enable/disable check for gzip file.
157// Using an empty name is a no-op and always returns false.
158bool isFile
159(
160 const fileName& name,
161 const bool checkGzip=true,
162 const bool followLink=true
163);
164
165//- Return size of file or -1 on failure (normally follows symbolic links).
166// Using an empty name is a no-op and always returns -1.
167off_t fileSize(const fileName& name, const bool followLink=true);
168
169//- Return time of last file modification (normally follows symbolic links).
170// Using an empty name is a no-op and always returns 0.
171time_t lastModified(const fileName& name, const bool followLink=true);
172
173//- Return time of last file modification
174// Using an empty name is a no-op and always returns 0.
175double highResLastModified(const fileName&, const bool followLink=true);
176
177//- Read a directory and return the entries as a fileName List.
178// Using an empty directory name returns an empty list.
180(
181 const fileName& directory,
182 const fileName::Type type=fileName::FILE,
183 const bool filtergz=true,
184 const bool followLink=true
185);
186
187//- Copy the source to the destination (recursively if necessary).
188// An empty source name is a no-op and always returns false.
189bool cp(const fileName& src, const fileName& dst, const bool followLink=true);
190
191//- Create a softlink. dst should not exist. Returns true if successful.
192// An empty source or destination name is a no-op that always returns false,
193// but also produces a warning.
194bool ln(const fileName& src, const fileName& dst);
195
196//- Rename src to dst.
197// An empty source or destination name is a no-op that always returns false.
198bool mv
199(
200 const fileName& src,
201 const fileName& dst,
202 const bool followLink=false
203);
204
205//- Rename to a corresponding backup file
206// If the backup file already exists, attempt with "01" .. "99" suffix
207// An empty name or extension is a no-op that always returns false.
208bool mvBak(const fileName& src, const std::string& ext = "bak");
209
210//- Remove a file (or its gz equivalent), returning true if successful.
211// An empty name is a no-op that always returns false.
212bool rm(const fileName& file);
213
214//- Remove a directory and its contents (optionally silencing warnings)
215// An empty directory name is a no-op that always returns false,
216// but also produces a warning.
217bool rmDir(const fileName& directory, const bool silent=false);
218
219//- Sleep for the specified number of seconds
220unsigned int sleep(const unsigned int sec);
221
222//- Close file descriptor
223void fdClose(const int fd);
224
225//- Check if machine is up by pinging given port
226bool ping(const std::string& destName, const label port, const label timeOut);
227
228//- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
229bool ping(const std::string& host, const label timeOut=10);
230
231//- Execute the specified command via the shell.
232// Uses vfork/execl internally.
233// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
234//
235// Where possible, use the list version instead.
236//
237// \param bg return immediately to parent process instead of waiting
238// for the child. Can be used (with moderation) to create background
239// processes.
240//
241// \note treats an empty command as a successful no-op.
242// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
243int system(const std::string& command, const bool bg = false);
244
245//- Execute the specified command with arguments.
246// Uses vfork/execvp internally
247// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
248//
249// \param bg return immediately to parent process instead of waiting
250// for the child. Can be used (with moderation) to create background
251// processes.
252//
253// \note treats an empty command as a successful no-op.
254int system(const UList<string>& command, const bool bg = false);
255
256//- Execute the specified command with arguments.
257// Uses vfork/execvp internally
258// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
259//
260// \param bg return immediately to parent process instead of waiting
261// for the child. Can be used (with moderation) to create background
262// processes.
263//
264// \note treats an empty command as a successful no-op.
265int system(const CStringList& command, const bool bg = false);
266
267//- Open a shared library and return handle to library.
268// A leading "lib" and ".so" suffix are added silently as required.
269// Prints warning if a library cannot be loaded (suppress with check=false)
270void* dlOpen(const fileName& libName, const bool check=true);
271
272//- Open a shared library and return handle to library.
273// A leading "lib" and ".so" suffix are added silently as required.
274// Captures any error messages in the second parameter (empty if no errors).
275void* dlOpen(const fileName& libName, std::string& errorMsg);
276
277//- Open shared libraries and return number of libraries loaded.
278// A leading "lib" and ".so" suffix are added silently as required.
279// Prints warning if a library cannot be loaded (suppress with check=false)
280label dlOpen(std::initializer_list<fileName> libNames, const bool check=true);
281
282//- Close a dlopened library using handle. Return true if successful
283bool dlClose(void* handle);
284
285
286//- Look for symbol in a dlopened library.
287// If the symbol is not 'required', using a null handle or an empty symbol
288// name is a no-op and returns nullptr without error.
289//
290// \return the symbol or nullptr.
291void* dlSymFind(void* handle, const std::string& symbol, bool required=false);
292
293//- Lookup a symbol in a dlopened library using handle to library
294// \return the symbol or nullptr.
295inline void* dlSym(void* handle, const std::string& symbol)
296{
297 return dlSymFind(handle, symbol, true);
298}
299
300//- Check for symbol in a dlopened library.
301// Using a null handle or an empty symbol name is a no-op and always
302// returns nullptr.
303// \return the symbol or nullptr.
304inline void* dlSymFound(void* handle, const std::string& symbol)
305{
306 return dlSymFind(handle, symbol, false);
307}
308
309
310//- Return all loaded libraries
312
313
314// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315
316} // End namespace Foam
317
318// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319
320#endif
321
322// ************************************************************************* //
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1012
fileName cwd()
The physical or logical current working directory path name.
Definition: MSwindows.C:476
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable, return true on success.
Definition: MSwindows.C:395
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:58
time_t lastModified(const fileName &name, const bool followLink=true)
Return time of last file modification (normally follows symbolic links).
Definition: MSwindows.C:700
void fdClose(const int fd)
Close file descriptor.
Definition: MSwindows.C:1122
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:633
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1158
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:515
bool isAdministrator()
Is the current user the administrator (root)
Definition: MSwindows.C:450
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
Definition: MSwindows.C:1319
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition: OSspecific.H:82
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:572
static void check(const int retVal, const char *what)
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition: MSwindows.C:1114
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition: MSwindows.C:565
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
string userName()
Return the user's login name.
Definition: MSwindows.C:437
pid_t pgid()
Return the group PID of this process.
Definition: MSwindows.C:350
string hostName()
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1036
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition: MSwindows.C:1339
bool ping(const std::string &destName, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: MSwindows.C:1134
pid_t ppid()
Return the parent PID of this process.
Definition: MSwindows.C:337
off_t fileSize(const fileName &name, const bool followLink=true)
Return size of file or -1 on failure (normally follows symbolic links).
Definition: MSwindows.C:684
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: MSwindows.C:707
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition: MSwindows.C:1224
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition: MSwindows.C:715
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
bool mvBak(const fileName &src, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: MSwindows.C:976
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:666
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: OSspecific.H:295
fileNameList dlLoaded()
Return all loaded libraries.
Definition: MSwindows.C:1372
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:947
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:457
string domainName()
Deprecated(2022-01) domain name resolution may be unreliable.
Definition: MSwindows.C:428
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: MSwindows.C:933
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
void * dlSymFound(void *handle, const std::string &symbol)
Check for symbol in a dlopened library.
Definition: OSspecific.H:304
bool hasEnv(const std::string &envName)
True if environment variable of given name is defined.
Definition: MSwindows.C:363
bool chDir(const fileName &dir)
Change current directory to the one specified and return true on success.
Definition: MSwindows.C:508
const volScalarField & cp
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:52