From d5f0a2b396ddbc8b333a65285d2c8d62ba52f8dc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 22 Oct 2002 22:16:49 +0000 Subject: [PATCH] implemented GetTimes() for the directories git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@17611 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/filename.cpp | 94 +++++++++++++++++++++++++++-------------- src/msw/dir.cpp | 33 +++++++++++++++ 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/src/common/filename.cpp b/src/common/filename.cpp index e073ba1fca..63d3a482bf 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -1617,24 +1617,32 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, const wxDateTime *dtCreate) { #if defined(__WIN32__) - wxFileHandle fh(GetFullPath(), wxFileHandle::Write); - if ( fh.IsOk() ) + if ( IsDir() ) { - FILETIME ftAccess, ftCreate, ftWrite; - - if ( dtCreate ) - ConvertWxToFileTime(&ftCreate, *dtCreate); - if ( dtAccess ) - ConvertWxToFileTime(&ftAccess, *dtAccess); - if ( dtMod ) - ConvertWxToFileTime(&ftWrite, *dtMod); - - if ( ::SetFileTime(fh, - dtCreate ? &ftCreate : NULL, - dtAccess ? &ftAccess : NULL, - dtMod ? &ftWrite : NULL) ) + // VZ: please let me know how to do this if you can + wxFAIL_MSG( _T("SetTimes() not implemented for the directories") ); + } + else // file + { + wxFileHandle fh(GetFullPath(), wxFileHandle::Write); + if ( fh.IsOk() ) { - return TRUE; + FILETIME ftAccess, ftCreate, ftWrite; + + if ( dtCreate ) + ConvertWxToFileTime(&ftCreate, *dtCreate); + if ( dtAccess ) + ConvertWxToFileTime(&ftAccess, *dtAccess); + if ( dtMod ) + ConvertWxToFileTime(&ftWrite, *dtMod); + + if ( ::SetFileTime(fh, + dtCreate ? &ftCreate : NULL, + dtAccess ? &ftAccess : NULL, + dtMod ? &ftWrite : NULL) ) + { + return TRUE; + } } } #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) @@ -1686,25 +1694,49 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, wxDateTime *dtCreate) const { #if defined(__WIN32__) - wxFileHandle fh(GetFullPath(), wxFileHandle::Read); - if ( fh.IsOk() ) + // we must use different methods for the files and directories under + // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and + // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and + // not 9x + bool ok; + FILETIME ftAccess, ftCreate, ftWrite; + if ( IsDir() ) { - FILETIME ftAccess, ftCreate, ftWrite; + // implemented in msw/dir.cpp + extern bool wxGetDirectoryTimes(const wxString& dirname, + FILETIME *, FILETIME *, FILETIME *); - if ( ::GetFileTime(fh, - dtCreate ? &ftCreate : NULL, - dtAccess ? &ftAccess : NULL, - dtMod ? &ftWrite : NULL) ) + // we should pass the path without the trailing separator to + // wxGetDirectoryTimes() + ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), + &ftAccess, &ftCreate, &ftWrite); + } + else // file + { + wxFileHandle fh(GetFullPath(), wxFileHandle::Read); + if ( fh.IsOk() ) { - if ( dtCreate ) - ConvertFileTimeToWx(dtCreate, ftCreate); - if ( dtAccess ) - ConvertFileTimeToWx(dtAccess, ftAccess); - if ( dtMod ) - ConvertFileTimeToWx(dtMod, ftWrite); - - return TRUE; + ok = ::GetFileTime(fh, + dtCreate ? &ftCreate : NULL, + dtAccess ? &ftAccess : NULL, + dtMod ? &ftWrite : NULL) != 0; } + else + { + ok = FALSE; + } + } + + if ( ok ) + { + if ( dtCreate ) + ConvertFileTimeToWx(dtCreate, ftCreate); + if ( dtAccess ) + ConvertFileTimeToWx(dtAccess, ftAccess); + if ( dtMod ) + ConvertFileTimeToWx(dtMod, ftWrite); + + return TRUE; } #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) wxStructStat stBuf; diff --git a/src/msw/dir.cpp b/src/msw/dir.cpp index 7d505e56bc..6e51506f8b 100644 --- a/src/msw/dir.cpp +++ b/src/msw/dir.cpp @@ -458,3 +458,36 @@ bool wxDir::GetNext(wxString *filename) const return M_DIR->Read(filename); } + +// ---------------------------------------------------------------------------- +// wxGetDirectoryTimes: used by wxFileName::GetTimes() +// ---------------------------------------------------------------------------- + +#ifdef __WIN32__ + +extern bool +wxGetDirectoryTimes(const wxString& dirname, + FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod) +{ + // FindFirst() is going to fail + wxASSERT_MSG( !dirname.empty() && dirname.Last() != _T('\\'), + _T("incorrect directory name format in wxGetDirectoryTimes") ); + + FIND_STRUCT fs; + FIND_DATA fd = FindFirst(dirname, &fs); + if ( !IsFindDataOk(fd) ) + { + return FALSE; + } + + *ftAccess = fs.ftLastAccessTime; + *ftCreate = fs.ftCreationTime; + *ftMod = fs.ftLastWriteTime; + + FindClose(fd); + + return TRUE; +} + +#endif // __WIN32__ +