From f93fc78a9410f8ac328a50a3f8e71fc47f0f26f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 14 Dec 2013 18:44:12 +0100 Subject: [PATCH] cp and mv programs will now accept a directory as the target location and append the original file name automatically in that case --- assets/opencomputers/lua/rom/bin/cp.lua | 15 +++++++++++---- assets/opencomputers/lua/rom/bin/mv.lua | 16 +++++++++++++--- li/cil/oc/server/fs/VirtualFileSystem.scala | 4 +++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/assets/opencomputers/lua/rom/bin/cp.lua b/assets/opencomputers/lua/rom/bin/cp.lua index ebf3571f9..21c38db0e 100644 --- a/assets/opencomputers/lua/rom/bin/cp.lua +++ b/assets/opencomputers/lua/rom/bin/cp.lua @@ -1,11 +1,18 @@ -local args = shell.parse(...) +local args, options = shell.parse(...) if #args < 2 then - print("Usage: cp ") + print("Usage: cp [-f] ") + print(" -f: overwrite file if it already exists.") return end -local from, reason = shell.resolve(args[1]) -local to, reason = shell.resolve(args[2]) +local from = shell.resolve(args[1]) +local to = shell.resolve(args[2]) +if fs.isDirectory(to) then + to = to .. "/" .. fs.name(from) +end +if fs.exists(to) and not options.f then + error("target file exists") +end local result, reason = fs.copy(from, to) if not result then print(reason) diff --git a/assets/opencomputers/lua/rom/bin/mv.lua b/assets/opencomputers/lua/rom/bin/mv.lua index 5ad2e38c4..7f3932111 100644 --- a/assets/opencomputers/lua/rom/bin/mv.lua +++ b/assets/opencomputers/lua/rom/bin/mv.lua @@ -1,12 +1,22 @@ -local args = shell.parse(...) +local args, options = shell.parse(...) if #args < 2 then - print("Usage: mv ") + print("Usage: mv [-f] ") + print(" -f: overwrite file if it already exists.") return end local from = shell.resolve(args[1]) local to = shell.resolve(args[2]) +if fs.isDirectory(to) then + to = to .. "/" .. fs.name(from) +end +if fs.exists(to) then + if not options.f then + error("target file exists") + end + fs.remove(to) +end local result, reason = os.rename(from, to) if not result then - print(reason) + print(reason or "unknown error") end diff --git a/li/cil/oc/server/fs/VirtualFileSystem.scala b/li/cil/oc/server/fs/VirtualFileSystem.scala index 2cb6af129..b7fc5882b 100644 --- a/li/cil/oc/server/fs/VirtualFileSystem.scala +++ b/li/cil/oc/server/fs/VirtualFileSystem.scala @@ -1,6 +1,7 @@ package li.cil.oc.server.fs import java.io +import java.io.FileNotFoundException import li.cil.oc.api.fs.Mode import net.minecraft.nbt.{NBTTagList, NBTTagCompound} import scala.collection.mutable @@ -62,7 +63,8 @@ trait VirtualFileSystem extends OutputStreamFileSystem { } override def rename(from: String, to: String) = - if (from != "" && exists(from) && !exists(to)) { + if (from == "" || !exists(from)) throw new FileNotFoundException() + else if (!exists(to)) { val segmentsTo = segments(to) root.get(segmentsTo.dropRight(1)) match { case Some(toParent: VirtualDirectory) =>