OSspecific: altered file tests

- removed the previously added fileName type(), isDir(), isFile() methods.

- added optional bool parameter to isFile() function to explicitly
  enable/disable the check for gzip files.

- fixed minor bugginess where the default usage of isFile() would result in
  false positive matches.

- be slightly more stringent and use isDir() and isFile() instead of
  exists() function when we actually know the expected type.
This commit is contained in:
Mark Olesen
2009-02-06 10:25:41 +01:00
parent fb29e08539
commit ffd9bb08a8
23 changed files with 66 additions and 79 deletions

View File

@ -615,7 +615,7 @@ int main(int argc, char *argv[])
{ {
fileName ccmFile(args.additionalArgs()[0]); fileName ccmFile(args.additionalArgs()[0]);
if (!exists(ccmFile)) if (!isFile(ccmFile))
{ {
FatalErrorIn(args.executable()) FatalErrorIn(args.executable())
<< "Cannot read file " << ccmFile << "Cannot read file " << ccmFile

View File

@ -129,14 +129,14 @@ int main(int argc, char *argv[])
Info<< "Reading .face file for boundary information" << nl << endl; Info<< "Reading .face file for boundary information" << nl << endl;
} }
if (!exists(nodeFile) || !exists(eleFile)) if (!isFile(nodeFile) || !isFile(eleFile))
{ {
FatalErrorIn(args.executable()) FatalErrorIn(args.executable())
<< "Cannot read " << nodeFile << " or " << eleFile << "Cannot read " << nodeFile << " or " << eleFile
<< exit(FatalError); << exit(FatalError);
} }
if (readFaceFile && !exists(faceFile)) if (readFaceFile && !isFile(faceFile))
{ {
FatalErrorIn(args.executable()) FatalErrorIn(args.executable())
<< "Cannot read " << faceFile << endl << "Cannot read " << faceFile << endl

View File

@ -108,7 +108,11 @@ int main(int argc, char *argv[])
( (
new IOobject new IOobject
( (
( dictPath.isDir() ? dictPath/dictName : dictPath ), (
isDir(dictPath)
? dictPath/dictName
: dictPath
),
runTime, runTime,
IOobject::MUST_READ, IOobject::MUST_READ,
IOobject::NO_WRITE, IOobject::NO_WRITE,

View File

@ -746,7 +746,8 @@ int main(int argc, char *argv[])
Pout<< "Reading commands from file " << batchFile << endl; Pout<< "Reading commands from file " << batchFile << endl;
if (!exists(batchFile)) // we also cannot handle .gz files
if (!isFile(batchFile, false))
{ {
FatalErrorIn(args.executable()) FatalErrorIn(args.executable())
<< "Cannot open file " << batchFile << exit(FatalError); << "Cannot open file " << batchFile << exit(FatalError);

View File

@ -627,7 +627,7 @@ autoPtr<mapPolyMesh> createRegionMesh
Info<< "Testing:" << io.objectPath() << endl; Info<< "Testing:" << io.objectPath() << endl;
if (!io.headerOk()) if (!io.headerOk())
//if (!exists(io.objectPath())) // if (!exists(io.objectPath()))
{ {
Info<< "Writing dummy " << regionName/io.name() << endl; Info<< "Writing dummy " << regionName/io.name() << endl;
dictionary dummyDict; dictionary dummyDict;

View File

@ -355,7 +355,7 @@ int main(int argc, char *argv[])
while while
( (
exists isDir
( (
args.rootPath() args.rootPath()
/ args.caseName() / args.caseName()

View File

@ -122,7 +122,11 @@ int main(int argc, char *argv[])
( (
new IOobject new IOobject
( (
( dictPath.isDir() ? dictPath/coordinateSystems::typeName : dictPath ), (
isDir(dictPath)
? dictPath/coordinateSystems::typeName
: dictPath
),
runTime, runTime,
IOobject::MUST_READ, IOobject::MUST_READ,
IOobject::NO_WRITE, IOobject::NO_WRITE,

View File

@ -119,7 +119,7 @@ int main(int argc, char *argv[])
new IOobject new IOobject
( (
( (
dictPath.isDir() isDir(dictPath)
? dictPath/coordinateSystems::typeName ? dictPath/coordinateSystems::typeName
: dictPath : dictPath
), ),

View File

@ -133,7 +133,7 @@ int main(int argc, char *argv[])
new IOobject new IOobject
( (
( (
dictPath.isDir() isDir(dictPath)
? dictPath/coordinateSystems::typeName ? dictPath/coordinateSystems::typeName
: dictPath : dictPath
), ),

View File

@ -488,9 +488,9 @@ bool Foam::isDir(const fileName& name)
// Does the file exist // Does the file exist
bool Foam::isFile(const fileName& name) bool Foam::isFile(const fileName& name, const bool checkGzip)
{ {
return S_ISREG(mode(name)) || S_ISREG(mode(name + ".gz")); return S_ISREG(mode(name)) || (checkGzip && S_ISREG(mode(name + ".gz")));
} }
@ -719,19 +719,19 @@ bool Foam::cp(const fileName& src, const fileName& dest)
} }
// Create a softlink. destFile should not exist. Returns true if successful. // Create a softlink. dst should not exist. Returns true if successful.
bool Foam::ln(const fileName& src, const fileName& dest) bool Foam::ln(const fileName& src, const fileName& dst)
{ {
if (Unix::debug) if (Unix::debug)
{ {
Info<< "Create softlink from : " << src << " to " << dest Info<< "Create softlink from : " << src << " to " << dst
<< endl; << endl;
} }
if (exists(dest)) if (exists(dst))
{ {
WarningIn("ln(const fileName&, const fileName&)") WarningIn("ln(const fileName&, const fileName&)")
<< "destination " << dest << " already exists. Not linking." << "destination " << dst << " already exists. Not linking."
<< endl; << endl;
return false; return false;
} }
@ -743,40 +743,40 @@ bool Foam::ln(const fileName& src, const fileName& dest)
return false; return false;
} }
if (symlink(src.c_str(), dest.c_str()) == 0) if (symlink(src.c_str(), dst.c_str()) == 0)
{ {
return true; return true;
} }
else else
{ {
WarningIn("ln(const fileName&, const fileName&)") WarningIn("ln(const fileName&, const fileName&)")
<< "symlink from " << src << " to " << dest << " failed." << endl; << "symlink from " << src << " to " << dst << " failed." << endl;
return false; return false;
} }
} }
// Rename srcFile destFile // Rename srcFile dstFile
bool Foam::mv(const fileName& srcFile, const fileName& destFile) bool Foam::mv(const fileName& srcFile, const fileName& dstFile)
{ {
if (Unix::debug) if (Unix::debug)
{ {
Info<< "Move : " << srcFile << " to " << destFile << endl; Info<< "Move : " << srcFile << " to " << dstFile << endl;
} }
if if
( (
(destFile.type() == fileName::DIRECTORY) dstFile.type() == fileName::DIRECTORY
&& (srcFile.type() != fileName::DIRECTORY) && srcFile.type() != fileName::DIRECTORY
) )
{ {
const fileName destName(destFile/srcFile.name()); const fileName dstName(dstFile/srcFile.name());
return rename(srcFile.c_str(), destName.c_str()) == 0; return rename(srcFile.c_str(), dstName.c_str()) == 0;
} }
else else
{ {
return rename(srcFile.c_str(), destFile.c_str()) == 0; return rename(srcFile.c_str(), dstFile.c_str()) == 0;
} }
} }

View File

@ -61,7 +61,7 @@ bool Foam::IOobject::IOobject::fileNameComponents
name.clear(); name.clear();
// called with directory // called with directory
if (::Foam::isDir(path)) if (!isDir(path))
{ {
WarningIn("IOobject::fileNameComponents(const fileName&, ...)") WarningIn("IOobject::fileNameComponents(const fileName&, ...)")
<< " called with directory: " << path << "\n"; << " called with directory: " << path << "\n";

View File

@ -54,7 +54,7 @@ Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
ifPtr_ = new ifstream(pathname.c_str()); ifPtr_ = new ifstream(pathname.c_str());
// If the file is compressed, decompress it before reading. // If the file is compressed, decompress it before reading.
if (!ifPtr_->good() && isFile(pathname + ".gz")) if (!ifPtr_->good() && isFile(pathname + ".gz", false))
{ {
if (IFstream::debug) if (IFstream::debug)
{ {
@ -120,8 +120,8 @@ Foam::IFstream::IFstream
if (debug) if (debug)
{ {
Info<< "IFstream::IFstream(const fileName&," Info<< "IFstream::IFstream(const fileName&,"
"streamFormat format=ASCII," "streamFormat=ASCII,"
"versionNumber version=currentVersion) : " "versionNumber=currentVersion) : "
"could not open file for input" "could not open file for input"
<< endl << info() << endl; << endl << info() << endl;
} }
@ -159,17 +159,18 @@ Foam::IFstream& Foam::IFstream::operator()() const
{ {
if (!good()) if (!good())
{ {
if (!isFile(pathname_) && !isFile(pathname_ + ".gz")) // also checks .gz file
if (isFile(pathname_, true))
{
check("IFstream::operator()");
FatalIOError.exit();
}
else
{ {
FatalIOErrorIn("IFstream::operator()", *this) FatalIOErrorIn("IFstream::operator()", *this)
<< "file " << pathname_ << " does not exist" << "file " << pathname_ << " does not exist"
<< exit(FatalIOError); << exit(FatalIOError);
} }
else
{
check("IFstream::operator()");
FatalIOError.exit();
}
} }
return const_cast<IFstream&>(*this); return const_cast<IFstream&>(*this);

View File

@ -57,7 +57,8 @@ Foam::OFstreamAllocator::OFstreamAllocator
if (compression == IOstream::COMPRESSED) if (compression == IOstream::COMPRESSED)
{ {
if (isFile(pathname)) // get identically named uncompressed version out of the way
if (isFile(pathname, false))
{ {
rm(pathname); rm(pathname);
} }
@ -66,7 +67,8 @@ Foam::OFstreamAllocator::OFstreamAllocator
} }
else else
{ {
if (isFile(pathname + ".gz")) // get identically named compressed version out of the way
if (isFile(pathname + ".gz", false))
{ {
rm(pathname + ".gz"); rm(pathname + ".gz");
} }

View File

@ -624,7 +624,7 @@ void Foam::argList::displayDoc(bool source) const
docFile = docDirs[dirI]/executable_ + docExts[extI]; docFile = docDirs[dirI]/executable_ + docExts[extI];
docFile.expand(); docFile.expand();
if (exists(docFile)) if (isFile(docFile))
{ {
found = true; found = true;
break; break;

View File

@ -115,18 +115,19 @@ bool chmod(const fileName&, const mode_t);
//- Return the file mode //- Return the file mode
mode_t mode(const fileName&); mode_t mode(const fileName&);
//- Return the file type: FILE or DIRECTORY //- Return the file type: DIRECTORY or FILE
fileName::Type type(const fileName&); fileName::Type type(const fileName&);
//- Does the name exist in the file system? //- Does the name exist (as DIRECTORY or FILE) in the file system?
bool exists(const fileName&); bool exists(const fileName&);
//- Does the name exist as a FILE in the file system?
bool isFile(const fileName&);
//- Does the name exist as a DIRECTORY in the file system? //- Does the name exist as a DIRECTORY in the file system?
bool isDir(const fileName&); bool isDir(const fileName&);
//- Does the name exist as a FILE in the file system?
// Optionally enable/disable check for gzip file.
bool isFile(const fileName&, const bool checkGzip=true);
//- Return size of file //- Return size of file
off_t fileSize(const fileName&); off_t fileSize(const fileName&);

View File

@ -55,24 +55,6 @@ Foam::fileName::Type Foam::fileName::type() const
} }
bool Foam::fileName::exists() const
{
return ::Foam::exists(*this);
}
bool Foam::fileName::isDir() const
{
return ::Foam::isDir(*this);
}
bool Foam::fileName::isFile() const
{
return ::Foam::isFile(*this);
}
// Return file name (part beyond last /) // Return file name (part beyond last /)
// //
// behaviour compared to /usr/bin/basename: // behaviour compared to /usr/bin/basename:

View File

@ -134,15 +134,6 @@ public:
//- Return the file type: FILE, DIRECTORY or UNDEFINED //- Return the file type: FILE, DIRECTORY or UNDEFINED
Type type() const; Type type() const;
//- Does it exist (as FILE or DIRECTORY) in the file system?
bool exists() const;
//- Does it exist as DIRECTORY in the file system?
bool isDir() const;
//- Does it exist as FILE in the file system?
bool isFile() const;
// Decomposition // Decomposition
//- Return file name (part beyond last /) //- Return file name (part beyond last /)

View File

@ -302,7 +302,7 @@ void Foam::dxSurfaceWriter<Type>::write
{ {
fileName surfaceDir(samplePath/timeDir); fileName surfaceDir(samplePath/timeDir);
if (!exists(surfaceDir)) if (!isDir(surfaceDir))
{ {
mkDir(surfaceDir); mkDir(surfaceDir);
} }

View File

@ -63,7 +63,7 @@ void Foam::foamFileSurfaceWriter<Type>::write
{ {
fileName surfaceDir(samplePath/timeDir/surfaceName); fileName surfaceDir(samplePath/timeDir/surfaceName);
if (!exists(surfaceDir)) if (!isDir(surfaceDir))
{ {
mkDir(surfaceDir); mkDir(surfaceDir);
} }
@ -84,7 +84,7 @@ void Foam::foamFileSurfaceWriter<Type>::write
fileName foamName(pTraits<Type>::typeName); fileName foamName(pTraits<Type>::typeName);
fileName valuesDir(surfaceDir / (foamName + Field<Type>::typeName)); fileName valuesDir(surfaceDir / (foamName + Field<Type>::typeName));
if (!exists(valuesDir)) if (!isDir(valuesDir))
{ {
mkDir(valuesDir); mkDir(valuesDir);
} }

View File

@ -342,7 +342,7 @@ void Foam::rawSurfaceWriter<Type>::write
{ {
fileName surfaceDir(samplePath/timeDir); fileName surfaceDir(samplePath/timeDir);
if (!exists(surfaceDir)) if (!isDir(surfaceDir))
{ {
mkDir(surfaceDir); mkDir(surfaceDir);
} }

View File

@ -64,7 +64,7 @@ void Foam::stlSurfaceWriter<Type>::write
{ {
fileName surfaceDir(samplePath/timeDir); fileName surfaceDir(samplePath/timeDir);
if (!exists(surfaceDir)) if (!isDir(surfaceDir))
{ {
mkDir(surfaceDir); mkDir(surfaceDir);
} }

View File

@ -309,7 +309,7 @@ void Foam::vtkSurfaceWriter<Type>::write
{ {
fileName surfaceDir(samplePath/timeDir); fileName surfaceDir(samplePath/timeDir);
if (!exists(surfaceDir)) if (!isDir(surfaceDir))
{ {
mkDir(surfaceDir); mkDir(surfaceDir);
} }

View File

@ -47,8 +47,9 @@ bool triSurface::readSTLBINARY(const fileName& STLfileName)
( (
new ifstream(STLfileName.c_str(), std::ios::binary) new ifstream(STLfileName.c_str(), std::ios::binary)
); );
// If the file is compressed, decompress it before reading. // If the file is compressed, decompress it before reading.
if (!STLfilePtr->good() && isFile(STLfileName + ".gz")) if (!STLfilePtr->good() && isFile(STLfileName + ".gz", false))
{ {
compressed = true; compressed = true;
STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str())); STLfilePtr.reset(new igzstream((STLfileName + ".gz").c_str()));