Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Virtual File System

edited June 27 in Code Sharing Posts: 198

Hi All,

I’ve recently been working on a virtual file system library to restore some functionality missing from the usual Lua file io API. Namely, folder management.

The library maintains a current working directory and file paths can be provided using an absolute filepath (”/demo/my_sub_dir/cool.txt”) or relative to the current working directory (”..” not currently supported). Following the load of a virtual filesystem, the working directory points to the root ”/“ by default.

The files themselves when viewed in the iOS Files app will simply use numbers as filenames and no folders will actually be visible. This is primarily intended to ease runtime filesystem usage.

The following short examples display the libraries capability:

  • Creating or loading a virtual file system named demo (multiple can be open at once)
    local vfs = VFS(“demo”)

  • Formatting a virtual file system object
    vfs:format()

  • Creating a new folder at the root of the vfs
    vfs:mkdir(“/super_awesome_folder”)

  • Changing the current working directory
    vfs:cd(“super_awesome_folder”) — Use relative path

  • Create a new file in the current working directory (the file object is the same return value as io.open())

    local file = vfs:open(“demo.txt”, “w”)
    file:write(“Hello Codea Community!”)
    file:close()
  • Reading a file in the current working directory (the file object is the same return value as io.open())

    local file = vfs:open(“demo.txt”, “r”)
    print(file:read(“*a”)) — Print the content of the file
    file:close()
  • List the contents of a folder (return values are arrays of strings)
    local files, dirs = vfs:ls(“/super_awesome_folder”)

  • Delete a file
    vfs:rm(“/super_awesome_folder/demo.txt”)

  • Delete a folder (this is recursive so will also delete the folder’s content)
    vfs:rm(“/super_awesome_folder”)

The main purpose of this for myself is to handle backing storage for the next evolution of WebRepo so stay tuned for cool things to come!

This is also available for download using WebRepo

I hope this helps someone :smile:

Cheers,
Steppers

Sign In or Register to comment.