Skip to content

Instantly share code, notes, and snippets.

@thebirk
Last active November 20, 2020 12:26
Show Gist options
  • Save thebirk/4d78d0884b963ab7175e708a6933b681 to your computer and use it in GitHub Desktop.
Save thebirk/4d78d0884b963ab7175e708a6933b681 to your computer and use it in GitHub Desktop.
read_tar :: proc(reader: ^io.Reader) {
Posix_Ustar_Header :: struct #packed {
name: [100]u8,
mode: [8]u8,
uid: [8]u8,
gid: [8]u8,
size: [12]u8,
mtime: [12]u8,
checksum: [8]u8,
typeflag: u8,
linkname: [100]u8,
magic: [6]u8,
version: [2]u8,
uname: [32]u8,
gname: [32]u8,
devmajor: [8]u8,
devminor: [8]u8,
prefix: [155]u8,
pad: [12]u8,
};
USTAR_MAGIC := [?]byte{'u', 's', 't', 'a', 'r', 0};
loop:
for {
// io.read_typeid is confusingly named, this read the struct Posix_Ustar_Header from the file, not an actual `typeid`
header, err := io.read_typeid(reader, Posix_Ustar_Header);
if header.magic != USTAR_MAGIC {
break loop;
}
size := parse_i64(string(header.size[:len(header.size)-1]), 8);
fmt.printf("name: %s, size: %v\n", cstring(&header.name[0]), size);
if size == 0 do continue;
read_size := size;
if read_size % 512 != 0 {
read_size += (512 - (size % 512));
}
data := make([]u8, read_size);
io.read(reader, data);
delete(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment