Skip to content

Commit

Permalink
Use existing code to get lastAccessTime.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Aug 29, 2023
1 parent d46003c commit a27c0b0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 46 deletions.
4 changes: 0 additions & 4 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,6 @@ namespace Utility {
OCSYNC_EXPORT bool registryWalkValues(HKEY hRootKey, const QString &subKey, const std::function<void(const QString &, bool *)> &callback);
OCSYNC_EXPORT QRect getTaskbarDimensions();

OCSYNC_EXPORT quint64 getLastAccessTime(const QString &filename);

OCSYNC_EXPORT quint64 fileTimeToUnixTime(const FILETIME &fileTime);

// Possibly refactor to share code with UnixTimevalToFileTime in c_time.c
OCSYNC_EXPORT void UnixTimeToFiletime(time_t t, FILETIME *filetime);
OCSYNC_EXPORT void FiletimeToLargeIntegerFiletime(FILETIME *filetime, LARGE_INTEGER *hundredNSecs);
Expand Down
39 changes: 0 additions & 39 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,45 +173,6 @@ QRect Utility::getTaskbarDimensions()
return QRect(barRect.left, barRect.top, (barRect.right - barRect.left), (barRect.bottom - barRect.top));
}

quint64 Utility::fileTimeToUnixTime(const FILETIME &fileTime)
{
ULARGE_INTEGER uli;
uli.LowPart = fileTime.dwLowDateTime;
uli.HighPart = fileTime.dwHighDateTime;

const auto epochDifference = 116444736000000000ULL; // 100-nanosecond intervals between 1601 and 1970
return (uli.QuadPart - epochDifference) / 10000000ULL; // Convert to seconds
}

quint64 Utility::getLastAccessTime(const QString &filename)
{
if (filename.isEmpty()) {
return 0;
}

const auto handle = CreateFileW(filename.toStdWString().c_str(),
GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
nullptr);
if (handle == INVALID_HANDLE_VALUE) {
qCWarning(lcUtility) << "Could not retrieve lastAccessTime for file" << filename << formatWinError(GetLastError());
return 0;
}

FILETIME lastAccessTime;
const auto resultGetFileTime = GetFileTime(handle, nullptr, &lastAccessTime, nullptr);
CloseHandle(handle);

if (!resultGetFileTime) {
qCWarning(lcUtility) << "Could not retrieve lastAccessTime for file" << filename << formatWinError(GetLastError());
}

return fileTimeToUnixTime(lastAccessTime);
}

bool Utility::registryKeyExists(HKEY hRootKey, const QString &subKey)
{
HKEY hKey;
Expand Down
7 changes: 5 additions & 2 deletions src/csync/vio/csync_vio_local_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h
file_stat = std::make_unique<csync_file_stat_t>();
file_stat->path = path.toUtf8();

file_stat->lastAccessTime = OCC::Utility::fileTimeToUnixTime(handle->ffd.ftLastAccessTime);

if (vfs && vfs->statTypeVirtualFile(file_stat.get(), &handle->ffd)) {
// all good
} else if (handle->ffd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Expand Down Expand Up @@ -174,6 +172,8 @@ std::unique_ptr<csync_file_stat_t> csync_vio_local_readdir(csync_vio_handle_t *h

file_stat->size = (handle->ffd.nFileSizeHigh * ((int64_t)(MAXDWORD)+1)) + handle->ffd.nFileSizeLow;
file_stat->modtime = FileTimeToUnixTime(&handle->ffd.ftLastWriteTime, &rem);
rem = 0;
file_stat->lastAccessTime = FileTimeToUnixTime(&handle->ffd.ftLastAccessTime, &rem);

// path always ends with '\', by construction

Expand Down Expand Up @@ -224,6 +224,9 @@ int csync_vio_local_stat(const QString &uri, csync_file_stat_t *buf)
DWORD rem = 0;
buf->modtime = FileTimeToUnixTime(&fileInfo.ftLastWriteTime, &rem);

rem = 0;
buf->lastAccessTime = FileTimeToUnixTime(&fileInfo.ftLastAccessTime, &rem);

CloseHandle(h);
return 0;
}
13 changes: 12 additions & 1 deletion src/libsync/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ static qint64 getSizeWithCsync(const QString &filename)
}
return result;
}
static quint64 getlastAccessTimeWithCsync(const QString &filename)
{
quint64 result = 0;
csync_file_stat_t stat;
if (csync_vio_local_stat(filename, &stat) != -1) {
result = stat.lastAccessTime;
} else {
qCWarning(lcFileSystem) << "Could not get last access time for" << filename << "with csync" << Utility::formatWinError(errno);
}
return result;
}
#endif

qint64 FileSystem::getSize(const QString &filename)
Expand All @@ -139,7 +150,7 @@ qint64 FileSystem::getSize(const QString &filename)
quint64 FileSystem::getLastAccessTime(const QString &filename)
{
#ifdef Q_OS_WIN
return Utility::getLastAccessTime(filename);
return getlastAccessTimeWithCsync(filename);
#endif
return 0;
}
Expand Down

0 comments on commit a27c0b0

Please sign in to comment.