541 posts
Location
Melbourne, Australia
Posted 09 January 2015 - 06:48 AM
I was working on a partitioning program, and I ran into a minor error, although I cant solve it due to my lacking debugging skills. On another note, if you have any tips or advice for debugging it would be greatly appreciated.
Error: qpart:28: Expected string
--[[
qPart
--]]
if not fs.exists("/.qpart-dat") then
local dat = { }
dat["primary"] = "/primary"
for k, v in pairs(fs.list("/")) do
if v:find("disk") then
dat[v] = "/"..v
end
end
local file = fs.open("/.qpart-dat", "w")
file.write(textutils.serialize(dat))
file.close()
end
local file = fs.open("/.qpart-dat", "r")
local dat = file.readAll()
dat = textutils.unserialize(dat)
file.close()
print(textutils.serialize(dat))
for k, v in pairs(dat) do
print(v)
print(dat.v)
if fs.isDir(dat[v]) == nil then
fs.makeDir(v)
end
end
for j, h in pairs(fs.list("/")) do
if fs.isDir(h) then
if not dat[h] then
if h ~= "rom" then
fs.delete(h)
end
end
end
end
376 posts
Location
[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)
Posted 09 January 2015 - 08:32 AM
I tested it. Error for me is at line 24
I fixed the error
--[[
qPart
--]]
if not fs.exists("/.qpart-dat") then
local dat = { }
dat["primary"] = "/primary"
for k, v in pairs(fs.list("/")) do
if v:find("disk") then
dat[v] = "/"..v
end
end
local file = fs.open("/.qpart-dat", "w")
file.write(textutils.serialize(dat))
file.close()
end
local file = fs.open("/.qpart-dat", "r")
local dat = file.readAll()
dat = textutils.unserialize(dat)
file.close()
print(textutils.serialize(dat))
for k, v in pairs(dat) do
print(v)
print(dat.v)
if fs.isDir(v) == nil then
fs.makeDir(v)
end
end
for j, h in pairs(fs.list("/")) do
if fs.isDir(h) then
if not dat[h] then
if h ~= "rom" then
fs.delete(h)
end
end
end
end
What had happened was you tried to call "dat[v]" which is the value not the key, and since you are iterating through "dat", you can just use fs.exists(v) instead of fs.exists(dat
)
Edited on 09 January 2015 - 07:32 AM
24 posts
Posted 09 January 2015 - 09:37 AM
Requiem of Silence is correct. A couple suggestions though.
1. fs.makeDir already does nothing if the folder exists. Skip the check.
--make missing dirs
for _,path in pairs(dat) do
fs.makeDir(path)
end
2. If you're planning on using saved disk paths, your code is fine. If youre just whitelisting them to prevent an error, try pcall.
Delete all folders. Rom and disks are protected and pcall catches the 'access denied' errors when trying it.
--delete unsaved folders
for _,h in pairs(fs.list("/")) do
if fs.isDir(h) and not dat[h] then
local status,err=pcall(fs.delete,h)
end
end
Good luck
1140 posts
Location
Kaunas, Lithuania
Posted 09 January 2015 - 12:01 PM
Requiem of Silence is correct. A couple suggestions though.
1. fs.makeDir already does nothing if the folder exists. Skip the check.
--make missing dirs
for _,path in pairs(dat) do
fs.makeDir(path)
end
2. If you're planning on using saved disk paths, your code is fine. If youre just whitelisting them to prevent an error, try pcall.
Delete all folders. Rom and disks are protected and pcall catches the 'access denied' errors when trying it.
--delete unsaved folders
for _,h in pairs(fs.list("/")) do
if fs.isDir(h) and not dat[h] then
local status,err=pcall(fs.delete,h)
end
end
Good luck
Instead of calling fs.delete in a pcall (which is actually not a bad idea) you could check that the folder/file you are trying to delete is not read-only (fs.isReadOnly( path )).
541 posts
Location
Melbourne, Australia
Posted 09 January 2015 - 12:27 PM
The concept was to remove partitions in case it was created on the disk and then they removed it.