DoxigAlpha

setPath

Function parameters

Parameters

#
w:*Header
prefix:[]const u8
sub_path:[]const u8

Options for writing file/dir/link.

Types

#
Options
Options for writing file/dir/link.
Header
A struct that is exactly 512 bytes and matches tar file format.

Sets prefix for all other write* method paths.

Functions

#
setRoot
Sets prefix for all other write* method paths.
writeFileStream
Writes file reading file content from `reader`.
writeFileBytes
Writes file using bytes buffer `content` for size and file content.
finishPedantically
According to the specification, tar should finish with two zero blocks, but

Error sets in this namespace

Error Sets

#

Source

Implementation

#
pub fn setPath(w: *Header, prefix: []const u8, sub_path: []const u8) !void {
    const max_prefix = w.prefix.len;
    const max_name = w.name.len;
    const sep = std.fs.path.sep_posix;

    if (prefix.len + sub_path.len > max_name + max_prefix or prefix.len > max_prefix)
        return error.NameTooLong;

    // both fit into name
    if (prefix.len > 0 and prefix.len + sub_path.len < max_name) {
        @memcpy(w.name[0..prefix.len], prefix);
        w.name[prefix.len] = sep;
        @memcpy(w.name[prefix.len + 1 ..][0..sub_path.len], sub_path);
        return;
    }

    // sub_path fits into name
    // there is no prefix or prefix fits into prefix
    if (sub_path.len <= max_name) {
        @memcpy(w.name[0..sub_path.len], sub_path);
        @memcpy(w.prefix[0..prefix.len], prefix);
        return;
    }

    if (prefix.len > 0) {
        @memcpy(w.prefix[0..prefix.len], prefix);
        w.prefix[prefix.len] = sep;
    }
    const prefix_pos = if (prefix.len > 0) prefix.len + 1 else 0;

    // add as much to prefix as you can, must split at /
    const prefix_remaining = max_prefix - prefix_pos;
    if (std.mem.lastIndexOf(u8, sub_path[0..@min(prefix_remaining, sub_path.len)], &.{'/'})) |sep_pos| {
        @memcpy(w.prefix[prefix_pos..][0..sep_pos], sub_path[0..sep_pos]);
        if ((sub_path.len - sep_pos - 1) > max_name) return error.NameTooLong;
        @memcpy(w.name[0..][0 .. sub_path.len - sep_pos - 1], sub_path[sep_pos + 1 ..]);
        return;
    }

    return error.NameTooLong;
}