跳转到内容

std.os.uefi.protocol.file

标准库Zig 0.17.0官方原文
源码std/os/uefi/protocol/file.zig

第 1 行

std.os.uefi.protocol.file.cc

别名

第 6 行

别名目标 std.os.uefi.cc

std.os.uefi.protocol.file.File

容器

第 8 行

字段

revision: u64
_open: *const fn (*const File, **File, [*:0]const u16, OpenMode, Attributes) callconv(cc) Status
_close: *const fn (*File) callconv(cc) Status
_delete: *const fn (*File) callconv(cc) Status
_read: *const fn (*File, *usize, [*]u8) callconv(cc) Status
_write: *const fn (*File, *usize, [*]const u8) callconv(cc) Status
_get_position: *const fn (*const File, *u64) callconv(cc) Status
_set_position: *const fn (*File, u64) callconv(cc) Status
_get_info: *const fn (*const File, *align(8) const Guid, *usize, ?[*]u8) callconv(cc) Status
_set_info: *const fn (*File, *align(8) const Guid, usize, [*]const u8) callconv(cc) Status
_flush: *const fn (*File) callconv(cc) Status

std.os.uefi.protocol.file.File.Attributes

容器

第 302 行

字段

read_only: bool = false
hidden: bool = false
system: bool = false
reserved: bool = false
directory: bool = false
archive: bool = false
_pad: u58 = 0

std.os.uefi.protocol.file.File.close

函数

第 109 行

pub fn close(self: *File) CloseError!void

参数

self: *File

错误

Unexpected UnexpectedError

std.os.uefi.protocol.file.File.CloseError

别名

第 33 行

别名目标 std.os.uefi.UnexpectedError

std.os.uefi.protocol.file.File.delete

函数

第 120 行

删除文件。

已删除文件则返回 true,未删除则返回 false;按照 UEFI 规范, 后者属于警告。

pub fn delete(self: *File) uefi.UnexpectedError!bool

参数

self: *File

错误

Unexpected

std.os.uefi.protocol.file.File.end_of_file

常量

第 387 行

u64

std.os.uefi.protocol.file.File.flush

函数

第 274 行

pub fn flush(self: *File) FlushError!void

参数

self: *File

错误

AccessDenied
DeviceError
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.FlushError

错误集合

第 72 行

错误

AccessDenied
DeviceError
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.getInfo

函数

第 219 行

如果 buffer 不足以容纳全部信息,此函数返回 Error.BufferTooSmall。应先调用 getInfoSize 确定 安全调用此函数所需的缓冲区大小。

pub fn getInfo( self: *const File, comptime info: std.meta.Tag(Info), buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8, ) GetInfoError!*@FieldType(Info, @tagName(info))

参数

self: *const File
info: std.meta.Tag(Info)
buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8

错误

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.GetInfoError

错误集合

第 59 行

错误

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.getInfoSize

函数

第 202 行

pub fn getInfoSize(self: *const File, comptime info: std.meta.Tag(Info)) GetInfoError!usize

参数

self: *const File
info: std.meta.Tag(Info)

错误

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.GetInfoSizeError

错误集合

第 53 行

错误

DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted

std.os.uefi.protocol.file.File.getPosition

函数

第 171 行

pub fn getPosition(self: *const File) SeekError!u64

参数

self: *const File

错误

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.Info

容器

第 318 行

字段

file: Info.File
file_system: FileSystem
volume_label: VolumeLabel

std.os.uefi.protocol.file.File.Info.File

容器

第 323 行

字段

size: u64
file_size: u64
physical_size: u64
create_time: Time
last_access_time: Time
modification_time: Time
attribute: Attributes
_file_name: u16

std.os.uefi.protocol.file.File.Info.File.getFileName

函数

第 333 行

pub fn getFileName(self: *const Info.File) [*:0]const u16

参数

self: *const Info.File

std.os.uefi.protocol.file.File.Info.File.guid

常量

第 337 行

std.os.uefi.protocol.file.File.Info.FileSystem

容器

第 347 行

字段

size: u64
read_only: bool
volume_size: u64
free_space: u64
block_size: u32
_volume_label: u16

std.os.uefi.protocol.file.File.Info.FileSystem.getVolumeLabel

函数

第 355 行

pub fn getVolumeLabel(self: *const FileSystem) [*:0]const u16

参数

