Moved checksum calculation to desc function

main
Jef Roosens 2022-01-19 17:22:36 +01:00
parent df3310944e
commit 4bc3bfb8cf
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
2 changed files with 21 additions and 11 deletions

View File

@ -109,5 +109,6 @@ fn main() {
return return
} }
// println(info) // println(info)
print(res.info.to_desc()) println('hey')
print(res.to_desc())
} }

View File

@ -56,6 +56,7 @@ fn C.strcmp(&char, &char) int
// Represents a read archive // Represents a read archive
struct Pkg { struct Pkg {
pub: pub:
path string [required]
info PkgInfo [required] info PkgInfo [required]
files []string [required] files []string [required]
} }
@ -74,8 +75,8 @@ mut:
arch string arch string
build_date i64 build_date i64
packager string packager string
md5sum string // md5sum string
sha256sum string // sha256sum string
pgpsig string pgpsig string
pgpsigsize i64 pgpsigsize i64
// Array values // Array values
@ -90,6 +91,10 @@ mut:
checkdepends []string checkdepends []string
} }
pub fn (p &Pkg) checksum() ?(string, string) {
return util.hash_file(p.path)
}
// parse_pkg_info_string parses a PkgInfo object from a string // parse_pkg_info_string parses a PkgInfo object from a string
fn parse_pkg_info_string(pkg_info_str &string) ?PkgInfo { fn parse_pkg_info_string(pkg_info_str &string) ?PkgInfo {
mut pkg_info := PkgInfo{} mut pkg_info := PkgInfo{}
@ -195,11 +200,9 @@ pub fn read_pkg(pkg_path string) ?Pkg {
mut pkg_info := parse_pkg_info_string(unsafe { cstring_to_vstring(&char(buf)) }) ? mut pkg_info := parse_pkg_info_string(unsafe { cstring_to_vstring(&char(buf)) }) ?
pkg_info.csize = i64(os.file_size(pkg_path)) pkg_info.csize = i64(os.file_size(pkg_path))
pkg_info.md5sum, pkg_info.sha256sum = util.hash_file(pkg_path) or {
return error('Failed to hash package.')
}
return Pkg{ return Pkg{
path: pkg_path
info: pkg_info info: pkg_info
files: files files: files
} }
@ -211,7 +214,9 @@ fn format_entry(key string, value string) string {
// to_desc returns a desc file valid string representation // to_desc returns a desc file valid string representation
// TODO calculate md5 & sha256 instead of believing the file // TODO calculate md5 & sha256 instead of believing the file
pub fn (p &PkgInfo) to_desc() string { pub fn (pkg &Pkg) to_desc() string {
p := pkg.info
// filename // filename
mut desc := '%FILENAME%\n$p.name-$p.version-${p.arch}.pkg.tar.zst\n' mut desc := '%FILENAME%\n$p.name-$p.version-${p.arch}.pkg.tar.zst\n'
@ -230,8 +235,12 @@ pub fn (p &PkgInfo) to_desc() string {
desc += format_entry('CSIZE', p.csize.str()) desc += format_entry('CSIZE', p.csize.str())
desc += format_entry('ISIZE', p.size.str()) desc += format_entry('ISIZE', p.size.str())
desc += format_entry('MD5SUM', p.md5sum) md5sum, sha256sum := pkg.checksum() or { '', '' }
desc += format_entry('SHA256SUM', p.sha256sum)
desc += format_entry('MD5SUM', md5sum)
// TODO add this
// desc += format_entry('SHA256SUM', sha256sum)
// TODO add pgpsig stuff // TODO add pgpsig stuff