Skip to content

Commit

Permalink
Merge pull request #347 from Infomaniak/KDESKTOP-1245-Exclude-files-f…
Browse files Browse the repository at this point in the history
…olders-with-name-length-255-characters-2

Kdesktop 1245 exclude files folders with name length 255 characters
  • Loading branch information
ClementKunz authored Oct 18, 2024
2 parents c740c66 + af0d5d2 commit 258aa7d
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 152 deletions.
10 changes: 5 additions & 5 deletions src/libsyncengine/propagation/executor/executorworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void ExecutorWorker::handleCreateOp(SyncOpPtr syncOp, std::shared_ptr<AbstractJo
if (isDehydratedPlaceholder) {
// Blacklist dehydrated placeholder
PlatformInconsistencyCheckerUtility::renameLocalFile(absoluteLocalFilePath,
PlatformInconsistencyCheckerUtility::SuffixTypeBlacklisted);
PlatformInconsistencyCheckerUtility::SuffixType::Blacklisted);

// Remove from update tree
if (!affectedUpdateTree(syncOp)->deleteNode(syncOp->affectedNode())) {
Expand Down Expand Up @@ -509,7 +509,7 @@ bool ExecutorWorker::checkAlreadyExcluded(const SyncPath &absolutePath, const No

// The item already exists, exclude it
exitCode = PlatformInconsistencyCheckerUtility::renameLocalFile(absolutePath,
PlatformInconsistencyCheckerUtility::SuffixTypeBlacklisted);
PlatformInconsistencyCheckerUtility::SuffixType::Blacklisted);
if (exitCode != ExitCode::Ok) {
LOGW_SYNCPAL_WARN(_logger, L"Failed to rename file: " << Utility::formatSyncPath(absolutePath).c_str());
_executorExitCode = exitCode;
Expand Down Expand Up @@ -1969,7 +1969,7 @@ void ExecutorWorker::handleForbiddenAction(SyncOpPtr syncOp, const SyncPath &rel
ignored = true;
removeFromDb = false;
PlatformInconsistencyCheckerUtility::renameLocalFile(absoluteLocalFilePath,
PlatformInconsistencyCheckerUtility::SuffixTypeBlacklisted);
PlatformInconsistencyCheckerUtility::SuffixType::Blacklisted);
break;
}
case OperationType::Move: {
Expand All @@ -1988,7 +1988,7 @@ void ExecutorWorker::handleForbiddenAction(SyncOpPtr syncOp, const SyncPath &rel
// Rename the file so as not to lose any information
SyncPath newSyncPath;
PlatformInconsistencyCheckerUtility::renameLocalFile(
absoluteLocalFilePath, PlatformInconsistencyCheckerUtility::SuffixTypeConflict, &newSyncPath);
absoluteLocalFilePath, PlatformInconsistencyCheckerUtility::SuffixType::Conflict, &newSyncPath);

// Exclude file from sync
if (!_syncPal->vfsFileStatusChanged(newSyncPath, SyncFileStatus::Ignored)) {
Expand Down Expand Up @@ -2668,7 +2668,7 @@ bool ExecutorWorker::runCreateDirJob(SyncOpPtr syncOp, std::shared_ptr<AbstractJ
LOGW_SYNCPAL_WARN(_logger, L"Item: " << Utility::formatSyncPath(localCreateDirJob->destFilePath()).c_str()
<< L" already exist. Blacklisting it on local replica.");
PlatformInconsistencyCheckerUtility::renameLocalFile(_syncPal->localPath() / localCreateDirJob->destFilePath(),
PlatformInconsistencyCheckerUtility::SuffixTypeBlacklisted);
PlatformInconsistencyCheckerUtility::SuffixType::Blacklisted);
}
return false;
} else if (job->exitCode() != ExitCode::Ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ bool ConflictResolverWorker::generateConflictedName(const std::shared_ptr<Node>
bool isOrphanNode /*= false*/) const {
SyncPath absoluteLocalFilePath = _syncPal->localPath() / node->getPath();
newName = PlatformInconsistencyCheckerUtility::instance()->generateNewValidName(
absoluteLocalFilePath, isOrphanNode ? PlatformInconsistencyCheckerUtility::SuffixTypeOrphan
: PlatformInconsistencyCheckerUtility::SuffixTypeConflict);
absoluteLocalFilePath, isOrphanNode ? PlatformInconsistencyCheckerUtility::SuffixType::Orphan
: PlatformInconsistencyCheckerUtility::SuffixType::Conflict);

// Check path size
size_t pathSize = absoluteLocalFilePath.parent_path().native().size() + 1 + newName.size();
if (PlatformInconsistencyCheckerUtility::instance()->checkPathLength(pathSize)) {
if (PlatformInconsistencyCheckerUtility::instance()->isPathTooLong(pathSize)) {
// Path is now too long, file needs to be moved to root directory
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static const char forbiddenFilenameChars[] = {'/', '\0'};
#endif
#endif

#define MAX_NAME_LENGTH 255
static const int maxNameLengh = 255; // Max filename length is uniformized to 255 characters for all platforms and backends

namespace KDC {

Expand All @@ -62,7 +62,7 @@ std::shared_ptr<PlatformInconsistencyCheckerUtility> PlatformInconsistencyChecke

SyncName PlatformInconsistencyCheckerUtility::generateNewValidName(const SyncPath &name, SuffixType suffixType) {
SyncName suffix = generateSuffix(suffixType);
SyncName sub = name.stem().native().substr(0, MAX_NAME_LENGTH - suffix.size() - name.extension().native().size());
const SyncName sub = name.stem().native().substr(0, maxNameLengh - suffix.size() - name.extension().native().size());

#ifdef _WIN32
SyncName nameStr(name.native());
Expand Down Expand Up @@ -90,7 +90,7 @@ ExitCode PlatformInconsistencyCheckerUtility::renameLocalFile(const SyncPath &ab
return moveJob.exitCode();
}

bool PlatformInconsistencyCheckerUtility::checkNameForbiddenChars(const SyncPath &name) {
bool PlatformInconsistencyCheckerUtility::nameHasForbiddenChars(const SyncPath &name) {
for (auto c: forbiddenFilenameChars) {
if (name.native().find(c) != std::string::npos) {
return true;
Expand Down Expand Up @@ -131,12 +131,8 @@ bool PlatformInconsistencyCheckerUtility::fixNameWithBackslash(const SyncName &n
}
#endif

bool PlatformInconsistencyCheckerUtility::checkNameSize(const SyncName &name) {
if (name.size() > MAX_NAME_LENGTH) {
return true;
}

return false;
bool PlatformInconsistencyCheckerUtility::isNameTooLong(const SyncName &name) const {
return name.size() > maxNameLengh;
}

// return false if the file name is ok
Expand Down Expand Up @@ -171,7 +167,7 @@ bool PlatformInconsistencyCheckerUtility::checkReservedNames(const SyncName &nam
return false;
}

bool PlatformInconsistencyCheckerUtility::checkPathLength(size_t pathSize) {
bool PlatformInconsistencyCheckerUtility::isPathTooLong(size_t pathSize) {
return pathSize > _maxPathLength;
}

Expand All @@ -189,7 +185,7 @@ void PlatformInconsistencyCheckerUtility::setMaxPath() {
_maxPathLength = CommonUtility::maxPathLength();
}

SyncName PlatformInconsistencyCheckerUtility::generateSuffix(SuffixType suffixType /*= SuffixTypeRename*/) {
SyncName PlatformInconsistencyCheckerUtility::generateSuffix(SuffixType suffixType) {
std::time_t now = std::time(nullptr);
std::tm tm = *std::localtime(&now);

Expand All @@ -198,16 +194,13 @@ SyncName PlatformInconsistencyCheckerUtility::generateSuffix(SuffixType suffixTy
ss << std::put_time(&tm, Str("%Y%m%d_%H%M%S"));

switch (suffixType) {
case SuffixTypeRename:
suffix = Str("_renamed_");
break;
case SuffixTypeConflict:
case SuffixType::Conflict:
suffix = Str("_conflict_");
break;
case SuffixTypeOrphan:
case SuffixType::Orphan:
suffix = Str("_orphan_");
break;
case SuffixTypeBlacklisted:
case SuffixType::Blacklisted:
suffix = Str("_blacklisted_");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,27 @@ namespace KDC {

class PlatformInconsistencyCheckerUtility {
public:
typedef enum { SuffixTypeRename, SuffixTypeConflict, SuffixTypeOrphan, SuffixTypeBlacklisted } SuffixType;
enum class SuffixType { Conflict, Orphan, Blacklisted };

public:
static std::shared_ptr<PlatformInconsistencyCheckerUtility> instance();

SyncName generateNewValidName(const SyncPath &name, SuffixType suffixType);
static ExitCode renameLocalFile(const SyncPath &absoluteLocalPath, SuffixType suffixType, SyncPath *newPathPtr = nullptr);
bool isNameTooLong(const SyncName &name) const;
bool isPathTooLong(size_t pathSize);
bool nameHasForbiddenChars(const SyncPath &name);

bool checkNameForbiddenChars(const SyncPath &name);
#ifdef _WIN32
bool fixNameWithBackslash(const SyncName &name, SyncName &newName);
#endif
bool checkNameSize(const SyncName &name);
bool checkReservedNames(const SyncName &name);
bool checkPathLength(size_t pathSize);

SyncName generateNewValidName(const SyncPath &name, SuffixType suffixType);
static ExitCode renameLocalFile(const SyncPath &absoluteLocalPath, SuffixType suffixType, SyncPath *newPathPtr = nullptr);
private:
PlatformInconsistencyCheckerUtility();

SyncName charToHex(unsigned int c);
void setMaxPath();
SyncName generateSuffix(SuffixType suffixType = SuffixTypeRename);
SyncName generateSuffix(SuffixType suffixType);

static std::shared_ptr<PlatformInconsistencyCheckerUtility> _instance;
static size_t _maxPathLength;
Expand Down
Loading

0 comments on commit 258aa7d

Please sign in to comment.