200 lines
6.5 KiB
HTML
200 lines
6.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="description" content="MontaukOS manual page: file(2) - file I/O system calls">
|
|
<title>file(2) - MontaukOS Manual</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" rel="stylesheet">
|
|
<style>
|
|
body {
|
|
font-family: 'Open Sans', Arial, sans-serif;
|
|
max-width: 960px;
|
|
margin: 0 auto;
|
|
padding: 1em;
|
|
line-height: 1.5;
|
|
display: flex;
|
|
gap: 2em;
|
|
}
|
|
p { margin: 0 0 0.5em; }
|
|
h2 { margin: 0.5em 0; }
|
|
.sidebar {
|
|
width: 160px;
|
|
flex-shrink: 0;
|
|
}
|
|
.sidebar ul {
|
|
list-style: none;
|
|
padding: 0;
|
|
}
|
|
.sidebar li {
|
|
margin: 0.5em 0;
|
|
}
|
|
.sidebar a {
|
|
color: #0066CC;
|
|
text-decoration: none;
|
|
font-weight: 600;
|
|
}
|
|
.sidebar a:hover {
|
|
color: #004499;
|
|
text-decoration: underline;
|
|
}
|
|
.sidebar .current {
|
|
color: #004499;
|
|
}
|
|
.sidebar hr {
|
|
border: none;
|
|
border-top: 1px solid #999;
|
|
margin: 0.75em 0;
|
|
}
|
|
.main {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
a { color: #0000EE; }
|
|
a:visited { color: #0066CC; }
|
|
hr { border-style: solid; border-width: 1px 0 0 0; border-color: #999; }
|
|
pre { background: #f0f0f0; padding: 0.5em; overflow-x: auto; }
|
|
code { background: #f0f0f0; padding: 0 0.15em; }
|
|
pre code { padding: 0; }
|
|
.center { text-align: center; }
|
|
.box {
|
|
border: 1px solid #999;
|
|
padding: 0.5em;
|
|
margin: 0.5em 0;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="sidebar">
|
|
<ul>
|
|
<li><a href="../../index.html">Home</a></li>
|
|
<li><a href="../index.html">Documentation</a></li>
|
|
</ul>
|
|
<hr>
|
|
<ul>
|
|
<li><a href="index.html" class="current">Man Pages</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="main">
|
|
<div class="center">
|
|
<h1>file(2)</h1>
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<pre><code><strong>NAME</strong>
|
|
open, read, getsize, close, readdir - file I/O system calls
|
|
|
|
<strong>SYNOPSIS</strong>
|
|
<strong> int montauk::open(const char* path);</strong>
|
|
<strong> int montauk::read(int handle, uint8_t* buf, uint64_t offset, uint64_t size);</strong>
|
|
<strong> uint64_t montauk::getsize(int handle);</strong>
|
|
<strong> void montauk::close(int handle);</strong>
|
|
<strong> int montauk::readdir(const char* path, const char** names, int max);</strong>
|
|
|
|
<strong>DESCRIPTION</strong>
|
|
MontaukOS provides a Virtual File System (VFS) with read/write
|
|
support. Drive 0 is the boot ramdisk; additional drives may be
|
|
mounted from GPT partitions backed by FAT32 or ext2 (see
|
|
syscalls(2), STORAGE section). Files are accessed via paths in
|
|
the format "<drive>:/<path>".
|
|
|
|
<strong>open</strong>
|
|
Opens a file and returns a non-negative handle on success, or a
|
|
negative value on error (file not found, no free handles).
|
|
|
|
int h = montauk::open("0:/os/hello.elf");
|
|
|
|
<strong>read</strong>
|
|
Reads up to 'size' bytes starting at 'offset' into 'buf'.
|
|
Returns the number of bytes actually read, or negative on error.
|
|
There is no implicit file position -- the offset is explicit on
|
|
every call.
|
|
|
|
uint8_t buf[512];
|
|
int n = montauk::read(h, buf, 0, 512);
|
|
|
|
<strong>getsize</strong>
|
|
Returns the total size in bytes of the file.
|
|
|
|
uint64_t sz = montauk::getsize(h);
|
|
|
|
<strong>close</strong>
|
|
Closes the file handle and frees kernel resources.
|
|
|
|
montauk::close(h);
|
|
|
|
<strong>readdir</strong>
|
|
Lists entries in a directory. Up to 'max' entry names (VFS cap
|
|
256, driver-backed listings such as 0:/os/ cap 128) are written
|
|
to the 'names' array. The kernel allocates a user-accessible
|
|
page for the string data automatically. Directory entries are
|
|
returned with a trailing slash.
|
|
|
|
const char* entries[64];
|
|
int count = montauk::readdir("0:/", entries, 64);
|
|
// entries: "os/", "apps/", "man/", "www/", "users/", ...
|
|
|
|
For directories that may contain more entries than fit in one
|
|
call, use montauk::readdir_at(path, names, max, startIndex) and
|
|
advance startIndex by the returned count until it returns 0.
|
|
|
|
<strong>READING PATTERN</strong>
|
|
The standard pattern for reading a file:
|
|
|
|
int h = montauk::open("0:/man/intro.1");
|
|
uint64_t size = montauk::getsize(h);
|
|
uint8_t buf[512];
|
|
uint64_t off = 0;
|
|
while (off < size) {
|
|
uint64_t chunk = size - off;
|
|
if (chunk > 511) chunk = 511;
|
|
int n = montauk::read(h, buf, off, chunk);
|
|
if (n <= 0) break;
|
|
buf[n] = '\0';
|
|
montauk::print((const char*)buf);
|
|
off += n;
|
|
}
|
|
montauk::close(h);
|
|
|
|
<strong>WRITING, DELETING, RENAMING</strong>
|
|
<strong> int montauk::fcreate(const char* path);</strong>
|
|
<strong> int montauk::fwrite(int handle, const uint8_t* buf, uint64_t offset, uint64_t size);</strong>
|
|
<strong> int montauk::fdelete(const char* path);</strong>
|
|
<strong> int montauk::fmkdir(const char* path);</strong>
|
|
<strong> int montauk::frename(const char* oldPath, const char* newPath);</strong>
|
|
|
|
fcreate creates a new file and returns a handle. fwrite writes
|
|
bytes at the given offset. fdelete removes a file, fmkdir
|
|
creates a directory, and frename renames or moves a file or
|
|
directory (the basis for file manager move operations).
|
|
|
|
On drive 0 (the ramdisk), changes persist only until reboot --
|
|
the ramdisk is reloaded from the USTAR archive on each boot. On
|
|
disk-backed drives (FAT32/ext2 partitions mounted with
|
|
montauk::fs_mount), changes are written through to storage; use
|
|
montauk::fs_sync() to flush caches before power-off.
|
|
|
|
<strong>NOTES</strong>
|
|
Drive 0 is loaded at boot from a USTAR tar archive into RAM.
|
|
Other drives are mounted on demand from GPT partitions on
|
|
SATA/NVMe/USB block devices; see syscalls(2), STORAGE and
|
|
DEVICES sections.
|
|
|
|
<strong>SEE ALSO</strong>
|
|
syscalls(2), spawn(2), malloc(3)</code></pre>
|
|
|
|
<hr>
|
|
|
|
<div class="center">
|
|
<a href="../index.html">Back to Documentation Index</a>
|
|
</div>
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|