125125}
126126
127127
128- def _path (path : Path ) -> str :
128+ def _adapt_path (path : Path , / ) -> str :
129129 """
130130 Return the string needed to work with data from the database from a given *path*.
131131 It is used across the whole file and also in the SQLite adapter to convert
132132 a Path object to str before insertion into database.
133+
134+ Note: The starting forward-slash will be automatically added if not present
135+ and if the *path* is not absolute (Direct Transfer paths for instance).
133136 """
134137 posix_path = path .as_posix ()
135- if posix_path == "." : # Note: ROOT.as_posix() and Path().as_posix() will return "."
138+ # Note: ROOT.as_posix(), Path.path().as_posix(), Path.path("").as_posix() and Path(".").as_posix() will return "."
139+ if posix_path == "." :
136140 return "/"
137141 if posix_path [0 ] != "/" and not path .is_absolute ():
138142 return f"/{ posix_path } "
139143 return posix_path
140144
141145
142- register_adapter (WindowsPath if WINDOWS else PosixPath , _path )
146+ register_adapter (WindowsPath if WINDOWS else PosixPath , _adapt_path )
143147
144148
145149class AutoRetryCursor (Cursor ):
@@ -1207,10 +1211,6 @@ def insert_local_state(
12071211 c = con .cursor ()
12081212 pair_state = PAIR_STATES [("created" , "unknown" )]
12091213
1210- # parent_path is None when registering a new sync root
1211- local_path = _path (info .path ) if parent_path else "/"
1212- local_parent_path = _path (parent_path ) if parent_path else None
1213-
12141214 c .execute (
12151215 "INSERT INTO States "
12161216 "(last_local_updated, local_digest, local_path, "
@@ -1220,8 +1220,8 @@ def insert_local_state(
12201220 (
12211221 info .last_modification_time ,
12221222 digest ,
1223- local_path ,
1224- local_parent_path ,
1223+ info . path ,
1224+ parent_path ,
12251225 info .path .name ,
12261226 info .folderish ,
12271227 info .size ,
@@ -1231,14 +1231,14 @@ def insert_local_state(
12311231 row_id = c .lastrowid
12321232 parent = (
12331233 c .execute (
1234- "SELECT * FROM States WHERE local_path = ?" , (local_parent_path ,)
1234+ "SELECT * FROM States WHERE local_path = ?" , (parent_path ,)
12351235 ).fetchone ()
1236- if local_parent_path
1236+ if parent_path
12371237 else None
12381238 )
12391239
12401240 # Don't queue if parent is not yet created
1241- if (parent is None and local_parent_path is None ) or (
1241+ if (parent is None and parent_path is None ) or (
12421242 parent and parent .pair_state != "locally_created"
12431243 ):
12441244 self ._queue_pair_state (row_id , info .folderish , pair_state )
@@ -1443,7 +1443,7 @@ def update_local_state(
14431443 log .debug (f"Increasing version to { row .version + 1 } for pair { row !r} " )
14441444
14451445 with self .lock :
1446- parent_path = _path ( info .path .parent )
1446+ parent_path = info .path .parent
14471447 con = self ._get_write_connection ()
14481448 c = con .cursor ()
14491449 c .execute (
@@ -1461,7 +1461,7 @@ def update_local_state(
14611461 (
14621462 info .last_modification_time ,
14631463 row .local_digest ,
1464- _path ( info .path ) ,
1464+ info .path ,
14651465 parent_path ,
14661466 info .path .name ,
14671467 row .local_state ,
@@ -1608,19 +1608,18 @@ def get_errors(self, *, limit: int = 3) -> DocPairs:
16081608 def get_local_children (self , path : Path , / ) -> DocPairs :
16091609 c = self ._get_read_connection ().cursor ()
16101610 return c .execute (
1611- "SELECT * FROM States WHERE local_parent_path = ?" , (_path ( path ) ,)
1611+ "SELECT * FROM States WHERE local_parent_path = ?" , (path ,)
16121612 ).fetchall ()
16131613
16141614 def get_states_from_partial_local (
16151615 self , path : Path , / , * , strict : bool = True
16161616 ) -> DocPairs :
16171617 c = self ._get_read_connection ().cursor ()
16181618
1619- local_path = _path (path )
1620- if path == ROOT :
1621- local_path += "%"
1622- else :
1623- local_path += "/%" if strict else "%"
1619+ local_path = _adapt_path (path )
1620+ if local_path [- 1 ] != "/" and strict :
1621+ local_path += "/"
1622+ local_path += "%"
16241623
16251624 return c .execute (
16261625 "SELECT * FROM States WHERE local_path LIKE ?" , (local_path ,)
@@ -1677,7 +1676,7 @@ def get_state_from_id(
16771676 return state
16781677
16791678 def _get_recursive_condition (self , doc_pair : DocPair , / ) -> str :
1680- path = self ._escape (_path (doc_pair .local_path ))
1679+ path = self ._escape (_adapt_path (doc_pair .local_path ))
16811680 res = (
16821681 f" WHERE (local_parent_path LIKE '{ path } /%'"
16831682 f" OR local_parent_path = '{ path } ')"
@@ -1701,8 +1700,8 @@ def replace_local_paths(self, old_path: Path, new_path: Path) -> None:
17011700 (including starting and ending slashes).
17021701 """
17031702
1704- old = f"{ _path (old_path )} /"
1705- new = f"{ _path (new_path )} /"
1703+ old = f"{ _adapt_path (old_path )} /"
1704+ new = f"{ _adapt_path (new_path )} /"
17061705 log .debug (f"Updating all local paths from { old !r} to { new !r} " )
17071706
17081707 with self .lock :
@@ -1746,8 +1745,8 @@ def update_local_parent_path(
17461745 con = self ._get_write_connection ()
17471746 c = con .cursor ()
17481747 if doc_pair .folderish :
1749- path = self ._escape (_path (new_path / new_name ))
1750- count = len (self ._escape (_path (doc_pair .local_path )))
1748+ path = self ._escape (_adapt_path (new_path / new_name ))
1749+ count = len (self ._escape (_adapt_path (doc_pair .local_path )))
17511750 query = (
17521751 "UPDATE States"
17531752 f" SET local_parent_path = '{ path } '"
@@ -1760,7 +1759,7 @@ def update_local_parent_path(
17601759 # Don't need to update the path as it is refresh later
17611760 c .execute (
17621761 "UPDATE States SET local_parent_path = ? WHERE id = ?" ,
1763- (_path ( new_path ) , doc_pair .id ),
1762+ (new_path , doc_pair .id ),
17641763 )
17651764
17661765 def update_remote_parent_path_dt (
@@ -1778,14 +1777,14 @@ def update_remote_parent_path_dt(
17781777 doc_pairs = c .execute (
17791778 "SELECT * FROM States WHERE local_state = 'direct' AND remote_state = 'todo'"
17801779 " AND local_parent_path = ?" ,
1781- (_path ( local_parent_path ) ,),
1780+ (local_parent_path ,),
17821781 ).fetchall ()
17831782
17841783 c .execute (
17851784 "UPDATE States SET remote_state = 'unknown', remote_parent_path = ?,"
17861785 " remote_parent_ref = ?, processor = 0"
17871786 " WHERE local_state = 'direct' AND remote_state = 'todo' AND local_parent_path = ?" ,
1788- (remote_parent_path , remote_parent_ref , _path ( local_parent_path ) ),
1787+ (remote_parent_path , remote_parent_ref , local_parent_path ),
17891788 )
17901789
17911790 for doc_pair in doc_pairs :
@@ -1844,7 +1843,7 @@ def remove_state_children(
18441843 def get_state_from_local (self , path : Path , / ) -> Optional [DocPair ]:
18451844 c = self ._get_read_connection ().cursor ()
18461845 return c .execute (
1847- "SELECT * FROM States WHERE local_path = ?" , (_path ( path ) ,)
1846+ "SELECT * FROM States WHERE local_path = ?" , (path ,)
18481847 ).fetchone ()
18491848
18501849 def insert_remote_state (
@@ -1884,8 +1883,8 @@ def insert_remote_state(
18841883 info .digest ,
18851884 info .folderish ,
18861885 info .last_contributor ,
1887- _path ( local_path ) ,
1888- _path ( local_parent_path ) ,
1886+ local_path ,
1887+ local_parent_path ,
18891888 pair_state ,
18901889 info .name ,
18911890 info .creation_time ,
@@ -1914,7 +1913,7 @@ def queue_children(self, row: DocPair, /) -> None:
19141913 " WHERE remote_parent_ref = ?"
19151914 " OR local_parent_path = ?"
19161915 " AND " + self ._get_to_sync_condition (),
1917- (row .remote_ref , _path ( row .local_path ) ),
1916+ (row .remote_ref , row .local_path ),
19181917 ).fetchall ()
19191918 if children :
19201919 log .info (f"Queuing { len (children )} children of { row } " )
@@ -2046,7 +2045,7 @@ def unset_unsychronised(self, row: DocPair, /) -> None:
20462045 row .remote_state ,
20472046 row .pair_state ,
20482047 datetime .utcnow (),
2049- f"{ _path (row .local_path )} %" ,
2048+ f"{ _adapt_path (row .local_path )} %" ,
20502049 ),
20512050 )
20522051
@@ -2120,7 +2119,7 @@ def synchronize_state(
21202119 row .pair_state ,
21212120 datetime .utcnow (),
21222121 row .id ,
2123- _path ( row .local_path ) ,
2122+ row .local_path ,
21242123 row .remote_name ,
21252124 row .remote_ref ,
21262125 row .remote_parent_ref ,
0 commit comments