Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace src/csync/std/c_time.cpp with std::filesystem::last_write_time #9912

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
115 changes: 0 additions & 115 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.

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

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

#ifdef Q_OS_WIN32
#include "common/utility_win.h"
#include <winsock2.h>
#endif

#include <filesystem>

namespace OCC {

bool FileSystem::fileEquals(const QString &fn1, const QString &fn2)
Expand Down Expand Up @@ -62,28 +63,25 @@ 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
<< "with csync, using QFileInfo:" << result;
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) {
qCWarning(lcFileSystem) << "Could not get modification time for" << filename << error.value() << QString::fromStdString(error.message());
Q_ASSERT(false);
return {};
}
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) {
qCWarning(lcFileSystem) << "Error setting mtime for" << filename
<< "failed: rc" << rc << ", error message:" << strerror(errno);
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" << error.value()
<< ", errno:" << QString::fromStdString(error.message());
Q_ASSERT(false);
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/libsync/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// Chain in the base include and extend the namespace
#include "common/filesystembase.h"

#include <filesystem>

class QFile;

namespace OCC {
Expand All @@ -38,6 +40,12 @@ 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