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(const fileName&, const bool followLink = true);
104 
105 //- Return the file type: DIRECTORY or FILE
106 fileName::Type type(const fileName&, const bool followLink = true);
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
111 (
112  const fileName&,
113  const bool checkGzip=true,
114  const bool followLink = true
115 );
116 
117 //- Does the name exist as a DIRECTORY in the file system?
118 bool isDir(const fileName&, const bool followLink = true);
119 
120 //- Does the name exist as a FILE in the file system?
121 // Optionally enable/disable check for gzip file.
122 bool isFile
123 (
124  const fileName&,
125  const bool checkGzip=true,
126  const bool followLink = true
127 );
128 
129 //- Return size of file
130 off_t fileSize(const fileName&, const bool followLink = true);
131 
132 //- Return time of last file modification
133 time_t lastModified(const fileName&, const bool followLink = true);
134 
135 //- Return time of last file modification
136 double highResLastModified(const fileName&, const bool followLink = true);
137 
138 //- Read a directory and return the entries as a string list
140 (
141  const fileName&,
143  const bool filtergz=true,
144  const bool followLink = true
145 );
146 
147 //- Copy, recursively if necessary, the source to the destination
148 bool cp(const fileName& src, const fileName& dst, const bool followLink = true);
149 
150 //- Create a softlink. dst should not exist. Returns true if successful.
151 bool ln(const fileName& src, const fileName& dst);
152 
153 //- Rename src to dst
154 bool mv
155 (
156  const fileName& src,
157  const fileName& dst,
158  const bool followLink = false
159 );
160 
161 //- Rename to a corresponding backup file
162 // If the backup file already exists, attempt with "01" .. "99" suffix
163 bool mvBak(const fileName&, const std::string& ext = "bak");
164 
165 //- Remove a file, returning true if successful otherwise false
166 bool rm(const fileName&);
167 
168 //- Remove a directory and its contents
169 bool rmDir(const fileName&);
170 
171 //- Sleep for the specified number of seconds
172 unsigned int sleep(const unsigned int);
173 
174 //- Close file descriptor
175 void fdClose(const int);
176 
177 //- Check if machine is up by pinging given port
178 bool ping(const string&, const label port, const label timeOut);
179 
180 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
181 bool ping(const string&, const label timeOut=10);
182 
183 //- Execute the specified command
184 int system(const std::string& command);
185 
186 //- Open a shared library. Return handle to library. Print error message
187 // if library cannot be loaded (check = true)
188 void* dlOpen(const fileName& lib, const bool check = true);
189 
190 //- Close a dlopened library using handle. Return true if successful
191 bool dlClose(void*);
192 
193 //- Lookup a symbol in a dlopened library using handle to library
194 void* dlSym(void* handle, const std::string& symbol);
195 
196 //- Report if symbol in a dlopened library could be found
197 bool dlSymFound(void* handle, const std::string& symbol);
198 
199 //- Return all loaded libraries
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace Foam
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 #endif
209 
210 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:96
time_t lastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: POSIX.C:588
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 mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:937
off_t fileSize(const fileName &, const bool followLink=true)
Return size of file.
Definition: POSIX.C:566
pid_t ppid()
Return the parent PID of this process.
Definition: POSIX.C:78
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
Definition: POSIX.C:737
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
Definition: POSIX.C:283
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: POSIX.C:1134
fileNameList dlLoaded()
Return all loaded libraries.
Definition: POSIX.C:1332
fileName home()
Return home directory path name for the current user.
Definition: POSIX.C:185
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:966
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:124
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1251
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:528
string domainName()
Return the system&#39;s domain name, as per hostname(1) with the &#39;-d&#39; option.
Definition: POSIX.C:143
mode_t mode(const fileName &, const bool followLink=true)
Return the file mode.
Definition: POSIX.C:459
string userName()
Return the user&#39;s login name.
Definition: POSIX.C:164
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: POSIX.C:896
bool rmDir(const fileName &)
Remove a directory and its contents.
Definition: POSIX.C:1032
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:72
bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:543
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:289
pid_t pgid()
Return the group PID of this process.
Definition: POSIX.C:84
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:481
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool isAdministrator()
Is user administrator.
Definition: POSIX.C:179
bool chMod(const fileName &, const mode_t)
Set the file mode.
Definition: POSIX.C:445
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:240
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:635
void fdClose(const int)
Close file descriptor.
Definition: POSIX.C:1122
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:90
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1008
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:1116
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1223
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:114
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
Definition: POSIX.C:1263
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Definition: POSIX.C:1291
bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:509
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:1217
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: POSIX.C:610