Implemented move and copy command. Needs to be reworked to work without `-f' and '-t' flags but with spaces.

This commit is contained in:
Jakob Friedl
2025-07-09 15:13:37 +02:00
parent bb56ed42f2
commit 2d2c94ed38
5 changed files with 91 additions and 3 deletions

View File

@@ -224,7 +224,6 @@ proc taskRm*(task: Task): TaskResult =
echo fmt"Deleting file {target}." echo fmt"Deleting file {target}."
try: try:
# Get current working directory using GetCurrentDirectory
if DeleteFile(target) == FALSE: if DeleteFile(target) == FALSE:
raise newException(OSError, fmt"Failed to delete file ({GetLastError()}).") raise newException(OSError, fmt"Failed to delete file ({GetLastError()}).")
@@ -252,7 +251,6 @@ proc taskRmdir*(task: Task): TaskResult =
echo fmt"Deleting directory {target}." echo fmt"Deleting directory {target}."
try: try:
# Get current working directory using GetCurrentDirectory
if RemoveDirectoryA(target) == FALSE: if RemoveDirectoryA(target) == FALSE:
raise newException(OSError, fmt"Failed to delete directory ({GetLastError()}).") raise newException(OSError, fmt"Failed to delete directory ({GetLastError()}).")
@@ -263,6 +261,68 @@ proc taskRmdir*(task: Task): TaskResult =
status: Completed status: Completed
) )
except CatchableError as err:
return TaskResult(
task: task.id,
agent: task.agent,
data: encode(fmt"An error occured: {err.msg}" & "\n"),
status: Failed
)
# Move file or directory
proc taskMove*(task: Task): TaskResult =
# Parse arguments
echo task.args
let
params = parseJson(task.args)
lpExistingFileName = params["from"].getStr()
lpNewFileName = params["to"].getStr()
echo fmt"Moving {lpExistingFileName} to {lpNewFileName}."
try:
if MoveFile(lpExistingFileName, lpNewFileName) == FALSE:
raise newException(OSError, fmt"Failed to move file or directory ({GetLastError()}).")
return TaskResult(
task: task.id,
agent: task.agent,
data: encode(""),
status: Completed
)
except CatchableError as err:
return TaskResult(
task: task.id,
agent: task.agent,
data: encode(fmt"An error occured: {err.msg}" & "\n"),
status: Failed
)
# Copy file or directory
proc taskCopy*(task: Task): TaskResult =
# Parse arguments
let
params = parseJson(task.args)
lpExistingFileName = params["from"].getStr()
lpNewFileName = params["to"].getStr()
echo fmt"Copying {lpExistingFileName} to {lpNewFileName}."
try:
# Copy file to new location, overwrite if a file with the same name already exists
if CopyFile(lpExistingFileName, lpNewFileName, FALSE) == FALSE:
raise newException(OSError, fmt"Failed to copy file or directory ({GetLastError()}).")
return TaskResult(
task: task.id,
agent: task.agent,
data: encode(""),
status: Completed
)
except CatchableError as err: except CatchableError as err:
return TaskResult( return TaskResult(
task: task.id, task: task.id,

View File

@@ -13,7 +13,9 @@ proc handleTask*(task: Task, config: AgentConfig): TaskResult =
SetWorkingDirectory: taskCd, SetWorkingDirectory: taskCd,
ListDirectory: taskDir, ListDirectory: taskDir,
RemoveFile: taskRm, RemoveFile: taskRm,
RemoveDirectory: taskRmdir RemoveDirectory: taskRmdir,
Move: taskMove,
Copy: taskCopy
}.toTable }.toTable
# Handle task command # Handle task command

View File

@@ -39,6 +39,16 @@ var parser = newParser:
help("Remove directory.") help("Remove directory.")
arg("directory", help="Relative or absolute path to the directory to delete.", nargs = -1) arg("directory", help="Relative or absolute path to the directory to delete.", nargs = -1)
command("move"):
help("Move a file or directory.")
option("-f", "--from", help="Original file name.", required=true)
option("-t", "--to", help="New file name.", required=true)
command("copy"):
help("Copy a file or directory.")
option("-f", "--from", help="Original file name.", required=true)
option("-t", "--to", help="New file name.", required=true)
command("bof"): command("bof"):
help("Execute COFF or BOF file (.o) in memory.") help("Execute COFF or BOF file (.o) in memory.")
arg("file", help="Local path to object file.", nargs = 1) arg("file", help="Local path to object file.", nargs = 1)
@@ -93,6 +103,12 @@ proc handleAgentCommand*(cq: Conquest, args: varargs[string]) =
of "rmdir": of "rmdir":
cq.taskRemoveDirectory(opts.rmdir.get.directory) cq.taskRemoveDirectory(opts.rmdir.get.directory)
of "move":
cq.taskMove(opts.move.get.from, opts.move.get.to)
of "copy":
cq.taskCopy(opts.copy.get.from, opts.copy.get.to)
of "bof": of "bof":
cq.taskExecuteBof(opts.bof.get.file, opts.bof.get.arguments) cq.taskExecuteBof(opts.bof.get.file, opts.bof.get.arguments)

View File

@@ -55,6 +55,14 @@ proc taskRemoveDirectory*(cq: Conquest, arguments: seq[string]) =
let payload = %*{ "directory": arguments.join(" ").replace("\"").replace("'")} let payload = %*{ "directory": arguments.join(" ").replace("\"").replace("'")}
cq.createTask(RemoveDirectory, $payload, "Tasked agent to remove directory.") cq.createTask(RemoveDirectory, $payload, "Tasked agent to remove directory.")
proc taskMove*(cq: Conquest, oldPath, newPath: string) =
let payload = %*{ "from": oldPath, "to": newPath}
cq.createTask(Move, $payload, "Tasked agent to move a file or directory.")
proc taskCopy*(cq: Conquest, oldPath, newPath: string) =
let payload = %*{ "from": oldPath, "to": newPath}
cq.createTask(Copy, $payload, "Tasked agent to copy a file or directory.")
proc taskExecuteBof*(cq: Conquest, file: string, arguments: seq[string]) = proc taskExecuteBof*(cq: Conquest, file: string, arguments: seq[string]) =
# Verify that the object file exists # Verify that the object file exists

View File

@@ -20,6 +20,8 @@ type
ListDirectory = "ls" ListDirectory = "ls"
RemoveFile = "rm" RemoveFile = "rm"
RemoveDirectory = "rmdir" RemoveDirectory = "rmdir"
Move = "move"
Copy = "copy"
TaskStatus* = enum TaskStatus* = enum
Completed = "completed" Completed = "completed"