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-2018 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 OSspecific_H
40 #define 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 // \note the full name (as per the '-f' option) may be unreliable
86 string hostName(bool full=false);
87 
88 //- Return the system's domain name, as per hostname(1) with the '-d' option
89 string domainName();
90 
91 //- Return the user's login name
92 string userName();
93 
94 //- Is the current user the administrator (root)
95 bool isAdministrator();
96 
97 //- Return home directory path name for the current user
98 fileName home();
99 
100 //- Return home directory path name for a particular user
101 fileName home(const std::string& userName);
102 
103 //- The physical or logical current working directory path name.
104 // The behaviour is controlled by the \c cwd optimisation Switch
105 // A value of 0 corresponds to the physical value, which is identical
106 // to what getcwd and pwd -P would deliver.
107 // A value of 1 corresponds to the logical value, which corresponds
108 // to the PWD environment value and to what pwd -L would deliver.
109 fileName cwd();
110 
111 //- The physical or logical current working directory path name.
112 fileName cwd(bool logical);
113 
114 //- Change current directory to the one specified and return true on success.
115 // Using an empty name is a no-op and always returns false.
116 bool chDir(const fileName& dir);
117 
118 //- Make a directory and return an error if it could not be created
119 // and does not already exist.
120 // Using an empty pathName is a no-op and always returns false.
121 bool mkDir(const fileName& pathName, mode_t mode=0777);
122 
123 //- Set the file/directory mode, return true on success.
124 // Using an empty name is a no-op and always returns false.
125 bool chMod(const fileName& name, const mode_t mode);
126 
127 //- Return the file mode, normally following symbolic links
128 // Using an empty name is a no-op and always returns 0.
129 mode_t mode(const fileName& name, const bool followLink=true);
130 
131 //- Return the file type: DIRECTORY or FILE, normally following symbolic links
132 // Using an empty name is a no-op and always returns UNDEFINED.
133 fileName::Type type(const fileName& name, const bool followLink=true);
134 
135 //- Does the name exist (as DIRECTORY or FILE) in the file system?
136 // Optionally enable/disable check for gzip file.
137 // Using an empty name is a no-op and always returns false.
138 bool exists
139 (
140  const fileName& name,
141  const bool checkGzip=true,
142  const bool followLink=true
143 );
144 
145 //- Does the name exist as a DIRECTORY in the file system?
146 // Using an empty name is a no-op and always returns false.
147 bool isDir(const fileName& name, const bool followLink=true);
148 
149 //- Does the name exist as a FILE in the file system?
150 // Optionally enable/disable check for gzip file.
151 // Using an empty name is a no-op and always returns false.
152 bool isFile
153 (
154  const fileName& name,
155  const bool checkGzip=true,
156  const bool followLink=true
157 );
158 
159 //- Return size of file or -1 on failure (normally follows symbolic links).
160 // Using an empty name is a no-op and always returns -1.
161 off_t fileSize(const fileName& name, const bool followLink=true);
162 
163 //- Return time of last file modification (normally follows symbolic links).
164 // Using an empty name is a no-op and always returns 0.
165 time_t lastModified(const fileName& name, const bool followLink=true);
166 
167 //- Return time of last file modification
168 // Using an empty name is a no-op and always returns 0.
169 double highResLastModified(const fileName&, const bool followLink=true);
170 
171 //- Read a directory and return the entries as a fileName List.
172 // Using an empty directory name returns an empty list.
174 (
175  const fileName& directory,
177  const bool filtergz=true,
178  const bool followLink=true
179 );
180 
181 //- Copy the source to the destination (recursively if necessary).
182 // An empty source name is a no-op and always returns false.
183 bool cp(const fileName& src, const fileName& dst, const bool followLink=true);
184 
185 //- Create a softlink. dst should not exist. Returns true if successful.
186 // An empty source or destination name is a no-op that always returns false,
187 // but also produces a warning.
188 bool ln(const fileName& src, const fileName& dst);
189 
190 //- Rename src to dst.
191 // An empty source or destination name is a no-op that always returns false.
192 bool mv
193 (
194  const fileName& src,
195  const fileName& dst,
196  const bool followLink=false
197 );
198 
199 //- Rename to a corresponding backup file
200 // If the backup file already exists, attempt with "01" .. "99" suffix
201 // An empty name or extension is a no-op that always returns false.
202 bool mvBak(const fileName& src, const std::string& ext = "bak");
203 
204 //- Remove a file (or its gz equivalent), returning true if successful.
205 // An empty name is a no-op that always returns false.
206 bool rm(const fileName& file);
207 
208 //- Remove a directory and its contents (optionally silencing warnings)
209 // An empty directory name is a no-op that always returns false,
210 // but also produces a warning.
211 bool rmDir(const fileName& directory, const bool silent=false);
212 
213 //- Sleep for the specified number of seconds
214 unsigned int sleep(const unsigned int sec);
215 
216 //- Close file descriptor
217 void fdClose(const int fd);
218 
219 //- Check if machine is up by pinging given port
220 bool ping(const std::string& destName, const label port, const label timeOut);
221 
222 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
223 bool ping(const std::string& host, const label timeOut=10);
224 
225 //- Execute the specified command via the shell.
226 // Uses vfork/execl internally.
227 // When Foam::infoDetailLevel is zero, redirects stdout to stderr.
228 //
229 // Where possible, use the list version instead.
230 //
231 // \param bg return immediately to parent process instead of waiting
232 // for the child. Can be used (with moderation) to create background
233 // processes.
234 //
235 // \note treats an empty command as a successful no-op.
236 // When Foam::infoDetailLevel is zero, redirects stdout to stderr.
237 int system(const std::string& command, const bool bg = false);
238 
239 //- Execute the specified command with arguments.
240 // Uses vfork/execvp internally
241 // When Foam::infoDetailLevel is zero, redirects stdout to stderr.
242 //
243 // \param bg return immediately to parent process instead of waiting
244 // for the child. Can be used (with moderation) to create background
245 // processes.
246 //
247 // \note treats an empty command as a successful no-op.
248 int system(const UList<string>& command, const bool bg = false);
249 
250 //- Execute the specified command with arguments.
251 // Uses vfork/execvp internally
252 // When Foam::infoDetailLevel is zero, redirects stdout to stderr.
253 //
254 // \param bg return immediately to parent process instead of waiting
255 // for the child. Can be used (with moderation) to create background
256 // processes.
257 //
258 // \note treats an empty command as a successful no-op.
259 int system(const CStringList& command, const bool bg = false);
260 
261 //- Open a shared library and return handle to library.
262 // A leading "lib" and ".so" suffix are added silently as required.
263 // Prints warning if a library cannot be loaded (suppress with check=false)
264 void* dlOpen(const fileName& libName, const bool check=true);
265 
266 //- Open a shared library and return handle to library.
267 // A leading "lib" and ".so" suffix are added silently as required.
268 // Captures any error messages in the second parameter (empty if no errors).
269 void* dlOpen(const fileName& libName, std::string& errorMsg);
270 
271 //- Open shared libraries and return number of libraries loaded.
272 // A leading "lib" and ".so" suffix are added silently as required.
273 // Prints warning if a library cannot be loaded (suppress with check=false)
274 label dlOpen(std::initializer_list<fileName> libNames, const bool check=true);
275 
276 //- Close a dlopened library using handle. Return true if successful
277 bool dlClose(void* handle);
278 
279 
280 //- Look for symbol in a dlopened library.
281 // If the symbol is not 'required', using a null handle or an empty symbol
282 // name is a no-op and returns nullptr without error.
283 //
284 // \return the symbol or nullptr.
285 void* dlSymFind(void* handle, const std::string& symbol, bool required=false);
286 
287 //- Lookup a symbol in a dlopened library using handle to library
288 // \return the symbol or nullptr.
289 inline void* dlSym(void* handle, const std::string& symbol)
290 {
291  return dlSymFind(handle, symbol, true);
292 }
293 
294 //- Check for symbol in a dlopened library.
295 // Using a null handle or an empty symbol name is a no-op and always
296 // returns nullptr.
297 // \return the symbol or nullptr.
298 inline void* dlSymFound(void* handle, const std::string& symbol)
299 {
300  return dlSymFind(handle, symbol, false);
301 }
302 
303 
304 //- Return all loaded libraries
306 
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 } // End namespace Foam
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 #endif
315 
316 // ************************************************************************* //
Foam::domainName
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
Definition: MSwindows.C:420
Foam::exists
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:625
Foam::fileName::FILE
A file.
Definition: fileName.H:83
Foam::chMod
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition: MSwindows.C:557
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1150
Foam::fileName::Type
Type
Enumerations to handle directory entry types.
Definition: fileName.H:80
Foam::isFile
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:658
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1004
Foam::fdClose
void fdClose(const int fd)
Close file descriptor.
Definition: MSwindows.C:1114
Foam::setEnv
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable, return true on success.
Definition: MSwindows.C:395
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
Foam::pgid
pid_t pgid()
Return the group PID of this process.
Definition: MSwindows.C:350
Foam::isAdministrator
bool isAdministrator()
Is the current user the administrator (root)
Definition: MSwindows.C:442
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
Foam::hasEnv
bool hasEnv(const std::string &envName)
True if environment variable of given name is defined.
Definition: MSwindows.C:363
Foam::userName
string userName()
Return the user's login name.
Definition: MSwindows.C:429
Foam::fileSize
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:676
Foam::check
static void check(const int retVal, const char *what)
Definition: ptscotchDecomp.C:80
Foam::dlSymFind
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition: MSwindows.C:1331
Foam::dlSym
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: OSspecific.H:289
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
Foam::ppid
pid_t ppid()
Return the parent PID of this process.
Definition: MSwindows.C:337
Foam::highResLastModified
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: MSwindows.C:699
Foam::ping
bool ping(const std::string &destName, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: MSwindows.C:1126
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:939
Foam::dlSymFound
void * dlSymFound(void *handle, const std::string &symbol)
Check for symbol in a dlopened library.
Definition: OSspecific.H:298
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::fileNameList
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:58
Foam::mvBak
bool mvBak(const fileName &src, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: MSwindows.C:968
Foam::chDir
bool chDir(const fileName &dir)
Change current directory to the one specified and return true on success.
Definition: MSwindows.C:500
Foam::home
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:449
fileNameList.H
Foam::cp
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: MSwindows.C:802
Foam::rmDir
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1028
Foam::env
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition: OSspecific.H:82
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::hostName
string hostName(bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
Foam::dlClose
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
Definition: MSwindows.C:1311
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: MSwindows.C:925
Foam::cwd
fileName cwd()
The physical or logical current working directory path name.
Definition: MSwindows.C:468
Foam::dlLoaded
fileNameList dlLoaded()
Return all loaded libraries.
Definition: MSwindows.C:1364
Foam::mkDir
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:507
Foam::readDir
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:707
stringList.H
Foam::sleep
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition: MSwindows.C:1106
Foam::dlOpen
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition: MSwindows.C:1216
Foam::lastModified
time_t lastModified(const fileName &name, const bool followLink=true)
Return time of last file modification (normally follows symbolic links).
Definition: MSwindows.C:692
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643