This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SquidDev's profile picture

[1.65][SMP/SSP] Making directories increases free space

Started by SquidDev, 04 January 2015 - 08:11 PM
SquidDev #1
Posted 04 January 2015 - 09:11 PM
Running this code:

local old = fs.getFreeSpace("/")
print("Original: ", old)
fs.makeDir("testing")
local new = fs.getFreeSpace("/")
print("New:	  ", new)
print("Change:   ", new - old)

This prints out on my computer:


Original: 73078682
New:	  73079194
Change:   512

It is possible that I am being really stupid, but I think change should be negative not positive. This looks like space is 'created' when making a folder.

Also creating several directories ("a/b/c") should decrease the free space by 512*3, but only does it by 512.
Edited on 04 January 2015 - 08:23 PM
Cranium #2
Posted 05 January 2015 - 03:21 AM
If I recall, each file or directory has a minimum memory reference size of 512 bytes in the LuaJ environment. From what I can tell, the fs API always returns the exact memory amount allocated to the LuaJ environment, even though certain parts of it are fudged. So when we get down to the smaller bits of the filesystem, things get a little wonky. There are workarounds, but it's nothing we can change on the backend without writing our own Lua-Java interpreter.
SquidDev #3
Posted 09 January 2015 - 07:46 PM
I'll try to clarify what I mean. I created a computer with 1024 bytes of free space (512 for the root folder). In a Lua prompt:


fs.getFreeSpace("/") -- Prints 512

local a = fs.open("T", "w") a.close()
fs.getFreeSpace("/") -- Prints 0

fs.delete("T")
fs.getFreeSpace("/") -- Prints 512

So far, so good. When we start with directories, this is when it gets odd.

fs.getFreeSpace("/") -- Prints 512

fs.makeDir("T")
fs.getFreeSpace("/") -- Prints 1024

fs.delete("T")
fs.getFreeSpace("/") -- Prints 1536

As you can see, each time you create a folder, your free space increases. Technically you can get free space with:


local function giveMeSpace(space)
  for i = 1, math.ceil(space/1204) do
    fs.makeDir("T")
    fs.delete("T")
  end
end

Also, because the whole path is only allocated 512, not each component:


fs.getFreeSpace("/") -- 512

fs.makeDir("1/2/3")
fs.getFreeSpace("/") -- 1024

fs.delete("1")
fs.getFreeSpace("/") -- 2560

Because when 1/2/3 is created, it uses 512 bytes. But when 1 is deleted it frees 3 * 512 bytes
Xtansia #4
Posted 02 February 2015 - 09:46 AM
I tweeted Dan about this, he says it's fixed for the next version fixed for the next version
dan200 #5
Posted 02 February 2015 - 09:21 PM
Fixed in CC1.66