v1.4.0 - Added better path handling to the filesystem library, cleaned up mv, cp, cd, cat and their help files.

This commit is contained in:
TheWahlolly
2025-05-22 21:00:38 +03:00
parent f52d0f7015
commit 500419de59
12 changed files with 95 additions and 86 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
local agcfg = { local agcfg = {
["halyde"] = { ["halyde"] = {
["maindir"] = "", ["maindir"] = "",
["version"] = "1.3.1", ["version"] = "1.4.0",
["description"] = "A universal, customizable and feature-packed operating system for OpenComputers.", ["description"] = "A universal, customizable and feature-packed operating system for OpenComputers.",
["directories"] = { ["directories"] = {
"halyde/apps", "halyde/apps",
+1 -1
View File
@@ -4,7 +4,7 @@ Ahalyde/core/
Ahalyde/config/ Ahalyde/config/
Ahalyde/apps/helpdb/ Ahalyde/apps/helpdb/
Ahalyde/apps/ Ahalyde/apps/
V1.3.0 V1.4.0
Ainit.lua Ainit.lua
Ahalyde/apps/helpdb/cat.txt Ahalyde/apps/helpdb/cat.txt
Ahalyde/apps/helpdb/cd.txt Ahalyde/apps/helpdb/cd.txt
+13 -13
View File
@@ -1,20 +1,20 @@
local args = {...} local files = {...}
local file = args[1]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not file then if not files or not files[1] then
shell.run("help cat") shell.run("help cat")
return return
end end
if file:sub(1, 1) ~= "/" then for _, file in ipairs(files) do
file = shell.workingDirectory .. file if file:sub(1, 1) ~= "/" then
end file = fs.concat(shell.workingDirectory, file)
if not fs.exists(file) then end
if not fs.exists(file) then
print("\27[91mFile does not exist.") print("\27[91mFile does not exist.")
end end
local handle = fs.open(file, "r") local handle = fs.open(file, "r")
local data local data
repeat repeat
data = handle:read(math.huge or math.maxinteger) data = handle:read(math.huge or math.maxinteger)
print(data, false) print(data, false)
until not data until not data
end
+6 -21
View File
@@ -1,29 +1,14 @@
local args = {...} local directory = ...
local directory = args[1]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not directory then if not directory then
return return
end end
if directory == ".." then if directory:sub(1, 1) ~= "/" then
local backDirectory = shell.workingDirectory:match("(.+)/.-/") directory = fs.concat(shell.workingDirectory, directory)
if backDirectory then end
backDirectory = backDirectory .. "/" if fs.exists(directory) and fs.isDirectory(directory) then
else shell.workingDirectory = fs.canonical(directory)
backDirectory = "/"
end
shell.workingDirectory = backDirectory
else else
if directory:sub(-1, -1) ~= "/" then
directory = directory .. "/"
end
if directory:sub(1, 1) ~= "/" then
directory = shell.workingDirectory .. directory
end
if fs.exists(directory) and fs.isDirectory(directory) or fs.exists(shell.workingDirectory .. directory) and fs.isDirectory(shell.workingDirectory .. directory) then
shell.workingDirectory = directory
else
print("\27[91mNo such directory.") print("\27[91mNo such directory.")
end
end end
+3 -5
View File
@@ -1,6 +1,4 @@
local args = {...} local fromFile, toFile = ...
local fromFile, toFile = args[1], args[2]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not fromFile or not toFile then if not fromFile or not toFile then
@@ -8,10 +6,10 @@ if not fromFile or not toFile then
return return
end end
if fromFile:sub(1, 1) ~= "/" then if fromFile:sub(1, 1) ~= "/" then
fromFile = shell.workingDirectory .. fromFile fromFile = fs.concat(shell.workingDirectory, fromFile)
end end
if toFile:sub(1, 1) ~= "/" then if toFile:sub(1, 1) ~= "/" then
toFile = shell.workingDirectory .. toFile toFile = fs.concat(shell.workingDirectory, toFile)
end end
if fromFile == toFile then if fromFile == toFile then
print("\27[91mSource and destination are the same.") print("\27[91mSource and destination are the same.")
+3 -3
View File
@@ -1,8 +1,8 @@
Usage: cat [FILE] Usage: cat [FILES]...
Concatenates and prints a file. Concatenates and prints a file.
FILE Specifies the path to the file to print. FILES Specifies the paths to the files to print.
Examples: Examples:
cat /init.lua Concatenates and prints init.lua in the root directory. cat /init.lua Concatenates and prints init.lua in the root directory.
cat help.lua Concatenates and prints help.lua in the current working directory. cat help.lua cat.lua Concatenates and prints help.lua and cat.lua in the current working directory.
+1
View File
@@ -1,6 +1,7 @@
Usage: cp [FLAGS] [SOURCE] [DESTINATION] Usage: cp [FLAGS] [SOURCE] [DESTINATION]
Copies a file. Copies a file.
FLAGS Specifies extra options when executing the command.
-o, --overwrite Allows any file that might be at the destination to be overwritten. -o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be copied. SOURCE Specifies the file to be copied.
DESTINATION Specifies the path to copy the file to. DESTINATION Specifies the path to copy the file to.
+1
View File
@@ -1,6 +1,7 @@
Usage: mv [FLAGS] [SOURCE] [DESTINATION] Usage: mv [FLAGS] [SOURCE] [DESTINATION]
Moves/renames a file. Moves/renames a file.
FLAGS Specifies extra options when executing the command.
-o, --overwrite Allows any file that might be at the destination to be overwritten. -o, --overwrite Allows any file that might be at the destination to be overwritten.
SOURCE Specifies the file to be moved/renamed. SOURCE Specifies the file to be moved/renamed.
DESTINATION Specifies the path/filename to move/rename the file to. DESTINATION Specifies the path/filename to move/rename the file to.
+2 -4
View File
@@ -1,6 +1,4 @@
local args = {...} local directory = ...
local directory = args[1]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not directory then if not directory then
@@ -8,7 +6,7 @@ if not directory then
return return
end end
if directory:sub(1, 1) ~= "/" then if directory:sub(1, 1) ~= "/" then
directory = shell.workingDirectory .. directory directory = fs.concat(shell.workingDirectory, directory)
end end
if fs.exists(directory) then if fs.exists(directory) then
print("\27[91mAn object already exists at the specified path.") print("\27[91mAn object already exists at the specified path.")
+3 -5
View File
@@ -1,6 +1,4 @@
local args = {...} local fromFile, toFile = ...
local fromFile, toFile = args[1], args[2]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not fromFile or not toFile then if not fromFile or not toFile then
@@ -8,10 +6,10 @@ if not fromFile or not toFile then
return return
end end
if fromFile:sub(1, 1) ~= "/" then if fromFile:sub(1, 1) ~= "/" then
fromFile = shell.workingDirectory .. fromFile fromFile = fs.concat(shell.workingDirectory, fromFile)
end end
if toFile:sub(1, 1) ~= "/" then if toFile:sub(1, 1) ~= "/" then
toFile = shell.workingDirectory .. toFile toFile = fs.concat(shell.workingDirectory, toFile)
end end
if fromFile == toFile then if fromFile == toFile then
print("\27[91mSource and destination are the same.") print("\27[91mSource and destination are the same.")
+3 -4
View File
@@ -1,6 +1,4 @@
local args = {...} local file = ...
local file = args[1]
args = nil
local fs = import("filesystem") local fs = import("filesystem")
if not file then if not file then
@@ -8,9 +6,10 @@ if not file then
return return
end end
if file:sub(1, 1) ~= "/" then if file:sub(1, 1) ~= "/" then
file = shell.workingDirectory .. file file = fs.concat(shell.workingDirectory, file)
end end
if not fs.exists(file) then if not fs.exists(file) then
print("\27[91mFile does not exist.") print("\27[91mFile does not exist.")
return
end end
fs.remove(file) fs.remove(file)
+47 -18
View File
@@ -9,29 +9,58 @@ end
local filesystem = {} local filesystem = {}
function filesystem.processPath(path) -- returns the address and absolute path of a filesystem path as well as sanitizing it function filesystem.canonical(path)
checkArg(1, path, "string") checkArg(1, path, "string")
absPath = path:gsub("/+", "/") local segList = {}
if path:sub(1, 1) ~= "/" then
path = "/" .. path
end
path = path:gsub("/+", "/")
for segment in path:gmatch("[^/]+") do
if segment == ".." and segList[1] then
table.remove(segList, #segList)
elseif segment ~= "." then
table.insert(segList, segment)
end
end
return "/" .. table.concat(segList, "/")
end
function filesystem.concat(path1, path2)
checkArg(1, path1, "string")
checkArg(2, path2, "string")
if path1:sub(-1, -1) == "/" then
path1 = path1:sub(1, -2)
end
if path2:sub(1, 1) ~= "/" then
path2 = "/" .. path2
end
return path1 .. path2
end
function filesystem.absolutePath(path) -- returns the address and absolute path of an object
checkArg(1, path, "string")
path = filesystem.canonical(path)
local address = nil local address = nil
if absPath:find("^/mnt/.../") then if path:find("^/mnt/.../") then
address = component.get(path:sub(6,8)) address = component.get(path:sub(6,8))
if not address then if not address then
address = computer.getBootAddress() address = computer.getBootAddress()
else else
absPath = absPath:sub(9) path = path:sub(9)
end end
else else
address = computer.getBootAddress() address = computer.getBootAddress()
end end
if not address then if not address then
return nil, "no such device" return nil, "no such component"
end end
return address, absPath return address, path
end end
function filesystem.exists(path) -- check if path exists function filesystem.exists(path) -- check if path exists
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -47,7 +76,7 @@ function filesystem.open(path, mode) -- opens a file and returns its handle
if not (mode == "r" or mode == "w" or mode == "rb" or mode == "wb") then if not (mode == "r" or mode == "w" or mode == "rb" or mode == "wb") then
return nil, "invalid handle type" return nil, "invalid handle type"
end end
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
local handleArgs = {component.invoke(address, "open", absPath, mode)} local handleArgs = {component.invoke(address, "open", absPath, mode)}
local handle = handleArgs[1] local handle = handleArgs[1]
if not handle then if not handle then
@@ -81,7 +110,7 @@ function filesystem.list(path)
end end
return returnTable return returnTable
else else
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -91,7 +120,7 @@ end
function filesystem.size(path) function filesystem.size(path)
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -100,7 +129,7 @@ end
function filesystem.isDirectory(path) function filesystem.isDirectory(path)
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -110,8 +139,8 @@ end
function filesystem.rename(fromPath, toPath) function filesystem.rename(fromPath, toPath)
checkArg(1, fromPath, "string") checkArg(1, fromPath, "string")
checkArg(2, toPath, "string") checkArg(2, toPath, "string")
local fromAddress, fromAbsPath = filesystem.processPath(fromPath) local fromAddress, fromAbsPath = filesystem.absolutePath(fromPath)
local toAddress, toAbsPath = filesystem.processPath(toPath) local toAddress, toAbsPath = filesystem.absolutePath(toPath)
if not fromAddress or not toAddress then if not fromAddress or not toAddress then
return false return false
end end
@@ -135,8 +164,8 @@ end
function filesystem.copy(fromPath, toPath) function filesystem.copy(fromPath, toPath)
checkArg(1, fromPath, "string") checkArg(1, fromPath, "string")
checkArg(2, toPath, "string") checkArg(2, toPath, "string")
local fromAddress, fromAbsPath = filesystem.processPath(fromPath) local fromAddress, fromAbsPath = filesystem.absolutePath(fromPath)
local toAddress, toAbsPath = filesystem.processPath(toPath) local toAddress, toAbsPath = filesystem.absolutePath(toPath)
if not fromAddress or not toAddress then if not fromAddress or not toAddress then
return false return false
end end
@@ -154,7 +183,7 @@ end
function filesystem.isDirectory(path) function filesystem.isDirectory(path)
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -163,7 +192,7 @@ end
function filesystem.remove(path) function filesystem.remove(path)
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end
@@ -172,7 +201,7 @@ end
function filesystem.makeDirectory(path) function filesystem.makeDirectory(path)
checkArg(1, path, "string") checkArg(1, path, "string")
local address, absPath = filesystem.processPath(path) local address, absPath = filesystem.absolutePath(path)
if not address then if not address then
return false return false
end end