Go to the documentation of this file.
51 #include <sys/types.h>
53 #include <sys/socket.h>
57 #include <netinet/in.h>
61 # if INT_MAX != 2147483647
62 # error "INT_MAX != 2147483647"
63 # error "The random number generator may not work!"
97 return ::getenv(envName.c_str()) != NULL;
103 char*
env = ::getenv(envName.c_str());
121 const std::string& value,
125 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
132 ::gethostname(buf,
sizeof(buf));
137 struct hostent *hp = ::gethostbyname(buf);
151 ::gethostname(buf,
sizeof(buf));
154 struct hostent *hp = ::gethostbyname(buf);
157 char *
p = ::strchr(hp->h_name,
'.');
171 struct passwd* pw = ::getpwuid(::getuid());
186 return (::geteuid() == 0);
193 char*
env = ::getenv(
"HOME");
201 struct passwd* pw = ::getpwuid(getuid());
209 return fileName::null;
225 char*
env = ::getenv(
"HOME");
232 pw = ::getpwuid(::getuid());
241 return fileName::null;
249 if (::getcwd(buf,
sizeof(buf)))
256 <<
"Couldn't get the current working directory"
259 return fileName::null;
266 return ::chdir(dir.c_str()) == 0;
284 if (
isDir(searchDir))
296 fullName = searchDir/
name;
311 searchDir =
getEnv(
"WM_PROJECT_SITE");
312 if (searchDir.size())
314 if (
isDir(searchDir))
326 fullName = searchDir/
name;
343 searchDir =
getEnv(
"WM_PROJECT_INST_DIR");
344 if (
isDir(searchDir))
356 fullName = searchDir/
"site"/
name;
371 searchDir =
getEnv(
"WM_PROJECT_DIR");
372 if (
isDir(searchDir))
392 <<
"--> FOAM FATAL ERROR in Foam::findEtcFiles() :"
393 " could not find mandatory file\n '"
422 if (pathName.empty())
428 if (::mkdir(pathName.c_str(),
mode) == 0)
440 <<
"The filesystem containing " << pathName
441 <<
" does not support the creation of directories."
457 <<
" points outside your accessible address space."
466 <<
"The parent directory does not allow write "
467 "permission to the process,"<<
nl
468 <<
"or one of the directories in " << pathName
469 <<
" did not allow search (execute) permission."
478 <<
"" << pathName <<
" is too long."
494 <<
"Couldn't create directory " << pathName
504 <<
"A component used as a directory in " << pathName
505 <<
" is not, in fact, a directory."
514 <<
"Insufficient kernel memory was available to make "
515 "directory " << pathName <<
'.'
525 <<
" refers to a file on a read-only filesystem."
534 <<
"Too many symbolic links were encountered in resolving "
544 <<
"The device containing " << pathName
545 <<
" has no room for the new directory or "
546 <<
"the user's disk quota is exhausted."
555 <<
"Couldn't create directory " << pathName
568 return ::chmod(
name.c_str(), m) == 0;
578 return fileStatus.
status().st_mode;
594 return fileName::FILE;
598 return fileName::DIRECTORY;
602 return fileName::UNDEFINED;
634 return fileStatus.
status().st_size;
649 return fileStatus.
status().st_mtime;
668 static const int maxNnames = 100;
672 Info<<
"readDir(const fileName&, const fileType, const bool filtergz)"
673 <<
" : reading directory " << directory <<
endl;
687 if ((source = ::opendir(directory.c_str())) == NULL)
693 Info<<
"readDir(const fileName&, const fileType, "
694 "const bool filtergz) : cannot open directory "
695 << directory <<
endl;
701 while ((list = ::readdir(source)) != NULL)
706 if (fName.size() && fName[0] !=
'.')
712 (
type == fileName::DIRECTORY)
715 type == fileName::FILE
716 && fName[fName.size()-1] !=
'~'
724 if ((directory/fName).
type() ==
type)
726 if (nEntries >= dirEntries.
size())
731 if (filtergz && fExt ==
"gz")
733 dirEntries[nEntries++] = fName.
lessExt();
737 dirEntries[nEntries++] = fName;
766 if (src.
type() == fileName::FILE)
769 if (destFile.
type() == fileName::DIRECTORY)
771 destFile = destFile/src.
name();
781 std::ifstream srcStream(src.c_str());
787 std::ofstream destStream(destFile.c_str());
795 while (srcStream.get(ch))
801 if (!srcStream.eof() || !destStream)
806 else if (src.
type() == fileName::DIRECTORY)
809 if (destFile.
type() == fileName::DIRECTORY)
826 Info<<
"Copying : " << src/contents[i]
827 <<
" to " << destFile/contents[i] <<
endl;
831 cp(src/contents[i], destFile/contents[i]);
840 Info<<
"Copying : " << src/subdirs[i]
841 <<
" to " << destFile <<
endl;
845 cp(src/subdirs[i], destFile);
858 Info<<
"Create softlink from : " << src <<
" to " << dst
865 <<
"destination " << dst <<
" already exists. Not linking."
873 <<
"source " << src <<
" does not exist." <<
endl;
877 if (::symlink(src.c_str(), dst.c_str()) == 0)
884 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
895 Info<<
"Move : " << src <<
" to " << dst <<
endl;
900 dst.
type() == fileName::DIRECTORY
901 && src.
type() != fileName::DIRECTORY
906 return ::rename(src.c_str(), dstName.c_str()) == 0;
910 return ::rename(src.c_str(), dst.c_str()) == 0;
921 Info<<
"mvBak : " << src <<
" to extension " << ext <<
endl;
926 const int maxIndex = 99;
929 for (
int n = 0;
n <= maxIndex;
n++)
934 sprintf(index,
"%02d",
n);
940 if (!
exists(dstName,
false) ||
n == maxIndex)
942 return ::rename(src.c_str(), dstName.c_str()) == 0;
958 Info<<
"Removing : " << file <<
endl;
962 if (remove(file.c_str()) == 0)
968 return ::remove(
string(file +
".gz").c_str()) == 0;
978 Info<<
"rmDir(const fileName&) : "
979 <<
"removing directory " << directory <<
endl;
987 if ((source = ::opendir(directory.c_str())) == NULL)
990 <<
"cannot open directory " << directory <<
endl;
997 while ((list = ::readdir(source)) != NULL)
1001 if (fName !=
"." && fName !=
"..")
1005 if (
path.type() == fileName::DIRECTORY)
1010 <<
"failed to remove directory " << fName
1011 <<
" while removing directory " << directory
1024 <<
"failed to remove file " << fName
1025 <<
" while removing directory " << directory
1040 <<
"failed to remove directory " << directory <<
endl;
1065 <<
"close error on " << fd <<
endl
1073 const string& destName,
1074 const label destPort,
1078 struct hostent *hostPtr;
1079 volatile int sockfd;
1080 struct sockaddr_in destAddr;
1083 if ((hostPtr = ::gethostbyname(destName.c_str())) == NULL)
1086 <<
"gethostbyname error " << h_errno <<
" for host " << destName
1091 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
1094 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1103 memset(
reinterpret_cast<char *
>(&destAddr),
'\0',
sizeof(destAddr));
1104 destAddr.sin_family = AF_INET;
1105 destAddr.sin_port = htons(ushort(destPort));
1106 destAddr.sin_addr.s_addr = addr;
1109 timer myTimer(timeOut);
1123 reinterpret_cast<struct sockaddr*
>(&destAddr),
1124 sizeof(
struct sockaddr)
1130 int connectErr = errno;
1134 if (connectErr == ECONNREFUSED)
1151 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1165 std::cout<<
"dlOpen(const fileName&)"
1166 <<
" : dlopen of " << libName <<
std::endl;
1170 char const *
const soExt =
".so";
1173 char const *
const unixExt =
".dylib";
1174 unixLibName.
replace(soExt, unixExt);
1176 char const *
const unixExt = soExt;
1180 void* handle = ::dlopen(unixLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1185 unixLibName =
"lib";
1186 unixLibName += libName;
1187 unixLibName += unixExt;
1190 ::dlopen(unixLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1193 if (!handle && check)
1196 <<
"dlopen error : " << ::dlerror()
1203 <<
"dlOpen(const fileName&)"
1204 <<
" : dlopen of " << libName
1218 <<
" : dlclose of handle " << handle <<
std::endl;
1220 return ::dlclose(handle) == 0;
1229 <<
"dlSym(void*, const std::string&)"
1230 <<
" : dlsym of " << symbol <<
std::endl;
1236 void* fun = ::dlsym(handle, symbol.c_str());
1239 char *
error = ::dlerror();
1244 <<
"Cannot lookup symbol " << symbol <<
" : " <<
error
1254 if (handle && !symbol.empty())
1259 <<
"dlSymFound(void*, const std::string&)"
1260 <<
" : dlsym of " << symbol <<
std::endl;
1267 (void) ::dlsym(handle, symbol.c_str());
1270 return !::dlerror();
1283 DynamicList<fileName> libs;
1293 struct dl_phdr_info *info,
1300 ptr->
append(info->dlpi_name);
1313 <<
" : determined loaded libraries :" << libs.size() <<
std::endl;
1318 #endif // not DARWIN
1324 srandom((
unsigned int)seed);
1344 return (scalar)
random()/INT_MAX;
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
bool env(const word &)
Return true if environment variable of given name is defined.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
PID_T ppid()
Return the parent PID of this process.
A class for handling words, derived from string.
A class for handling file names.
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
#define forAll(list, i)
Loop across all elements in list.
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
wordList components(const char delimiter='/') const
Return path components as wordList.
fileName path() const
Return directory path name (part before last /)
void fdClose(const int)
Close file descriptor.
bool isAbsolute() const
Return true if file name is absolute.
bool isValid() const
Did constructor fail.
Type
Enumerations to handle file types and modes.
Wrapper for stat() system call.
time_t lastModified(const fileName &)
Return time of last file modification.
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
fileName lessExt() const
Return file name without extension (part before last .)
A class for handling character strings derived from std::string.
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
void osRandomSeed(const label seed)
Seed random number generator.
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
mode_t mode(const fileName &)
Return the file mode.
bool chMod(const fileName &, const mode_t)
Set the file mode.
word name() const
Return file name (part beyond last /)
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
bool isAdministrator()
Is user administrator.
std::string toUnixPath(const std::string &path)
Convert to unix path separators.
string userName()
Return the user's login name.
PID_T pid()
Return the PID of this process.
Type type() const
Return the file type: FILE, DIRECTORY or UNDEFINED.
string getEnv(const word &)
Return environment variable of given name.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
void append(const T &)
Append an element at the end of the list.
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
#define timedOut(x)
Check it a timeout has occured.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
const volScalarField & cp
PID_T pgid()
Return the group PID of this process.
word ext() const
Return file name extension (part after last .)
errorManip< error > abort(error &err)
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
errorManipArg< error, int > exit(error &err, const int errNo=1)
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
void setSize(const label)
Reset size of List.
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
fileName home()
Return home directory path name for the current user.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
const struct stat & status() const
Raw status.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Implements a timeout mechanism via sigalarm.
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
fileName cwd()
Return current working directory path name.
fileNameList dlLoaded()
Return all loaded libraries.
void size(const label)
Override size to be inconsistent with allocated storage.
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true)
Read a directory and return the entries as a string list.
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
const char *const FOAMversion
defineTypeNameAndDebug(combustionModel, 0)
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
#define WarningInFunction
Report a warning using Foam::Warning.
Class to handle errors and exceptions in a simple, consistent stream-based manner.
int system(const std::string &command)
Execute the specified command.
off_t fileSize(const fileName &)
Return size of file.
bool rmDir(const fileName &)
Remove a dirctory and its contents.
word name(const complex &)
Return a string representation of a complex.