Skip to content

Commit f0f1915

Browse files
committed
add cp and rm command to sh.py
1 parent 8a07281 commit f0f1915

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

sh.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,45 @@ def do_mount(self, args):
130130
for item in self.root_fs.mounts:
131131
print(f"{item.filesystem} on {item.path}")
132132

133+
def do_cp(self, args):
134+
"Copy files"
135+
if len(args) != 2:
136+
print("cp: usage: cp <source> <destination>")
137+
return
138+
src = self.cwd / args[0]
139+
dst = self.cwd / args[1]
140+
if not src.exists():
141+
print("cp: no such file or directory: {src}".format(src=src))
142+
return
143+
if src.is_dir():
144+
if dst.exists() and not dst.is_dir():
145+
print("cp: cannot overwrite non-directory {dst} with directory {src}".format(dst=dst, src=src))
146+
return
147+
for item in src.iterdir():
148+
self.do_cp([str(item), str(dst / item.name)])
149+
else:
150+
try:
151+
dst.write_bytes(src.read_bytes())
152+
except Exception as ex:
153+
print("cp: error: {message}".format(message=str(ex)))
154+
155+
def do_rm(self, args):
156+
"Remove files"
157+
for arg in args:
158+
path = self.cwd / arg
159+
if not path.exists():
160+
print("rm: no such file or directory: {path}".format(path=path))
161+
continue
162+
try:
163+
if path.is_dir():
164+
for item in path.iterdir():
165+
self.do_rm([str(item)])
166+
path.rmdir()
167+
else:
168+
path.unlink()
169+
except Exception as ex:
170+
print("rm: error: {message}".format(message=str(ex)))
171+
133172
def do_version(self, args):
134173
"Show version"
135174
print(VERSION)

0 commit comments

Comments
 (0)