hollyhock
Loading...
Searching...
No Matches
file.h File Reference

Functions for interacting with the calculator's file system. More...

#include <stdint.h>
#include <stddef.h>
Include dependency graph for file.h:

Go to the source code of this file.

Classes

struct  File_Stat

Macros

#define constexpr
#define cstr(x)
#define ro(...)
#define rw(...)
#define wo(...)
#define wu   __attribute__((warn_unused_result))

Enumerations

enum  File_Error {
  FILE_OK = 0 , FILE_ENOMEM = -1 , FILE_EINVAL = -2 , FILE_EDEVFAIL = -3 ,
  FILE_EMOUNTED = -4 , FILE_EACCES = -5 , FILE_EBADFSID = -6 , FILE_ENOVOLUME = -7 ,
  FILE_ENOPATH = -8 , FILE_EEXIST = -9 , FILE_ENAMETOOLONG = -10 , FILE_EOUTOFBOUND = -11 ,
  FILE_EUNFORMAT = -12 , FILE_ENOSPC = -13 , FILE_ENOENT = -14 , FILE_EISDIRECTORY = -15 ,
  FILE_ESHARE = -16 , FILE_EMFILE = -17 , FILE_EBADF = -18 , FILE_EEOF = -19 ,
  FILE_ENOTEMPTY = -20 , FILE_ECLUSTERSIZEMISMATCH = -40 , FILE_ESYSTEM = -99
}
enum  File_Whence { FILE_SEEK_SET = 0 , FILE_SEEK_CUR = 1 , FILE_SEEK_END = 2 }
enum  File_Open { FILE_OPEN_READ = 1 << 0 , FILE_OPEN_WRITE = 1 << 1 , FILE_OPEN_CREATE = 1 << 2 , FILE_OPEN_APPEND = 1 << 4 }
enum  File_Attributes {
  FILE_ATTR_READ_ONLY = 0x01 , FILE_ATTR_HIDDEN = 0x02 , FILE_ATTR_SYSTEM = 0x04 , FILE_ATTR_DIRECTORY = 0x10 ,
  FILE_ATTR_ARCHIVE = 0x20 , FILE_ATTR_SETTABLE_MASK = 0x37
}

Functions

struct __attribute__ ((packed, aligned(4))) File_FsInfo

Variables

