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());
204 return fileName::null;
220 char*
env = ::getenv(
"HOME");
227 pw = ::getpwuid(::getuid());
236 return fileName::null;
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"
280 return fileName::null;
286 return ::chdir(dir.c_str()) == 0;
296 if ((POSIX::debug & 2) && !Pstream::master())
298 error::printStack(
Pout);
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."
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
451 if ((POSIX::debug & 2) && !Pstream::master())
453 error::printStack(
Pout);
456 return ::chmod(
name.c_str(), m) == 0;
463 const bool checkVariants,
464 const bool followLink
470 if ((POSIX::debug & 2) && !Pstream::master())
472 error::printStack(
Pout);
478 return fileStatus.
status().st_mode;
490 const bool checkVariants,
491 const bool followLink
498 mode_t m =
mode(
name, checkVariants, followLink);
502 return fileType::file;
506 return fileType::link;
510 return fileType::directory;
514 return fileType::undefined;
522 const bool checkVariants,
523 const bool followLink
529 << bool(checkVariants) <<
" followLink:" << followLink <<
endl;
530 if ((POSIX::debug & 2) && !Pstream::master())
532 error::printStack(
Pout);
535 return mode(
name, checkVariants, followLink);
544 << followLink <<
endl;
545 if ((POSIX::debug & 2) && !Pstream::master())
547 error::printStack(
Pout);
550 return S_ISDIR(
mode(
name,
false, followLink));
557 const bool checkVariants,
558 const bool followLink
564 << bool(checkVariants) <<
" followLink:" << followLink <<
endl;
565 if ((POSIX::debug & 2) && !Pstream::master())
567 error::printStack(
Pout);
571 return S_ISREG(
mode(
name, checkVariants, followLink));
578 const bool checkVariants,
579 const bool followLink
585 << bool(checkVariants) <<
" followLink:" << followLink <<
endl;
586 if ((POSIX::debug & 2) && !Pstream::master())
588 error::printStack(
Pout);
594 return fileStatus.
status().st_size;
606 const bool checkVariants,
607 const bool followLink
613 << bool(checkVariants) <<
" followLink:" << followLink <<
endl;
614 if ((POSIX::debug & 2) && !Pstream::master())
616 error::printStack(
Pout);
622 return fileStatus.
status().st_mtime;
634 const bool checkVariants,
635 const bool followLink
641 << bool(checkVariants) <<
" followLink:" << followLink <<
endl;
642 if ((POSIX::debug & 2) && !Pstream::master())
644 error::printStack(
Pout);
651 fileStatus.
status().st_mtime
652 + 1
e-9*fileStatus.
status().st_atim.tv_nsec;
665 const bool filterVariants,
666 const bool followLink
672 if ((POSIX::debug & 2) && !Pstream::master())
674 error::printStack(
Pout);
686 if ((source = ::opendir(
directory.c_str())) ==
nullptr)
697 while ((list = ::readdir(source)) !=
nullptr)
702 if (fName.size() && fName[0] !=
'.')
708 (
type == fileType::directory)
711 type == fileType::file
712 && fName[fName.size()-1] !=
'~'
722 bool filtered =
false;
726 for (
label i = 0; i < fileStat::nVariants_; ++ i)
728 if (fExt == fileStat::variantExts_[i])
749 return dirEntries.
toc();
758 if ((POSIX::debug & 2) && !Pstream::master())
760 error::printStack(
Pout);
774 if (srcType == fileType::file)
777 if (destFile.
type() == fileType::directory)
779 destFile = destFile/src.
name();
789 std::ifstream srcStream(src.c_str());
795 std::ofstream destStream(destFile.c_str());
802 destStream << srcStream.rdbuf();
810 else if (srcType == fileType::link)
813 if (destFile.
type() == fileType::directory)
815 destFile = destFile/src.
name();
826 else if (srcType == fileType::directory)
829 if (destFile.
type() == fileType::directory)
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;
914 if ((POSIX::debug & 2) && !Pstream::master())
916 error::printStack(
Pout);
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;
953 if ((POSIX::debug & 2) && !Pstream::master())
955 error::printStack(
Pout);
961 dst.
type() == fileType::directory
962 && src.
type(
false, followLink) != fileType::directory
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;
982 if ((POSIX::debug & 2) && !Pstream::master())
984 error::printStack(
Pout);
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;
1022 if ((POSIX::debug & 2) && !Pstream::master())
1024 error::printStack(
Pout);
1029 if (remove(
file.c_str()) == 0)
1034 for (
label i = 0; i < fileStat::nVariants_; ++ i)
1036 const fileName fileVar =
file +
"." + fileStat::variantExts_[i];
1037 if (::remove(
string(fileVar).c_str()) == 0)
1052 if ((POSIX::debug & 2) && !Pstream::master())
1054 error::printStack(
Pout);
1060 struct dirent *list;
1063 if ((source = ::opendir(
directory.c_str())) ==
nullptr)
1073 while ((list = ::readdir(source)) !=
nullptr)
1077 if (fName !=
"." && fName !=
"..")
1081 if (path.
type(
false,
false) == fileType::directory)
1086 <<
"failed to remove directory " << fName
1087 <<
" while removing directory " <<
directory
1100 <<
"failed to remove file " << fName
1101 <<
" while removing directory " <<
directory
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;
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
#define forAll(list, i)
Loop across all elements in list.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
A HashTable with keys but without contents.
bool insert(const Key &key)
Insert a new entry.
List< Key > toc() const
Return the table of contents.
void size(const label)
Override size to be inconsistent with allocated storage.
void setSize(const label)
Reset size of List.
T * data()
Return a pointer to the first data element,.
Class to handle errors and exceptions in a simple, consistent stream-based manner.
A class for handling file names.
bool isAbsolute() const
Return true if file name is absolute.
wordList components(const char delimiter='/') const
Return path components as wordList.
word name() const
Return file name (part beyond last /)
fileName lessExt() const
Return file name without extension (part before last .)
fileType type(const bool checkVariants=true, const bool followLink=true) const
Return the file type: file, directory, undefined or.
word ext() const
Return file name extension (part after last .)
fileName path() const
Return directory path name (part before last /)
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
Wrapper for stat() system call.
const struct stat & status() const
Raw status.
bool isValid() const
Did constructor fail.
A class for handling character strings derived from std::string.
Implements a timeout mechanism via sigalarm.
A class for handling words, derived from string.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
const label pathLengthMax
const label pathLengthChunk
const dimensionedScalar e
Elementary charge.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
errorManipArg< error, int > exit(error &err, const int errNo=1)
dlLibraryTable libs
Table of loaded dynamic libraries.
fileName cwd()
Return current working directory path name.
int system(const std::string &command)
Execute the specified command.
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
off_t fileSize(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return size of file.
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
mode_t mode(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file mode.
bool env(const word &)
Return true if environment variable of given name is defined.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
bool dlSymFound(void *handle, const std::string &symbol)
Report if symbol in a dlopened library could be found.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Ostream & endl(Ostream &os)
Add newline and flush stream.
bool isAdministrator()
Is user administrator.
word name(const bool)
Return a word representation of a bool.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
double highResLastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
errorManip< error > abort(error &err)
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
void fdClose(const int)
Close file descriptor.
string userName()
Return the user's login name.
pid_t pgid()
Return the group PID of this process.
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
bool chMod(const fileName &, const mode_t)
Set the file mode.
bool rmDir(const fileName &)
Remove a directory and its contents.
defineTypeNameAndDebug(combustionModel, 0)
pid_t ppid()
Return the parent PID of this process.
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
bool ping(const string &, const label port, const label timeOut)
Check if machine is up by pinging given port.
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.
pid_t pid()
Return the PID of this process.
fileType
Enumeration of file types.
prefixOSstream Pout(cout, "Pout")
void * dlSym(void *handle, const std::string &symbol)
Lookup a symbol in a dlopened library using handle to library.
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy, recursively if necessary, the source to the destination.
fileNameList dlLoaded()
Return all loaded libraries.
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
fileName home()
Return home directory path name for the current user.
unsigned int sleep(const unsigned int)
Sleep for the specified number of seconds.
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.
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
bool chDir(const fileName &dir)
Change the current directory to the one given and return true,.
#define timedOut(x)
Check it a timeout has occurred.