53 #include <sys/types.h> 55 #include <sys/socket.h> 60 #include <netinet/in.h> 92 return ::getenv(envName.c_str()) !=
nullptr;
98 char*
env = ::getenv(envName.c_str());
116 const std::string& value,
120 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
127 ::gethostname(buf,
sizeof(buf));
132 struct hostent *hp = ::gethostbyname(buf);
146 ::gethostname(buf,
sizeof(buf));
149 struct hostent *hp = ::gethostbyname(buf);
152 char *
p = ::strchr(hp->h_name,
'.');
166 struct passwd* pw = ::getpwuid(::getuid());
181 return (::geteuid() == 0);
187 char*
env = ::getenv(
"HOME");
195 struct passwd* pw = ::getpwuid(getuid());
215 pw = ::getpwnam(userName.c_str());
219 char*
env = ::getenv(
"HOME");
226 pw = ::getpwuid(::getuid());
246 while(pathLengthLimit == path.
size())
248 if (::getcwd(path.
data(), path.
size()))
252 else if(errno == ERANGE)
262 <<
"Attempt to increase path length beyond limit of " 276 <<
"Couldn't get the current working directory" 285 return ::chdir(dir.c_str()) == 0;
302 if (pathName.empty())
308 if (::mkdir(pathName.c_str(),
mode) == 0)
320 <<
"The filesystem containing " << pathName
321 <<
" does not support the creation of directories." 337 <<
" points outside your accessible address space." 346 <<
"The parent directory does not allow write " 347 "permission to the process,"<<
nl 348 <<
"or one of the directories in " << pathName
349 <<
" did not allow search (execute) permission." 358 <<
"" << pathName <<
" is too long." 369 return mkDir(pathName, mode);
374 <<
"Couldn't create directory " << pathName
384 <<
"A component used as a directory in " << pathName
385 <<
" is not, in fact, a directory." 394 <<
"Insufficient kernel memory was available to make " 395 "directory " << pathName <<
'.' 405 <<
" refers to a file on a read-only filesystem." 414 <<
"Too many symbolic links were encountered in resolving " 424 <<
"The device containing " << pathName
425 <<
" has no room for the new directory or " 426 <<
"the user's disk quota is exhausted." 435 <<
"Couldn't create directory " << pathName
455 return ::chmod(name.c_str(), m) == 0;
469 fileStat fileStatus(name, followLink);
472 return fileStatus.
status().st_mode;
487 mode_t m =
mode(name, followLink);
511 const bool checkGzip,
512 const bool followLink
524 return mode(name, followLink) ||
isFile(name, checkGzip, followLink);
538 return S_ISDIR(
mode(name, followLink));
545 const bool checkGzip,
546 const bool followLink
560 S_ISREG(
mode(name, followLink))
561 || (checkGzip && S_ISREG(
mode(name +
".gz", followLink)))
562 || (checkGzip && S_ISREG(
mode(name +
".orig", followLink)));
576 fileStat fileStatus(name, followLink);
579 return fileStatus.
status().st_size;
598 fileStat fileStatus(name, followLink);
601 return fileStatus.
status().st_mtime;
620 fileStat fileStatus(name, followLink);
624 fileStatus.
status().st_mtime
625 + 1
e-9*fileStatus.
status().st_atim.tv_nsec;
639 const bool followLink
644 static const int maxNnames = 100;
667 if ((source = ::opendir(directory.c_str())) ==
nullptr)
674 <<
"cannot open directory " << directory <<
endl;
680 while ((list = ::readdir(source)) !=
nullptr)
685 if (fName.size() && fName[0] !=
'.')
695 && fName[fName.size()-1] !=
'~' 703 if ((directory/fName).type(followLink) ==
type)
705 if (nEntries >= dirEntries.
size())
710 if (filtergz && fExt ==
"gz")
712 dirEntries[nEntries++] = fName.
lessExt();
714 else if (filtergz && fExt ==
"orig")
716 dirEntries[nEntries++] = fName.
lessExt();
720 dirEntries[nEntries++] = fName;
763 destFile = destFile/src.
name();
773 std::ifstream srcStream(src.c_str());
779 std::ofstream destStream(destFile.c_str());
787 while (srcStream.get(ch))
793 if (!srcStream.eof() || !destStream)
803 destFile = destFile/src.
name();
828 char* realSrcPath = realpath(src.c_str(),
nullptr);
829 char* realDestPath = realpath(destFile.c_str(),
nullptr);
830 const bool samePath = strcmp(realSrcPath, realDestPath) == 0;
832 if (POSIX::debug && samePath)
835 <<
"Attempt to copy " << realSrcPath <<
" to itself" <<
endl;
861 <<
"Copying : " << src/contents[i]
862 <<
" to " << destFile/contents[i] <<
endl;
866 cp(src/contents[i], destFile/contents[i], followLink);
883 <<
"Copying : " << src/subdirs[i]
884 <<
" to " << destFile <<
endl;
888 cp(src/subdirs[i], destFile, followLink);
902 <<
" : Create softlink from : " << src <<
" to " << dst <<
endl;
912 <<
"destination " << dst <<
" already exists. Not linking." 920 <<
"source " << src <<
" does not exist." <<
endl;
924 if (::symlink(src.c_str(), dst.c_str()) == 0)
931 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
957 return ::rename(src.c_str(), dstName.c_str()) == 0;
961 return ::rename(src.c_str(), dst.c_str()) == 0;
972 <<
" : moving : " << src <<
" to extension " << ext <<
endl;
981 const int maxIndex = 99;
984 for (
int n = 0;
n <= maxIndex;
n++)
989 sprintf(index,
"%02d",
n);
995 if (!
exists(dstName,
false) ||
n == maxIndex)
997 return ::rename(src.c_str(), dstName.c_str()) == 0;
1021 if (
remove(file.c_str()) == 0)
1027 return ::remove(
string(file +
".gz").c_str()) == 0;
1046 struct dirent *list;
1049 if ((source = ::opendir(directory.c_str())) ==
nullptr)
1052 <<
"cannot open directory " << directory <<
endl;
1059 while ((list = ::readdir(source)) !=
nullptr)
1063 if (fName !=
"." && fName !=
"..")
1072 <<
"failed to remove directory " << fName
1073 <<
" while removing directory " << directory
1086 <<
"failed to remove file " << fName
1087 <<
" while removing directory " << directory
1102 <<
"failed to remove directory " << directory <<
endl;
1127 <<
"close error on " << fd <<
endl 1135 const string& destName,
1136 const label destPort,
1140 struct hostent *hostPtr;
1141 volatile int sockfd;
1142 struct sockaddr_in destAddr;
1145 if ((hostPtr = ::gethostbyname(destName.c_str())) ==
nullptr)
1148 <<
"gethostbyname error " << h_errno <<
" for host " << destName
1153 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
1156 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1165 memset(reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
1166 destAddr.sin_family = AF_INET;
1167 destAddr.sin_port = htons(ushort(destPort));
1168 destAddr.sin_addr.s_addr = addr;
1171 timer myTimer(timeOut);
1185 reinterpret_cast<struct sockaddr*>(&destAddr),
1186 sizeof(
struct sockaddr)
1192 int connectErr = errno;
1196 if (connectErr == ECONNREFUSED)
1213 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1227 std::cout<<
"dlOpen(const fileName&)" 1230 void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1232 if (!handle && check)
1235 <<
"dlopen error : " << ::dlerror()
1242 <<
"dlOpen(const fileName&)" 1243 <<
" : dlopen of " << lib
1257 <<
" : dlclose of handle " << handle <<
std::endl;
1259 return ::dlclose(handle) == 0;
1268 <<
"dlSym(void*, const std::string&)" 1269 <<
" : dlsym of " << symbol <<
std::endl;
1275 void* fun = ::dlsym(handle, symbol.c_str());
1278 char *
error = ::dlerror();
1283 <<
"Cannot lookup symbol " << symbol <<
" : " << error
1293 if (handle && !symbol.empty())
1298 <<
"dlSymFound(void*, const std::string&)" 1299 <<
" : dlsym of " << symbol <<
std::endl;
1306 (void) ::dlsym(handle, symbol.c_str());
1309 return !::dlerror();
1320 struct dl_phdr_info *info,
1327 ptr->
append(info->dlpi_name);
1340 <<
" : determined loaded libraries :" << libs.
size() <<
std::endl;
string getEnv(const word &)
Return environment variable of given name.
time_t lastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
static void printStack(Ostream &)
Helper function to print a stack.
#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.
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)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
const label pathLengthMax
off_t fileSize(const fileName &, const bool followLink=true)
Return size of file.
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 mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
word ext() const
Return file name extension (part after last .)
Type type(const bool followLink=true) const
Return the file type: FILE, DIRECTORY, UNDEFINED or.
wordList components(const char delimiter='/') const
Return path components as wordList.
const struct stat & status() const
Raw status.
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.
mode_t mode(const fileName &, const bool followLink=true)
Return the file mode.
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.
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.
bool isFile(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
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.
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
bool isAdministrator()
Is user administrator.
void setSize(const label)
Reset size of List.
T * data()
Return a pointer to the first data element,.
#define WarningInFunction
Report a warning using Foam::Warning.
prefixOSstream Pout(cout, "Pout")
bool chMod(const fileName &, const mode_t)
Set the file mode.
fileName lessExt() const
Return file name without extension (part before last .)
fileName cwd()
Return current working directory path name.
fileNameList readDir(const fileName &, const fileName::Type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a string list.
void fdClose(const int)
Close file descriptor.
fileName path() const
Return directory path name (part before last /)
const doubleScalar e
Elementary charge.
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 exists(const fileName &, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Type
Enumerations to handle file types and modes.
bool isValid() const
Did constructor fail.
int system(const std::string &command)
Execute the specified command.
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
#define InfoInFunction
Report an information message using Foam::Info.