53 #include <sys/types.h> 55 #include <sys/socket.h> 60 #include <netinet/in.h> 64 #if INT_MAX != 2147483647 65 #error "INT_MAX != 2147483647" 66 #error "The random number generator may not work!" 100 return ::getenv(envName.c_str()) !=
nullptr;
106 char*
env = ::getenv(envName.c_str());
124 const std::string& value,
128 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
135 ::gethostname(buf,
sizeof(buf));
140 struct hostent *hp = ::gethostbyname(buf);
154 ::gethostname(buf,
sizeof(buf));
157 struct hostent *hp = ::gethostbyname(buf);
160 char *
p = ::strchr(hp->h_name,
'.');
174 struct passwd* pw = ::getpwuid(::getuid());
189 return (::geteuid() == 0);
195 char*
env = ::getenv(
"HOME");
203 struct passwd* pw = ::getpwuid(getuid());
223 pw = ::getpwnam(userName.c_str());
227 char*
env = ::getenv(
"HOME");
234 pw = ::getpwuid(::getuid());
254 while(pathLengthLimit == path.
size())
256 if (::getcwd(path.
data(), path.
size()))
260 else if(errno == ERANGE)
270 <<
"Attempt to increase path length beyond limit of " 284 <<
"Couldn't get the current working directory" 293 return ::chdir(dir.c_str()) == 0;
310 if (pathName.empty())
316 if (::mkdir(pathName.c_str(),
mode) == 0)
328 <<
"The filesystem containing " << pathName
329 <<
" does not support the creation of directories." 345 <<
" points outside your accessible address space." 354 <<
"The parent directory does not allow write " 355 "permission to the process,"<<
nl 356 <<
"or one of the directories in " << pathName
357 <<
" did not allow search (execute) permission." 366 <<
"" << pathName <<
" is too long." 377 return mkDir(pathName, mode);
382 <<
"Couldn't create directory " << pathName
392 <<
"A component used as a directory in " << pathName
393 <<
" is not, in fact, a directory." 402 <<
"Insufficient kernel memory was available to make " 403 "directory " << pathName <<
'.' 413 <<
" refers to a file on a read-only filesystem." 422 <<
"Too many symbolic links were encountered in resolving " 432 <<
"The device containing " << pathName
433 <<
" has no room for the new directory or " 434 <<
"the user's disk quota is exhausted." 443 <<
"Couldn't create directory " << pathName
463 return ::chmod(name.c_str(), m) == 0;
473 fileStat fileStatus(name, followLink);
476 return fileStatus.
status().st_mode;
495 mode_t m =
mode(name, followLink);
519 const bool checkGzip,
520 const bool followLink
532 return mode(name, followLink) ||
isFile(name, checkGzip, followLink);
546 return S_ISDIR(
mode(name, followLink));
553 const bool checkGzip,
554 const bool followLink
567 S_ISREG(
mode(name, followLink))
568 || (checkGzip && S_ISREG(
mode(name +
".gz", followLink)));
582 fileStat fileStatus(name, followLink);
585 return fileStatus.
status().st_size;
604 fileStat fileStatus(name, followLink);
607 return fileStatus.
status().st_mtime;
626 fileStat fileStatus(name, followLink);
630 fileStatus.
status().st_mtime
631 + 1
e-9*fileStatus.
status().st_atim.tv_nsec;
645 const bool followLink
650 static const int maxNnames = 100;
673 if ((source = ::opendir(directory.c_str())) ==
nullptr)
680 <<
"cannot open directory " << directory <<
endl;
686 while ((list = ::readdir(source)) !=
nullptr)
691 if (fName.size() && fName[0] !=
'.')
701 && fName[fName.size()-1] !=
'~' 709 if ((directory/fName).type(followLink) ==
type)
711 if (nEntries >= dirEntries.
size())
716 if (filtergz && fExt ==
"gz")
718 dirEntries[nEntries++] = fName.
lessExt();
722 dirEntries[nEntries++] = fName;
765 destFile = destFile/src.
name();
775 std::ifstream srcStream(src.c_str());
781 std::ofstream destStream(destFile.c_str());
789 while (srcStream.get(ch))
795 if (!srcStream.eof() || !destStream)
805 destFile = destFile/src.
name();
837 <<
"Copying : " << src/contents[i]
838 <<
" to " << destFile/contents[i] <<
endl;
842 cp(src/contents[i], destFile/contents[i], followLink);
859 <<
"Copying : " << src/subdirs[i]
860 <<
" to " << destFile <<
endl;
864 cp(src/subdirs[i], destFile, followLink);
878 <<
" : Create softlink from : " << src <<
" to " << dst <<
endl;
888 <<
"destination " << dst <<
" already exists. Not linking." 896 <<
"source " << src <<
" does not exist." <<
endl;
900 if (::symlink(src.c_str(), dst.c_str()) == 0)
907 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
933 return ::rename(src.c_str(), dstName.c_str()) == 0;
937 return ::rename(src.c_str(), dst.c_str()) == 0;
948 <<
" : moving : " << src <<
" to extension " << ext <<
endl;
957 const int maxIndex = 99;
960 for (
int n = 0;
n <= maxIndex;
n++)
965 sprintf(index,
"%02d",
n);
971 if (!
exists(dstName,
false) ||
n == maxIndex)
973 return ::rename(src.c_str(), dstName.c_str()) == 0;
997 if (
remove(file.c_str()) == 0)
1003 return ::remove(
string(file +
".gz").c_str()) == 0;
1022 struct dirent *list;
1025 if ((source = ::opendir(directory.c_str())) ==
nullptr)
1028 <<
"cannot open directory " << directory <<
endl;
1035 while ((list = ::readdir(source)) !=
nullptr)
1039 if (fName !=
"." && fName !=
"..")
1048 <<
"failed to remove directory " << fName
1049 <<
" while removing directory " << directory
1062 <<
"failed to remove file " << fName
1063 <<
" while removing directory " << directory
1078 <<
"failed to remove directory " << directory <<
endl;
1103 <<
"close error on " << fd <<
endl 1111 const string& destName,
1112 const label destPort,
1116 struct hostent *hostPtr;
1117 volatile int sockfd;
1118 struct sockaddr_in destAddr;
1121 if ((hostPtr = ::gethostbyname(destName.c_str())) ==
nullptr)
1124 <<
"gethostbyname error " << h_errno <<
" for host " << destName
1129 addr = (
reinterpret_cast<struct in_addr*
>(*(hostPtr->h_addr_list)))->s_addr;
1132 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1141 memset(reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
1142 destAddr.sin_family = AF_INET;
1143 destAddr.sin_port = htons(ushort(destPort));
1144 destAddr.sin_addr.s_addr = addr;
1147 timer myTimer(timeOut);
1161 reinterpret_cast<struct sockaddr*>(&destAddr),
1162 sizeof(
struct sockaddr)
1168 int connectErr = errno;
1172 if (connectErr == ECONNREFUSED)
1189 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);
1203 std::cout<<
"dlOpen(const fileName&)" 1206 void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
1208 if (!handle && check)
1211 <<
"dlopen error : " << ::dlerror()
1218 <<
"dlOpen(const fileName&)" 1219 <<
" : dlopen of " << lib
1233 <<
" : dlclose of handle " << handle <<
std::endl;
1235 return ::dlclose(handle) == 0;
1244 <<
"dlSym(void*, const std::string&)" 1245 <<
" : dlsym of " << symbol <<
std::endl;
1251 void* fun = ::dlsym(handle, symbol.c_str());
1254 char *
error = ::dlerror();
1259 <<
"Cannot lookup symbol " << symbol <<
" : " << error
1269 if (handle && !symbol.empty())
1274 <<
"dlSymFound(void*, const std::string&)" 1275 <<
" : dlsym of " << symbol <<
std::endl;
1282 (void) ::dlsym(handle, symbol.c_str());
1285 return !::dlerror();
1296 struct dl_phdr_info *info,
1303 ptr->
append(info->dlpi_name);
1316 <<
" : determined loaded libraries :" << libs.
size() <<
std::endl;
1325 srandom((
unsigned int)seed);
1345 return (scalar)
random()/INT_MAX;
1360 if (!threads_[i].valid())
1364 Pout<<
"allocateThread : reusing index:" << i <<
endl;
1367 threads_[i].reset(
new pthread_t());
1375 Pout<<
"allocateThread : new index:" << index <<
endl;
1386 void *(*start_routine) (
void *),
1392 Pout<<
"createThread : index:" << index <<
endl;
1394 if (pthread_create(&threads_[index](),
nullptr, start_routine, arg))
1406 Pout<<
"freeThread : join:" << index <<
endl;
1408 if (pthread_join(threads_[index](),
nullptr))
1420 Pout<<
"freeThread : index:" << index <<
endl;
1422 threads_[index].
clear();
1430 if (!mutexes_[i].valid())
1434 Pout<<
"allocateMutex : reusing index:" << i <<
endl;
1437 mutexes_[i].reset(
new pthread_mutex_t());
1446 Pout<<
"allocateMutex : new index:" << index <<
endl;
1457 Pout<<
"lockMutex : index:" << index <<
endl;
1459 if (pthread_mutex_lock(&mutexes_[index]()))
1471 Pout<<
"unlockMutex : index:" << index <<
endl;
1473 if (pthread_mutex_unlock(&mutexes_[index]()))
1485 Pout<<
"freeMutex : index:" << index <<
endl;
1487 mutexes_[index].
clear();
void freeMutex(const label)
Free a mutex variable.
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)
const double e
Elementary charge.
#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.
scalar osRandomDouble()
Return random double precision (uniform distribution between 0 and 1)
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.
void unlockMutex(const label)
Unlock a mutex variable.
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.
void freeThread(const label)
Delete a thread.
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.
label osRandomInteger()
Return random integer (uniform distribution between 0 and 2^31)
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.
void joinThread(const label)
Wait for thread.
void createThread(const label, void *(*start_routine)(void *), void *arg)
Start a thread.
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 dirctory 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.
void lockMutex(const label)
Lock a mutex variable.
#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.
fileName::Type type(const fileName &, const bool followLink=true)
Return the file type: DIRECTORY or FILE.
bool isAdministrator()
Is user administrator.
label allocateThread()
Allocate a thread.
void setSize(const label)
Reset size of List.
T * data()
Return a pointer to the first data element,.
static Foam::DynamicList< Foam::autoPtr< pthread_mutex_t > > mutexes_
#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 /)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
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.
label allocateMutex()
Allocate a mutex variable.
void clear()
Clear the addressed list, i.e. set the size to zero.
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.
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.
static Foam::DynamicList< Foam::autoPtr< pthread_t > > threads_
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.