Skip to content

Commit

Permalink
NXDRIVE-2694: Fix bad handling of remote changes (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1win authored Aug 4, 2021
1 parent 7d9b7d7 commit f5ddd0c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/changes/5.2.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Release date: `2021-xx-xx`

## Technical Changes

- Added `EngineDAO.get_local_roots_names()`
- Added `Options.sync_root_max_level`
- Added `Remote.expand_sync_root_name()`
- Added `Remote.is_sync_root()`
Expand Down
10 changes: 9 additions & 1 deletion nxdrive/client/remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def expand_sync_root_name(self, sync_root: RemoteFileInfo) -> RemoteFileInfo:
"""
glue = " - "
level = 0
limit = 50 - len(glue)
limit = 49 - len(glue)
uid = sync_root.uid.split("#")[-1]

# Be sure the current name is shortened
Expand All @@ -594,6 +594,14 @@ def expand_sync_root_name(self, sync_root: RemoteFileInfo) -> RemoteFileInfo:
sync_root.name = f"{name}{glue}{sync_root.name}"
level += 1

local_roots = self.dao.get_local_roots_names()
if sync_root.name not in local_roots:
return sync_root
for n in range(1, 100):
if f"{sync_root.name}_{n}" not in local_roots:
sync_root.name = f"{sync_root.name}_{n}"
return sync_root
sync_root.name = f"{sync_root.name}_{n}"
return sync_root

def get_fs_info(
Expand Down
9 changes: 8 additions & 1 deletion nxdrive/dao/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from nuxeo.utils import get_digest_algorithm

from ..client.local import FileInfo
from ..constants import ROOT, UNACCESSIBLE_HASH, WINDOWS, TransferStatus
from ..constants import ROOT, SYNC_ROOT, UNACCESSIBLE_HASH, WINDOWS, TransferStatus
from ..exceptions import UnknownPairState
from ..objects import (
DocPair,
Expand Down Expand Up @@ -1176,6 +1176,13 @@ def get_local_children(self, path: Path, /) -> DocPairs:
"SELECT * FROM States WHERE local_parent_path = ?", (path,)
).fetchall()

def get_local_roots_names(self) -> List[str]:
c = self._get_read_connection().cursor()
root_list = c.execute(
f"SELECT local_name FROM States WHERE remote_parent_path = '{SYNC_ROOT}'"
).fetchall()
return [item[0] for item in root_list]

def get_states_from_partial_local(
self, path: Path, /, *, strict: bool = True
) -> DocPairs:
Expand Down
13 changes: 9 additions & 4 deletions nxdrive/engine/watcher/remote_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DOCUMENT_LOCKED,
DOCUMENT_MOVED,
DOCUMENT_UNLOCKED,
ROOT_REGISTERED,
SECURITY_UPDATED_EVENT,
)

Expand Down Expand Up @@ -908,10 +909,6 @@ def _update_remote_states(self) -> None:
# Perform a regular document update on a document
# that has been updated, renamed or moved

# Keep the sync root name format as expected
if self.engine.remote.is_sync_root(new_info):
self.engine.remote.expand_sync_root_name(new_info)

if doc_pair.remote_state != "created" and any(
(
new_info.digest != doc_pair.remote_digest,
Expand Down Expand Up @@ -1009,6 +1006,14 @@ def _update_remote_states(self) -> None:
if new_info and not updated:
# Handle new document creations
created = False

# Keep the sync root name format as expected
if (
self.engine.remote.is_sync_root(new_info)
and event_id == ROOT_REGISTERED
):
self.engine.remote.expand_sync_root_name(new_info)

parent_pairs = self.dao.get_states_from_remote(new_info.parent_uid)
for parent_pair in parent_pairs:
match_pair = self._find_remote_child_match_or_create(
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_expand_sync_root_name_length(option, manager_factory, obj_factory):
potential_names = []
for num in range(option + 1):
title = "folder" + "r" * 50 + f" {num}" # > 50 chars
potential_names.append(shortify(title, limit=47))
potential_names.append(shortify(title, limit=46))
doc = obj_factory(title=title, parent=parent, user=remote.user_id)
parent = doc.path

Expand Down

0 comments on commit f5ddd0c

Please sign in to comment.