Made repo safe to use concurrently

main
Jef Roosens 2022-01-11 14:40:25 +01:00
parent dc98c910c4
commit e0d22b195e
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import web
import os
import log
import io
import repo
const port = 8000
@ -13,8 +14,9 @@ const db_name = 'pieter.db.tar.gz'
struct App {
web.Context
api_key string [required; web_global]
repo_dir string [required; web_global]
api_key string [required; web_global]
repo_dir string [required; web_global]
repo shared repo.Repo [required]
}
[noreturn]
@ -87,8 +89,11 @@ fn main() {
}
web.run(&App{
logger: logger
api_key: key
repo_dir: repo_dir
logger: logger
repo: repo.Repo{
path: os.join_path_single(repo_dir, db_name)
}
}, port)
}

View File

@ -2,8 +2,12 @@ module repo
import os
pub fn add_package(db_path string, pkg_path string) ? {
res := os.execute("repo-add '$db_path' '$pkg_path'")
pub struct Repo {
path string
}
pub fn (r Repo) add_package(pkg_path string) ? {
res := os.execute("repo-add '$r.path' '$pkg_path'")
if res.exit_code != 0 {
println(res.output)

View File

@ -26,16 +26,19 @@ fn (mut app App) put_package(pkg string) web.Result {
return app.text("Content-Type header isn't set.")
}
repo.add_package(os.join_path_single(app.repo_dir, db_name), full_path) or {
app.linfo("Failed to add package '$pkg' to database.")
lock app.repo {
app.repo.add_package(full_path) or {
app.linfo("Failed to add package '$pkg' to database.")
os.rm(full_path) or { println('Failed to remove $full_path') }
os.rm(full_path) or { println('Failed to remove $full_path') }
return app.text('Failed to add package to repo.')
return app.text('Failed to add package to repo.')
}
app.linfo("Added '$pkg' to repository.")
}
app.linfo("Uploaded package '$pkg'.")
return app.text('Package added successfully.')
}