Skip to content

Commit

Permalink
Fix review comments.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Feb 12, 2024
1 parent 3d20058 commit 4b37f7a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
60 changes: 41 additions & 19 deletions src/libsync/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

namespace
{
constexpr auto editorForSigningName = "PowerPDF";
constexpr auto fileExtensionToCheckIfOpenForSigning = ".pdf";
constexpr const char *editorNamesForDelayedUpload[] = {"PowerPDF"};
constexpr const char *fileExtensionsToCheckIfOpenForSigning[] = {".pdf"};
constexpr auto delayIntervalForSyncRetryForOpenedForSigningFilesSeconds = 60;
}

Expand Down Expand Up @@ -1028,13 +1028,17 @@ void ProcessDirectoryJob::processFileAnalyzeLocalInfo(
item->_status = SyncFileItem::Status::NormalError;
}

if (isAnyEditorKeepingFileBusy(item, path)) {
item->_instruction = CSYNC_INSTRUCTION_ERROR;
qCInfo(lcDisco) << "Failed, because it is open in the editor." << item->_file << "direction" << item->_direction << editorForSigningName;
item->_errorString = tr("Could not upload file, because it is open in \"%1\".").arg(editorForSigningName);
item->_status = SyncFileItem::Status::SoftError;
_discoveryData->_anotherSyncNeeded = true;
_discoveryData->_filesNeedingScheduledSync.insert(path._original, delayIntervalForSyncRetryForOpenedForSigningFilesSeconds);
{
const auto foundEditorsKeepingFileBusy = queryEditorsKeepingFileBusy(item, path);
if (!foundEditorsKeepingFileBusy.isEmpty()) {
item->_instruction = CSYNC_INSTRUCTION_ERROR;
const auto editorsString = foundEditorsKeepingFileBusy.join(", ");
qCInfo(lcDisco) << "Failed, because it is open in the editor." << item->_file << "direction" << item->_direction << editorsString;
item->_errorString = tr("Could not upload file, because it is open in \"%1\".").arg(editorsString);
item->_status = SyncFileItem::Status::SoftError;
_discoveryData->_anotherSyncNeeded = true;
_discoveryData->_filesNeedingScheduledSync.insert(path._original, delayIntervalForSyncRetryForOpenedForSigningFilesSeconds);
}
}

if (dbEntry.isValid() && item->isDirectory()) {
Expand Down Expand Up @@ -1854,23 +1858,41 @@ bool ProcessDirectoryJob::isRename(const QString &originalPath) const
*/
}

bool ProcessDirectoryJob::isAnyEditorKeepingFileBusy(const SyncFileItemPtr &item, const PathTuple &path) const
QStringList ProcessDirectoryJob::queryEditorsKeepingFileBusy(const SyncFileItemPtr &item, const PathTuple &path) const
{
if (item->isDirectory() || item->_direction != SyncFileItem::Up || !path._local.endsWith(fileExtensionToCheckIfOpenForSigning)) {
return false;
QStringList matchingEditorsKeepingFileBusy;

if (item->isDirectory() || item->_direction != SyncFileItem::Up) {
return matchingEditorsKeepingFileBusy;
}

const auto isMatchingFileExtension = std::find_if(std::cbegin(fileExtensionsToCheckIfOpenForSigning), std::cend(fileExtensionsToCheckIfOpenForSigning),
[path](const auto &matchingExtension) {
return path._local.endsWith(matchingExtension, Qt::CaseInsensitive);
}) != std::cend(fileExtensionsToCheckIfOpenForSigning);

if (!isMatchingFileExtension) {
return matchingEditorsKeepingFileBusy;
}

const QString fullLocalPath(_discoveryData->_localDir + path._local);
const auto editorsKeepingFileBusy = Utility::queryProcessInfosKeepingFileOpen(fullLocalPath);

const auto isAnyEditorsKeepingFileBusyFound = std::find_if(
std::cbegin(editorsKeepingFileBusy),
std::cend(editorsKeepingFileBusy),
[](const Utility::ProcessInfosForOpenFile &processInfosForFile) {
return processInfosForFile.processName.startsWith(editorForSigningName, Qt::CaseInsensitive);
}) != std::cend(editorsKeepingFileBusy);
for (const auto &detectedEditorName : editorsKeepingFileBusy) {
const auto isMatchingEditorFound = std::find_if(std::cbegin(editorNamesForDelayedUpload), std::cend(editorNamesForDelayedUpload),
[detectedEditorName](const auto &matchingEditorName) {
return detectedEditorName.processName.startsWith(matchingEditorName, Qt::CaseInsensitive);
}) != std::cend(editorNamesForDelayedUpload);
if (isMatchingEditorFound) {
matchingEditorsKeepingFileBusy.push_back(detectedEditorName.processName);
}
}

if (!matchingEditorsKeepingFileBusy.isEmpty()) {
matchingEditorsKeepingFileBusy.push_back("PowerPDF.exe");
}

return isAnyEditorsKeepingFileBusyFound;
return matchingEditorsKeepingFileBusy;
}

auto ProcessDirectoryJob::checkMovePermissions(RemotePermissions srcPerm, const QString &srcPath,
Expand Down
2 changes: 1 addition & 1 deletion src/libsync/discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ProcessDirectoryJob : public QObject

[[nodiscard]] bool isRename(const QString &originalPath) const;

[[nodiscard]] bool isAnyEditorKeepingFileBusy(const SyncFileItemPtr &item, const PathTuple &path) const;
[[nodiscard]] QStringList queryEditorsKeepingFileBusy(const SyncFileItemPtr &item, const PathTuple &path) const;

struct MovePermissionResult
{
Expand Down

0 comments on commit 4b37f7a

Please sign in to comment.