mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: improve robustness of fileStat class (issue #794)
- quick ignore of empty filenames. - relocated some implementation details from POSIX.C to the fileStat class, where they make more sense.
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -59,7 +59,12 @@ Description
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <dlfcn.h>
|
||||
#include <link.h>
|
||||
|
||||
#ifdef darwin
|
||||
#include <mach-o/dyld.h>
|
||||
#else
|
||||
#include <link.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_RANDOM
|
||||
#include <climits>
|
||||
@ -652,16 +657,7 @@ time_t Foam::lastModified(const fileName& name, const bool followLink)
|
||||
}
|
||||
|
||||
// Ignore an empty name
|
||||
if (!name.empty())
|
||||
{
|
||||
fileStat fileStatus(name, followLink);
|
||||
if (fileStatus.isValid())
|
||||
{
|
||||
return fileStatus.status().st_mtime;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return name.empty() ? 0 : fileStat(name, followLink).modTime();
|
||||
}
|
||||
|
||||
|
||||
@ -677,18 +673,7 @@ double Foam::highResLastModified(const fileName& name, const bool followLink)
|
||||
}
|
||||
|
||||
// Ignore an empty name
|
||||
if (!name.empty())
|
||||
{
|
||||
fileStat fileStatus(name, followLink);
|
||||
if (fileStatus.isValid())
|
||||
{
|
||||
return
|
||||
fileStatus.status().st_mtime
|
||||
+ 1e-9*fileStatus.status().st_mtim.tv_nsec;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return name.empty() ? 0 : fileStat(name, followLink).dmodTime();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -29,7 +29,9 @@ License
|
||||
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#ifndef darwin
|
||||
#include <sys/sysmacros.h>
|
||||
#endif
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
@ -48,6 +50,11 @@ Foam::fileStat::fileStat
|
||||
:
|
||||
isValid_(false)
|
||||
{
|
||||
if (fName.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Work on volatile
|
||||
volatile bool locIsValid = false;
|
||||
|
||||
@ -84,20 +91,42 @@ Foam::label Foam::fileStat::size() const
|
||||
}
|
||||
|
||||
|
||||
bool Foam::fileStat::sameDevice(const fileStat& stat2) const
|
||||
time_t Foam::fileStat::modTime() const
|
||||
{
|
||||
return isValid_ ? status_.st_mtime : 0;
|
||||
}
|
||||
|
||||
|
||||
double Foam::fileStat::dmodTime() const
|
||||
{
|
||||
return
|
||||
(
|
||||
isValid_
|
||||
?
|
||||
#ifdef darwin
|
||||
(status_.st_mtime + 1e-9*status_.st_mtimespec.tv_nsec)
|
||||
#else
|
||||
(status_.st_mtime + 1e-9*status_.st_mtim.tv_nsec)
|
||||
#endif
|
||||
: 0
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
bool Foam::fileStat::sameDevice(const fileStat& other) const
|
||||
{
|
||||
return
|
||||
isValid_
|
||||
&& (
|
||||
major(status_.st_dev) == major(stat2.status().st_dev)
|
||||
&& minor(status_.st_dev) == minor(stat2.status().st_dev)
|
||||
major(status_.st_dev) == major(other.status_.st_dev)
|
||||
&& minor(status_.st_dev) == minor(other.status_.st_dev)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
bool Foam::fileStat::sameINode(const fileStat& stat2) const
|
||||
bool Foam::fileStat::sameINode(const fileStat& other) const
|
||||
{
|
||||
return isValid_ && (status_.st_ino == stat2.status().st_ino);
|
||||
return isValid_ && (status_.st_ino == other.status_.st_ino);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -51,8 +51,7 @@ SourceFiles
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
// Forward declaration of friend functions and operators
|
||||
|
||||
// Forward declarations
|
||||
class fileStat;
|
||||
|
||||
Istream& operator>>(Istream& is, fileStat& fStat);
|
||||
@ -88,6 +87,8 @@ public:
|
||||
//
|
||||
// \param maxTime \n
|
||||
// The timeout value.
|
||||
//
|
||||
// \note an empty filename is a no-op.
|
||||
fileStat
|
||||
(
|
||||
const fileName& fName,
|
||||
@ -101,34 +102,42 @@ public:
|
||||
|
||||
// Member Functions
|
||||
|
||||
// Access
|
||||
// Access
|
||||
|
||||
//- Raw status
|
||||
const struct stat& status() const
|
||||
{
|
||||
return status_;
|
||||
}
|
||||
//- Raw status
|
||||
const struct stat& status() const
|
||||
{
|
||||
return status_;
|
||||
}
|
||||
|
||||
//- Was file-stat successful?
|
||||
bool isValid() const
|
||||
{
|
||||
return isValid_;
|
||||
}
|
||||
//- Was file-stat successful?
|
||||
bool isValid() const
|
||||
{
|
||||
return isValid_;
|
||||
}
|
||||
|
||||
//- Size in bytes. Zero for invalid file-stat.
|
||||
label size() const;
|
||||
//- Size in bytes. Zero for an invalid file-stat.
|
||||
label size() const;
|
||||
|
||||
//- Return the modification time in seconds.
|
||||
// Zero for an invalid file-stat.
|
||||
time_t modTime() const;
|
||||
|
||||
//- Return the modification time in seconds (nanosecond resolution)
|
||||
// Zero for an invalid file-stat.
|
||||
double dmodTime() const;
|
||||
|
||||
|
||||
// Check
|
||||
// Check
|
||||
|
||||
//- Compare two fileStats for same device
|
||||
bool sameDevice(const fileStat& stat2) const;
|
||||
//- Compare two fileStats for same device
|
||||
bool sameDevice(const fileStat& other) const;
|
||||
|
||||
//- Compare two fileStats for same Inode
|
||||
bool sameINode(const fileStat& stat2) const;
|
||||
//- Compare two fileStats for same Inode
|
||||
bool sameINode(const fileStat& other) const;
|
||||
|
||||
//- Compare state against inode
|
||||
bool sameINode(const label iNode) const;
|
||||
//- Compare state against inode
|
||||
bool sameINode(const label iNode) const;
|
||||
|
||||
|
||||
// IOstream Operators
|
||||
|
||||
@ -86,9 +86,9 @@ string pOpen(const string& cmd, label line=0)
|
||||
|
||||
inline word addressToWord(const uintptr_t addr)
|
||||
{
|
||||
OStringStream nStream;
|
||||
nStream << "0x" << hex << addr;
|
||||
return nStream.str();
|
||||
OStringStream os;
|
||||
os << "0x" << hex << addr;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
|
||||
@ -122,7 +122,7 @@ void printSourceFileAndLine
|
||||
1
|
||||
);
|
||||
|
||||
if (line == "")
|
||||
if (line.empty())
|
||||
{
|
||||
os << " addr2line failed";
|
||||
}
|
||||
@ -161,7 +161,6 @@ fileName absolutePath(const char* fn)
|
||||
|
||||
word demangleSymbol(const char* sn)
|
||||
{
|
||||
word res;
|
||||
int st;
|
||||
char* cxx_sname = abi::__cxa_demangle
|
||||
(
|
||||
@ -173,15 +172,13 @@ word demangleSymbol(const char* sn)
|
||||
|
||||
if (st == 0 && cxx_sname)
|
||||
{
|
||||
res = word(cxx_sname);
|
||||
word demangled(cxx_sname);
|
||||
free(cxx_sname);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = word(sn);
|
||||
|
||||
return demangled;
|
||||
}
|
||||
|
||||
return res;
|
||||
return sn;
|
||||
}
|
||||
|
||||
|
||||
@ -189,8 +186,8 @@ word demangleSymbol(const char* sn)
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
void Foam::error::safePrintStack(std::ostream& os)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user