self: *const FileSystem

std.os.uefi.protocol.file.File.Info.FileSystem.guid

常量

第 359 行

std.os.uefi.protocol.file.File.Info.VolumeLabel

容器

第 369 行

字段

_volume_label: u16

std.os.uefi.protocol.file.File.Info.VolumeLabel.getVolumeLabel

函数

第 372 行

pub fn getVolumeLabel(self: *const VolumeLabel) [*:0]const u16

参数

self: *const VolumeLabel

std.os.uefi.protocol.file.File.Info.VolumeLabel.guid

常量

第 376 行

std.os.uefi.protocol.file.File.open

函数

第 80 行

pub fn open( self: *const File, file_name: [*:0]const u16, mode: OpenMode, create_attributes: Attributes, ) OpenError!*File

参数

self: *const File
file_name: [*:0]const u16
mode: OpenMode
create_attributes: Attributes

错误

AccessDenied
DeviceError
InvalidParameter
MediaChanged
NoMedia
NotFound
OutOfResources
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.OpenError

错误集合

第 21 行

错误

AccessDenied
DeviceError
InvalidParameter
MediaChanged
NoMedia
NotFound
OutOfResources
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.OpenMode

容器

第 286 行

字段

read = @bitCast(Bits{ .read = true })
read_write = @bitCast(Bits{ .read = true, .write = true })
read_write_create = @bitCast(Bits{ .read = true, .write = true, .create = true })

std.os.uefi.protocol.file.File.OpenMode.Bits

容器

第 287 行

字段

read: bool = false
write: bool = false
_pad: u61 = 0
create: bool = false

std.os.uefi.protocol.file.File.read

函数

第 144 行

若 self 为目录条目,且 buffer 太小而无法容纳 下一条目,则返回 Error.BufferTooSmall。可在此前 或此后调用 readSize,确定调用此函数所需的 缓冲区大小。

pub fn read(self: *File, buffer: []u8) ReadError!usize

参数

self: *File
buffer: []u8

错误

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.ReadError

错误集合

第 38 行

错误

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.readSize

函数

第 128 行

pub fn readSize(self: *File) ReadError!usize

参数

self: *File

错误

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.seekBy

函数

第 190 行

fn seekBy(self: *File, offset: i64) SeekError!void

参数

self: *File
offset: i64

错误

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.SeekError

错误集合

第 34 行

错误

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.setInfo

函数

第 243 行

pub fn setInfo( self: *File, comptime info: std.meta.Tag(Info), data: *const @FieldType(Info, @tagName(info)), ) SetInfoError!void

参数

self: *File
info: std.meta.Tag(Info)
data: *const @FieldType(Info, @tagName(info))

错误

AccessDenied
BadBufferSize
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.SetInfoError

错误集合

第 62 行

错误

AccessDenied
BadBufferSize
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.setPosition

函数

第 181 行

pub fn setPosition(self: *File, position: u64) SeekError!void

参数

self: *File
position: u64

错误

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.write

函数

第 156 行

pub fn write(self: *File, buffer: []const u8) WriteError!usize

参数

self: *File
buffer: []const u8

错误

AccessDenied
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.WriteError

错误集合

第 44 行

错误

AccessDenied
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.Guid

别名

第 3 行

别名目标 std.os.uefi.Guid

std.os.uefi.protocol.file.Status

别名

第 5 行

别名目标 std.os.uefi.Status

std.os.uefi.protocol.file.std

别名

第 1 行

别名目标 std

std.os.uefi.protocol.file.Time

别名

第 4 行

别名目标 std.os.uefi.Time

std.os.uefi.protocol.file.uefi

别名

第 2 行

别名目标 std.os.uefi

源码

std/os/uefi/protocol/file.zig

const std = @import("std");
const uefi = std.os.uefi;
const Guid = uefi.Guid;
const Time = uefi.Time;
const Status = uefi.Status;
const cc = uefi.cc;

