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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 InNamespace
25  Foam
26 
27 Description
28  Functions used by OpenFOAM that are specific to POSIX compliant
29  operating systems and need to be replaced or emulated on other systems.
30 
31 SourceFiles
32  POSIX.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef OSspecific_H
37 #define OSspecific_H
38 
39 #include "fileNameList.H"
40 
41 #include <sys/types.h>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 // Avoids assumptions about pid_t type
51 typedef pid_t PID_T;
52 
53 //- Return the PID of this process
54 PID_T pid();
55 
56 //- Return the parent PID of this process
57 PID_T ppid();
58 
59 //- Return the group PID of this process
60 PID_T pgid();
61 
62 //- Return true if environment variable of given name is defined
63 bool env(const word&);
64 
65 //- Return environment variable of given name
66 // Return string() if the environment is undefined
67 string getEnv(const word&);
68 
69 //- Set an environment variable
70 bool setEnv(const word& name, const std::string& value, const bool overwrite);
71 
72 //- Return the system's host name, as per hostname(1)
73 // Optionally with the full name (as per the '-f' option)
74 string hostName(const bool full=false);
75 
76 //- Return the system's domain name, as per hostname(1) with the '-d' option
77 string domainName();
78 
79 //- Return the user's login name
80 string userName();
81 
82 //- Is user administrator
83 bool isAdministrator();
84 
85 //- Return home directory path name for the current user
86 fileName home();
87 
88 //- Return home directory path name for a particular user
89 fileName home(const string& userName);
90 
91 //- Return current working directory path name
92 fileName cwd();
93 
94 //- Change the current directory to the one given and return true,
95 // else return false
96 bool chDir(const fileName& dir);
97 
98 //- Search for files from user/group/shipped directories.
99 // The search scheme allows for version-specific and
100 // version-independent files using the following hierarchy:
101 // - \b user settings:
102 // - ~/.OpenFOAM/<VERSION>
103 // - ~/.OpenFOAM/
104 // - \b group (site) settings (when $WM_PROJECT_SITE is set):
105 // - $WM_PROJECT_SITE/<VERSION>
106 // - $WM_PROJECT_SITE
107 // - \b group (site) settings (when $WM_PROJECT_SITE is not set):
108 // - $WM_PROJECT_INST_DIR/site/<VERSION>
109 // - $WM_PROJECT_INST_DIR/site/
110 // - \b other (shipped) settings:
111 // - $WM_PROJECT_DIR/etc/
112 //
113 // \return The list of full paths of all the matching files or
114 // an empty list if the name cannot be found.
115 // Optionally abort if the file cannot be found.
116 // Optionally stop search after the first file has been found.
118 (
119  const fileName&,
120  bool mandatory=false,
121  bool findFirst=false
122 );
123 
124 //- Search for a file using findEtcFiles.
125 // \return The full path name of the first file found in the
126 // search hierarchy or an empty fileName if the name cannot be found.
127 // Optionally abort if the file cannot be found.
128 fileName findEtcFile(const fileName&, bool mandatory=false);
129 
130 //- Make a directory and return an error if it could not be created
131 // and does not already exist
132 bool mkDir(const fileName&, mode_t=0777);
133 
134 //- Set the file mode
135 bool chMod(const fileName&, const mode_t);
136 
137 //- Return the file mode
138 mode_t mode(const fileName&);
139 
140 //- Return the file type: DIRECTORY or FILE
142 
143 //- Does the name exist (as DIRECTORY or FILE) in the file system?
144 // Optionally enable/disable check for gzip file.
145 bool exists(const fileName&, const bool checkGzip=true);
146 
147 //- Does the name exist as a DIRECTORY in the file system?
148 bool isDir(const fileName&);
149 
150 //- Does the name exist as a FILE in the file system?
151 // Optionally enable/disable check for gzip file.
152 bool isFile(const fileName&, const bool checkGzip=true);
153 
154 //- Return size of file
155 off_t fileSize(const fileName&);
156 
157 //- Return time of last file modification
158 time_t lastModified(const fileName&);
159 
160 //- Read a directory and return the entries as a string list
162 (
163  const fileName&,
165  const bool filtergz=true
166 );
167 
168 //- Copy, recursively if necessary, the source to the destination
169 bool cp(const fileName& src, const fileName& dst);
170 
171 //- Create a softlink. dst should not exist. Returns true if successful.
172 bool ln(const fileName& src, const fileName& dst);
173 
174 //- Rename src to dst
175 bool mv(const fileName& src, const fileName& dst);
176 
177 //- Rename to a corresponding backup file
178 // If the backup file already exists, attempt with "01" .. "99" suffix
179 bool mvBak(const fileName&, const std::string& ext = "bak");
180 
181 //- Remove a file, returning true if successful otherwise false
182 bool rm(const fileName&);
183 
184 //- Remove a dirctory and its contents
185 bool rmDir(const fileName&);
186 
187 //- Sleep for the specified number of seconds
188 unsigned int sleep(const unsigned int);
189 
190 //- Close file descriptor
191 void fdClose(const int);
192 
193 //- Check if machine is up by pinging given port
194 bool ping(const string&, const label port, const label timeOut);
195 
196 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
197 bool ping(const string&, const label timeOut=10);
198 
199 //- Execute the specified command
200 int system(const std::string& command);
201 
202 //- Open a shared library. Return handle to library. Print error message
203 // if library cannot be loaded (check = true)
204 void* dlOpen(const fileName& lib, const bool check = true);
205 
206 //- Close a dlopened library using handle. Return true if successful
207 bool dlClose(void*);
208 
209 //- Lookup a symbol in a dlopened library using handle to library
210 void* dlSym(void* handle, const std::string& symbol);
211 
212 //- Report if symbol in a dlopened library could be found
213 bool dlSymFound(void* handle, const std::string& symbol);
214 
215 //- Return all loaded libraries
217 
218 
219 // Low level random numbers. Use Random class instead.
220 
221 //- Seed random number generator.
222 void osRandomSeed(const label seed);
223 
224 //- Return random integer (uniform distribution between 0 and 2^31)
226 
227 //- Return random double precision (uniform distribution between 0 and 1)
228 scalar osRandomDouble();
229 
230 //- Convert to unix path separators
231 std::string toUnixPath(const std::string& path);
232 
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 
235 } // End namespace Foam
236 
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 
239 #endif
240 
241 // ************************************************************************* //
Foam::osRandomInteger
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
Definition: POSIX.C:1331
Foam::domainName
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
Definition: POSIX.C:148
Foam::findEtcFile
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: POSIX.C:404
Foam::env
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:95
Foam::fileName::FILE
@ FILE
Definition: fileName.H:85
Foam::ppid
PID_T ppid()
Return the parent PID of this process.
Definition: POSIX.C:83
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::exists
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:608
Foam::dlSym
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1224
Foam::rm
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:954
Foam::fdClose
void fdClose(const int)
Close file descriptor.
Definition: POSIX.C:1060
Foam::fileName::Type
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
Foam::lastModified
time_t lastModified(const fileName &)
Return time of last file modification.
Definition: POSIX.C:644
Foam::setEnv
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:119
Foam::ping
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:1072
Foam::osRandomSeed
void osRandomSeed(const label seed)
Seed random number generator.
Definition: POSIX.C:1321
Foam::cp
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:755
Foam::dlSymFound
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Definition: POSIX.C:1252
Foam::sleep
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
Definition: POSIX.C:1054
Foam::mv
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
Definition: POSIX.C:891
Foam::mode
mode_t mode(const fileName &)
Return the file mode.
Definition: POSIX.C:573
Foam::chMod
bool chMod(const fileName &, const mode_t)
Set the file mode.
Definition: POSIX.C:566
Foam::isAdministrator
bool isAdministrator()
Is user administrator.
Definition: POSIX.C:184
Foam::toUnixPath
std::string toUnixPath(const std::string &path)
Convert to unix path separators.
Definition: POSIX.C:1351
Foam::userName
string userName()
Return the user's login name.
Definition: POSIX.C:169
Foam::pid
PID_T pid()
Return the PID of this process.
Definition: POSIX.C:77
Foam::getEnv
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:101
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:59
Foam::mvBak
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:917
Foam::dlOpen
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1161
Foam::pgid
PID_T pgid()
Return the group PID of this process.
Definition: POSIX.C:89
Foam::PID_T
pid_t PID_T
Definition: OSspecific.H:51
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::chDir
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
Definition: POSIX.C:264
Foam::findEtcFiles
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
Definition: POSIX.C:271
Foam::home
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:191
fileNameList.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::hostName
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: POSIX.C:129
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::dlClose
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1212
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:854
Foam::cwd
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:246
Foam::dlLoaded
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1305
Foam::readDir
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:660
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::osRandomDouble
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
Definition: POSIX.C:1341
Foam::system
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1155
Foam::fileSize
off_t fileSize(const fileName &)
Return size of file.
Definition: POSIX.C:629
Foam::rmDir
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:974
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47