// be_local.c reads the version with fscanf("%zu"); conversion policy stays in V. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os #include fn C.fputs(&char, &C.FILE) i32 fn C.isspace(i32) i32 fn local_version_byte(stream &C.FILE) !i32 { value := C.getc(stream) if value == C.EOF && C.ferror(stream) != 0 { return os.last_error() } return value } fn read_local_version(stream &C.FILE) !u64 { mut current := local_version_byte(stream)! for C.isspace(current) != 0 { current = local_version_byte(stream)! } negative := current == `-` if current in [i32(`+`), `-`] { current = local_version_byte(stream)! } if current < `0` || current > `9` { return error('invalid local database version') } limit := u64(C.SIZE_MAX) mut number := u64(0) mut overflow := false for current >= `0` && current <= `9` { digit := u64(current - `0`) if !overflow { if number > limit / 10 || (number == limit / 10 && digit > limit % 10) { number = limit overflow = true } else { number = number * 10 + digit } } current = local_version_byte(stream)! } return if negative && !overflow { (u64(0) - number) & limit } else { number } } fn read_and_close_local_version(stream &C.FILE) !u64 { version := read_local_version(stream) or { if C.fclose(stream) != 0 { return error_with_code('${err.msg()}; could not close version file: ${os.last_error().msg()}', err.code()) } return err } if C.fclose(stream) != 0 { return os.last_error() } return version } fn validate_local_version(path string) ! { filename := os.join_path(path, 'ALPM_DB_VERSION') stream := C.fopen(unsafe { &char(filename.str) }, c'r') if stream == unsafe { nil } { return os.last_error() } version := read_and_close_local_version(stream)! if version != 9 { return error('unsupported local database version: ${version}') } } fn write_local_version(path string) ! { filename := os.join_path(path, 'ALPM_DB_VERSION') stream := C.fopen(unsafe { &char(filename.str) }, c'w') if stream == unsafe { nil } { return os.last_error() } status := C.fputs(c'9\n', stream) problem := os.last_error() closed := C.fclose(stream) if status < 0 { return problem } if closed != 0 { return os.last_error() } }