OSspecific.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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
104 (
105  const fileName&,
106  const bool checkVariants = true,
107  const bool followLink = true
108 );
109 
110 //- Return the file type: directory or file
112 (
113  const fileName&,
114  const bool checkVariants = true,
115  const bool followLink = true
116 );
117 
118 //- Does the name exist (as directory or file) in the file system?
119 // Optionally enable/disable check for variant files.
120 bool exists
121 (
122  const fileName&,
123  const bool checkVariants = true,
124  const bool followLink = true
125 );
126 
127 //- Does the name exist as a directory in the file system?
128 bool isDir(const fileName&, const bool followLink = true);
129 
130 //- Does the name exist as a file in the file system?
131 // Optionally enable/disable check for variant files.
132 bool isFile
133 (
134  const fileName&,
135  const bool checkVariants = true,
136  const bool followLink = true
137 );
138 
139 //- Return size of file
140 off_t fileSize
141 (
142  const fileName&,
143  const bool checkVariants = true,
144  const bool followLink = true
145 );
146 
147 //- Return time of last file modification
148 time_t lastModified
149 (
150  const fileName&,
151  const bool checkVariants = true,
152  const bool followLink = true
153 );
154 
155 //- Return time of last file modification
156 double highResLastModified
157 (
158  const fileName&,
159  const bool checkVariants = true,
160  const bool followLink = true
161 );
162 
163 //- Read a directory and return the entries as a string list
165 (
166  const fileName&,
167  const fileType = fileType::file,
168  const bool filterVariants = true,
169  const bool followLink = true
170 );
171 
172 //- Copy, recursively if necessary, the source to the destination
173 bool cp(const fileName& src, const fileName& dst, const bool followLink = true);
174 
175 //- Create a softlink. dst should not exist. Returns true if successful.
176 bool ln(const fileName& src, const fileName& dst);
177 
178 //- Rename src to dst
179 bool mv
180 (
181  const fileName& src,
182  const fileName& dst,
183  const bool followLink = false
184 );
185 
186 //- Rename to a corresponding backup file
187 // If the backup file already exists, attempt with "01" .. "99" suffix
188 bool mvBak(const fileName&, const std::string& ext = "bak");
189 
190 //- Remove a file, returning true if successful otherwise false
191 bool rm(const fileName&);
192 
193 //- Remove a directory and its contents
194 bool rmDir(const fileName&);
195 
196 //- Sleep for the specified number of seconds
197 unsigned int sleep(const unsigned int);
198 
199 //- Close file descriptor
200 void fdClose(const int);
201 
202 //- Check if machine is up by pinging given port
203 bool ping(const string&, const label port, const label timeOut);
204 
205 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
206 bool ping(const string&, const label timeOut=10);
207 
208 //- Execute the specified command
209 int system(const std::string& command);
210 
211 //- Open a shared library. Return handle to library. Print error message
212 // if library cannot be loaded (check = true)
213 void* dlOpen(const fileName& lib, const bool check = true);
214 
215 //- Close a dlopened library using handle. Return true if successful
216 bool dlClose(void*);
217 
218 //- Lookup a symbol in a dlopened library using handle to library
219 void* dlSym(void* handle, const std::string& symbol);
220 
221 //- Report if symbol in a dlopened library could be found
222 bool dlSymFound(void* handle, const std::string& symbol);
223 
224 //- Return all loaded libraries
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 } // End namespace Foam
230 
231 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 
233 #endif
234 
235 // ************************************************************************* //
Namespace for OpenFOAM.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
Definition: POSIX.C:555
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:241
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:115
off_t fileSize(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return size of file.
Definition: POSIX.C:576
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:976
mode_t mode(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file mode.
Definition: POSIX.C:461
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:50
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:91
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
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Definition: POSIX.C:1305
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
bool isAdministrator()
Is user administrator.
Definition: POSIX.C:180
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1236
double highResLastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
Definition: POSIX.C:632
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1264
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
Definition: POSIX.C:520
void fdClose(const int)
Close file descriptor.
Definition: POSIX.C:1136
string userName()
Return the user's login name.
Definition: POSIX.C:165
pid_t pgid()
Return the group PID of this process.
Definition: POSIX.C:85
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: POSIX.C:125
bool chMod(const fileName &, const mode_t)
Set the file mode.
Definition: POSIX.C:446
bool rmDir(const fileName &)
Remove a directory and its contents.
Definition: POSIX.C:1047
pid_t ppid()
Return the parent PID of this process.
Definition: POSIX.C:79
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:1148
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
time_t lastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
Definition: POSIX.C:604
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
fileType
Enumeration of file types.
Definition: fileName.H:67
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1276
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:753
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1346
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:948
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:186
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
Definition: POSIX.C:1130
fileNameList readDir(const fileName &, const fileType=fileType::file, const bool filterVariants=true, const bool followLink=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:662
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
Definition: POSIX.C:144
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:908
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
Definition: POSIX.C:284