How do I get the full path of a `std.fs.Dir`?

Viewed 61

Is there any way to access the full path of a std.fs.Dir struct? I've looked through all of the methods in the source but I can't find anything that gets path-related information on the directory.

1 Answers

One option is realpath

std.fs.Dir.realpathAlloc(self: Dir, allocator: Allocator, pathname: []const u8) ![]u8

const std = @import("std");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const alloc = arena.allocator();

    std.log.info("cwd: {s}", .{
        try std.fs.cwd().realpathAlloc(alloc, "."),
    });
}

realpath internally calls std.os.getFdPath and there doesn't seem to be any function on Dir that just does that right now.

Related