@@ -968,15 +968,21 @@ final class ItemDatabase {
968
968
string driveId = driveIdInput.idup;
969
969
string id = itemIdInput.idup;
970
970
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
972
977
auto s = db.prepare(" SELECT * FROM item WHERE driveId = ?1 AND id = ?2" );
973
978
auto s2 = db.prepare(" SELECT driveId, id FROM item WHERE remoteDriveId = ?1 AND remoteId = ?2" );
974
979
975
980
scope (exit) {
976
981
s.finalise(); // Ensure that the prepared statement is finalised after execution.
977
982
s2.finalise(); // Ensure that the prepared statement is finalised after execution.
978
983
}
979
-
984
+
985
+ // Attempt to compute the path based on the elements provided
980
986
try {
981
987
while (true ) {
982
988
s.bind(1 , driveId);
@@ -986,38 +992,51 @@ final class ItemDatabase {
986
992
if (! r.empty) {
987
993
item = buildItem(r);
988
994
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
+ }
991
1000
1001
+ // Build path (your existing behaviour)
1002
+ const bool skipAppend = (item.name == " root" ) && (item.type == ItemType.root);
992
1003
if (! skipAppend) {
993
1004
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;
996
1008
} else {
997
1009
path = path.length ? item.name ~ " /" ~ path : item.name;
998
1010
}
999
1011
}
1000
1012
1001
- // Move up one level
1013
+ // Move up one level (within the same drive)
1002
1014
id = item.parentId;
1003
1015
1004
- // Check for relocation
1016
+ // Check for relocation and handle the relocation
1005
1017
if (item.type == ItemType.root && item.relocDriveId ! is null && item.relocParentId ! is null ) {
1006
1018
driveId = item.relocDriveId;
1007
1019
id = item.relocParentId;
1008
1020
}
1009
1021
} else {
1010
- // Handle remote-to-dir link
1022
+ // We fell off the top (id == null). Try to jump to the anchor (mount point).
1011
1023
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
+ }
1018
1037
} else {
1019
- driveId = r2.front[ 0 ]. dup ;
1020
- id = r2.front[ 1 ]. dup ;
1038
+ // no candidate (single item or broken tree)
1039
+ break ;
1021
1040
}
1022
1041
} else {
1023
1042
// broken database tree
0 commit comments