fixed filesystem erroring out when seeking while not buffered

This commit is contained in:
Ponali
2025-10-11 10:22:31 +02:00
parent 33f2573eb6
commit 1798d63864
+9 -13
View File
@@ -196,7 +196,7 @@ function filesystem.open(path, mode, buffered) -- opens a file and returns its h
properHandle.address = address properHandle.address = address
local content = nil local content = nil
local bufferOffset = 0 -- Position in file where buffer starts local bufferOffset = 0 -- Position in file where buffer starts
local readCursor = 1 -- Position within buffer (1-based) local readCursor = 1 -- Position within buffer (1-based)
if buffered and mode == "r" then if buffered and mode == "r" then
content = component.invoke(address, "read", handle, bufferSize) or "" content = component.invoke(address, "read", handle, bufferSize) or ""
@@ -294,10 +294,7 @@ function filesystem.open(path, mode, buffered) -- opens a file and returns its h
end end
local startSByte = ((readCursor - 1) % sectorSize) + 1 local startSByte = ((readCursor - 1) % sectorSize) + 1
local sect = unmanagedProxy.readSector(startSector) local sect = unmanagedProxy.readSector(startSector)
unmanagedProxy.writeSector( unmanagedProxy.writeSector(startSector, sect:sub(1, startSByte - 1) .. data:sub(1, sectorSize - startSByte + 1))
startSector,
sect:sub(1, startSByte - 1) .. data:sub(1, sectorSize - startSByte + 1)
)
for i = 2, (#data + startSByte) // sectorSize do for i = 2, (#data + startSByte) // sectorSize do
if startSector + i - 1 > sectorCount then if startSector + i - 1 > sectorCount then
return nil, "not enough space" return nil, "not enough space"
@@ -375,7 +372,7 @@ function filesystem.open(path, mode, buffered) -- opens a file and returns its h
readCursor = 1 readCursor = 1
return newPos return newPos
else else
return component.invoke(self.address, "seek", self.handle, whence, math.max(offset, -currentAbsolutePos)) return component.invoke(self.address, "seek", self.handle, whence, offset)
end end
end end
@@ -560,19 +557,18 @@ function filesystem.rename(fromPath, toPath)
return component.invoke(fromAddress, "rename", fromAbsPath, toAbsPath) return component.invoke(fromAddress, "rename", fromAbsPath, toAbsPath)
elseif filesystem.isDirectory(fromPath) then -- component.invoke(fromAddress, "isDirectory", fromAbsPath) then elseif filesystem.isDirectory(fromPath) then -- component.invoke(fromAddress, "isDirectory", fromAbsPath) then
copyRecursive(fromAddress, fromAbsPath, toAddress, toAbsPath) copyRecursive(fromAddress, fromAbsPath, toAddress, toAbsPath)
filesystem.remove(fromPath) -- component.invoke(fromAddress,"remove", fromAbsPath) filesystem.remove(fromPath) -- component.invoke(fromAddress,"remove", fromAbsPath)
else else
local handle, data, tmpdata = filesystem.open(fromPath), "", local handle, data, tmpdata = filesystem.open(fromPath), "", nil -- component.invoke(fromAddress, "open", fromAbsPath, "r"), "", nil
nil -- component.invoke(fromAddress, "open", fromAbsPath, "r"), "", nil
repeat repeat
tmpdata = handle:read(math.huge or math.maxinteger) -- component.invoke(fromAddress, "read", handle, math.huge or math.maxinteger) tmpdata = handle:read(math.huge or math.maxinteger) -- component.invoke(fromAddress, "read", handle, math.huge or math.maxinteger)
data = data .. (tmpdata or "") data = data .. (tmpdata or "")
until not tmpdata until not tmpdata
tmpdata = handle:close() -- component.invoke(fromAddress, "close", handle) tmpdata = handle:close() -- component.invoke(fromAddress, "close", handle)
local handle = filesystem.open(toPath) -- component.invoke(toAddress, "open", toAbsPath, "w") local handle = filesystem.open(toPath) -- component.invoke(toAddress, "open", toAbsPath, "w")
handle:write(data) -- component.invoke(toAddress, "write", handle, data) handle:write(data) -- component.invoke(toAddress, "write", handle, data)
handle:close() -- component.invoke(toAddress, "close", handle) handle:close() -- component.invoke(toAddress, "close", handle)
filesystem.remove(fromPath) -- component.invoke(fromAddress, "remove", fromAbsPath) filesystem.remove(fromPath) -- component.invoke(fromAddress, "remove", fromAbsPath)
end end
end end