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 (pathName.empty())
309 if (::mkdir(pathName.c_str(),
mode) == 0)
321 <<
"The filesystem containing " << pathName
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 " << pathName
350 <<
" did not allow search (execute) permission." 359 <<
"" << pathName <<
" is too long." 370 return mkDir(pathName, mode);
375 <<
"Couldn't create directory " << pathName
385 <<
"A component used as a directory in " << pathName
386 <<
" is not, in fact, a directory." 395 <<
"Insufficient kernel memory was available to make " 396 "directory " << pathName <<
'.' 406 <<
" refers to a file on a read-only filesystem." 415 <<
"Too many symbolic links were encountered in resolving " 425 <<
"The device containing " << pathName
426 <<
" has no room for the new directory or " 427 <<
"the user's disk quota is exhausted." 436 <<
"Couldn't create directory " << pathName
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());
803 while (srcStream.get(ch))
809 if (!srcStream.eof() || !destStream)
819 destFile = destFile/src.
name();
844 char* realSrcPath = realpath(src.c_str(),
nullptr);
845 char* realDestPath = realpath(destFile.c_str(),
nullptr);
846 const bool samePath = strcmp(realSrcPath, realDestPath) == 0;
848 if (POSIX::debug && samePath)
851 <<
"Attempt to copy " << realSrcPath <<
" to itself" <<
endl;
877 <<
"Copying : " << src/contents[i]
878 <<
" to " << destFile/contents[i] <<
endl;
882 cp(src/contents[i], destFile/contents[i], followLink);
899 <<
"Copying : " << src/subdirs[i]
900 <<
" to " << destFile <<
endl;
904 cp(src/subdirs[i], destFile, followLink);
917 <<
" : Create softlink from : " << src <<
" to " << dst <<
endl;
927 <<
"destination " << dst <<
" already exists. Not linking." 935 <<
"source " << src <<
" does not exist." <<
endl;
939 if (::symlink(src.c_str(), dst.c_str()) == 0)
946 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
971 return ::rename(src.c_str(), dstName.c_str()) == 0;
975 return ::rename(src.c_str(), dst.c_str()) == 0;
985 <<
" : moving : " << src <<
" to extension " << ext <<
endl;
992 if (
exists(src,
false,
false))
994 const int maxIndex = 99;
997 for (
int n = 0;
n <= maxIndex;
n++)
1002 sprintf(index,
"%02d",
n);
1008 if (!
exists(dstName,
false,
false) ||
n == maxIndex)
1010 return ::rename(src.c_str(), dstName.c_str()) == 0;
1033 if (
remove(file.c_str()) == 0)
1041 if (::
remove(
string(fileVar).c_str()) == 0)
1064 struct dirent *list;
1067 if ((source = ::opendir(directory.c_str())) ==
nullptr)
1070 <<
"cannot open directory " << directory <<
endl;
1077 while ((list = ::readdir(source)) !=
nullptr)
1081 if (fName !=
"." && fName !=
"..")
1090 <<
"failed to remove directory " << fName
1091 <<
" while removing directory " << directory
1104 <<
"failed to remove file " << fName
1105 <<
" while removing directory " << directory
1120 <<
"failed to remove directory " << directory <<
endl;
1145 <<
"close error on " << fd <<
endl 1153 const string& destName,
1154 const label destPort,
1158 struct hostent *hostPtr;
1159 volatile int sockfd;
1160 struct sockaddr_in destAddr;
1163 if ((hostPtr = ::gethostbyname(destName.c_str())) ==
nullptr)
1166 <<
"gethostbyname error " << h_errno <<
" for host " << destName
1171 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
1174 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1183 memset(reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
1184 destAddr.sin_family = AF_INET;
1185 destAddr.sin_port = htons(ushort(destPort));
1186 destAddr.sin_addr.s_addr = addr;
1189 timer myTimer(timeOut);
1203 reinterpret_cast<struct sockaddr*>(&destAddr),
1204 sizeof(
struct sockaddr)
1210 int connectErr = errno;
1214 if (connectErr == ECONNREFUSED)
1230 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1244 std::cout<<
"dlOpen(const fileName&)" 1247 void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1249 if (!handle && check)
1252 <<
"dlopen error : " << ::dlerror()
1259 <<
"dlOpen(const fileName&)" 1260 <<
" : dlopen of " << lib
1274 <<
" : dlclose of handle " << handle <<
std::endl;
1276 return ::dlclose(handle) == 0;
1285 <<
"dlSym(void*, const std::string&)" 1286 <<
" : dlsym of " << symbol <<
std::endl;
1293 void* fun = ::dlsym(handle, symbol.c_str());
1296 char *
error = ::dlerror();
1301 <<
"Cannot lookup symbol " << symbol <<
" : " << error
1311 if (handle && !symbol.empty())
1316 <<
"dlSymFound(void*, const std::string&)" 1317 <<
" : dlsym of " << symbol <<
std::endl;
1324 (void) ::dlsym(handle, symbol.c_str());
1327 return !::dlerror();
1338 struct dl_phdr_info *info,
1345 ptr->
append(info->dlpi_name);
1358 <<
" : 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.
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 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 .)
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.
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.
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())
#define InfoInFunction
Report an information message using Foam::Info.