2022-01-18 21:41:09 +01:00
|
|
|
module util
|
|
|
|
|
|
|
|
import os
|
2022-02-21 20:51:41 +01:00
|
|
|
import io
|
2022-01-18 21:41:09 +01:00
|
|
|
import crypto.md5
|
2022-01-31 14:43:57 +01:00
|
|
|
import crypto.sha256
|
2022-01-18 21:41:09 +01:00
|
|
|
|
2022-02-21 20:51:41 +01:00
|
|
|
const reader_buf_size = 1_000_000
|
|
|
|
|
2022-02-21 22:22:36 +01:00
|
|
|
const prefixes = ['B', 'KB', 'MB', 'GB']
|
|
|
|
|
|
|
|
// Dummy struct to work around the fact that you can only share structs, maps &
|
|
|
|
// arrays
|
|
|
|
pub struct Dummy {
|
|
|
|
x int
|
|
|
|
}
|
|
|
|
|
2022-02-22 08:14:20 +01:00
|
|
|
// exit_with_message exits the program with a given status code after having
|
|
|
|
// first printed a specific message to STDERR
|
2022-02-21 20:51:41 +01:00
|
|
|
[noreturn]
|
|
|
|
pub fn exit_with_message(code int, msg string) {
|
|
|
|
eprintln(msg)
|
|
|
|
exit(code)
|
|
|
|
}
|
|
|
|
|
2022-02-22 08:14:20 +01:00
|
|
|
// reader_to_file writes the contents of a BufferedReader to a file
|
2022-02-21 20:51:41 +01:00
|
|
|
pub fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
|
2022-05-14 20:06:08 +02:00
|
|
|
mut file := os.create(path)?
|
2022-02-21 20:51:41 +01:00
|
|
|
defer {
|
|
|
|
file.close()
|
|
|
|
}
|
|
|
|
|
2022-04-30 11:43:06 +02:00
|
|
|
mut buf := []u8{len: util.reader_buf_size}
|
2022-02-21 20:51:41 +01:00
|
|
|
mut bytes_left := length
|
|
|
|
|
|
|
|
// Repeat as long as the stream still has data
|
|
|
|
for bytes_left > 0 {
|
|
|
|
// TODO check if just breaking here is safe
|
|
|
|
bytes_read := reader.read(mut buf) or { break }
|
|
|
|
bytes_left -= bytes_read
|
|
|
|
|
|
|
|
mut to_write := bytes_read
|
|
|
|
|
|
|
|
for to_write > 0 {
|
|
|
|
// TODO don't just loop infinitely here
|
|
|
|
bytes_written := file.write(buf[bytes_read - to_write..bytes_read]) or { continue }
|
2022-04-10 16:17:50 +02:00
|
|
|
// file.flush()
|
2022-02-21 20:51:41 +01:00
|
|
|
|
|
|
|
to_write = to_write - bytes_written
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 21:41:09 +01:00
|
|
|
// hash_file returns the md5 & sha256 hash of a given file
|
2022-01-18 22:52:14 +01:00
|
|
|
// TODO actually implement sha256
|
2022-01-18 21:41:09 +01:00
|
|
|
pub fn hash_file(path &string) ?(string, string) {
|
|
|
|
file := os.open(path) or { return error('Failed to open file.') }
|
|
|
|
|
|
|
|
mut md5sum := md5.new()
|
2022-01-31 14:43:57 +01:00
|
|
|
mut sha256sum := sha256.new()
|
2022-01-18 21:41:09 +01:00
|
|
|
|
2022-01-18 22:52:14 +01:00
|
|
|
buf_size := int(1_000_000)
|
2022-04-30 11:43:06 +02:00
|
|
|
mut buf := []u8{len: buf_size}
|
2022-01-18 21:41:09 +01:00
|
|
|
mut bytes_left := os.file_size(path)
|
|
|
|
|
|
|
|
for bytes_left > 0 {
|
|
|
|
// TODO check if just breaking here is safe
|
2022-01-19 17:15:37 +01:00
|
|
|
bytes_read := file.read(mut buf) or { return error('Failed to read from file.') }
|
2022-01-18 21:41:09 +01:00
|
|
|
bytes_left -= u64(bytes_read)
|
|
|
|
|
2022-01-19 17:15:37 +01:00
|
|
|
// For now we'll assume that this always works
|
|
|
|
md5sum.write(buf[..bytes_read]) or {
|
2022-01-31 14:43:57 +01:00
|
|
|
return error('Failed to update md5 checksum. This should never happen.')
|
|
|
|
}
|
|
|
|
sha256sum.write(buf[..bytes_read]) or {
|
|
|
|
return error('Failed to update sha256 checksum. This should never happen.')
|
2022-01-18 21:41:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:43:57 +01:00
|
|
|
return md5sum.checksum().hex(), sha256sum.checksum().hex()
|
2022-01-18 21:41:09 +01:00
|
|
|
}
|
2022-02-21 22:22:36 +01:00
|
|
|
|
|
|
|
// pretty_bytes converts a byte count to human-readable version
|
|
|
|
pub fn pretty_bytes(bytes int) string {
|
|
|
|
mut i := 0
|
|
|
|
mut n := f32(bytes)
|
|
|
|
|
|
|
|
for n >= 1024 {
|
|
|
|
i++
|
|
|
|
n /= 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
return '${n:.2}${util.prefixes[i]}'
|
|
|
|
}
|
2022-05-14 21:55:19 +02:00
|
|
|
|
2022-05-14 23:42:20 +02:00
|
|
|
// match_array_in_array<T> returns how many elements of a2 overlap with a1. For
|
|
|
|
// example, if a1 = "abcd" & a2 = "cd", the result will be 2. If the match is
|
|
|
|
// not at the end of a1, the result is 0.
|
2022-05-14 21:55:19 +02:00
|
|
|
pub fn match_array_in_array<T>(a1 []T, a2 []T) int {
|
|
|
|
mut i := 0
|
|
|
|
mut match_len := 0
|
|
|
|
|
|
|
|
for i + match_len < a1.len {
|
|
|
|
if a1[i + match_len] == a2[match_len] {
|
|
|
|
match_len += 1
|
|
|
|
} else {
|
|
|
|
i += match_len + 1
|
|
|
|
match_len = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return match_len
|
|
|
|
}
|