From e0d22b195e3c65324e2766033b3db627b5b4c536 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 11 Jan 2022 14:40:25 +0100 Subject: [PATCH] Made repo safe to use concurrently --- vieter/main.v | 11 ++++++++--- vieter/repo.v | 8 ++++++-- vieter/routes.v | 13 ++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/vieter/main.v b/vieter/main.v index a958611..7d88ec5 100644 --- a/vieter/main.v +++ b/vieter/main.v @@ -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) } diff --git a/vieter/repo.v b/vieter/repo.v index 54dfc38..a090207 100644 --- a/vieter/repo.v +++ b/vieter/repo.v @@ -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) diff --git a/vieter/routes.v b/vieter/routes.v index f135a91..2b14920 100644 --- a/vieter/routes.v +++ b/vieter/routes.v @@ -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.') } -