Skip to content

Commit

Permalink
Replace src/csync/std/c_time.cpp with std::filesystem::last_write_time
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Jul 12, 2022
1 parent d2f080e commit b270adf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 170 deletions.
3 changes: 0 additions & 3 deletions src/csync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ include(../common/common.cmake)
set(csync_SRCS
csync.cpp
csync_exclude.cpp

std/c_time.cpp

)

if (WIN32)
Expand Down
114 changes: 0 additions & 114 deletions src/csync/std/c_time.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions src/csync/std/c_time.h

This file was deleted.

31 changes: 15 additions & 16 deletions src/libsync/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

#include "csync.h"
#include "vio/csync_vio_local.h"
#include "std/c_time.h"

#ifdef Q_OS_WIN32
#include <winsock2.h>
#endif

#include <filesystem>

namespace OCC {

bool FileSystem::fileEquals(const QString &fn1, const QString &fn2)
Expand Down Expand Up @@ -61,28 +62,26 @@ bool FileSystem::fileEquals(const QString &fn1, const QString &fn2)

time_t FileSystem::getModTime(const QString &filename)
{
csync_file_stat_t stat;
qint64 result = -1;
if (csync_vio_local_stat(filename, &stat) != -1
&& (stat.modtime != 0)) {
result = stat.modtime;
} else {
result = Utility::qDateTimeToTime_t(QFileInfo(filename).lastModified());
qCWarning(lcFileSystem) << "Could not get modification time for" << filename
std::error_code error;
const auto time = std::filesystem::last_write_time(toFilesystemPath(filename), error);
const auto systemTime = std::chrono::system_clock::now() + (time - std::filesystem::file_time_type::clock::now());
if (error) {
const auto result = Utility::qDateTimeToTime_t(QFileInfo(filename).lastModified());
qCWarning(lcFileSystem) << "Could not get modification time for" << filename << error.value() << QString::fromStdString(error.message())
<< "with csync, using QFileInfo:" << result;
return result;
}
return result;
return std::chrono::system_clock::to_time_t(systemTime);
}

bool FileSystem::setModTime(const QString &filename, time_t modTime)
{
struct timeval times[2];
times[0].tv_sec = times[1].tv_sec = modTime;
times[0].tv_usec = times[1].tv_usec = 0;
int rc = c_utimes(filename, times);
if (rc != 0) {
const auto time = std::filesystem::file_time_type::clock::now() + (std::chrono::system_clock::from_time_t(modTime) - std::chrono::system_clock::now());
std::error_code error;
std::filesystem::last_write_time(toFilesystemPath(filename), time, error);
if (error) {
qCWarning(lcFileSystem) << "Error setting mtime for" << filename
<< "failed: rc" << rc << ", errno:" << errno;
<< "failed: rc" << error.value() << ", errno:" << QString::fromStdString(error.message());
Q_ASSERT(false);
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions src/libsync/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// Chain in the base include and extend the namespace
#include "common/filesystembase.h"

#include <filesystem>

class QFile;

namespace OCC {
Expand All @@ -40,6 +42,13 @@ class SyncJournal;
*/
namespace FileSystem {

// https://github.com/qt/qtbase/blob/067b53864112c084587fa9a507eb4bde3d50a6e1/src/corelib/io/qfile.h#L40
inline std::filesystem::path toFilesystemPath(const QString &path)
{
return std::filesystem::path(reinterpret_cast<const char16_t *>(path.cbegin()),
reinterpret_cast<const char16_t *>(path.cend()));
}

/**
* @brief compare two files with given filename and return true if they have the same content
*/
Expand Down

0 comments on commit b270adf

Please sign in to comment.