Skip to content

Commit

Permalink
create a permanent log of delete actions
Browse files Browse the repository at this point in the history
will log all deletions with the result of the discovery by the sync
enginre

should enable analyze even long time after such a delete occured

Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Aug 26, 2024
1 parent e4e1bf9 commit c162e2e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/csync/csync.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@
#ifndef _CSYNC_H
#define _CSYNC_H

#include "std/c_private.h"
#include "ocsynclib.h"

#include <sys/stat.h>
#include <QByteArray>

Check failure on line 35 in src/csync/csync.h

View workflow job for this annotation

GitHub Actions / build

src/csync/csync.h:35:10 [clang-diagnostic-error]

'QByteArray' file not found
#include <QLoggingCategory>

#include <cstdint>
#include <sys/stat.h>
#include <sys/types.h>
#include <config_csync.h>
#include <functional>
#include <memory>
#include <QByteArray>

#include "ocsynclib.h"
#include "config_csync.h"
#include "std/c_private.h"
#include "common/remotepermissions.h"

namespace OCC {
Q_DECLARE_LOGGING_CATEGORY(lcPermanentLog)

class SyncJournalFileRecord;

namespace EncryptionStatusEnums {
Expand Down
4 changes: 4 additions & 0 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@ void Application::setupLogging()
#endif

logger->enterNextLogFile();
{
QDir logFolder{logger->logDir()};
logger->setPermanentDeleteLogFile(logFolder.filePath("permanent_delete.log"));
}

qCInfo(lcApplication) << "##################" << _theme->appName()
<< "locale:" << QLocale::system().name()
Expand Down
17 changes: 17 additions & 0 deletions src/libsync/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,23 @@ void ProcessDirectoryJob::processFile(PathTuple path,
item->_previousSize = dbEntry._fileSize;
item->_previousModtime = dbEntry._modtime;

QDebug deleteLogger{&item->_discoveryResult};
deleteLogger.nospace() << "Processing " << path._original
<< " | (db/local/remote)"
<< " | valid: " << dbEntry.isValid() << "/" << hasLocal << "/" << hasServer
<< " | mtime: " << dbEntry._modtime << "/" << localEntry.modtime << "/" << serverEntry.modtime
<< " | size: " << dbEntry._fileSize << "/" << localEntry.size << "/" << serverEntry.size
<< " | etag: " << dbEntry._etag << "//" << serverEntry.etag
<< " | checksum: " << dbEntry._checksumHeader << "//" << serverEntry.checksumHeader
<< " | perm: " << dbEntry._remotePerm << "//" << serverEntry.remotePerm
<< " | fileid: " << dbEntry._fileId << "//" << serverEntry.fileId
<< " | type: " << dbEntry._type << "/" << localEntry.type << "/" << (serverEntry.isDirectory ? ItemTypeDirectory : ItemTypeFile)
<< " | e2ee: " << dbEntry.isE2eEncrypted() << "/" << serverEntry.isE2eEncrypted()
<< " | e2eeMangledName: " << dbEntry.e2eMangledName() << "/" << serverEntry.e2eMangledName
<< " | file lock: " << localFileIsLocked << "//" << serverFileIsLocked
<< " | file lock type: " << localFileLockType << "//" << serverFileLockType
<< " | metadata missing: /" << localEntry.isMetadataMissing << '/';

if (dbEntry._modtime == localEntry.modtime && dbEntry._type == ItemTypeVirtualFile && localEntry.type == ItemTypeFile) {
item->_type = ItemTypeFile;
qCInfo(lcDisco) << "Changing item type from virtual to normal file" << item->_file;
Expand Down
43 changes: 43 additions & 0 deletions src/libsync/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static bool compressLog(const QString &originalName, const QString &targetName)

namespace OCC {

Q_LOGGING_CATEGORY(lcPermanentLog, "nextcloud.log.permanent")

Check warning on line 71 in src/libsync/logger.cpp

View workflow job for this annotation

GitHub Actions / build

src/libsync/logger.cpp:71:1 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'Q_LOGGING_CATEGORY' is non-const and globally accessible, consider making it const

Logger *Logger::instance()
{
static Logger log;
Expand Down Expand Up @@ -163,6 +165,10 @@ void Logger::doLog(QtMsgType type, const QMessageLogContext &ctx, const QString
if (_doFileFlush)
_logstream->flush();
}
if (_permanentDeleteLogStream && strcmp(ctx.category, "nextcloud.log.permanent") == 0) {
(*_permanentDeleteLogStream) << msg << "\n";
_permanentDeleteLogStream->flush();
}
if (type == QtFatalMsg) {
closeNoLock();
#if defined(Q_OS_WIN)
Expand Down Expand Up @@ -197,6 +203,12 @@ void Logger::setLogFile(const QString &name)
setLogFileNoLock(name);
}

void Logger::setPermanentDeleteLogFile(const QString &name)
{
QMutexLocker locker(&_mutex);
setPermanentDeleteLogFileNoLock(name);
}

void Logger::setLogExpire(int expire)
{
_logExpire = expire;
Expand Down Expand Up @@ -362,6 +374,37 @@ void Logger::setLogFileNoLock(const QString &name)
_logstream->setCodec(QTextCodec::codecForName("UTF-8"));
}

void Logger::setPermanentDeleteLogFileNoLock(const QString &name)
{
if (_permanentDeleteLogStream) {
_permanentDeleteLogStream.reset(nullptr);
_permanentDeleteLogFile.close();
}

if (name.isEmpty()) {
return;
}

bool openSucceeded = false;
if (name == QLatin1String("-")) {
openSucceeded = _permanentDeleteLogFile.open(stdout, QIODevice::WriteOnly);
} else {
_permanentDeleteLogFile.setFileName(name);
openSucceeded = _permanentDeleteLogFile.open(QIODevice::WriteOnly);
}

if (!openSucceeded) {
postGuiMessage(tr("Error"),
QString(tr("<nobr>File \"%1\"<br/>cannot be opened for writing.<br/><br/>"
"The log output <b>cannot</b> be saved!</nobr>"))
.arg(name));
return;
}

_permanentDeleteLogStream.reset(new QTextStream(&_permanentDeleteLogFile));
_permanentDeleteLogStream->setCodec(QTextCodec::codecForName("UTF-8"));
}

void Logger::enterNextLogFile()
{
QMutexLocker locker(&_mutex);
Expand Down
5 changes: 5 additions & 0 deletions src/libsync/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class OWNCLOUDSYNC_EXPORT Logger : public QObject
QString logFile() const;
void setLogFile(const QString &name);

void setPermanentDeleteLogFile(const QString &name);

void setLogExpire(int expire);

QString logDir() const;
Expand Down Expand Up @@ -98,6 +100,7 @@ public slots:
void dumpCrashLog();
void enterNextLogFileNoLock();
void setLogFileNoLock(const QString &name);
void setPermanentDeleteLogFileNoLock(const QString &name);

QFile _logFile;
bool _doFileFlush = false;
Expand All @@ -110,6 +113,8 @@ public slots:
QSet<QString> _logRules;
QVector<QString> _crashLog;
int _crashLogIndex = 0;
QFile _permanentDeleteLogFile;
QScopedPointer<QTextStream> _permanentDeleteLogStream;
};

} // namespace OCC
Expand Down
1 change: 1 addition & 0 deletions src/libsync/propagateremotedelete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Q_LOGGING_CATEGORY(lcPropagateRemoteDelete, "nextcloud.sync.propagator.remotedel
void PropagateRemoteDelete::start()
{
qCInfo(lcPropagateRemoteDelete) << "Start propagate remote delete job for" << _item->_file;
qCInfo(lcPermanentLog) << "delete" << _item->_file << _item->_discoveryResult;

if (propagator()->_abortRequested)
return;
Expand Down
1 change: 1 addition & 0 deletions src/libsync/propagatorjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ bool PropagateLocalRemove::removeRecursively(const QString &path)
void PropagateLocalRemove::start()
{
qCInfo(lcPropagateLocalRemove) << "Start propagate local remove job";
qCInfo(lcPermanentLog) << "delete" << _item->_file << _item->_discoveryResult;

_moveToTrash = propagator()->syncOptions()._moveFilesToTrash;

Expand Down
2 changes: 2 additions & 0 deletions src/libsync/syncfileitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ class OWNCLOUDSYNC_EXPORT SyncFileItem

bool _isAnyInvalidCharChild = false;
bool _isAnyCaseClashChild = false;

QString _discoveryResult;
};

inline bool operator<(const SyncFileItemPtr &item1, const SyncFileItemPtr &item2)
Expand Down

0 comments on commit c162e2e

Please sign in to comment.