1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-16 10:09:55 +00:00

Ensure files cannot be closed multiple times

Also fix an NPE if we try to close them twice.

Fixes #230
This commit is contained in:
SquidDev 2019-06-04 21:34:19 +01:00
parent ffa4cc241b
commit d8e1c73d26
6 changed files with 39 additions and 2 deletions

View File

@ -212,6 +212,7 @@ else if( count == 0 && m_seekable != null )
}
}
case 3: // close
checkOpen();
close();
return null;
case 4: // seek

View File

@ -95,6 +95,7 @@ else if( args.length > 0 && args[0] instanceof String )
return null;
}
case 2: // close
checkOpen();
close();
return null;
case 3: // seek

View File

@ -152,6 +152,7 @@ else if( count <= BUFFER_SIZE )
return null;
}
case 3: // close
checkOpen();
close();
return null;
default:

View File

@ -93,6 +93,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
return null;
}
case 3: // close
checkOpen();
close();
return null;
default:

View File

@ -37,8 +37,13 @@ protected void checkOpen() throws LuaException
protected final void close()
{
m_open = false;
IoUtil.closeQuietly( m_closable );
m_closable = null;
Closeable closeable = m_closable;
if( closeable != null )
{
IoUtil.closeQuietly( closeable );
m_closable = null;
}
}
/**

View File

@ -54,6 +54,20 @@ describe("The fs library", function()
expect { fs.open("rom/x", "r") }:same { nil, "/rom/x: No such file" }
expect { fs.open("x", "r") }:same { nil, "/x: No such file" }
end)
it("errors when closing twice", function()
local handle = fs.open("rom/startup.lua", "r")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("reading in binary mode", function()
it("errors when closing twice", function()
local handle = fs.open("rom/startup.lua", "rb")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("writing", function()
@ -64,6 +78,20 @@ describe("The fs library", function()
it("fails on read-only mounts", function()
expect { fs.open("rom/x", "w") }:same { nil, "/rom/x: Access denied" }
end)
it("errors when closing twice", function()
local handle = fs.open("test-files/out.txt", "w")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("writing in binary mode", function()
it("errors when closing twice", function()
local handle = fs.open("test-files/out.txt", "wb")
handle.close()
expect.error(handle.close):eq("attempt to use a closed file")
end)
end)
describe("appending", function()