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