Skip to content

Commit 52894d0

Browse files
authored
Fix the computePath() to track the parental path anchor (#3417)
* Fix the computePath() to track the parental path anchor when a Shared Folder is relocated with a deeper path
1 parent 5ee95b8 commit 52894d0

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/itemdb.d

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -968,15 +968,21 @@ final class ItemDatabase {
968968
string driveId = driveIdInput.idup;
969969
string id = itemIdInput.idup;
970970
Item item;
971-
971+
972+
// Remember the highest non-root node we saw in this drive
973+
string anchorCandidateDriveId;
974+
string anchorCandidateItemId;
975+
976+
// DB Statements
972977
auto s = db.prepare("SELECT * FROM item WHERE driveId = ?1 AND id = ?2");
973978
auto s2 = db.prepare("SELECT driveId, id FROM item WHERE remoteDriveId = ?1 AND remoteId = ?2");
974979

975980
scope(exit) {
976981
s.finalise(); // Ensure that the prepared statement is finalised after execution.
977982
s2.finalise(); // Ensure that the prepared statement is finalised after execution.
978983
}
979-
984+
985+
// Attempt to compute the path based on the elements provided
980986
try {
981987
while (true) {
982988
s.bind(1, driveId);
@@ -986,38 +992,51 @@ final class ItemDatabase {
986992
if (!r.empty) {
987993
item = buildItem(r);
988994

989-
// Skip only if name == "root" AND item.type == ItemType.root
990-
bool skipAppend = (item.name == "root") && (item.type == ItemType.root);
995+
// Track the highest non-root row we encounter
996+
if (item.type != ItemType.root) {
997+
anchorCandidateDriveId = driveId;
998+
anchorCandidateItemId = item.id;
999+
}
9911000

1001+
// Build path (your existing behaviour)
1002+
const bool skipAppend = (item.name == "root") && (item.type == ItemType.root);
9921003
if (!skipAppend) {
9931004
if (item.type == ItemType.remote) {
994-
ptrdiff_t idx = indexOf(path, '/');
995-
path = idx >= 0 ? item.name ~ path[idx .. $] : item.name;
1005+
// replace first segment with remote name
1006+
auto idx = indexOf(path, '/');
1007+
path = (idx >= 0) ? item.name ~ path[idx .. $] : item.name;
9961008
} else {
9971009
path = path.length ? item.name ~ "/" ~ path : item.name;
9981010
}
9991011
}
10001012

1001-
// Move up one level
1013+
// Move up one level (within the same drive)
10021014
id = item.parentId;
10031015

1004-
// Check for relocation
1016+
// Check for relocation and handle the relocation
10051017
if (item.type == ItemType.root && item.relocDriveId !is null && item.relocParentId !is null) {
10061018
driveId = item.relocDriveId;
10071019
id = item.relocParentId;
10081020
}
10091021
} else {
1010-
// Handle remote-to-dir link
1022+
// We fell off the top (id == null). Try to jump to the anchor (mount point).
10111023
if (id == null) {
1012-
s2.bind(1, item.driveId);
1013-
s2.bind(2, item.id);
1014-
auto r2 = s2.exec();
1015-
1016-
if (r2.empty) {
1017-
break;
1024+
// Use the top-most NON-ROOT we saw, not the root we just processed
1025+
if (anchorCandidateItemId.length) {
1026+
s2.bind(1, anchorCandidateDriveId); // remoteDriveId
1027+
s2.bind(2, anchorCandidateItemId); // remoteId (top-most folder)
1028+
auto r2 = s2.exec();
1029+
if (r2.empty) {
1030+
break; // no anchor -> done
1031+
} else {
1032+
// Jump into the drive that contains the remote mount point
1033+
driveId = r2.front[0].dup;
1034+
id = r2.front[1].dup;
1035+
// loop continues; next iteration will fetch the 'remote' row
1036+
}
10181037
} else {
1019-
driveId = r2.front[0].dup;
1020-
id = r2.front[1].dup;
1038+
// no candidate (single item or broken tree)
1039+
break;
10211040
}
10221041
} else {
10231042
// broken database tree

src/sync.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4419,7 +4419,7 @@ class SyncEngine {
44194419
if (debugLogging) {
44204420
string initialComputeLogMessage = format("Attempting to calculate local filesystem path for '%s' and '%s'", thisDriveId, thisItemId);
44214421
addLogEntry(initialComputeLogMessage, ["debug"]);
4422-
}
4422+
}
44234423

44244424
// Perform the original calculation of the path using the values provided
44254425
try {

0 commit comments

Comments
 (0)