1234567891011module remote1213import crypto.blake314import time15import os1617#flag -I@VMODROOT/vendor/shim18#flag @VMODROOT/vendor/shim/koh_create.c19#include "koh_create.h"2021fn C.koh_create_exclusive(path &char, data voidptr, len usize) int2223pub struct Folder {24pub:25 base string26}272829pub fn new_folder(base string) !Folder {30 os.mkdir_all(base, os.MkdirParams{})!31 return Folder{32 base: base33 }34}3536pub fn (mut f Folder) get(key string) !Object {37 path := f.path_of(key)38 if !os.is_file(path) {39 return Object{}40 }41 body := os.read_bytes(path)!42 return Object{43 found: true44 body: body45 tag: tag_of(body)46 }47}48495051pub fn (mut f Folder) list_dirs(prefix string) ![]string {52 dir := f.path_of(prefix)53 if !os.is_dir(dir) {54 return []string{}55 }56 mut names := []string{}57 for name in os.ls(dir)! {58 if os.is_dir(os.join_path(dir, name)) && !is_scratch(name) {59 names << name60 }61 }62 names.sort()63 return names64}6566pub fn (mut f Folder) list(prefix string) ![]string {67 dir := os.join_path(f.base, prefix)68 if !os.is_dir(dir) {69 return []string{}70 }71 mut keys := []string{}72 mut walk := [prefix]73 for walk.len > 0 {74 relative := walk.pop()75 for name in os.ls(f.path_of(relative))! {76 child := if relative == '' { name } else { '${relative}/${name}' }77 if os.is_dir(f.path_of(child)) {78 walk << child79 } else if !is_scratch(name) {80 keys << child81 }82 }83 }84 keys.sort()85 return keys86}878889909192pub fn (mut f Folder) put_if_absent(key string, body []u8) !bool {93 path := f.path_of(key)94 dir := os.dir(path)95 os.mkdir_all(dir, os.MkdirParams{})!96 temporary := '${path}.tmp-${os.getpid()}'97 os.write_file_array(temporary, body)!98 defer {99 if os.exists(temporary) {100 os.rm(temporary) or {}101 }102 }103 os.link(temporary, path) or {104 if os.exists(path) {105 return false106 }107 return create_exclusive(path, body)108 }109 return true110}111112113114115116fn create_exclusive(path string, body []u8) !bool {117 match unsafe { C.koh_create_exclusive(&char(path.str), body.data, usize(body.len)) } {118 1 { return true }119 0 { return false }120 else { return error('cannot create ${path}') }121 }122}123124125126127pub fn (mut f Folder) put_if_match(key string, body []u8, tag string) !bool {128 path := f.path_of(key)129 os.mkdir_all(os.dir(path), os.MkdirParams{})!130 guard := '${path}.lock'131 if !take_lock(guard)! {132 clear_stale_lock(guard)133 if !take_lock(guard)! {134 return false135 }136 }137 defer {138 release_lock(guard)139 }140 if tag != '' {141 current := f.get(key)!142 if !current.found || current.tag != tag {143 return false144 }145 } else if f.get(key)!.found {146 return false147 }148 temporary := '${path}.tmp-${os.getpid()}'149 os.write_file_array(temporary, body)!150 151 if os.is_dir(path) {152 os.rm(temporary) or {}153 return error('${key} is a directory on the remote')154 }155 os.rename(temporary, path)!156 return true157}158159160161162fn take_lock(path string) !bool {163 pid := '${os.getpid()}'164 fresh := '${path}.${pid}.${time.now().unix_nano()}'165 os.write_file(fresh, pid)!166 defer {167 os.rm(fresh) or {}168 }169 os.link(fresh, path) or {170 if os.exists(path) {171 return false172 }173 return create_exclusive(path, pid.bytes())174 }175 return true176}177178179180fn is_scratch(name string) bool {181 return name.contains('.tmp-') || name.contains('.lock')182}183184fn release_lock(path string) {185 os.rm(path) or {}186}187188189190191fn clear_stale_lock(path string) {192 info := os.stat(path) or { return }193 if time.now().unix() - info.mtime > 60 {194 os.rm(path) or {}195 }196}197198pub fn (mut f Folder) delete(key string) ! {199 path := f.path_of(key)200 if os.exists(path) {201 os.rm(path)!202 }203}204205fn (f &Folder) path_of(key string) string {206 return os.join_path(f.base, key)207}208209210211212pub fn tag_of(body []u8) string {213 return blake3.sum256(body).hex()214}