@@ -100,10 +100,41 @@ def chmod_rw(p: str) -> None:
100100 return True
101101
102102
103+ def ensure_extended_length_path (path : Path ) -> Path :
104+ """Get the extended-length version of a path (Windows).
105+
106+ On Windows, by default, the maximum length of a path (MAX_PATH) is 260
107+ characters, and operations on paths longer than that fail. But it is possible
108+ to overcome this by converting the path to "extended-length" form before
109+ performing the operation:
110+ https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
111+
112+ On Windows, this function returns the extended-length absolute version of path.
113+ On other platforms it returns path unchanged.
114+ """
115+ if sys .platform .startswith ("win32" ):
116+ path = path .resolve ()
117+ path = Path (get_extended_length_path_str (str (path )))
118+ return path
119+
120+
121+ def get_extended_length_path_str (path : str ) -> str :
122+ """Converts to extended length path as a str"""
123+ long_path_prefix = "\\ \\ ?\\ "
124+ unc_long_path_prefix = "\\ \\ ?\\ UNC\\ "
125+ if path .startswith ((long_path_prefix , unc_long_path_prefix )):
126+ return path
127+ # UNC
128+ if path .startswith ("\\ \\ " ):
129+ return unc_long_path_prefix + path [2 :]
130+ return long_path_prefix + path
131+
132+
103133def rm_rf (path : Path ) -> None :
104134 """Remove the path contents recursively, even if some elements
105135 are read-only.
106136 """
137+ path = ensure_extended_length_path (path )
107138 onerror = partial (on_rm_rf_error , start_path = path )
108139 shutil .rmtree (str (path ), onerror = onerror )
109140
@@ -220,6 +251,7 @@ def cleanup_on_exit(lock_path: Path = lock_path, original_pid: int = pid) -> Non
220251
221252def maybe_delete_a_numbered_dir (path : Path ) -> None :
222253 """removes a numbered directory if its lock can be obtained and it does not seem to be in use"""
254+ path = ensure_extended_length_path (path )
223255 lock_path = None
224256 try :
225257 lock_path = create_cleanup_lock (path )
0 commit comments