Skip to content

C++ class: NativeFile

Index · Français

Description

Noncopyable owner of an x64 file or directory handle.

Qualified name : gaspardos::NativeFile.

Header : bindings/cpp/gaspardos.hpp. C++98.

When to use it

Access native services outside stdio.

Simple program example

#include "gaspardos.hpp"
#include <cstdio>
#include <cstring>

int main()
{
    gaspardos::NativeFile file;
    if (file.open(L"\\AHCIDISK0\\readme.txt", GO64_RDONLY) < 0)
        return 1;
    char    data[128];
    int64_t count  = file.read(data, sizeof(data));
    int64_t closed = file.close();
    return count >= 0 && closed == 0 ? 0 : 1;
}

Building from Windows

Save the program above as build/api-examples/NativeFile.cpp from the repository root. Create the directory if necessary.

PowerShell — from the repository root:

New-Item -ItemType Directory -Force build/api-examples | Out-Null
& .\build-x64-install\bin\x86_64-gaspardos-gcc.exe -std=gnu1x -O2 -mno-red-zone -c bindings/native/gaspardos_x64_syscall.c -o build/api-examples/syscall.o
if ($LASTEXITCODE -ne 0) { throw "C compilation failed" }
& .\build-x64-install\bin\x86_64-gaspardos-g++.exe -std=c++98 -O2 -Ibindings/cpp build/api-examples/NativeFile.cpp build/api-examples/syscall.o -o build/api-examples/NativeFile-x64.elf
if ($LASTEXITCODE -ne 0) { throw "C++ compilation/link failed" }

Run the ELF on GaspardOS, not Windows. The driver supplies CRT and libraries. Prepare the required volumes/services; examples bound their waits and may exit on backpressure rather than retry. Successful compilation does not validate kernel execution.

The headers, implementation files, native wrappers and complete examples are available under Source files.

Class methods

NativeFile()

Initial handle is -1.

~NativeFile()

Try close once.

int64_t handle() const

Raw handle, -1 when closed.

int64_t close()

Zero when closed; retain the handle on error, including -11.

int64_t open(const wchar_t* path, uint64_t flags)

32-bit wchar_t path; GO64 flags; -16 if busy; return a handle or negative error.

int64_t read(void* p, uint64_t n)

Byte count or negative error; short reads are possible.

int64_t write(const void* p, uint64_t n)

Byte count or negative error; advance the buffer after a short write.

int64_t seek(int64_t offset, uint64_t origin)

Signed offset and native origin; raw kernel result.

int64_t stat(GO64_FSInfo& info)

Fill GO64_FSInfo for this handle.

int64_t truncate(uint64_t size)

Change file size.

int64_t next(GO64_FSInfo& info)

Read a directory entry; preserve the raw readdir result.

int64_t flags(uint64_t command, uint64_t value = 0)

Native fcntl command; value defaults to zero.

Native contract

LP64 ABI, 32-bit wchar_t, no packing. Native negative results are not errno. Handle -11 with yield and an application limit. Referenced objects must outlive their users; do not copy handle owners. File uses stdio/errno and invalidates its stream on close, even on error.