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-2016 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 //- Return the PID of this process
51 pid_t pid();
52 
53 //- Return the parent PID of this process
54 pid_t ppid();
55 
56 //- Return the group PID of this process
57 pid_t pgid();
58 
59 //- Return true if environment variable of given name is defined
60 bool env(const word&);
61 
62 //- Return environment variable of given name
63 // Return string() if the environment is undefined
64 string getEnv(const word&);
65 
66 //- Set an environment variable
67 bool setEnv(const word& name, const std::string& value, const bool overwrite);
68 
69 //- Return the system's host name, as per hostname(1)
70 // Optionally with the full name (as per the '-f' option)
71 string hostName(const bool full=false);
72 
73 //- Return the system's domain name, as per hostname(1) with the '-d' option
74 string domainName();
75 
76 //- Return the user's login name
77 string userName();
78 
79 //- Is user administrator
80 bool isAdministrator();
81 
82 //- Return home directory path name for the current user
83 fileName home();
84 
85 //- Return home directory path name for a particular user
86 fileName home(const string& userName);
87 
88 //- Return current working directory path name
89 fileName cwd();
90 
91 //- Change the current directory to the one given and return true,
92 // else return false
93 bool chDir(const fileName& dir);
94 
95 //- Make a directory and return an error if it could not be created
96 // and does not already exist
97 bool mkDir(const fileName&, mode_t=0777);
98 
99 //- Set the file mode
100 bool chMod(const fileName&, const mode_t);
101 
102 //- Return the file mode
103 mode_t mode(const fileName&);
104 
105 //- Return the file type: DIRECTORY or FILE
106 fileName::Type type(const fileName&);
107 
108 //- Does the name exist (as DIRECTORY or FILE) in the file system?
109 // Optionally enable/disable check for gzip file.
110 bool exists(const fileName&, const bool checkGzip=true);
111 
112 //- Does the name exist as a DIRECTORY in the file system?
113 bool isDir(const fileName&);
114 
115 //- Does the name exist as a FILE in the file system?
116 // Optionally enable/disable check for gzip file.
117 bool isFile(const fileName&, const bool checkGzip=true);
118 
119 //- Return size of file
120 off_t fileSize(const fileName&);
121 
122 //- Return time of last file modification
123 time_t lastModified(const fileName&);
124 
125 //- Read a directory and return the entries as a string list
127 (
128  const fileName&,
130  const bool filtergz=true
131 );
132 
133 //- Copy, recursively if necessary, the source to the destination
134 bool cp(const fileName& src, const fileName& dst);
135 
136 //- Create a softlink. dst should not exist. Returns true if successful.
137 bool ln(const fileName& src, const fileName& dst);
138 
139 //- Rename src to dst
140 bool mv(const fileName& src, const fileName& dst);
141 
142 //- Rename to a corresponding backup file
143 // If the backup file already exists, attempt with "01" .. "99" suffix
144 bool mvBak(const fileName&, const std::string& ext = "bak");
145 
146 //- Remove a file, returning true if successful otherwise false
147 bool rm(const fileName&);
148 
149 //- Remove a dirctory and its contents
150 bool rmDir(const fileName&);
151 
152 //- Sleep for the specified number of seconds
153 unsigned int sleep(const unsigned int);
154 
155 //- Close file descriptor
156 void fdClose(const int);
157 
158 //- Check if machine is up by pinging given port
159 bool ping(const string&, const label port, const label timeOut);
160 
161 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
162 bool ping(const string&, const label timeOut=10);
163 
164 //- Execute the specified command
165 int system(const std::string& command);
166 
167 //- Open a shared library. Return handle to library. Print error message
168 // if library cannot be loaded (check = true)
169 void* dlOpen(const fileName& lib, const bool check = true);
170 
171 //- Close a dlopened library using handle. Return true if successful
172 bool dlClose(void*);
173 
174 //- Lookup a symbol in a dlopened library using handle to library
175 void* dlSym(void* handle, const std::string& symbol);
176 
177 //- Report if symbol in a dlopened library could be found
178 bool dlSymFound(void* handle, const std::string& symbol);
179 
180 //- Return all loaded libraries
182 
183 
184 // Low level random numbers. Use Random class instead.
185 
186 //- Seed random number generator.
187 void osRandomSeed(const label seed);
188 
189 //- Return random integer (uniform distribution between 0 and 2^31)
191 
192 //- Return random double precision (uniform distribution between 0 and 1)
193 scalar osRandomDouble();
194 
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 } // End namespace Foam
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #endif
203 
204 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:102
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
pid_t ppid()
Return the parent PID of this process.
Definition: POSIX.C:84
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
Definition: POSIX.C:1169
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
Definition: POSIX.C:289
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:937
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:620
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1135
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:486
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:191
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:782
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
Definition: POSIX.C:757
time_t lastModified(const fileName &)
Return time of last file modification.
Definition: POSIX.C:512
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:130
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1054
string domainName()
Return the system&#39;s domain name, as per hostname(1) with the &#39;-d&#39; option.
Definition: POSIX.C:149
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
Definition: POSIX.C:1159
string userName()
Return the user&#39;s login name.
Definition: POSIX.C:170
off_t fileSize(const fileName &)
Return size of file.
Definition: POSIX.C:498
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:720
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:839
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:78
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:295
pid_t pgid()
Return the group PID of this process.
Definition: POSIX.C:90
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool isAdministrator()
Is user administrator.
Definition: POSIX.C:185
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:480
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
mode_t mode(const fileName &)
Return the file mode.
Definition: POSIX.C:447
bool chMod(const fileName &, const mode_t)
Set the file mode.
Definition: POSIX.C:441
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:246
void fdClose(const int)
Close file descriptor.
Definition: POSIX.C:925
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:96
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:819
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:50
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
Definition: POSIX.C:919
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1026
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:527
void osRandomSeed(const label seed)
Seed random number generator.
Definition: POSIX.C:1149
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:120
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1066
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Definition: POSIX.C:1094
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
Namespace for OpenFOAM.
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1020