54 #include <sys/types.h> 56 #include <sys/socket.h> 61 #include <netinet/in.h> 93 return ::getenv(envName.c_str()) !=
nullptr;
99 char*
env = ::getenv(envName.c_str());
117 const std::string& value,
121 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
128 ::gethostname(buf,
sizeof(buf));
133 struct hostent *hp = ::gethostbyname(buf);
147 ::gethostname(buf,
sizeof(buf));
150 struct hostent *hp = ::gethostbyname(buf);
153 char *
p = ::strchr(hp->h_name,
'.');
167 struct passwd* pw = ::getpwuid(::getuid());
182 return (::geteuid() == 0);
188 char*
env = ::getenv(
"HOME");
196 struct passwd* pw = ::getpwuid(getuid());
216 pw = ::getpwnam(userName.c_str());
220 char*
env = ::getenv(
"HOME");
227 pw = ::getpwuid(::getuid());
247 while(pathLengthLimit == path.
size())
249 if (::getcwd(path.
data(), path.
size()))
253 else if(errno == ERANGE)
263 <<
"Attempt to increase path length beyond limit of " 277 <<
"Couldn't get the current working directory" 286 return ::chdir(dir.c_str()) == 0;
303 if (filePath.empty())
309 if (::mkdir(filePath.c_str(),
mode) == 0)
321 <<
"The filesystem containing " << filePath
322 <<
" does not support the creation of directories." 338 <<
" points outside your accessible address space." 347 <<
"The parent directory does not allow write " 348 "permission to the process,"<<
nl 349 <<
"or one of the directories in " << filePath
350 <<
" did not allow search (execute) permission." 359 <<
"" << filePath <<
" is too long." 370 return mkDir(filePath, mode);
375 <<
"Couldn't create directory " << filePath
385 <<
"A component used as a directory in " << filePath
386 <<
" is not, in fact, a directory." 395 <<
"Insufficient kernel memory was available to make " 396 "directory " << filePath <<
'.' 406 <<
" refers to a file on a read-only filesystem." 415 <<
"Too many symbolic links were encountered in resolving " 425 <<
"The device containing " << filePath
426 <<
" has no room for the new directory or " 427 <<
"the user's disk quota is exhausted." 436 <<
"Couldn't create directory " << filePath
456 return ::chmod(name.c_str(), m) == 0;
463 const bool checkVariants,
464 const bool followLink
475 fileStat fileStatus(name, checkVariants, followLink);
478 return fileStatus.
status().st_mode;
490 const bool checkVariants,
491 const bool followLink
498 mode_t m =
mode(name, checkVariants, followLink);
522 const bool checkVariants,
523 const bool followLink
529 <<
bool(checkVariants) <<
" followLink:" << followLink <<
endl;
535 return mode(name, checkVariants, followLink);
544 << followLink <<
endl;
550 return S_ISDIR(
mode(name,
false, followLink));
557 const bool checkVariants,
558 const bool followLink
564 <<
bool(checkVariants) <<
" followLink:" << followLink <<
endl;
571 return S_ISREG(
mode(name, checkVariants, followLink));
578 const bool checkVariants,
579 const bool followLink
585 <<
bool(checkVariants) <<
" followLink:" << followLink <<
endl;
591 fileStat fileStatus(name, checkVariants, followLink);
594 return fileStatus.
status().st_size;
606 const bool checkVariants,
607 const bool followLink
613 <<
bool(checkVariants) <<
" followLink:" << followLink <<
endl;
619 fileStat fileStatus(name, checkVariants, followLink);
622 return fileStatus.
status().st_mtime;
634 const bool checkVariants,
635 const bool followLink
641 <<
bool(checkVariants) <<
" followLink:" << followLink <<
endl;
647 fileStat fileStatus(name, checkVariants, followLink);
651 fileStatus.
status().st_mtime
652 + 1
e-9*fileStatus.
status().st_atim.tv_nsec;
665 const bool filterVariants,
666 const bool followLink
686 if ((source = ::opendir(directory.c_str())) ==
nullptr)
691 <<
"cannot open directory " << directory <<
endl;
697 while ((list = ::readdir(source)) !=
nullptr)
702 if (fName.size() && fName[0] !=
'.')
712 && fName[fName.size()-1] !=
'~' 720 if ((directory/fName).type(
false, followLink) ==
type)
722 bool filtered =
false;
749 return dirEntries.
toc();
779 destFile = destFile/src.
name();
789 std::ifstream srcStream(src.c_str());
795 std::ofstream destStream(destFile.c_str());
802 destStream << srcStream.rdbuf();
815 destFile = destFile/src.
name();
840 char* realSrcPath = realpath(src.c_str(),
nullptr);
841 char* realDestPath = realpath(destFile.c_str(),
nullptr);
842 const bool samePath = strcmp(realSrcPath, realDestPath) == 0;
844 if (POSIX::debug && samePath)
847 <<
"Attempt to copy " << realSrcPath <<
" to itself" <<
endl;
873 <<
"Copying : " << src/contents[i]
874 <<
" to " << destFile/contents[i] <<
endl;
878 cp(src/contents[i], destFile/contents[i], followLink);
895 <<
"Copying : " << src/subdirs[i]
896 <<
" to " << destFile <<
endl;
900 cp(src/subdirs[i], destFile, followLink);
913 <<
" : Create softlink from : " << src <<
" to " << dst <<
endl;
923 <<
"destination " << dst <<
" already exists. Not linking." 931 <<
"source " << src <<
" does not exist." <<
endl;
935 if (::symlink(src.c_str(), dst.c_str()) == 0)
942 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
967 return ::rename(src.c_str(), dstName.c_str()) == 0;
971 return ::rename(src.c_str(), dst.c_str()) == 0;
981 <<
" : moving : " << src <<
" to extension " << ext <<
endl;
988 if (
exists(src,
false,
false))
990 const int maxIndex = 99;
993 for (
int n = 0;
n <= maxIndex;
n++)
998 sprintf(index,
"%02d",
n);
1004 if (!
exists(dstName,
false,
false) ||
n == maxIndex)
1006 return ::rename(src.c_str(), dstName.c_str()) == 0;
1029 if (
remove(file.c_str()) == 0)
1037 if (::
remove(
string(fileVar).c_str()) == 0)
1060 struct dirent *list;
1063 if ((source = ::opendir(directory.c_str())) ==
nullptr)
1066 <<
"cannot open directory " << directory <<
endl;
1073 while ((list = ::readdir(source)) !=
nullptr)
1077 if (fName !=
"." && fName !=
"..")
1086 <<
"failed to remove directory " << fName
1087 <<
" while removing directory " << directory
1100 <<
"failed to remove file " << fName
1101 <<
" while removing directory " << directory
1116 <<
"failed to remove directory " << directory <<
endl;
1141 <<
"close error on " << fd <<
endl 1149 const string& destName,
1150 const label destPort,
1154 struct hostent *hostPtr;
1155 volatile int sockfd;
1156 struct sockaddr_in destAddr;
1159 if ((hostPtr = ::gethostbyname(destName.c_str())) ==
nullptr)
1162 <<
"gethostbyname error " << h_errno <<
" for host " << destName
1167 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
1170 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1179 memset(reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
1180 destAddr.sin_family = AF_INET;
1181 destAddr.sin_port = htons(ushort(destPort));
1182 destAddr.sin_addr.s_addr = addr;
1185 timer myTimer(timeOut);
1199 reinterpret_cast<struct sockaddr*>(&destAddr),
1200 sizeof(
struct sockaddr)
1206 int connectErr = errno;
1210 if (connectErr == ECONNREFUSED)
1226 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1240 std::cout<<
"dlOpen(const fileName&)" 1243 void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1245 if (!handle && check)
1248 <<
"dlopen error : " << ::dlerror()
1255 <<
"dlOpen(const fileName&)" 1256 <<
" : dlopen of " << lib
1270 <<
" : dlclose of handle " << handle <<
std::endl;
1272 return ::dlclose(handle) == 0;
1281 <<
"dlSym(void*, const std::string&)" 1282 <<
" : dlsym of " << symbol <<
std::endl;
1289 void* fun = ::dlsym(handle, symbol.c_str());
1292 char *
error = ::dlerror();
1297 <<
"Cannot lookup symbol " << symbol <<
" : " << error
1307 if (handle && !symbol.empty())
1312 <<
"dlSymFound(void*, const std::string&)" 1313 <<
" : dlsym of " << symbol <<
std::endl;
1320 (void) ::dlsym(handle, symbol.c_str());
1323 return !::dlerror();
1334 struct dl_phdr_info *info,
1341 ptr->
append(info->dlpi_name);
1354 <<
" : determined loaded libraries :" << libs.
size() <<
std::endl;
string getEnv(const word &)
Return environment variable of given name.
time_t lastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
static void printStack(Ostream &)
Helper function to print a stack.
A HashTable with keys but without contents.
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
#define forAll(list, i)
Loop across all elements in list.
mode_t mode(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file mode.
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
A class for handling file names.
errorManipArg< error, int > exit(error &err, const int errNo=1)
static const label nVariants_
Number of file variants.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
const label pathLengthMax
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
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.
static const fileName null
An empty fileName.
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
static bool master(const label communicator=0)
Am I the master process.
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.
fileNameList dlLoaded()
Return all loaded libraries.
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 insert(const Key &key)
Insert a new entry.
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
word ext() const
Return file name extension (part after last .)
dlLibraryTable libs
Table of loaded dynamic libraries.
wordList components(const char delimiter='/') const
Return path components as wordList.
const struct stat & status() const
Raw status.
fileType type(const bool checkVariants=true, const bool followLink=true) const
Return the file type: file, directory, undefined or.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
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...
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.
bool isAbsolute() const
Return true if file name is absolute.
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
A class for handling words, derived from string.
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
word name() const
Return file name (part beyond last /)
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.
fileType
Enumeration of file types.
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.
errorManip< error > abort(error &err)
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
static const string null
An empty string.
bool rmDir(const fileName &)
Remove a directory and its contents.
pid_t pid()
Return the PID of this process.
defineTypeNameAndDebug(combustionModel, 0)
Wrapper for stat() system call.
#define timedOut(x)
Check it a timeout has occurred.
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.
bool isAdministrator()
Is user administrator.
void setSize(const label)
Reset size of List.
T * data()
Return a pointer to the first data element,.
double highResLastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
#define WarningInFunction
Report a warning using Foam::Warning.
prefixOSstream Pout(cout, "Pout")
bool chMod(const fileName &, const mode_t)
Set the file mode.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
fileName lessExt() const
Return file name without extension (part before last .)
fileName cwd()
Return current working directory path name.
void fdClose(const int)
Close file descriptor.
static const char * variantExts_[]
Extensions of the file variants.
fileName path() const
Return directory path name (part before last /)
const doubleScalar e
Elementary charge.
off_t fileSize(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return size of file.
List< Key > toc() const
Return the table of contents.
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.
FvWallInfoData< WallInfo, bool > bool
A class for handling character strings derived from std::string.
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
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.
bool isValid() const
Did constructor fail.
int system(const std::string &command)
Execute the specified command.
#define InfoInFunction
Report an information message using Foam::Info.