Lua io: remove debug prints

This commit is contained in:
ihaveahax 2026-03-26 22:26:28 -05:00 committed by d0k3
parent 21e421709d
commit 2e80d3706a

View File

@ -66,13 +66,10 @@ function file.new(filename, mode)
if mode == nil then if mode == nil then
mode = "r" mode = "r"
end end
debugf("opening", filename, mode)
of = setmetatable({_filename=filename, _mode=mode, _seek=0, _open=true, _readable=false, _writable=false, _append_only=false}, file) of = setmetatable({_filename=filename, _mode=mode, _seek=0, _open=true, _readable=false, _writable=false, _append_only=false}, file)
if string.find(mode, "w", 1, true) then if string.find(mode, "w", 1, true) then
debugf("opening", filename, "for writing")
-- preemptively allow writing instead of having that prompt at file:write -- preemptively allow writing instead of having that prompt at file:write
allowed = fs.allow(filename) allowed = fs.allow(filename)
debugf("allowed:", allowed)
if not allowed then return nil, filename..": Permission denied", 13 end if not allowed then return nil, filename..": Permission denied", 13 end
-- write mode truncates the file to 0 normally -- write mode truncates the file to 0 normally
success = pcall(fs.truncate, filename, 0) success = pcall(fs.truncate, filename, 0)
@ -82,14 +79,10 @@ function file.new(filename, mode)
of._readable = false of._readable = false
of._writable = true of._writable = true
elseif string.find(mode, "r+", 1, true) then elseif string.find(mode, "r+", 1, true) then
debugf("opening", filename, "for updating")
allowed = fs.allow(filename) allowed = fs.allow(filename)
debugf("allowed:", allowed)
if not allowed then return nil, filename..": Permission denied", 13 end if not allowed then return nil, filename..": Permission denied", 13 end
success, stat = pcall(fs.stat, filename) success, stat = pcall(fs.stat, filename)
debugf("stat success:", success)
if success then if success then
debugf("type:", stat.type)
if stat.type == "dir" then return nil end if stat.type == "dir" then return nil end
of._stat = stat of._stat = stat
of._size = stat.size of._size = stat.size
@ -98,16 +91,12 @@ function file.new(filename, mode)
of._size = 0 of._size = 0
end end
elseif string.find(mode, "a", 1, true) then elseif string.find(mode, "a", 1, true) then
debugf("opening", filename, "for appending")
allowed = fs.allow(filename) allowed = fs.allow(filename)
debugf("allowed:", allowed)
if not allowed then return nil, filename..": Permission denied", 13 end if not allowed then return nil, filename..": Permission denied", 13 end
of._append_only = true of._append_only = true
of._writable = true of._writable = true
success, stat = pcall(fs.stat, filename) success, stat = pcall(fs.stat, filename)
debugf("stat success:", success)
if success then if success then
debugf("type:", stat.type)
if stat.type == "dir" then return nil end if stat.type == "dir" then return nil end
of._stat = stat of._stat = stat
of._size = stat.size of._size = stat.size
@ -116,28 +105,22 @@ function file.new(filename, mode)
of._size = 0 of._size = 0
end end
if string.find(mode, "+", 1, true) then if string.find(mode, "+", 1, true) then
debugf("append update mode")
of._readable = true of._readable = true
else else
debugf("append only mode")
of._readable = false of._readable = false
of._seek = of._size of._seek = of._size
end end
else else
debugf("opening", filename, "for reading")
-- check if file exists first -- check if file exists first
success, stat = pcall(fs.stat, filename) success, stat = pcall(fs.stat, filename)
debugf("stat success:", success)
-- lua returns nil if it fails to open for some reason -- lua returns nil if it fails to open for some reason
if not success then return nil, filename..": No such file or directory", 2 end if not success then return nil, filename..": No such file or directory", 2 end
debugf("type:", stat.type)
if stat.type == "dir" then return nil end if stat.type == "dir" then return nil end
of._stat = stat of._stat = stat
-- this is so i can adjust the size when data is written -- this is so i can adjust the size when data is written
of._size = stat.size of._size = stat.size
of._readable = true of._readable = true
end end
debugf("returning of")
return of return of
end end
@ -146,7 +129,6 @@ function file:_closed_check()
end end
function file:_bad_desc() function file:_bad_desc()
debugf("returning bad desc on file", self._filename)
return nil, "Bad file descriptor", 9 return nil, "Bad file descriptor", 9
end end
@ -238,12 +220,10 @@ function file:write(...)
to_write = to_write..tostring(v) to_write = to_write..tostring(v)
end end
local len = string.len(to_write) local len = string.len(to_write)
debugf("attempting to write "..tostring(len).." bytes to "..self._filename)
if self._append_only then if self._append_only then
self:seek("end") self:seek("end")
end end
local br = fs.write_file(self._filename, self._seek, to_write) local br = fs.write_file(self._filename, self._seek, to_write)
debugf("wrote "..tostring(br).." bytes to "..self._filename)
self._seek = self._seek + br self._seek = self._seek + br
if self._seek > self._size then if self._seek > self._size then
self._size = self._seek self._size = self._seek