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

fs.getTotalSpace

Started by LBPHacker, 15 July 2013 - 03:46 PM
LBPHacker #1
Posted 15 July 2013 - 05:46 PM
Hey, LBPHacker here again with yet another suggestion…

As the title explains, I'd be happy to see fs.getTotalSpace implemented. Yes, I know it has been suggested before, though that topic has been locked upon user request before getting the chance to be answered. And, come on, not being able to get the total space of a device (computer or floppy) while being able to get the remaining space, that's kind of weird in my opinion. And finally, yes, I know the OP of the topic I've mentioned had written a function to do what fs.getTotalSpace should do:
Spoiler
fs.getSpaceLimit = function(_path, _space, _final)
    local final = true
    if(_final ~= nil) then
        final = _final
    end 
    local space = 0
    if(_space ~= nil) then
        space = _space
    end
    local sDir = ""
    if _path ~= nil then sDir = _path end
    local tContent = fs.list( sDir )
    for i, j in pairs( tContent ) do
        local sPath = fs.combine( sDir, j )
        if fs.isDir( sPath ) then
            if(sPath ~= "rom") then
                space = space + 512
                space = fs.getSpaceLimit(sPath, space, false)
            end
        else
            space = space + fs.getSize(sPath)
        end
    end
    if(final == true) then
        return space + fs.getFreeSpace("")
    else
        return space
    end
end
But as you can see, rom is an exception there (since the files in the rom don't consume disk space). As a coder, I don't really like exceptions.

That's it. Thanks for reading.
Cranium #2
Posted 15 July 2013 - 06:23 PM
I'm pretty sure this can be done with the tools already available to you…
Lyqyd #3
Posted 15 July 2013 - 06:29 PM
Considering the presence of the code sample that does just that, this is technically on the list of Suggestions not to Make. I'll let it stand for now though.
gamax92 #4
Posted 16 July 2013 - 11:23 AM
I know this really isn't about the suggestion on hand but I'd like to mention to the coder:
    local final = true
    if(_final ~= nil) then
        final = _final
    end 
    local space = 0
    if(_space ~= nil) then
        space = _space
    end
    local sDir = ""
    if _path ~= nil then sDir = _path end

Can simply be shortened to:

local final = _final or true
local space = _space or 0
local sDir = _path or ""
theoriginalbit #5
Posted 16 July 2013 - 11:33 AM
I know this really isn't about the suggestion on hand but I'd like to mention to the coder:
	local final = true
	if(_final ~= nil) then
		final = _final
	end
	local space = 0
	if(_space ~= nil) then
		space = _space
	end
	local sDir = ""
	if _path ~= nil then sDir = _path end

Can simply be shortened to:

local final = _final or true
local space = _space or 0
local sDir = _path or ""
Except final is a boolean and doing


local final = _final or true
would make it true when it's false
the solution to that one would be


local final = _final == true
Tiin57 #6
Posted 16 July 2013 - 11:55 AM
Except final is a boolean and doing


local final = _final or true
would make it true when it's false
the solution to that one would be


local final = _final == true
Or

local final = _final
theoriginalbit #7
Posted 16 July 2013 - 11:56 AM
Or

local final = _final
Well that would leave unwanted variables, as opposed to checking against a boolean results in only a boolean.
Lyqyd #8
Posted 16 July 2013 - 12:42 PM
The script as it stands leaves alone whatever is passed in, unless it is nil. You could as easily remove the whole check and just do `if final then` for the return conditions if you wanted to preserve the behavior.

But since we didn't feel like staying on topic, I'm going to lock this.