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 -------------------------------------------------------------------------------
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 InNamespace
28  Foam
29 
30 Description
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 
34 SourceFiles
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 
49 namespace Foam
50 {
51 
52 // Forward declarations
53 class CStringList;
54 
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 //- Return the PID of this process
59 pid_t pid();
60 
61 //- Return the parent PID of this process
62 pid_t ppid();
63 
64 //- Return the group PID of this process
65 pid_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.
69 bool 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.
73 string 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.
77 bool 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
81 FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function")
82 inline bool env(const std::string& envName) { return Foam::hasEnv(envName); }
83 
84 //- Return the system's host name, as per hostname(1)
85 string hostName();
86 
87 //- Deprecated(2022-01) full hostname resolution may be unreliable
88 // \deprecated(2022-01) - use hostname() function without parameter
89 FOAM_DEPRECATED_FOR(2022-01, "hostname() function without parameter")
90 string hostName(bool full);
91 
92 //- Deprecated(2022-01) domain name resolution may be unreliable
93 // \deprecated(2022-01) - avoid usage entirely
94 FOAM_DEPRECATED_FOR(2022-01, "nothing : avoid using at all")
95 string domainName();
96 
97 //- Return the user's login name
98 string userName();
99 
100 //- Is the current user the administrator (root)
101 bool isAdministrator();
102 
103 //- Return home directory path name for the current user
104 fileName home();
105 
106 //- Return home directory path name for a particular user
107 fileName 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.
115 fileName cwd();
116 
117 //- The physical or logical current working directory path name.
118 fileName 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.
122 bool 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.
127 bool 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.
131 bool 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.
135 mode_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.
139 fileName::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.
144 bool 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.
153 bool 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.
158 bool 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.
167 off_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.
171 time_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.
175 double 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.
193 bool 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.
198 bool 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.
205 fileName 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.
209 bool 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.
219 bool 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.
223 bool 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)
228 bool 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
239 unsigned int sleep(const unsigned int sec);
240 
241 //- Close file descriptor
242 void fdClose(const int fd);
243 
244 //- Check if machine is up by pinging given port
245 bool 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)
248 bool 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.
262 int 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.
273 int 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.
284 int 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)
289 void* 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).
294 void* 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)
299 label dlOpen(std::initializer_list<fileName> libNames, const bool check=true);
300 
301 //- Close a dlopened library using handle. Return true if successful
302 bool 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.
310 void* 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.
314 inline 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.
323 inline 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 // ************************************************************************* //
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition: POSIX.C:1939
bool mvBak(const fileName &src, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:1357
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:1324
A class for handling file names.
Definition: fileName.H:72
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:905
fileName readLink(const fileName &link)
Return the contents (target) of a symlink.
Definition: POSIX.C:1290
bool hasEnv(const std::string &envName)
True if environment variable of given name is defined.
Definition: POSIX.C:332
pid_t ppid()
Return the parent PID of this process.
Definition: POSIX.C:320
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: POSIX.C:1063
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1927
bool chDir(const fileName &dir)
Change current directory to the one specified and return true on success.
Definition: POSIX.C:607
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1994
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: POSIX.C:339
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:440
bool ping(const std::string &destName, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:1565
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition: OSspecific.H:96
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
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:614
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
string domainName()
Deprecated(2022-01) domain name resolution may be unreliable.
Definition: POSIX.C:401
void fdClose(const int fd)
Close file descriptor.
Definition: POSIX.C:1553
time_t lastModified(const fileName &name, const bool followLink=true)
Return time of last file modification (normally follows symbolic links).
Definition: POSIX.C:930
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:835
string userName()
Return the user&#39;s login name.
Definition: POSIX.C:422
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
string hostName()
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:371
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:1237
bool rmDir(const fileName &directory, const bool silent=false, const bool emptyOnly=false)
Remove a directory and its contents recursively,.
Definition: POSIX.C:1433
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition: POSIX.C:1547
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:314
static void check(const int retVal, const char *what)
pid_t pgid()
Return the group PID of this process.
Definition: POSIX.C:326
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition: POSIX.C:757
bool isAdministrator()
Is the current user the administrator (root)
Definition: POSIX.C:434
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:877
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: OSspecific.H:440
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition: POSIX.C:1824
fileName cwd()
The physical or logical current working directory path name.
Definition: POSIX.C:590
bool all(const UList< bool > &bools)
True if all entries are &#39;true&#39; or if the set is empty.
Definition: BitOps.H:83
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:63
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: POSIX.C:1702
List< fileName > fileNameList
List of fileName.
Definition: fileNameList.H:32
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:963
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: POSIX.C:773
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable, return true on success.
Definition: POSIX.C:356
void * dlSymFound(void *handle, const std::string &symbol)
Check for symbol in a dlopened library.
Definition: OSspecific.H:452
Namespace for OpenFOAM.
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: POSIX.C:946
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: POSIX.C:1404