Skip to content

Instantly share code, notes, and snippets.

@jaredallard
Created May 31, 2015 05:22
Show Gist options
  • Save jaredallard/eb221011a7d73d7c6fa6 to your computer and use it in GitHub Desktop.
Save jaredallard/eb221011a7d73d7c6fa6 to your computer and use it in GitHub Desktop.
ccDocker's cext fs implementation in the works!
--[[
A lightweight ext-like FS implemented in pure lua.
Specs: <insert link here>
@author Jared Allard <jaredallard@outlook.com>
@license MIT
@notice uses JSDoc like insource documention.
]]
-- intial declearation of the FS table.
cext = {
["version"] = 001,
["date"] = 20150530,
["inodeAllocations"] = 500, -- amount of lines to save for inodes untill dynamic.
["print"] = function(msg)
print(tostring(msg))
end,
["fs"] = nil, -- assurance.
}
-- temporary.
-- split a string
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
--[[
Create a filesystem.
@return {boolean} - success or failure
]]
cext.createFS = function(this, location, size)
-- this.checkArgs(this, size)
-- superblockHeader.
local superblockHeader = this.version..",0,"..size..",2"
local inodeAllocation = ""
-- generate whitespace.
this.print("generating lines of whitespace.")
for i = 1,this.inodeAllocations do
inodeAllocation = inodeAllocation.."\n"
end
-- remove a stale filesystem.
if fs.exists(location) then
this.print("removing stale fs")
fs.delete(location)
end
-- open a io file handle and inject the superblockHeader & inodeAllocation ws.
this.print("writing fs")
local iohandle = io.open(location, "w")
iohandle:write(superblockHeader..inodeAllocation)
iohandle:close()
this.print("done")
-- return true since we probably had no issues.
return true
end
--[[
Open the file system (essentially load it into memory)
@return {boolean} - success or fail.
]]
cext.open = function(this, fs)
-- give the fs object an initial start.
this.fs = {
["superblock"] = nil,
["raw"] = nil,
}
-- load the raw filesystem.
local iohandle = io.open(fs)
this.fs.superblock = iohandle.read("*line")
this.fs.raw = this.fs.superblock.."\n"..iohandle.read("*all")
iohandle:close()
-- look at the superblock for the next available inode line.
local superblockSplit = string.split(this.fs.superblock, ",")
-- assign it to the table.
this.fs.superblock = {}
this.fs.superblock.version = superblockSplit[1]
this.fs.superblock.inodes = superblockSplit[2]
this.fs.superblock.size = superblockSplit[3]
this.fs.superblock.nextInode = superblockSplit[4]
print('opened cext filesystem version: '..this.fs.superblock.version)
-- return true as we probably had no issues.
return true
end
--[[
Write to the filesystem
@return {boolean} - success or fail, if fail then error message
on second param return.
]]
cext.write = function(this, filename, data)
if this.fs == nil then
error("fs isn't loaded. load with this:open(fs)")
end
local fileLocation = fs.getDir(filename)
local fileName = fs.getName(filename)
-- generate the object.
this.print("generating inode for '"..tostring(filename).."'")
local inodeObject = fileLocation..","..fileName
print(inodeObject)
return true
end
-- TESTING
cext:createFS("test.fs", 1000)
cext:open("test.fs")
cext:write("file", "data\nmoredata\ndata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment