Skip to content

Commit cebc9b0

Browse files
committed
tools/mpremote: Fix absolute path usage in remote mounted VFS.
Prior to this fix the current working path in the remote VFS would always be prepended to the requested path to get the full path, even if the requested path was already absolute, ie starting with "/". So `os.chdir("/remote/dir1")` would set the working path to "/dir1/", and a subsequent call with an absolute path like `os.listdir("/remote/dir2")` would try to list the directory "/dir1/dir2/". Fixes issue micropython#15308. Signed-off-by: Damien George <[email protected]>
1 parent 0619f26 commit cebc9b0

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tools/mpremote/mpremote/transport_serial.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,9 @@ class RemoteFS:
838838
def __init__(self, cmd):
839839
self.cmd = cmd
840840
841+
def _abspath(self, path):
842+
return path if path.startswith("/") else self.path + path
843+
841844
def mount(self, readonly, mkfs):
842845
pass
843846
@@ -859,7 +862,7 @@ def getcwd(self):
859862
def remove(self, path):
860863
c = self.cmd
861864
c.begin(CMD_REMOVE)
862-
c.wr_str(self.path + path)
865+
c.wr_str(self._abspath(path))
863866
res = c.rd_s32()
864867
c.end()
865868
if res < 0:
@@ -868,8 +871,8 @@ def remove(self, path):
868871
def rename(self, old, new):
869872
c = self.cmd
870873
c.begin(CMD_RENAME)
871-
c.wr_str(self.path + old)
872-
c.wr_str(self.path + new)
874+
c.wr_str(self._abspath(old))
875+
c.wr_str(self._abspath(new))
873876
res = c.rd_s32()
874877
c.end()
875878
if res < 0:
@@ -878,7 +881,7 @@ def rename(self, old, new):
878881
def mkdir(self, path):
879882
c = self.cmd
880883
c.begin(CMD_MKDIR)
881-
c.wr_str(self.path + path)
884+
c.wr_str(self._abspath(path))
882885
res = c.rd_s32()
883886
c.end()
884887
if res < 0:
@@ -887,7 +890,7 @@ def mkdir(self, path):
887890
def rmdir(self, path):
888891
c = self.cmd
889892
c.begin(CMD_RMDIR)
890-
c.wr_str(self.path + path)
893+
c.wr_str(self._abspath(path))
891894
res = c.rd_s32()
892895
c.end()
893896
if res < 0:
@@ -896,7 +899,7 @@ def rmdir(self, path):
896899
def stat(self, path):
897900
c = self.cmd
898901
c.begin(CMD_STAT)
899-
c.wr_str(self.path + path)
902+
c.wr_str(self._abspath(path))
900903
res = c.rd_s8()
901904
if res < 0:
902905
c.end()
@@ -912,7 +915,7 @@ def stat(self, path):
912915
def ilistdir(self, path):
913916
c = self.cmd
914917
c.begin(CMD_ILISTDIR_START)
915-
c.wr_str(self.path + path)
918+
c.wr_str(self._abspath(path))
916919
res = c.rd_s8()
917920
c.end()
918921
if res < 0:
@@ -933,7 +936,7 @@ def next():
933936
def open(self, path, mode):
934937
c = self.cmd
935938
c.begin(CMD_OPEN)
936-
c.wr_str(self.path + path)
939+
c.wr_str(self._abspath(path))
937940
c.wr_str(mode)
938941
fd = c.rd_s8()
939942
c.end()

0 commit comments

Comments
 (0)