49 #include <sys/types.h>
51 #include <sys/socket.h>
54 #include <netinet/in.h>
79 return getenv(envName.c_str()) != NULL;
85 char*
env = getenv(envName.c_str());
107 return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
114 gethostname(buffer, 256);
122 struct passwd* pw = getpwuid(getuid());
138 char*
env = getenv(
"HOME");
146 struct passwd* pw = getpwuid(getuid());
154 return fileName::null;
166 pw = getpwnam(userName.c_str());
170 char*
env = getenv(
"HOME");
177 pw = getpwuid(getuid());
186 return fileName::null;
194 if (getcwd(buf, 255))
201 <<
"Couldn't get the current working directory"
204 return fileName::null;
211 return chdir(dir.c_str()) != 0;
226 if(
env(
"FREEFOAM_CONFIG_DIR"))
228 searchDir =
getEnv(
"FREEFOAM_CONFIG_DIR");
230 if (
isDir(searchDir))
242 searchDir =
home()/
".FreeFOAM";
243 if (
isDir(searchDir))
253 fullName = searchDir/
name;
263 if (
isDir(searchDir))
277 cerr<<
"--> FOAM FATAL ERROR in Foam::findEtcFile() :"
278 " could not find mandatory file\n '"
292 if (pathName.empty())
298 if (::mkdir(pathName.c_str(),
mode) == 0)
310 <<
"The filesystem containing " << pathName
311 <<
" does not support the creation of directories."
327 <<
" points outside your accessible address space."
336 <<
"The parent directory does not allow write "
337 "permission to the process,"<<
nl
338 <<
"or one of the directories in " << pathName
339 <<
" did not allow search (execute) permission."
348 <<
"" << pathName <<
" is too long."
359 return mkDir(pathName, mode);
364 <<
"Couldn't create directory " << pathName
374 <<
"A component used as a directory in " << pathName
375 <<
" is not, in fact, a directory."
384 <<
"Insufficient kernel memory was available to make "
385 "directory " << pathName <<
'.'
395 <<
" refers to a file on a read-only filesystem."
404 <<
"Too many symbolic links were encountered in resolving "
414 <<
"The device containing " << pathName
415 <<
" has no room for the new directory or "
416 <<
"the user's disk quota is exhausted."
425 <<
"Couldn't create directory " << pathName
438 return ::chmod(name.c_str(), m) == 0;
448 return fileStatus.
status().st_mode;
460 mode_t m =
mode(name);
464 return fileName::FILE;
468 return fileName::DIRECTORY;
472 return fileName::UNDEFINED;
487 return S_ISDIR(
mode(name));
494 return S_ISREG(
mode(name)) || (checkGzip && S_ISREG(
mode(name +
".gz")));
504 return fileStatus.
status().st_size;
519 return fileStatus.
status().st_mtime;
538 static const int maxNnames = 100;
542 Info<<
"readDir(const fileName&, const fileType, const bool filtergz)"
543 <<
" : reading directory " << directory <<
endl;
557 if ((source = opendir(directory.c_str())) == NULL)
563 Info<<
"readDir(const fileName&, const fileType, "
564 "const bool filtergz) : cannot open directory "
565 << directory <<
endl;
571 while ((list = readdir(source)) != NULL)
576 if (fName.size() && fName[0] !=
'.')
582 (type == fileName::DIRECTORY)
585 type == fileName::FILE
586 && fName[fName.size()-1] !=
'~'
594 if ((directory/fName).type() ==
type)
596 if (nEntries >= dirEntries.
size())
601 if (filtergz && fExt ==
"gz")
603 dirEntries[nEntries++] = fName.
lessExt();
607 dirEntries[nEntries++] = fName;
636 if (src.
type() == fileName::FILE)
639 if (destFile.
type() == fileName::DIRECTORY)
641 destFile = destFile/src.
name();
651 std::ifstream srcStream(src.c_str());
657 std::ofstream destStream(destFile.c_str());
665 while (srcStream.get(ch))
671 if (!srcStream.eof() || !destStream)
676 else if (src.
type() == fileName::DIRECTORY)
679 if (destFile.
type() == fileName::DIRECTORY)
696 Info<<
"Copying : " << src/contents[i]
697 <<
" to " << destFile/contents[i] <<
endl;
701 cp(src/contents[i], destFile/contents[i]);
710 Info<<
"Copying : " << src/subdirs[i]
711 <<
" to " << destFile <<
endl;
715 cp(src/subdirs[i], destFile);
728 Info<<
"Create softlink from : " << src <<
" to " << dst
734 WarningIn(
"ln(const fileName&, const fileName&)")
735 <<
"destination " << dst <<
" already exists. Not linking."
742 WarningIn(
"ln(const fileName&, const fileName&)")
743 <<
"source " << src <<
" does not exist." <<
endl;
747 if (symlink(src.c_str(), dst.c_str()) == 0)
753 WarningIn(
"ln(const fileName&, const fileName&)")
754 <<
"symlink from " << src <<
" to " << dst <<
" failed." <<
endl;
765 Info<<
"Move : " << src <<
" to " << dst <<
endl;
770 dst.
type() == fileName::DIRECTORY
771 && src.
type() != fileName::DIRECTORY
776 return rename(src.c_str(), dstName.c_str()) == 0;
780 return rename(src.c_str(), dst.c_str()) == 0;
791 Info<<
"mvBak : " << src <<
" to extension " << ext <<
endl;
796 const int maxIndex = 99;
799 for (
int n = 0; n <= maxIndex; n++)
804 sprintf(index,
"%02d", n);
810 if (!
exists(dstName,
false) || n == maxIndex)
812 return rename(src.c_str(), dstName.c_str()) == 0;
829 Info<<
"Removing : " << file <<
endl;
833 if (
remove(file.c_str()) == 0)
839 return remove(
string(file +
".gz").c_str()) == 0;
849 Info<<
"rmDir(const fileName&) : "
850 <<
"removing directory " << directory <<
endl;
858 if ((source = opendir(directory.c_str())) == NULL)
861 <<
"cannot open directory " << directory <<
endl;
868 while ((list = readdir(source)) != NULL)
872 if (fName !=
"." && fName !=
"..")
876 if (path.
type() == fileName::DIRECTORY)
881 <<
"failed to remove directory " << fName
882 <<
" while removing directory " << directory
895 <<
"failed to remove file " << fName
896 <<
" while removing directory " << directory
911 <<
"failed to remove directory " << directory <<
endl;
937 "fdClose(const int fd)"
938 ) <<
"close error on " << fd <<
endl
946 const word& destName,
947 const label destPort,
953 struct hostent *hostPtr;
955 struct sockaddr_in destAddr;
958 if ((hostPtr = gethostbyname(destName.c_str())) == NULL)
962 "Foam::ping(const word&, const label)"
963 ) <<
"gethostbyname error " << h_errno <<
" for host " << destName
968 serverAddress = *(hostPtr->h_addr_list);
969 ptr =
reinterpret_cast<struct in_addr*
>(serverAddress);
973 sockfd = socket(AF_INET, SOCK_STREAM, 0);
978 "Foam::ping(const word&, const label)"
984 memset (reinterpret_cast<char *>(&destAddr),
'\0',
sizeof(destAddr));
985 destAddr.sin_family = AF_INET;
986 destAddr.sin_port = htons(ushort(destPort));
987 destAddr.sin_addr.s_addr = addr;
990 timer myTimer(timeOut);
1004 reinterpret_cast<struct sockaddr*>(&destAddr),
1005 sizeof(
struct sockaddr)
1011 int connectErr = errno;
1015 if (connectErr == ECONNREFUSED)
1032 return ping(hostname, 222, timeOut) ||
ping(hostname, 22, timeOut);