Loading...
Searching...
No Matches
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();
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(
182 const fileName& directory,
184 const fileName::Type type = fileName::Type::FILE,
186 const bool filtergz = true,
188 const bool followLink = true
189);
190
191//- Copy the source to the destination (recursively if necessary).
192// An empty source name is a no-op and always returns false.
193bool cp(const fileName& src, const fileName& dst, const bool followLink=true);
194
195//- Create a softlink. dst should not exist. Returns true if successful.
196// An empty source or destination name is a no-op that always returns false,
197// but also produces a warning.
198bool ln(const fileName& src, const fileName& dst);
199
200//- Return the contents (target) of a symlink.
201// For example, returns \c target for <code>ln -s target linkName</code>
202//
203// An empty link name is a no-op that always returns an empty string.
204// Following a non-link is an error that returns an empty string.
205fileName readLink(const fileName& link);
206
207//- Rename src to dst.
208// An empty source or destination name is a no-op that always returns false.
209bool mv
210(
211 const fileName& src,
212 const fileName& dst,
213 const bool followLink=false
214);
215
216//- Rename to a corresponding backup file
217// If the backup file already exists, attempt with "01" .. "99" suffix
218// An empty name or extension is a no-op that always returns false.
219bool mvBak(const fileName& src, const std::string& ext = "bak");
220
221//- Remove a file (or its gz equivalent), returning true if successful.
222// An empty name is a no-op that always returns false.
223bool rm(const fileName& file);
224
225//- Remove a directory and its contents recursively,
226// returning true if successful.
227// An empty directory name is a no-op that always returns false (silently)
228bool rmDir
229(
231 const fileName& directory,
233 const bool silent = false,
235 const bool emptyOnly = false
236);
237
238//- Sleep for the specified number of seconds
239unsigned int sleep(const unsigned int sec);
240
241//- Close file descriptor
242void fdClose(const int fd);
243
244//- Check if machine is up by pinging given port
245bool ping(const std::string& destName, const label port, const label timeOut);
246
247//- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
248bool ping(const std::string& host, const label timeOut=10);
249
250//- Execute the specified command via the shell.
251// Uses vfork/execl internally.
252// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
253//
254// Where possible, use the list version instead.
255//
256// \param bg return immediately to parent process instead of waiting
257// for the child. Can be used (with moderation) to create background
258// processes.
259//
260// \note treats an empty command as a successful no-op.
261// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
262int system(const std::string& command, const bool bg = false);
263
264//- Execute the specified command with arguments.
265// Uses vfork/execvp internally
266// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
267//
268// \param bg return immediately to parent process instead of waiting
269// for the child. Can be used (with moderation) to create background
270// processes.
271//
272// \note treats an empty command as a successful no-op.
273int system(const UList<string>& command, const bool bg = false);
274
275//- Execute the specified command with arguments.
276// Uses vfork/execvp internally
277// When Foam::infoDetailLevel is zero, redirects stdout to stderr.
278//
279// \param bg return immediately to parent process instead of waiting
280// for the child. Can be used (with moderation) to create background
281// processes.
282//
283// \note treats an empty command as a successful no-op.
284int system(const CStringList& command, const bool bg = false);
285
286//- Open a shared library and return handle to library.
287// A leading "lib" and ".so" suffix are added silently as required.
288// Prints warning if a library cannot be loaded (suppress with check=false)
289void* dlOpen(const fileName& libName, const bool check=true);
290
291//- Open a shared library and return handle to library.
292// A leading "lib" and ".so" suffix are added silently as required.
293// Captures any error messages in the second parameter (empty if no errors).
294void* dlOpen(const fileName& libName, std::string& errorMsg);
295
296//- Open shared libraries and return number of libraries loaded.
297// A leading "lib" and ".so" suffix are added silently as required.
298// Prints warning if a library cannot be loaded (suppress with check=false)
299label dlOpen(std::initializer_list<fileName> libNames, const bool check=true);
300
301//- Close a dlopened library using handle. Return true if successful
302bool dlClose(void* handle);
303
304
305//- Look for symbol in a dlopened library.
306// If the symbol is not 'required', using a null handle or an empty symbol
307// name is a no-op and returns nullptr without error.
308//
309// \return the symbol or nullptr.
310void* dlSymFind(void* handle, const std::string& symbol, bool required=false);
311
312//- Lookup a symbol in a dlopened library using handle to library
313// \return the symbol or nullptr.
314inline void* dlSym(void* handle, const std::string& symbol)
315{
316 return dlSymFind(handle, symbol, true);
317}
318
319//- Check for symbol in a dlopened library.
320// Using a null handle or an empty symbol name is a no-op and always
321// returns nullptr.
322// \return the symbol or nullptr.
323inline void* dlSymFound(void* handle, const std::string& symbol)
324{
325 return dlSymFind(handle, symbol, false);
326}
327
328
329//- Return all loaded libraries
331
332
333// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334
335} // End namespace Foam
336
337// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338
339#endif
340
341// ************************************************************************* //
An adapter for copying a list of C++ strings into a list of C-style strings for passing to C code tha...
Definition CStringList.H:68
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
A class for handling file names.
Definition fileName.H:75
A class for handling character strings derived from std::string.
Definition string.H:76
A class for handling words, derived from Foam::string.
Definition word.H:66
auto & name
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition POSIX.C:1406
fileName readLink(const fileName &link)
Return the contents (target) of a symlink.
Definition POSIX.C:1292
fileName cwd()
The physical or logical current working directory path name.
Definition POSIX.C:592
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition POSIX.C:341
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable, return true on success.
Definition POSIX.C:358
time_t lastModified(const fileName &name, const bool followLink=true)
Return time of last file modification (normally follows symbolic links).
Definition POSIX.C:932
void fdClose(const int fd)
Close file descriptor.
Definition POSIX.C:1555
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 POSIX.C:837
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition POSIX.C:1704
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition POSIX.C:616
bool isAdministrator()
Is the current user the administrator (root).
Definition POSIX.C:436
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
Definition POSIX.C:1929
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition OSspecific.H:96
List< fileName > fileNameList
List of fileName.
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition POSIX.C:775
static void check(const int retVal, const char *what)
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition POSIX.C:1549
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition POSIX.C:759
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
string userName()
Return the user's login name.
Definition POSIX.C:424
pid_t pgid()
Return the group PID of this process.
Definition POSIX.C:328
string hostName()
Return the system's host name, as per hostname(1).
Definition POSIX.C:373
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition POSIX.C:1941
bool ping(const std::string &destName, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition POSIX.C:1567
pid_t ppid()
Return the parent PID of this process.
Definition POSIX.C:322
off_t fileSize(const fileName &name, const bool followLink=true)
Return size of file or -1 on failure (normally follows symbolic links).
Definition POSIX.C:907
bool rmDir(const fileName &directory, const bool silent=false, const bool emptyOnly=false)
Remove a directory and its contents recursively,.
Definition POSIX.C:1435
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition POSIX.C:948
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition POSIX.C:1826
pid_t pid()
Return the PID of this process.
Definition POSIX.C:316
bool mvBak(const fileName &src, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition POSIX.C:1359
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 POSIX.C:879
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition OSspecific.H:440
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition POSIX.C:1065
fileNameList dlLoaded()
Return all loaded libraries.
Definition POSIX.C:1996
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition POSIX.C:1326
fileName home()
Return home directory path name for the current user.
Definition POSIX.C:442
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition NamedEnum.H:66
string domainName()
Deprecated(2022-01) domain name resolution may be unreliable.
Definition POSIX.C:403
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition POSIX.C:1239
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition POSIX.C:862
void * dlSymFound(void *handle, const std::string &symbol)
Check for symbol in a dlopened library.
Definition OSspecific.H:452
bool hasEnv(const std::string &envName)
True if environment variable of given name is defined.
Definition POSIX.C:334
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::Type::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition POSIX.C:965
bool chDir(const fileName &dir)
Change current directory to the one specified and return true on success.
Definition POSIX.C:609
const volScalarField & cp
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
mkDir(pdfPath)