File System¶
Provides file system operations.
libuv reference: http://docs.libuv.org/en/v1.x/fs.html
Input Events¶
UV_FS¶
input _uv_fs_t&& UV_FS;
- Occurrence:
- Whenever a filesystem operation completes.
- Payload:
_uv_fs_t&&
: pointer to the operation request
libuv reference: http://docs.libuv.org/en/v1.x/fs.html
Data Abstractions¶
UV_FS_File¶
data UV_FS_File with
event void ok;
var int fd;
end
Code Abstractions¶
UV_FS_Open¶
Opens a file.
code/await UV_FS_Open (var _char&& path, var int flags, var int mode)
-> (var& UV_FS_File file)
-> int
- Parameters
path
: path to the fileflags
: access mode flagsmode
: file permission mode
- Initialization
file
: created file handle
- Return
int
: open error- returns only case of error (always
<0
)
- returns only case of error (always
The file is only ready for use after UV_FS_Open
triggers file.ok
.
Céu-libuv references:
UV_FS
.
libuv references:
ceu_uv_fs_open
,
uv_fs_close
,
uv_fs_req_cleanup
.
Note: all allocated libuv resources are automatically released on termination.
Example¶
Opens file.txt
and prints open ok after the file is ready for use.
In case of failure, prints open error along with the error code:
#include "uv/fs.ceu"
var& UV_FS_File file;
var int? err =
watching UV_FS_Open("file.txt", _O_RDONLY, 0) -> (&file) do
await file.ok;
// file is ready for use
_printf("open ok\n");
end;
if err? then
_printf("open error: %d\n", err!);
end
escape 0;
UV_FS_Read¶
Reads bytes from a file.
code/await UV_FS_Read (var& UV_FS_File file, vector&[] byte buf, var usize size, var usize offset)
-> ssize
- Parameters
file
: file handle to read frombuf
: destination buffersize
: number of bytes to readoffset
: starting file offset
- Return
ssize
: actual number of bytes read>=0
: number of bytes<0
: read error
Céu-libuv references:
ceu_uv_fs_read
,
UV_FS
.
libuv references:
uv_buf_init
,
uv_fs_req_cleanup
.
Note: all allocated libuv resources are automatically released on termination.
Example¶
Prints the contents of file.txt
in a loop that reads the file in chunks of 10
bytes:
#include "uv/fs.ceu"
var& UV_FS_File file;
var int? err =
watching UV_FS_Open("file.txt", _O_RDONLY, 0) -> (&file) do
await file.ok;
var usize offset = 0;
loop do
vector[11] byte buf;
var ssize n = await UV_FS_Read(&file,&buf,$$buf-1,offset);
if n == 0 then
break;
end
buf = buf .. [{'\0'}];
_printf("%s", &&buf[0]);
offset = offset + ($$buf-1);
end
end;
_ceu_dbg_assert(not err?);
escape 0;
UV_FS_ReadLine¶
Reads a line from a file.
code/await UV_FS_ReadLine (var& UV_FS_File file, vector&[] byte buf, var usize offset)
-> ssize
- Parameters
file
: file handle to read frombuf
: destination buffer (excludes the leading\n
)offset
: starting file offset
- Return
ssize
: actual number of bytes read>=0
: number of bytes (includes the leading\n
)<0
: read error
TODO: the file is currently read byte by byte.
Céu-libuv references:
UV_FS_Read
.
Example¶
Prints the contents of file.txt
in a loop that reads the file line by line:
#include "uv/fs.ceu"
var& UV_FS_File file;
watching UV_FS_Open("file.txt", _O_RDONLY, 0) -> (&file) do
await file.ok;
var usize off = 0;
loop do
vector[] byte line;
var ssize n = await UV_FS_ReadLine(&file,&line,off);
if n <= 0 then
break;
end
_printf("line = %s [%d]\n", &&line[0], n as int);
off = off + (n as usize);
end
end
escape 0;
UV_FS_Write¶
Write bytes from a file.
code/await UV_FS_Write (var& UV_FS_File file, vector&[] byte buf, var usize size, var usize offset)
-> ssize
- Parameters
file
: file handle to write tobuf
: source buffersize
: number of bytes to writeoffset
: starting file offset
- Return
ssize
: actual number of bytes written>=0
: number of bytes<0
: write error
Céu-libuv references:
ceu_uv_fs_write
,
UV_FS
.
libuv references:
uv_buf_init
,
uv_fs_req_cleanup
.
Note: all allocated libuv resources are automatically released on termination.
Example¶
Writes the string Hello World to hello.txt
:
#include "uv/fs.ceu"
var& UV_FS_File file;
var _mode_t mode = _S_IRUSR|_S_IWUSR|_S_IRGRP|_S_IWGRP|_S_IROTH;
var int? err =
watching UV_FS_Open("hello.txt", _O_CREAT|_O_WRONLY, mode) -> (&file) do
await file.ok;
vector[] byte buf = [] .. "Hello World!\n";
var ssize n = await UV_FS_Write(&file,&buf,$buf,0);
if (n<0) or (n as usize)!=$buf then
_printf("write error\n");
end
end;
if err? then
_printf("open error: %d\n", err!);
end
escape 0;
UV_FS_Fstat¶
Reads information about a file.
code/await UV_FS_Fstat (var& UV_FS_File file, var& _uv_stat_t stat)
-> int
- Parameters
file
: file handle to write tostat
: destination buffer
- Return
int
: operation status0
: success<0
: error
Céu-libuv references:
ceu_uv_fs_fstat
,
UV_FS
.
libuv references:
uv_fs_req_cleanup
.
Note: all allocated libuv resources are automatically released on termination.
Example¶
Prints the size of file.txt
in bytes:
#include "uv/fs.ceu"
var& UV_FS_File file;
var int? err =
watching UV_FS_Open("file.txt", _O_RDONLY, 0) -> (&file)
do
await file.ok;
var _uv_stat_t stat = _;
await UV_FS_Fstat(&file, &stat);
_printf("size = %ld\n", stat.st_size);
end;
if err? then
_printf("open error: %d\n", err!);
end
escape 0;