// Create a softlink. dst should not exist. Returns true if successful. bool Foam::ln(const fileName& src, const fileName& dst) { if (POSIX::debug) { Info<< "Create softlink from : " << src << " to " << dst << endl; } if (exists(dst)) { WarningIn("ln(const fileName&, const fileName&)") << "destination " << dst << " already exists. Not linking." << endl; return false; } if (!exists(src)) { WarningIn("ln(const fileName&, const fileName&)") << "source " << src << " does not exist." << endl; return false; } if (symlink(src.c_str(), dst.c_str()) == 0) { return true; } else { WarningIn("ln(const fileName&, const fileName&)") << "symlink from " << src << " to " << dst << " failed." << endl; return false; } }
void writePointSet ( const bool binary, const vtkMesh& vMesh, const pointSet& set, const fileName& fileName ) { std::ofstream ostr(fileName.c_str()); writeFuns::writeHeader ( ostr, binary, set.name() ); ostr<< "DATASET POLYDATA" << std::endl; //------------------------------------------------------------------ // // Write topology // //------------------------------------------------------------------ // Write points ostr<< "POINTS " << set.size() << " float" << std::endl; DynamicList<floatScalar> ptField(3*set.size()); writeFuns::insert ( UIndirectList<point>(vMesh.mesh().points(), set.toc())(), ptField ); writeFuns::write(ostr, binary, ptField); //----------------------------------------------------------------- // // Write data // //----------------------------------------------------------------- // Write faceID ostr << "POINT_DATA " << set.size() << std::endl << "FIELD attributes 1" << std::endl; // Cell ids first ostr<< "pointID 1 " << set.size() << " int" << std::endl; labelList pointIDs(set.toc()); writeFuns::write(ostr, binary, pointIDs); }
//- Rename to a corresponding backup file // If the backup file already exists, attempt with "01" .. "99" index bool Foam::mvBak(const fileName& src, const std::string& ext) { if (POSIX::debug) { Info<< "mvBak : " << src << " to extension " << ext << endl; } if (exists(src, false)) { const int maxIndex = 99; char index[3]; for (int n = 0; n <= maxIndex; n++) { fileName dstName(src + "." + ext); if (n) { sprintf(index, "%02d", n); dstName += index; } // avoid overwriting existing files, except for the last // possible index where we have no choice if (!exists(dstName, false) || n == maxIndex) { return rename(src.c_str(), dstName.c_str()) == 0; } } } // fall-through: nothing to do return false; }
Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) : ifPtr_(NULL), compression_(IOstream::UNCOMPRESSED) { if (pathname.empty()) { if (IFstream::debug) { Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : " "cannot open null file " << endl; } } ifPtr_ = new ifstream(pathname.c_str()); // If the file is compressed, decompress it before reading. if (!ifPtr_->good() && isFile(pathname + ".gz", false)) { if (IFstream::debug) { Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : " "decompressing " << pathname + ".gz" << endl; } delete ifPtr_; ifPtr_ = new igzstream((pathname + ".gz").c_str()); if (ifPtr_->good()) { compression_ = IOstream::COMPRESSED; } } }
// Does the directory exist bool isDir(const fileName& name) { const DWORD attrs = ::GetFileAttributes(name.c_str()); bool success = (attrs != INVALID_FILE_ATTRIBUTES) && (attrs & FILE_ATTRIBUTE_DIRECTORY); return success; }
// Does the name exist in the filing system? bool exists(const fileName& name, const bool checkGzip) { const DWORD attrs = ::GetFileAttributes(name.c_str()); const bool success = (attrs != INVALID_FILE_ATTRIBUTES) || (checkGzip && isGzFile(name)); return success; }
// Does the file exist bool isFile(const fileName& name, const bool checkGzip) { const DWORD attrs = ::GetFileAttributes(name.c_str()); const bool success = ((attrs != INVALID_FILE_ATTRIBUTES) && !(attrs & FILE_ATTRIBUTE_DIRECTORY)) || (checkGzip && isGzFile(name)); return success; }
// Rename srcFile destFile bool mv(const fileName& srcFile, const fileName& destFile) { if (MSwindows::debug) { Info<< "Move : " << srcFile << " to " << destFile << endl; } const fileName destName = ((destFile.type() == fileName::DIRECTORY) && (srcFile.type() != fileName::DIRECTORY)) ? destFile/srcFile.name() : destFile; const bool success = (0 == std::rename(srcFile.c_str(), destName.c_str())); return success; }
// Rename srcFile dstFile bool Foam::mv(const fileName& src, const fileName& dst) { if (POSIX::debug) { Info<< "Move : " << src << " to " << dst << endl; } if ( dst.type() == fileName::DIRECTORY && src.type() != fileName::DIRECTORY ) { const fileName dstName(dst/src.name()); return rename(src.c_str(), dstName.c_str()) == 0; } else { return rename(src.c_str(), dst.c_str()) == 0; } }
// Return the file type: FILE or DIRECTORY fileName::Type type(const fileName& name) { fileName::Type fileType = fileName::UNDEFINED; const DWORD attrs = ::GetFileAttributes(name.c_str()); if (attrs != INVALID_FILE_ATTRIBUTES) { fileType = (attrs & FILE_ATTRIBUTE_DIRECTORY) ? fileName::DIRECTORY : fileName::FILE; } return fileType; }
bool mkDir(const fileName& pathName, const mode_t mode) { if (pathName.empty()) { return false; } bool success = ::CreateDirectory(pathName.c_str(), NULL); if (success) { chMod(pathName, mode); } else { const DWORD error = ::GetLastError(); switch (error) { case ERROR_ALREADY_EXISTS: { success = true; break; } case ERROR_PATH_NOT_FOUND: { // Part of the path does not exist so try to create it const fileName& parentName = pathName.path(); if (parentName.size() && mkDir(parentName, mode)) { success = mkDir(pathName, mode); } break; } } if (!success) { FatalErrorIn("mkDir(const fileName&, mode_t)") << "Couldn't create directory: " << pathName << " " << MSwindows::getLastError() << exit(FatalError); } } return success; }
Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory) { // Search user files: deprecated. HJ, 11/Dec/2013 // Search site files: fileName searchDir = getEnv("WM_PROJECT_INST_DIR"); if (isDir(searchDir)) { // Check for site file in $WM_PROJECT_INST_DIR/site/VERSION fileName fullName = searchDir/"site"/FOAMversion/name; if (isFile(fullName)) { return fullName; } // Check for version-independent site file in $WM_PROJECT_INST_DIR/site fullName = searchDir/"site"/name; if (isFile(fullName)) { return fullName; } } // Search installation files: searchDir = getEnv("WM_PROJECT_DIR"); if (isDir(searchDir)) { // Check for shipped FOAM file in $WM_PROJECT_DIR/etc fileName fullName = searchDir/"etc"/name; if (isFile(fullName)) { return fullName; } } // Not found // abort if the file is mandatory, otherwise return null if (mandatory) { cerr<< "--> FOAM FATAL ERROR in Foam::findEtcFile() :" " could not find mandatory file\n '" << name.c_str() << "'\n\n" << std::endl; ::exit(1); } // Return null-constructed fileName rather than fileName::null // to avoid cyclic dependencies in the construction of globals return fileName(); }
// Construct from components Foam::patchWriter::patchWriter ( const vtkMesh& vMesh, const bool binary, const bool nearCellValue, const fileName& fName, const labelList& patchIDs ) : vMesh_(vMesh), binary_(binary), nearCellValue_(nearCellValue), fName_(fName), patchIDs_(patchIDs), os_(fName.c_str()) { const fvMesh& mesh = vMesh_.mesh(); const polyBoundaryMesh& patches = mesh.boundaryMesh(); // Write header if (patchIDs_.size() == 1) { writeFuns::writeHeader(os_, binary_, patches[patchIDs_[0]].name()); } else { writeFuns::writeHeader(os_, binary_, "patches"); } os_ << "DATASET UNSTRUCTURED_GRID" << std::endl; // Write topology nPoints_ = 0; nFaces_ = 0; label nFaceVerts = 0; forAll(patchIDs_, i) { const polyPatch& pp = patches[patchIDs_[i]]; nPoints_ += pp.nPoints(); nFaces_ += pp.size(); forAll(pp, faceI) { nFaceVerts += pp[faceI].size() + 1; } }
// Remove a file, returning true if successful otherwise false bool Foam::rm(const fileName& file) { if (POSIX::debug) { Info<< "Removing : " << file << endl; } // Try returning plain file name; if not there, try with .gz if (remove(file.c_str()) == 0) { return true; } else { return remove(string(file + ".gz").c_str()) == 0; } }
// Remove a file returning true if successful otherwise false bool rm(const fileName& file) { if (MSwindows::debug) { Info<< "Removing : " << file << endl; } bool success = (0 == std::remove(file.c_str())); // If deleting plain file name failed try with .gz if (!success) { const std::string fileGz = file + ".gz"; success = (0 == std::remove(fileGz.c_str())); } return success; }
// Construct from components Foam::lagrangianWriter::lagrangianWriter ( const vtkMesh& vMesh, const bool binary, const fileName& fName, const word& cloudName, const bool dummyCloud ) : vMesh_(vMesh), binary_(binary), fName_(fName), cloudName_(cloudName), os_(fName.c_str()) { const fvMesh& mesh = vMesh_.mesh(); // Write header writeFuns::writeHeader(os_, binary_, mesh.time().caseName()); os_ << "DATASET POLYDATA" << std::endl; if (dummyCloud) { nParcels_ = 0; os_ << "POINTS " << nParcels_ << " float" << std::endl; } else { Cloud<passiveParticle> parcels(mesh, cloudName_, false); nParcels_ = parcels.size(); os_ << "POINTS " << nParcels_ << " float" << std::endl; DynamicList<floatScalar> partField(3*parcels.size()); forAllConstIter(Cloud<passiveParticle>, parcels, elmnt) { writeFuns::insert(elmnt().position(), partField); } writeFuns::write(os_, binary_, partField); }
// Construct from components Foam::internalWriter::internalWriter ( const vtkMesh& vMesh, const bool binary, const fileName& fName ) : vMesh_(vMesh), binary_(binary), fName_(fName), os_(fName.c_str()) { const fvMesh& mesh = vMesh_.mesh(); const vtkTopo& topo = vMesh_.topo(); // Write header writeFuns::writeHeader(os_, binary_, mesh.time().caseName()); os_ << "DATASET UNSTRUCTURED_GRID" << std::endl; //------------------------------------------------------------------ // // Write topology // //------------------------------------------------------------------ const labelList& addPointCellLabels = topo.addPointCellLabels(); const label nTotPoints = mesh.nPoints() + addPointCellLabels.size(); os_ << "POINTS " << nTotPoints << " float" << std::endl; DynamicList<floatScalar> ptField(3*nTotPoints); writeFuns::insert(mesh.points(), ptField); const pointField& ctrs = mesh.cellCentres(); forAll(addPointCellLabels, api) { writeFuns::insert(ctrs[addPointCellLabels[api]], ptField); }
void Foam::writeFaceSet ( const bool binary, const vtkMesh& vMesh, const faceSet& set, const fileName& fileName ) { const faceList& faces = vMesh.mesh().faces(); std::ofstream ostr(fileName.c_str()); writeFuns::writeHeader ( ostr, binary, set.name() ); ostr<< "DATASET POLYDATA" << std::endl; //------------------------------------------------------------------ // // Write topology // //------------------------------------------------------------------ // Construct primitivePatch of faces in faceSet. faceList setFaces(set.size()); labelList setFaceLabels(set.size()); label setFacei = 0; forAllConstIter(faceSet, set, iter) { setFaceLabels[setFacei] = iter.key(); setFaces[setFacei] = faces[iter.key()]; setFacei++; }
Foam::fileStat::fileStat(const fileName& fName, const unsigned int maxTime) { // Work on volatile volatile bool locIsValid = false; timer myTimer(maxTime); if (!timedOut(myTimer)) { if (::stat(fName.c_str(), &status_) != 0) { locIsValid = false; } else { locIsValid = true; } } // Copy into (non-volatile, possible register based) member var isValid_ = locIsValid; }
Foam::OFstreamAllocator::OFstreamAllocator ( const fileName& pathname, ios_base::openmode mode, IOstream::compressionType compression ) : ofPtr_(NULL) { if (pathname.empty()) { if (OFstream::debug) { Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : " "cannot open null file " << endl; } } if (compression == IOstream::COMPRESSED) { // get identically named uncompressed version out of the way if (isFile(pathname, false)) { rm(pathname); } ofPtr_ = new ogzstream((pathname + ".gz").c_str()); } else { // get identically named compressed version out of the way if (isFile(pathname + ".gz", false)) { rm(pathname + ".gz"); } ofPtr_ = new ofstream(pathname.c_str()); } }
// Construct from components Foam::faMeshWriter::faMeshWriter ( const faMesh& aMesh, const bool binary, const fileName& fName ) : aMesh_(aMesh), binary_(binary), os_(fName.c_str()) { // Write header writeFuns::writeHeader(os_, binary_, faMesh::typeName); os_ << "DATASET UNSTRUCTURED_GRID" << std::endl; // Write topology label nFaceVerts = 0; const faceList& pp = aMesh.faces(); forAll(pp, faceI) { nFaceVerts += pp[faceI].size() + 1; }
bool Foam::chDir(const fileName& dir) { return chdir(dir.c_str()) != 0; }
void writePointSet ( const bool binary, const vtkMesh& vMesh, const pointSet& set, const fileName& fileName ) { std::ofstream pStream(fileName.c_str()); pStream << "# vtk DataFile Version 2.0" << std::endl << set.name() << std::endl; if (binary) { pStream << "BINARY" << std::endl; } else { pStream << "ASCII" << std::endl; } pStream << "DATASET POLYDATA" << std::endl; //------------------------------------------------------------------ // // Write topology // //------------------------------------------------------------------ // Write points pStream << "POINTS " << set.size() << " float" << std::endl; DynamicList<floatScalar> ptField(3*set.size()); writeFuns::insert ( UIndirectList<point>(vMesh.mesh().points(), set.toc())(), ptField ); writeFuns::write(pStream, binary, ptField); //----------------------------------------------------------------- // // Write data // //----------------------------------------------------------------- // Write faceID pStream << "POINT_DATA " << set.size() << std::endl << "FIELD attributes 1" << std::endl; // Cell ids first pStream << "pointID 1 " << set.size() << " int" << std::endl; labelList pointIDs(set.toc()); writeFuns::write(pStream, binary, pointIDs); }
// Remove a dirctory and its contents bool Foam::rmDir(const fileName& directory) { if (POSIX::debug) { Info<< "rmDir(const fileName&) : " << "removing directory " << directory << endl; } // Pointers to the directory entries DIR *source; struct dirent *list; // Attempt to open directory and set the structure pointer if ((source = opendir(directory.c_str())) == NULL) { WarningIn("rmDir(const fileName&)") << "cannot open directory " << directory << endl; return false; } else { // Read and parse all the entries in the directory while ((list = readdir(source)) != NULL) { fileName fName(list->d_name); if (fName != "." && fName != "..") { fileName path = directory/fName; if (path.type() == fileName::DIRECTORY) { if (!rmDir(path)) { WarningIn("rmDir(const fileName&)") << "failed to remove directory " << fName << " while removing directory " << directory << endl; closedir(source); return false; } } else { if (!rm(path)) { WarningIn("rmDir(const fileName&)") << "failed to remove file " << fName << " while removing directory " << directory << endl; closedir(source); return false; } } } } if (!rm(directory)) { WarningIn("rmDir(const fileName&)") << "failed to remove directory " << directory << endl; closedir(source); return false; } closedir(source); return true; } }
bool chDir(const fileName& dir) { const bool success = ::SetCurrentDirectory(dir.c_str()); return success; }
// Set the file mode bool chMod(const fileName& name, const mode_t m) { const int success = _chmod(name.c_str(), m); return success; }
// Copy, recursively if necessary, the source to the destination bool Foam::cp(const fileName& src, const fileName& dest) { // Make sure source exists. if (!exists(src)) { return false; } fileName destFile(dest); // Check type of source file. if (src.type() == fileName::FILE) { // If dest is a directory, create the destination file name. if (destFile.type() == fileName::DIRECTORY) { destFile = destFile/src.name(); } // Make sure the destination directory exists. if (!isDir(destFile.path()) && !mkDir(destFile.path())) { return false; } // Open and check streams. std::ifstream srcStream(src.c_str()); if (!srcStream) { return false; } std::ofstream destStream(destFile.c_str()); if (!destStream) { return false; } // Copy character data. char ch; while (srcStream.get(ch)) { destStream.put(ch); } // Final check. if (!srcStream.eof() || !destStream) { return false; } } else if (src.type() == fileName::DIRECTORY) { // If dest is a directory, create the destination file name. if (destFile.type() == fileName::DIRECTORY) { destFile = destFile/src.component(src.components().size() -1); } // Make sure the destination directory exists. if (!isDir(destFile) && !mkDir(destFile)) { return false; } // Copy files fileNameList contents = readDir(src, fileName::FILE, false); forAll(contents, i) { if (POSIX::debug) { Info<< "Copying : " << src/contents[i] << " to " << destFile/contents[i] << endl; } // File to file. cp(src/contents[i], destFile/contents[i]); } // Copy sub directories. fileNameList subdirs = readDir(src, fileName::DIRECTORY); forAll(subdirs, i) { if (POSIX::debug) { Info<< "Copying : " << src/subdirs[i] << " to " << destFile << endl; } // Dir to Dir. cp(src/subdirs[i], destFile); } } return true; }
// Read a directory and return the entries as a string list Foam::fileNameList Foam::readDir ( const fileName& directory, const fileName::Type type, const bool filtergz ) { // Initial filename list size // also used as increment if initial size found to be insufficient static const int maxNnames = 100; if (POSIX::debug) { Info<< "readDir(const fileName&, const fileType, const bool filtergz)" << " : reading directory " << directory << endl; } // Setup empty string list MAXTVALUES long fileNameList dirEntries(maxNnames); // Pointers to the directory entries DIR *source; struct dirent *list; // Temporary variables and counters label nEntries = 0; // Attempt to open directory and set the structure pointer if ((source = opendir(directory.c_str())) == NULL) { dirEntries.setSize(0); if (POSIX::debug) { Info<< "readDir(const fileName&, const fileType, " "const bool filtergz) : cannot open directory " << directory << endl; } } else { // Read and parse all the entries in the directory while ((list = readdir(source)) != NULL) { fileName fName(list->d_name); // Ignore files begining with ., i.e. '.', '..' and '.*' if (fName.size() && fName[0] != '.') { word fExt = fName.ext(); if ( (type == fileName::DIRECTORY) || ( type == fileName::FILE && fName[fName.size() - 1] != '~' && fExt != "vtk" && fExt != "bak" && fExt != "BAK" && fExt != "old" && fExt != "save" ) ) { if ((directory/fName).type() == type) { if (nEntries >= dirEntries.size()) { dirEntries.setSize(dirEntries.size() + maxNnames); } if (filtergz && fExt == "gz") { dirEntries[nEntries++] = fName.lessExt(); } else { dirEntries[nEntries++] = fName; } } } } } // Reset the length of the entries list dirEntries.setSize(nEntries); closedir(source); } return dirEntries; }
// Set the file mode bool Foam::chMod(const fileName& name, const mode_t m) { return ::chmod(name.c_str(), m) == 0; }
bool Foam::mkDir(const fileName& pathName, mode_t mode) { // empty names are meaningless if (pathName.empty()) { return false; } // Construct instance path directory if does not exist if (::mkdir(pathName.c_str(), mode) == 0) { // Directory made OK so return true return true; } else { switch (errno) { case EPERM: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "The filesystem containing " << pathName << " does not support the creation of directories." << exit(FatalError); return false; } case EEXIST: { // Directory already exists so simply return true return true; } case EFAULT: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "" << pathName << " points outside your accessible address space." << exit(FatalError); return false; } case EACCES: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "The parent directory does not allow write " "permission to the process,"<< nl << "or one of the directories in " << pathName << " did not allow search (execute) permission." << exit(FatalError); return false; } case ENAMETOOLONG: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "" << pathName << " is too long." << exit(FatalError); return false; } case ENOENT: { // Part of the path does not exist so try to create it if (pathName.path().size() && mkDir(pathName.path(), mode)) { return mkDir(pathName, mode); } else { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "Couldn't create directory " << pathName << exit(FatalError); return false; } } case ENOTDIR: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "A component used as a directory in " << pathName << " is not, in fact, a directory." << exit(FatalError); return false; } case ENOMEM: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "Insufficient kernel memory was available to make " "directory " << pathName << '.' << exit(FatalError); return false; } case EROFS: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "" << pathName << " refers to a file on a read-only filesystem." << exit(FatalError); return false; } case ELOOP: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "Too many symbolic links were encountered in resolving " << pathName << '.' << exit(FatalError); return false; } case ENOSPC: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "The device containing " << pathName << " has no room for the new directory or " << "the user's disk quota is exhausted." << exit(FatalError); return false; } default: { FatalErrorIn("Foam::mkDir(const fileName&, mode_t)") << "Couldn't create directory " << pathName << exit(FatalError); return false; } } } }