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

VFS - Virtual File System

Started by Mr. Bateman, 22 June 2014 - 03:14 AM
Mr. Bateman #1
Posted 22 June 2014 - 05:14 AM
Virtual Filesystem


The VFS API hooks onto the fs API and allows "images" to be loaded into RAM and be treated as normal file systems. Any image that gets loaded will be given the "//" prefix instead of the normal "/" prefix. Example: If I loaded an image named "ext1", it will be accessible via "//ext1/".

Each image follows a certain format:

image = {

	name = "name",
	files = {

		file = {
			data = "",
			isDir = bool,
			isReadOnly = bool
		}
	}
}

name: The name of the image, ie. "ext1" in my example
files: A table of files.

Each file is a pair, the key is the relative name ("//ext1/directory/file" will be stored as "directory/file") and the value is table containing data associated with the file.

data: A string of hex (not pure ASCII) to preserve byte values due to LuaJ handling of strings
isDir: A boolean value representing if the file is a directory
isReadOnly: A boolean value representing if the file is readonly (most likely not)

Documentation:
SpoilerWarning: This API modifies the functions of the fs API to allow virtual paths, so beware if you need to use the exact fs functions
If you really do need the exact fs functions / having trouble using my modified ones, the original functions are avaliable at vfs.oldFS

vfs.mount( table image )
returns nil
Mounts image, allowing it to be accessed by the filesystem

vfs.unmount( string name )
returns nil
Unmounts a mounted image with the name of "name"

vfs.getMountedImage( string name )
returns table (image)
Returns a mounted image with the name of "name"

vfs.createImage( string name )
returns table (image)
Returns a blank image with the name of "name"

vfs.addFile( table image, string filePath )
returns nil
Opens the file at filePath and adds it to "image"

vfs.removeFile( table image, string path )
returns nil
Removes a file with the path of "path" from "image"
Doesn't error if the file doesn't exist

vfs.exportImage( string name )
returns table (image)
Returns an image with the name of "name" and has the contents of the entire HDD of the computer (excluding rom and any mounted images, but also includes any connected floppy disks (bug?))

vfs.importImage( table image, [ boolean append ] )
returns nil
Copies the contents of the image to the computer HDD, and if append is nil or false, it calls vfs.format()

vfs.format()
returns nil
Deletes all the contents of the HDD on the computer (including attached disks, is this a bug?)

vfs.isVirtual( string path )
returns boolean
Returns if the path is virtual

Other important variables:
vdrives: The global table that vfs uses to store mounted images
invalidNames: A list of names that vfs cannot use to retain compatibility with already-existing functions
oldFS: A table that contains the original (non-patched) fs functions

Link: http://pastebin.com/ZT1euz3x
Or alternatively (preferred):
pastebin get ZT1euz3x vfs

Please tell me any bugs or suggestions, as I have vigorously bugtested this myself but I cannot find all of the bugs in every scenario!
Edited on 14 August 2014 - 03:12 AM
Zudo #2
Posted 22 June 2014 - 09:35 AM
The best first post I have ever seen :D/> (seriously)
Edited on 22 June 2014 - 07:35 AM
Mr. Bateman #3
Posted 22 June 2014 - 10:01 AM
The best first post I have ever seen :D/> (seriously)
Thanks, I had to fight with chrome since it would keep jumping to random positions in the text every time I wanted to backspace.
oeed #4
Posted 22 June 2014 - 10:34 AM
I have to say, this looks pretty nifty!
Mr. Bateman #5
Posted 22 June 2014 - 01:26 PM
I have to say, this looks pretty nifty!
The original idea was to make a system that had the ability to use files on another computer (hence the "//" prefix), but this was a prerequisite.
Mr. Bateman #6
Posted 14 August 2014 - 05:14 AM
I've updated this API to use hex strings instead of a byte table for storing data, as it would cause serialized tables to be saved at a ratio of 1250% compared to 200% now (not to mention really long load times)
This means that all current images created are no longer compatible, so make sure to copy the data via fs.copy or the inbuilt program to preserve it.
tenshae #7
Posted 30 August 2014 - 05:44 AM
Can I use this to make a sandboxed filesystem for the kernel of my OS?
Mr. Bateman #8
Posted 30 August 2014 - 06:44 AM
Can I use this to make a sandboxed filesystem for the kernel of my OS?
It doesn't really work like that, as it creates a new path at //[your drive name here], allowing memory to be used as disk space.
But if you want to make a sandbox to cut off access to paths that are not allowed, I can create a kernel that does that.
tenshae #9
Posted 03 September 2014 - 06:02 AM
-snippety-
It doesn't really work like that, as it creates a new path at //[your drive name here], allowing memory to be used as disk space.
-snippety snippety-
I moved on to a different approach. Thanks for offering your services, but they won't be required! :)/>