Function metadata [src]

Prototype

pub fn metadata(self: File) MetadataError!Metadata

Parameters

self: File

Possible Errors

AccessDenied FStatError

In WASI, this error may occur when the file descriptor does not hold the required rights to get its filestat information.

PermissionDenied FStatError
SystemResources FStatError
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

Source

pub fn metadata(self: File) MetadataError!Metadata { return .{ .inner = switch (builtin.os.tag) { .windows => blk: { var io_status_block: windows.IO_STATUS_BLOCK = undefined; var info: windows.FILE_ALL_INFORMATION = undefined; const rc = windows.ntdll.NtQueryInformationFile(self.handle, &io_status_block, &info, @sizeOf(windows.FILE_ALL_INFORMATION), .FileAllInformation); switch (rc) { .SUCCESS => {}, // Buffer overflow here indicates that there is more information available than was able to be stored in the buffer // size provided. This is treated as success because the type of variable-length information that this would be relevant for // (name, volume name, etc) we don't care about. .BUFFER_OVERFLOW => {}, .INVALID_PARAMETER => unreachable, .ACCESS_DENIED => return error.AccessDenied, else => return windows.unexpectedStatus(rc), } const reparse_tag: windows.DWORD = reparse_blk: { if (info.BasicInformation.FileAttributes & windows.FILE_ATTRIBUTE_REPARSE_POINT != 0) { var tag_info: windows.FILE_ATTRIBUTE_TAG_INFO = undefined; const tag_rc = windows.ntdll.NtQueryInformationFile(self.handle, &io_status_block, &tag_info, @sizeOf(windows.FILE_ATTRIBUTE_TAG_INFO), .FileAttributeTagInformation); switch (tag_rc) { .SUCCESS => {}, // INFO_LENGTH_MISMATCH and ACCESS_DENIED are the only documented possible errors // https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/d295752f-ce89-4b98-8553-266d37c84f0e .INFO_LENGTH_MISMATCH => unreachable, .ACCESS_DENIED => return error.AccessDenied, else => return windows.unexpectedStatus(rc), } break :reparse_blk tag_info.ReparseTag; } break :reparse_blk 0; }; break :blk .{ .attributes = info.BasicInformation.FileAttributes, .reparse_tag = reparse_tag, ._size = @as(u64, @bitCast(info.StandardInformation.EndOfFile)), .access_time = windows.fromSysTime(info.BasicInformation.LastAccessTime), .modified_time = windows.fromSysTime(info.BasicInformation.LastWriteTime), .creation_time = windows.fromSysTime(info.BasicInformation.CreationTime), }; }, .linux => blk: { var stx = std.mem.zeroes(linux.Statx); // We are gathering information for Metadata, which is meant to contain all the // native OS information about the file, so use all known flags. const rc = linux.statx( self.handle, "", linux.AT.EMPTY_PATH, linux.STATX_BASIC_STATS | linux.STATX_BTIME, &stx, ); switch (linux.E.init(rc)) { .SUCCESS => {}, .ACCES => unreachable, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, .LOOP => unreachable, .NAMETOOLONG => unreachable, .NOENT => unreachable, .NOMEM => return error.SystemResources, .NOTDIR => unreachable, else => |err| return posix.unexpectedErrno(err), } break :blk .{ .statx = stx, }; }, .wasi => .{ .stat = try std.os.fstat_wasi(self.handle) }, else => .{ .stat = try posix.fstat(self.handle) }, }, }; }