The_Programmers_ByAble/Files; an overview.md
2023-06-26 03:03:51 -05:00

1.7 KiB

Continuing from the previous post we will define a very minimal example of a file and API

Here is our file definition

struct File{}

Files could have some metadata like a name

struct File{
    @documentation["The length of the file name is limited to 100 because of simplicity"]
    @validator[Length(100)]
    name: String,
}

and of course the data

struct File{
    @documentation["The length of the file name is limited to 100 because of simplicity"]
    @validator[Length(100)]
    name: String,
    @documentation["Untyped raw bytes of the file"]
    data: Vector<u8>,
}

Now we come to the fun part. Bikeshedding the api.

I propose a simple read/write/open/close api that supports offsets for read/write

struct File{
    @documentation["The length of the file name is limited to 100 because of simplicity"]
    @validator[Length(100)]
    name: String,
    @documentation["Untyped raw bytes of the file"]
    data: Vector<u8>,

    @documentation["The offset is to allow you do reads of specific data like the header of a file"]
    read: function(offset: u64, length: u64) -> Vector<u8>;

    @documentation["The offset is to allow you do write to specific data like the header of a file"]
    write: function(offset: u64, data: Vector<u8>) -> None;
    @documentation["Open a file based on a path"]
    open: function(Path) -> File;
    @documentation["Close a file and save all changes"]
    close: function(None) -> None;
}

Small post this time that is more hand holdy. Next time will be a full filesystem definition as well as a virtual file system.