vieter/src/archive.v

88 lines
2.3 KiB
Coq
Raw Normal View History

module archive
2022-01-13 12:35:05 +01:00
import os
#flag -larchive
#include "archive.h"
#include "archive_entry.h"
2022-01-13 12:35:05 +01:00
#include <string.h>
struct C.archive {}
struct C.archive_entry {}
2022-01-12 23:06:03 +01:00
// Create a new archive struct
fn C.archive_read_new() &C.archive
2022-01-13 12:35:05 +01:00
fn C.archive_entry_new() &C.archive_entry
fn C.archive_read_support_filter_all(&C.archive)
fn C.archive_read_support_format_all(&C.archive)
2022-01-13 12:35:05 +01:00
// Open an archive for reading
2022-01-12 21:50:17 +01:00
fn C.archive_read_open_filename(&C.archive, &char, int) int
fn C.archive_read_next_header(&C.archive, &&C.archive_entry) int
2022-01-13 12:35:05 +01:00
fn C.archive_read_next_header2(&C.archive, &C.archive_entry) int
fn C.archive_entry_pathname(&C.archive_entry) &char
2022-01-12 21:50:17 +01:00
fn C.archive_read_data_skip(&C.archive)
fn C.archive_read_free(&C.archive) int
2022-01-12 23:06:03 +01:00
fn C.archive_read_data(&C.archive, voidptr, int)
fn C.archive_entry_size(&C.archive_entry) int
2022-01-13 12:35:05 +01:00
fn C.strcmp(&char, &char) int
2022-01-12 21:50:17 +01:00
2022-01-13 12:35:05 +01:00
// pub fn list_filenames() {
// a := C.archive_read_new()
// entry := &C.archive_entry{}
// mut r := 0
2022-01-12 21:50:17 +01:00
2022-01-13 12:35:05 +01:00
// C.archive_read_support_filter_all(a)
// C.archive_read_support_format_all(a)
2022-01-12 21:50:17 +01:00
2022-01-13 12:35:05 +01:00
// r = C.archive_read_open_filename(a, c'test/homebank-5.5.1-1-x86_64.pkg.tar.zst', 10240)
2022-01-12 21:50:17 +01:00
2022-01-13 12:35:05 +01:00
// for (C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK) {
// println(cstring_to_vstring(C.archive_entry_pathname(entry)))
// C.archive_read_data_skip(a) // Note 2
// }
// r = C.archive_read_free(a) // Note 3
// }
2022-01-12 23:06:03 +01:00
pub fn get_pkg_info(pkg_path string) ?string {
2022-01-13 12:35:05 +01:00
if !os.is_file(pkg_path) {
return error("'$pkg_path' doesn't exist or isn't a file.")
}
2022-01-12 23:06:03 +01:00
a := C.archive_read_new()
2022-01-13 12:35:05 +01:00
entry := C.archive_entry_new()
2022-01-12 23:06:03 +01:00
mut r := 0
C.archive_read_support_filter_all(a)
C.archive_read_support_format_all(a)
// TODO find out where does this 10240 come from
r = C.archive_read_open_filename(a, &char(pkg_path.str), 10240)
2022-01-13 12:35:05 +01:00
defer {
C.archive_read_free(a)
}
2022-01-12 23:06:03 +01:00
if r != C.ARCHIVE_OK {
return error('Failed to open package.')
}
// We iterate over every header in search of the .PKGINFO one
2022-01-13 12:35:05 +01:00
mut buf := voidptr(0)
2022-01-12 23:06:03 +01:00
for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK {
2022-01-13 12:35:05 +01:00
if C.strcmp(C.archive_entry_pathname(entry), c'.PKGINFO') == 0 {
2022-01-12 23:06:03 +01:00
size := C.archive_entry_size(entry)
2022-01-13 12:35:05 +01:00
// TODO can this unsafe block be avoided?
buf = unsafe { malloc(size) }
C.archive_read_data(a, voidptr(buf), size)
2022-01-12 23:06:03 +01:00
break
}else{
C.archive_read_data_skip(a)
}
}
2022-01-13 12:35:05 +01:00
return unsafe { cstring_to_vstring(&char(buf)) }
2022-01-12 23:06:03 +01:00
}