pub const File = extern struct {
    revision: u64,
    _open: *const fn (*const File, **File, [*:0]const u16, OpenMode, Attributes) callconv(cc) Status,
    _close: *const fn (*File) callconv(cc) Status,
    _delete: *const fn (*File) callconv(cc) Status,
    _read: *const fn (*File, *usize, [*]u8) callconv(cc) Status,
    _write: *const fn (*File, *usize, [*]const u8) callconv(cc) Status,
    _get_position: *const fn (*const File, *u64) callconv(cc) Status,
    _set_position: *const fn (*File, u64) callconv(cc) Status,
    _get_info: *const fn (*const File, *align(8) const Guid, *usize, ?[*]u8) callconv(cc) Status,
    _set_info: *const fn (*File, *align(8) const Guid, usize, [*]const u8) callconv(cc) Status,
    _flush: *const fn (*File) callconv(cc) Status,

    pub const OpenError = uefi.UnexpectedError || error{
        NotFound,
        NoMedia,
        MediaChanged,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        OutOfResources,
        VolumeFull,
        InvalidParameter,
    };
    pub const CloseError = uefi.UnexpectedError;
    pub const SeekError = uefi.UnexpectedError || error{
        Unsupported,
        DeviceError,
    };
    pub const ReadError = uefi.UnexpectedError || error{
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        BufferTooSmall,
    };
    pub const WriteError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
    };
    pub const GetInfoSizeError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
    };
    pub const GetInfoError = GetInfoSizeError || error{
        BufferTooSmall,
    };
    pub const SetInfoError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
        BadBufferSize,
    };
    pub const FlushError = uefi.UnexpectedError || error{
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
    };

    pub fn open(
        self: *const File,
        file_name: [*:0]const u16,
        mode: OpenMode,
        create_attributes: Attributes,
    ) OpenError!*File {
        var new: *File = undefined;
        switch (self._open(
            self,
            &new,
            file_name,
            mode,
            create_attributes,
        )) {
            .success => return new,
            .not_found => return error.NotFound,
            .no_media => return error.NoMedia,
            .media_changed => return error.MediaChanged,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .out_of_resources => return error.OutOfResources,
            .volume_full => return error.VolumeFull,
            .invalid_parameter => return error.InvalidParameter,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn close(self: *File) CloseError!void {
        switch (self._close(self)) {
            .success => {},
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// 删除文件。
    ///
    /// 已删除文件则返回 true,未删除则返回 false;按照 UEFI 规范,
    /// 后者属于警告。
    pub fn delete(self: *File) uefi.UnexpectedError!bool {
        switch (self._delete(self)) {
            .success => return true,
            .warn_delete_failure => return false,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn readSize(self: *File) ReadError!usize {
        const zerobuf: [0]u8 = undefined;
        var size: usize = 0;
        switch (self._read(self, &size, &zerobuf)) {
            .success, .buffer_too_small => return size,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// 若 `self` 为目录条目,且 `buffer` 太小而无法容纳
    /// 下一条目,则返回 `Error.BufferTooSmall`。可在此前
    /// 或此后调用 `readSize`,确定调用此函数所需的
    /// 缓冲区大小。
    pub fn read(self: *File, buffer: []u8) ReadError!usize {
        var size: usize = buffer.len;
        switch (self._read(self, &size, buffer.ptr)) {
            .success => return size,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .buffer_too_small => return error.BufferTooSmall,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn write(self: *File, buffer: []const u8) WriteError!usize {
        var size: usize = buffer.len;
        switch (self._write(self, &size, buffer.ptr)) {
            .success => return size,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn getPosition(self: *const File) SeekError!u64 {
        var position: u64 = undefined;
        switch (self._get_position(self, &position)) {
            .success => return position,
            .unsupported => return error.Unsupported,
            .device_error => return error.DeviceError,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn setPosition(self: *File, position: u64) SeekError!void {
        switch (self._set_position(self, position)) {
            .success => {},
            .unsupported => return error.Unsupported,
            .device_error => return error.DeviceError,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    fn seekBy(self: *File, offset: i64) SeekError!void {
        var pos = try self.getPosition();
        const seek_back = offset < 0;
        const amt = @abs(offset);
        if (seek_back) {
            pos += amt;
        } else {
            pos -= amt;
        }
        try self.setPosition(pos);
    }

    pub fn getInfoSize(self: *const File, comptime info: std.meta.Tag(Info)) GetInfoError!usize {
        const InfoType = @FieldType(Info, @tagName(info));

        var len: usize = 0;
        switch (self._get_info(self, &InfoType.guid, &len, null)) {
            .success, .buffer_too_small => return len,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// 如果 `buffer` 不足以容纳全部信息,此函数返回
    /// `Error.BufferTooSmall`。应先调用 `getInfoSize` 确定
    /// 安全调用此函数所需的缓冲区大小。
    pub fn getInfo(
        self: *const File,
        comptime info: std.meta.Tag(Info),
        buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8,
    ) GetInfoError!*@FieldType(Info, @tagName(info)) {
        const InfoType = @FieldType(Info, @tagName(info));

        var len = buffer.len;
        switch (self._get_info(
            self,
            &InfoType.guid,
            &len,
            buffer.ptr,
        )) {
            .success => return @as(*InfoType, @ptrCast(buffer.ptr)),
            .buffer_too_small => return error.BufferTooSmall,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn setInfo(
        self: *File,
        comptime info: std.meta.Tag(Info),
        data: *const @FieldType(Info, @tagName(info)),
    ) SetInfoError!void {
        const InfoType = @FieldType(Info, @tagName(info));

        const attached_str: [*:0]const u16 = switch (info) {
            .file => data.getFileName(),
            .file_system, .volume_label => data.getVolumeLabel(),
        };
        const attached_str_len = std.mem.sliceTo(attached_str, 0).len;

        // 加上长度(不为哨兵额外加 1),因为 `@sizeOf(InfoType)`
        // 已包含第一个 utf16 字符
        const len = @sizeOf(InfoType) + (attached_str_len * 2);

        switch (self._set_info(self, &InfoType.guid, len, @ptrCast(data))) {
            .success => {},
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            .bad_buffer_size => return error.BadBufferSize,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn flush(self: *File) FlushError!void {
        switch (self._flush(self)) {
            .success => {},
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub const OpenMode = enum(u64) {
        pub const Bits = packed struct(u64) {
            // 0x0000000000000001
            read: bool = false,
            // 0x0000000000000002
            write: bool = false,
            _pad: u61 = 0,
            // 0x8000000000000000
            create: bool = false,
        };

        read = @bitCast(Bits{ .read = true }),
        read_write = @bitCast(Bits{ .read = true, .write = true }),
        read_write_create = @bitCast(Bits{ .read = true, .write = true, .create = true }),
    };

    pub const Attributes = packed struct(u64) {
        // 0x0000000000000001
        read_only: bool = false,
        // 0x0000000000000002
        hidden: bool = false,
        // 0x0000000000000004
        system: bool = false,
        // 0x0000000000000008
        reserved: bool = false,
        // 0x0000000000000010
        directory: bool = false,
        // 0x0000000000000020
        archive: bool = false,
        _pad: u58 = 0,
    };

    pub const Info = union(enum) {
        file: Info.File,
        file_system: FileSystem,
        volume_label: VolumeLabel,

        pub const File = extern struct {
            size: u64,
            file_size: u64,
            physical_size: u64,
            create_time: Time,
            last_access_time: Time,
            modification_time: Time,
            attribute: Attributes,
            _file_name: u16,

            pub fn getFileName(self: *const Info.File) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._file_name));
            }

            pub const guid align(8) = Guid{
                .time_low = 0x09576e92,
                .time_mid = 0x6d3f,
                .time_high_and_version = 0x11d2,
                .clock_seq_high_and_reserved = 0x8e,
                .clock_seq_low = 0x39,
                .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
            };
        };

        pub const FileSystem = extern struct {
            size: u64,
            read_only: bool,
            volume_size: u64,
            free_space: u64,
            block_size: u32,
            _volume_label: u16,

            pub fn getVolumeLabel(self: *const FileSystem) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._volume_label));
            }

            pub const guid align(8) = Guid{
                .time_low = 0x09576e93,
                .time_mid = 0x6d3f,
                .time_high_and_version = 0x11d2,
                .clock_seq_high_and_reserved = 0x8e,
                .clock_seq_low = 0x39,
                .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
            };
        };

        pub const VolumeLabel = extern struct {
            _volume_label: u16,

            pub fn getVolumeLabel(self: *const VolumeLabel) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._volume_label));
            }

            pub const guid align(8) = Guid{
                .time_low = 0xdb47d7d3,
                .time_mid = 0xfe81,
                .time_high_and_version = 0x11d3,
                .clock_seq_high_and_reserved = 0x9a,
                .clock_seq_low = 0x35,
                .node = [_]u8{ 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d },
            };
        };
    };

    const end_of_file: u64 = 0xffffffffffffffff;
};