44from typing import TypeVar
55
66from beets import context , util
7+ from beets .util import path_as_posix
78
89MaybeBytes = TypeVar ("MaybeBytes" , bytes , None )
10+ DB_PATH_SEP = b"/"
911
1012
1113def _is_same_path_or_child (path : bytes , music_dir : bytes ) -> bool :
@@ -17,6 +19,16 @@ def _is_same_path_or_child(path: bytes, music_dir: bytes) -> bool:
1719 )
1820
1921
22+ def _to_db_path (path : bytes ) -> bytes :
23+ """Store relative paths with a platform-neutral separator."""
24+ return path_as_posix (path )
25+
26+
27+ def _from_db_path (path : bytes ) -> bytes :
28+ """Convert a stored relative path to the current platform syntax."""
29+ return path .replace (DB_PATH_SEP , os .fsencode (os .sep ))
30+
31+
2032def normalize_path_for_db (path : MaybeBytes ) -> MaybeBytes :
2133 """Convert an absolute library path to its database representation."""
2234 if not path or not os .path .isabs (path ):
@@ -27,7 +39,7 @@ def normalize_path_for_db(path: MaybeBytes) -> MaybeBytes:
2739 return path
2840
2941 if _is_same_path_or_child (path , music_dir ):
30- return os .path .relpath (path , music_dir )
42+ return _to_db_path ( os .path .relpath (path , music_dir ) )
3143
3244 return path
3345
@@ -36,6 +48,6 @@ def expand_path_from_db(path: bytes) -> bytes:
3648 """Convert a stored database path to an absolute library path."""
3749 music_dir = context .get_music_dir ()
3850 if path and not os .path .isabs (path ) and music_dir :
39- return util .normpath (os .path .join (music_dir , path ))
51+ return util .normpath (os .path .join (music_dir , _from_db_path ( path ) ))
4052
4153 return path
0 commit comments