51 #include <sys/types.h> 53 #include <sys/socket.h> 58 #include <netinet/in.h> 62 #if INT_MAX != 2147483647 63 #error "INT_MAX != 2147483647" 64 #error "The random number generator may not work!" 98 return ::getenv(envName.c_str()) != NULL;
104 char*
env = ::getenv(envName.c_str());
122 const std::string& value,
126 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
133 ::gethostname(buf,
sizeof(buf));
138 struct hostent *hp = ::gethostbyname(buf);
152 ::gethostname(buf,
sizeof(buf));
155 struct hostent *hp = ::gethostbyname(buf);
158 char *
p = ::strchr(hp->h_name,
'.');
172 struct passwd* pw = ::getpwuid(::getuid());
187 return (::geteuid() == 0);
193 char*
env = ::getenv(
"HOME");
201 struct passwd* pw = ::getpwuid(getuid());
221 pw = ::getpwnam(userName.c_str());
225 char*
env = ::getenv(
"HOME");
232 pw = ::getpwuid(::getuid());
252 while(pathLengthLimit == path.
size())
254 if (::getcwd(path.
data(), path.
size()))
258 else if(errno == ERANGE)
268 <<
"Attempt to increase path length beyond limit of " 282 <<
"Couldn't get the current working directory" 291 return ::chdir(dir.c_str()) == 0;
298 if (pathName.empty())
304 if (::mkdir(pathName.c_str(),
mode) == 0)
316 <<
"The filesystem containing " << pathName
317 <<
" does not support the creation of directories." 333 <<
" points outside your accessible address space." 342 <<
"The parent directory does not allow write " 343 "permission to the process,"<<
nl 344 <<
"or one of the directories in " << pathName
345 <<
" did not allow search (execute) permission." 354 <<
"" << pathName <<
" is too long." 365 return mkDir(pathName, mode);
370 <<
"Couldn't create directory " << pathName
380 <<
"A component used as a directory in " << pathName
381 <<
" is not, in fact, a directory." 390 <<
"Insufficient kernel memory was available to make " 391 "directory " << pathName <<
'.' 401 <<
" refers to a file on a read-only filesystem." 410 <<
"Too many symbolic links were encountered in resolving " 420 <<
"The device containing " << pathName
421 <<
" has no room for the new directory or " 422 <<
"the user's disk quota is exhausted." 431 <<
"Couldn't create directory " << pathName
443 return ::chmod(name.c_str(), m) == 0;
452 return fileStatus.
status().st_mode;
463 mode_t m =
mode(name);
488 return S_ISDIR(
mode(name));
494 return S_ISREG(
mode(name)) || (checkGzip && S_ISREG(
mode(name +
".gz")));
503 return fileStatus.
status().st_size;
517 return fileStatus.
status().st_mtime;
535 static const int maxNnames = 100;
540 <<
"reading directory " << directory <<
endl;
554 if ((source = ::opendir(directory.c_str())) == NULL)
561 <<
"cannot open directory " << directory <<
endl;
567 while ((list = ::readdir(source)) != NULL)
572 if (fName.size() && fName[0] !=
'.')
582 && fName[fName.size()-1] !=
'~' 590 if ((directory/fName).type() ==
type)
592 if (nEntries >= dirEntries.
size())
597 if (filtergz && fExt ==
"gz")
599 dirEntries[nEntries++] = fName.
lessExt();
603 dirEntries[nEntries++] = fName;
636 destFile = destFile/src.
name();
646 std::ifstream srcStream(src.c_str());
652 std::ofstream destStream(destFile.c_str());
660 while (srcStream.get(ch))
666 if (!srcStream.eof() || !destStream)
692 <<
"Copying : " << src/contents[i]
693 <<
" to " << destFile/contents[i] <<
endl;
697 cp(src/contents[i], destFile/contents[i]);
707 <<
"Copying : " << src/subdirs[i]
708 <<
" to " << destFile <<
endl;
712 cp(src/subdirs[i], destFile);
725 <<
"Create softlink from : " << src <<
" to " << dst
732 <<
"destination " << dst <<
" already exists. Not linking." 740 <<
"source " << src <<
" does not exist." <<
endl;
744 if (::symlink(src.c_str(), dst.c_str()) == 0)
751 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
762 <<
"Move : " << src <<
" to " << dst <<
endl;
773 return ::rename(src.c_str(), dstName.c_str()) == 0;
777 return ::rename(src.c_str(), dst.c_str()) == 0;
787 <<
"mvBak : " << src <<
" to extension " << ext <<
endl;
792 const int maxIndex = 99;
795 for (
int n = 0;
n <= maxIndex;
n++)
800 sprintf(index,
"%02d",
n);
806 if (!
exists(dstName,
false) ||
n == maxIndex)
808 return ::rename(src.c_str(), dstName.c_str()) == 0;
824 <<
"Removing : " << file <<
endl;
828 if (
remove(file.c_str()) == 0)
834 return ::remove(
string(file +
".gz").c_str()) == 0;
844 <<
"removing directory " << directory <<
endl;
852 if ((source = ::opendir(directory.c_str())) == NULL)
855 <<
"cannot open directory " << directory <<
endl;
862 while ((list = ::readdir(source)) != NULL)
866 if (fName !=
"." && fName !=
"..")
875 <<
"failed to remove directory " << fName
876 <<
" while removing directory " << directory
889 <<
"failed to remove file " << fName
890 <<
" while removing directory " << directory
905 <<
"failed to remove directory " << directory <<
endl;
930 <<
"close error on " << fd <<
endl 938 const string& destName,
939 const label destPort,
943 struct hostent *hostPtr;
945 struct sockaddr_in destAddr;
948 if ((hostPtr = ::gethostbyname(destName.c_str())) == NULL)
951 <<
"gethostbyname error " << h_errno <<
" for host " << destName
956 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
959 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
968 memset(reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
969 destAddr.sin_family = AF_INET;
970 destAddr.sin_port = htons(ushort(destPort));
971 destAddr.sin_addr.s_addr = addr;
974 timer myTimer(timeOut);
988 reinterpret_cast<struct sockaddr*>(&destAddr),
989 sizeof(
struct sockaddr)
995 int connectErr = errno;
999 if (connectErr == ECONNREFUSED)
1016 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1030 std::cout<<
"dlOpen(const fileName&)" 1033 void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1035 if (!handle && check)
1038 <<
"dlopen error : " << ::dlerror()
1045 <<
"dlOpen(const fileName&)" 1046 <<
" : dlopen of " << lib
1060 <<
" : dlclose of handle " << handle <<
std::endl;
1062 return ::dlclose(handle) == 0;
1071 <<
"dlSym(void*, const std::string&)" 1072 <<
" : dlsym of " << symbol <<
std::endl;
1078 void* fun = ::dlsym(handle, symbol.c_str());
1081 char *
error = ::dlerror();
1086 <<
"Cannot lookup symbol " << symbol <<
" : " << error
1096 if (handle && !symbol.empty())
1101 <<
"dlSymFound(void*, const std::string&)" 1102 <<
" : dlsym of " << symbol <<
std::endl;
1109 (void) ::dlsym(handle, symbol.c_str());
1112 return !::dlerror();
1123 struct dl_phdr_info *info,
1130 ptr->
append(info->dlpi_name);
1143 <<
" : determined loaded libraries :" << libs.
size() <<
std::endl;
1152 srandom((
unsigned int)seed);
1172 return (scalar)
random()/INT_MAX;
string getEnv(const word &)
Return environment variable of given name.
#define forAll(list, i)
Loop across all elements in list.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
A class for handling file names.
errorManipArg< error, int > exit(error &err, const int errNo=1)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
const label pathLengthMax
Type type() const
Return the file type: FILE, DIRECTORY or UNDEFINED.
pid_t ppid()
Return the parent PID of this process.
Implements a timeout mechanism via sigalarm.
void size(const label)
Override size to be inconsistent with allocated storage.
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
static const fileName null
An empty fileName.
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
bool cp(const fileName &src, const fileName &dst)
Copy, recursively if necessary, the source to the destination.
fileNameList dlLoaded()
Return all loaded libraries.
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
const label pathLengthChunk
fileName home()
Return home directory path name for the current user.
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
time_t lastModified(const fileName &)
Return time of last file modification.
word name() const
Return file name (part beyond last /)
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))
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
A class for handling words, derived from string.
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
wordList components(const char delimiter='/') const
Return path components as wordList.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
string userName()
Return the user's login name.
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
bool isValid() const
Did constructor fail.
errorManip< error > abort(error &err)
off_t fileSize(const fileName &)
Return size of file.
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
bool isAbsolute() const
Return true if file name is absolute.
static const string null
An empty string.
bool rmDir(const fileName &)
Remove a dirctory and its contents.
pid_t pid()
Return the PID of this process.
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
defineTypeNameAndDebug(combustionModel, 0)
Wrapper for stat() system call.
fileName lessExt() const
Return file name without extension (part before last .)
#define timedOut(x)
Check it a timeout has occured.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
pid_t pgid()
Return the group PID of this process.
word name(const complex &)
Return a string representation of a complex.
bool isAdministrator()
Is user administrator.
void setSize(const label)
Reset size of List.
T * data()
Return a pointer to the first data element,.
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
#define WarningInFunction
Report a warning using Foam::Warning.
mode_t mode(const fileName &)
Return the file mode.
bool chMod(const fileName &, const mode_t)
Set the file mode.
word ext() const
Return file name extension (part after last .)
fileName cwd()
Return current working directory path name.
void fdClose(const int)
Close file descriptor.
bool env(const word &)
Return true if environment variable of given name is defined.
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
fileName path() const
Return directory path name (part before last /)
A class for handling character strings derived from std::string.
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
const struct stat & status() const
Raw status.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true)
Read a directory and return the entries as a string list.
void osRandomSeed(const label seed)
Seed random number generator.
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
Type
Enumerations to handle file types and modes.
int system(const std::string &command)
Execute the specified command.
#define InfoInFunction
Report an information message using Foam::Info.