@@ -317,22 +317,17 @@ def _sftp_channel(self):
317317 meta = channel .get_channel ()
318318 meta .set_name (channel_name )
319319 log .debug (f'Channel Name: [{ channel_name } ]' )
320+ self ._cache .__dict__ .setdefault ('cwd' , self ._default_path )
320321 self ._channels [channel_name ] = {
321322 'busy' : True , 'channel' : channel , 'meta' : meta
322323 }
323324
324- if self ._default_path :
325- self ._cache .__dict__ .setdefault ('cwd' , self ._default_path )
326-
327325 meta .settimeout (self ._timeout )
328326
329327 if self ._cache .cwd :
330328 channel .chdir (drivedrop (self ._cache .cwd ))
331329 log .info (f'Current Working Directory: [{ self ._cache .cwd } ]' )
332330
333- if not meta .closed :
334- self ._channels [channel_name ]['busy' ] = False
335-
336331 yield channel
337332 except IOError as err :
338333 log .error (f'Failed Directory Change: [{ self ._cache .cwd } ]' )
@@ -341,6 +336,9 @@ def _sftp_channel(self):
341336 if channel :
342337 channel .close ()
343338 raise err
339+ finally :
340+ if not meta .closed :
341+ self ._channels [channel_name ]['busy' ] = False
344342
345343 def _start_transport (self , host , port ):
346344 '''Start the transport and set connection options if specified.'''
@@ -1095,14 +1093,7 @@ def cd(self, remotepath=None):
10951093
10961094 try :
10971095 if remotepath is not None :
1098- if not all ([
1099- PurePosixPath (remotepath ).root ,
1100- PureWindowsPath (remotepath ).root
1101- ]):
1102- cwd = Path (original_path ).joinpath (remotepath ).as_posix ()
1103- self .chdir (cwd )
1104- else :
1105- self .chdir (remotepath )
1096+ self .chdir (remotepath )
11061097 yield
11071098 except Exception as err :
11081099 raise err
@@ -1119,7 +1110,15 @@ def chdir(self, remotepath):
11191110 :raises: IOError, if path does not exist
11201111 '''
11211112 with self ._sftp_channel () as channel :
1122- channel .chdir (drivedrop (remotepath ))
1113+ if not all ([
1114+ PurePosixPath (remotepath ).root ,
1115+ PureWindowsPath (remotepath ).root
1116+ ]) and self ._cache .cwd :
1117+ cwd = Path (self ._cache .cwd ).joinpath (remotepath ).as_posix ()
1118+ else :
1119+ cwd = drivedrop (remotepath )
1120+
1121+ channel .chdir (cwd )
11231122 self ._cache .__dict__ .setdefault (
11241123 'cwd' , drivedrop (channel .normalize ('.' ))
11251124 )
0 commit comments