SF [2062835] Logfile _creationDate is wrong;

This commit is contained in:
Aleksandar Fabijanic
2008-09-23 18:00:11 +00:00
parent d244df7f81
commit 34ae16b89a
4 changed files with 93 additions and 44 deletions

View File

@@ -62,6 +62,8 @@ public:
const std::string& pathImpl() const; const std::string& pathImpl() const;
private: private:
void createFile();
std::string _path; std::string _path;
HANDLE _hFile; HANDLE _hFile;
Timestamp _creationDate; Timestamp _creationDate;

View File

@@ -62,6 +62,8 @@ public:
const std::string& pathImpl() const; const std::string& pathImpl() const;
private: private:
void createFile();
std::string _path; std::string _path;
HANDLE _hFile; HANDLE _hFile;
Timestamp _creationDate; Timestamp _creationDate;

View File

@@ -42,28 +42,16 @@
namespace Poco { namespace Poco {
LogFileImpl::LogFileImpl(const std::string& path): _path(path) LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
{ {
_hFile = CreateFileA(path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); File file(path);
if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(path); if (file.exists())
SetFilePointer(_hFile, 0, 0, FILE_END); {
// There seems to be a strange "optimization" in the Windows NTFS if (0 == sizeImpl())
// filesystem that causes it to reuse directory entries of deleted _creationDate = file.getLastModified();
// files. Example: else
// 1. create a file named "test.dat" _creationDate = file.created();
// note the file's creation date }
// 2. delete the file "test.dat"
// 3. wait a few seconds
// 4. create a file named "test.dat"
// the new file will have the same creation
// date as the old one.
// We work around this bug by taking the file's
// modification date as a reference when the
// file is empty.
if (sizeImpl() == 0)
_creationDate = File(path).getLastModified();
else
_creationDate = File(path).created();
} }
@@ -75,6 +63,8 @@ LogFileImpl::~LogFileImpl()
void LogFileImpl::writeImpl(const std::string& text) void LogFileImpl::writeImpl(const std::string& text)
{ {
if (INVALID_HANDLE_VALUE == _hFile) createFile();
DWORD bytesWritten; DWORD bytesWritten;
BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL); BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
if (!res) throw WriteFileException(_path); if (!res) throw WriteFileException(_path);
@@ -87,6 +77,13 @@ void LogFileImpl::writeImpl(const std::string& text)
UInt64 LogFileImpl::sizeImpl() const UInt64 LogFileImpl::sizeImpl() const
{ {
if (INVALID_HANDLE_VALUE == _hFile)
{
File file(_path);
if (file.exists()) return file.getSize();
else return 0;
}
LARGE_INTEGER li; LARGE_INTEGER li;
li.HighPart = 0; li.HighPart = 0;
li.LowPart = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT); li.LowPart = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
@@ -106,4 +103,29 @@ const std::string& LogFileImpl::pathImpl() const
} }
void LogFileImpl::createFile()
{
_hFile = CreateFileW(_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
SetFilePointer(_hFile, 0, 0, FILE_END);
// There seems to be a strange "optimization" in the Windows NTFS
// filesystem that causes it to reuse directory entries of deleted
// files. Example:
// 1. create a file named "test.dat"
// note the file's creation date
// 2. delete the file "test.dat"
// 3. wait a few seconds
// 4. create a file named "test.dat"
// the new file will have the same creation
// date as the old one.
// We work around this bug by taking the file's
// modification date as a reference when the
// file is empty.
if (sizeImpl() == 0)
_creationDate = File(_path).getLastModified();
else
_creationDate = File(_path).created();
}
} // namespace Poco } // namespace Poco

View File

@@ -43,30 +43,16 @@
namespace Poco { namespace Poco {
LogFileImpl::LogFileImpl(const std::string& path): _path(path) LogFileImpl::LogFileImpl(const std::string& path): _path(path), _hFile(INVALID_HANDLE_VALUE)
{ {
std::wstring upath; File file(path);
UnicodeConverter::toUTF16(path, upath); if (file.exists())
_hFile = CreateFileW(upath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); {
if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(path); if (0 == sizeImpl())
SetFilePointer(_hFile, 0, 0, FILE_END); _creationDate = file.getLastModified();
// There seems to be a strange "optimization" in the Windows NTFS else
// filesystem that causes it to reuse directory entries of deleted _creationDate = file.created();
// files. Example: }
// 1. create a file named "test.dat"
// note the file's creation date
// 2. delete the file "test.dat"
// 3. wait a few seconds
// 4. create a file named "test.dat"
// the new file will have the same creation
// date as the old one.
// We work around this bug by taking the file's
// modification date as a reference when the
// file is empty.
if (sizeImpl() == 0)
_creationDate = File(path).getLastModified();
else
_creationDate = File(path).created();
} }
@@ -78,6 +64,8 @@ LogFileImpl::~LogFileImpl()
void LogFileImpl::writeImpl(const std::string& text) void LogFileImpl::writeImpl(const std::string& text)
{ {
if (INVALID_HANDLE_VALUE == _hFile) createFile();
DWORD bytesWritten; DWORD bytesWritten;
BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL); BOOL res = WriteFile(_hFile, text.data(), (DWORD) text.size(), &bytesWritten, NULL);
if (!res) throw WriteFileException(_path); if (!res) throw WriteFileException(_path);
@@ -90,6 +78,13 @@ void LogFileImpl::writeImpl(const std::string& text)
UInt64 LogFileImpl::sizeImpl() const UInt64 LogFileImpl::sizeImpl() const
{ {
if (INVALID_HANDLE_VALUE == _hFile)
{
File file(_path);
if (file.exists()) return file.getSize();
else return 0;
}
LARGE_INTEGER li; LARGE_INTEGER li;
li.HighPart = 0; li.HighPart = 0;
li.LowPart = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT); li.LowPart = SetFilePointer(_hFile, 0, &li.HighPart, FILE_CURRENT);
@@ -109,4 +104,32 @@ const std::string& LogFileImpl::pathImpl() const
} }
void LogFileImpl::createFile()
{
std::wstring upath;
UnicodeConverter::toUTF16(_path, upath);
_hFile = CreateFileW(upath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
SetFilePointer(_hFile, 0, 0, FILE_END);
// There seems to be a strange "optimization" in the Windows NTFS
// filesystem that causes it to reuse directory entries of deleted
// files. Example:
// 1. create a file named "test.dat"
// note the file's creation date
// 2. delete the file "test.dat"
// 3. wait a few seconds
// 4. create a file named "test.dat"
// the new file will have the same creation
// date as the old one.
// We work around this bug by taking the file's
// modification date as a reference when the
// file is empty.
if (sizeImpl() == 0)
_creationDate = File(_path).getLastModified();
else
_creationDate = File(_path).created();
}
} // namespace Poco } // namespace Poco