// directory modes follow pacman util.c, independent of the caller's umask. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os fn C.umask(u32) u32 fn C.fchmod(i32, u32) i32 fn make_directory_mode(path string, mode u32) ! { old_mask := C.umask(0) defer { C.umask(old_mask) } mut current := if path.starts_with('/') { '/' } else { '' } for component in path.split('/') { if component == '' { continue } current += component status := unsafe { C.mkdir(&char(current.str), mode) } if status != 0 && C.errno != C.EEXIST { return os.last_error() } current += '/' } } fn (mut handle Handle) remove_temporary_file(path string) { if C.unlink(unsafe { &char(path.str) }) != 0 && C.errno != C.ENOENT { handle.diagnostics << 'could not remove ${path}: ${os.last_error().msg()}' } } fn (mut handle Handle) temporary_download_name(directory string) !string { mut pattern := (directory + 'alpmtmp.XXXXXX\x00').bytes() fd := C.mkstemp(pattern.data) if fd < 0 { return handle.fail(.retrieve, os.last_error().msg()) } path := pattern[..pattern.len - 1].bytestr() mut keep := false defer { if !keep { handle.remove_temporary_file(path) } } mask := C.umask(0) C.umask(mask) if C.fchmod(fd, ~mask & u32(0o666)) != 0 { problem := os.last_error() if C.close(fd) != 0 { handle.diagnostics << 'could not close temporary download: ${os.last_error().msg()}' } return handle.fail(.retrieve, problem.msg()) } if C.close(fd) != 0 { return handle.fail(.retrieve, os.last_error().msg()) } keep = true return path }