enum File_Error(* File_Close )(int fd)
enum File_Error(* File_Fstat )(int fd, struct File_Stat *buf) wu wo(2)
enum File_Error(* File_GetAddr )(int fd, int offset, const void **addr) wu wo(3)
int(* File_Lseek )(int fd, int offset, enum File_Whence whence) wu
enum File_Error(* File_Mkdir )(const char *path) wu cstr(1) ro(1)
int(* File_Open )(const char *path, int flags) wu cstr(1) ro(1)
int(* File_Read )(int fd, void *buf, int count) wu wo(2
int enum File_Error(* File_Remove )(const char *path) cstr(1) ro(1)
enum File_Error(* File_Rename )(const char *oldPath, const char *newPath) wu cstr(1) cstr(2) ro(1) ro(2)
enum File_Error(* File_Stat )(const char *path, struct File_Stat *buf) wu cstr(1) ro(1) wo(2)
int(* File_Write )(int fd, const void *buf, int count) wu ro(2
int enum File_Error(* File_Flush )(int fd) wu
enum File_Error(* File_FsInfo )(const char *device, struct File_FsInfo *info) wu cstr(1) ro(1) wo(2)
enum File_Error(* File_SetAttr )(const char *path, const struct File_Stat *info) wu cstr(1) ro(1) ro(2)
int(* File_Reclaim )(const char *device, int workLimit) wu cstr(1) ro(1)
enum File_Error(* File_Salvage )(const char *device) wu cstr(1) ro(1)

Detailed Description

Functions for interacting with the calculator's file system.

Exposes the Fugue atomic API, with byte-string paths, Fugue descriptors and Fugue error codes. Directory searches belong to sdk/os/bfile.h and use a different descriptor, path encoding and error namespace.

The storage which appears when the calculator is attached to a PC is found under the path \fls0\ (remember to escape the \ in strings!).

Example: reading 256 bytes from a file called test.txt from the USB flash

int fd = File_Open("\\fls0\\test.txt", FILE_OPEN_READ);
if (fd < 0) {
// An error occurred calling open
goto exit;
}
uint8_t buf[256];
int ret = File_Read(fd, buf, sizeof(buf));
if (ret < 0) {
// An error occurred calling read
goto exit;
}
ret = File_Close(fd);
if (ret < 0) {
// An error occurred calling close
}
exit:
File_Open
Definition file.h:131
@ FILE_OPEN_READ
Open the file as readable.
Definition file.h:133
enum File_Error(* File_Close)(int fd)
int(* File_Read)(int fd, void *buf, int count) wu wo(2

Example: writing 16 bytes to a non-existant file called f.bin in a folder test in the USB flash

int fd = File_Open("\\fls0\\test\\f.bin", FILE_OPEN_WRITE | FILE_OPEN_CREATE);
if (fd < 0) {
// An error occurred calling open
goto exit;
}
uint8_t buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
int ret = File_Write(fd, buf, sizeof(buf));
if (ret < 0) {
// An error occurred calling write
goto exit;
}
ret = File_Close(fd);
if (ret < 0) {
// An error occurred calling close
}
exit:
@ FILE_OPEN_CREATE
Create the file, if it does not already exist.
Definition file.h:137
@ FILE_OPEN_WRITE
Open the file as writable.
Definition file.h:135
int(* File_Write)(int fd, const void *buf, int count) wu ro(2

Macro Definition Documentation

◆ cstr

#define cstr ( x)
Value:
__attribute__((null_terminated_string_arg(x)))

◆ ro

#define ro ( ...)
Value:
__attribute__((access(read_only, __VA_ARGS__)))

◆ rw

#define rw ( ...)
Value:
__attribute__((access(read_write, __VA_ARGS__)))

◆ wo

#define wo ( ...)
Value:
__attribute__((access(write_only, __VA_ARGS__)))

Enumeration Type Documentation

◆ File_Attributes

FAT attribute bits returned in File_Stat.attributes.

Enumerator
FILE_ATTR_SETTABLE_MASK 

Clear other status bits before passing a stat record to File_SetAttr.

◆ File_Error

enum File_Error

Errors returned by file system functions. All negative numbers.

◆ File_Open

enum File_Open

Values passed as the flags parameter to File_Open. Can be bitwise OR'd to combine effects.

Enumerator
FILE_OPEN_READ 

Open the file as readable.

FILE_OPEN_WRITE 

Open the file as writable.

FILE_OPEN_CREATE 

Create the file, if it does not already exist.

FILE_OPEN_APPEND 

Opens the file with the file offset set to the end of the file.

◆ File_Whence

Values passed as the whence parameter to File_Lseek.

Enumerator
FILE_SEEK_SET 

Set the file offset to offset.

FILE_SEEK_CUR 

Set the file offset to the current position, plus offset bytes.

FILE_SEEK_END 

Set the file offset to the end of the file, plus offset bytes.

Function Documentation

◆ __attribute__()

struct __attribute__ ( (packed, aligned(4)) )

Capacity and geometry returned by File_FsInfo.

The firmware writes a 0x34-byte, 4-byte-aligned record. The three byte counts are 64-bit values in native byte order. Reserved words are output fields with OS-specific meanings; callers should not interpret them.

Variable Documentation

◆ File_Close

enum File_Error(* File_Close) (int fd) ( int fd)
extern

Closes an open file.

Parameters
fdThe file descriptor for the open file.
Returns
0 on success, or a negative error code on failure.

◆ File_FsInfo

enum File_Error(* File_FsInfo) (const char *device, struct File_FsInfo *info) wu cstr(1) ro(1) wo(2) ( const char * device,
struct File_FsInfo * info )
extern

Query a mounted device's capacity and geometry.

Parameters
deviceDevice root without a trailing separator, eg. "\\fls0".
[out]infoCapacity and geometry record.
Returns
0 on success, or a Fugue error.

◆ File_Fstat

enum File_Error(* File_Fstat) (int fd, struct File_Stat *buf) wu wo(2) ( int fd,
struct File_Stat * buf )
extern

Retrieves information about an open file.

Parameters
fdThe file descriptor of an open file to retrieve the information of.
[out]bufThe retrieved information about the file.
Returns
0 on success, or a negative error code on failure.

◆ File_GetAddr

enum File_Error(* File_GetAddr) (int fd, int offset, const void **addr) wu wo(3) ( int fd,
int offset,
const void ** addr )
extern

Retrieves the memory address of a file.

If the file is empty or the offset would point outside of the file, FILE_EINVAL is returned.

Parameters
fdThe file descriptor of an open file.
offsetAn offset to apply to the pointer to the file's data.
[out]addrThe address of the file's data.
Returns
0 on success, or a negative error code on failure.

◆ File_Lseek

int(* File_Lseek) (int fd, int offset, enum File_Whence whence) wu ( int fd,
int offset,
enum File_Whence whence )
extern

Repositions the file offset of the file descriptor. The new position depends on the value of both offset and whence. See lseek_whence_values.

Parameters
fdThe file descriptor of an open file to change the file offset of.
offsetThe new offset, relative to some point.
whenceWhere offset is relative to.
Returns
The new file offset on success, or a negative error code on failure.

◆ File_Mkdir

enum File_Error(* File_Mkdir) (const char *path) wu cstr(1) ro(1) ( const char * path)
extern

Creates a directory.

Parameters
[in]pathThe path to the directory to be created.
Returns
0 on success, or a negative error code on failure.

◆ File_Open

int(* File_Open) (const char *path, int flags) wu cstr(1) ro(1) ( const char * path,
int flags )
extern

Opens a file on the file system.

The argument flags is fed a bitwise OR'd combination of flags. See open_flags_values.

Parameters
[in]pathThe path to the file to open.
flagsA bitfield describing the mode in which to open the file.
Returns
A file descriptor on success, or a negative error code on failure.

◆ File_Read

int(* File_Read) (int fd, void *buf, int count) wu wo(2 ( int fd,
void * buf,
int count )
extern

Reads up to count bytes from a file, and stores them in buf.

If count bytes cannot be read from the file, as many bytes as possible are read (i.e. between 0 and count - 1 bytes are read and stored in buf).

Parameters
fdThe file descriptor of an open file to read from.
[out]bufA buffer to place the read bytes into.
countThe maximum number of bytes to read.
Returns
The number of bytes read on success, or a negative error code on failure.

◆ File_Reclaim

int(* File_Reclaim) (const char *device, int workLimit) wu cstr(1) ro(1) ( const char * device,
int workLimit )
extern

Run the mounted device's reclaim/garbage-collection operation.

Parameters
deviceDevice root without a trailing separator, eg. "\\fls0".
workLimitNonnegative OS-specific work limit; its unit is not known.
Returns
A nonnegative device-specific result, or a Fugue error.

◆ File_Remove

int enum File_Error(* File_Remove) (const char *path) cstr(1) ro(1) ( const char * path)
extern

Deletes a file or directory.

Parameters
[in]pathThe path to the file or directory to be deleted.
Returns
0 on success, or a negative error code on failure.

◆ File_Rename

enum File_Error(* File_Rename) (const char *oldPath, const char *newPath) wu cstr(1) cstr(2) ro(1) ro(2) ( const char * oldPath,
const char * newPath )
extern

Renames a file or directory.

Parameters
[in]oldPathThe path to the file or directory to be renamed.
[in]newPathThe path to the new name for the file or directory.
Returns
0 on success, or a negative error code on failure.

◆ File_Salvage

enum File_Error(* File_Salvage) (const char *device) wu cstr(1) ro(1) ( const char * device)
extern

Repair a mounted filesystem's allocation metadata.

This is a potentially long-running, metadata-changing maintenance operation. The filesystem must not be in use; busy states return FILE_EACCES.

Parameters
deviceDevice root without a trailing separator, eg. "\\fls0".
Returns
0 on success, or a Fugue error.

◆ File_SetAttr

enum File_Error(* File_SetAttr) (const char *path, const struct File_Stat *info) wu cstr(1) ro(1) ro(2) ( const char * path,
const struct File_Stat * info )
extern

Update FAT attributes and timestamps from a File_Stat record.

Obtain the record with File_Stat first so existing timestamps are preserved. Mask attributes with FILE_ATTR_SETTABLE_MASK and preserve the directory bit: this call cannot change a file into a directory or vice versa. fileSize is ignored; this operation does not resize the file.

Returns
0 on success, or a Fugue error.

◆ File_Stat

enum File_Error(* File_Stat) (const char *path, struct File_Stat *buf) wu cstr(1) ro(1) wo(2) ( const char * path,
struct File_Stat * buf )
extern

Retrieves information about a file.

Parameters
[in]pathThe path to the file to retrieve the information of.
[out]bufThe retrieved information about the file.
Returns
0 on success, or a negative error code on failure.

◆ File_Write

int(* File_Write) (int fd, const void *buf, int count) wu ro(2 ( int fd,
const void * buf,
int count )
extern

Writes count bytes from buf to a file.

Parameters
fdThe file descriptor of an open file to write to.
[in]bufA buffer of bytes to write to the file.
countThe number of bytes from buf to write to the file.
Returns
The number of bytes written on success, or a negative error code on failure.