From d75eeb864cb8aa3601d3732bc8de41fabf121ba5 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Wed, 2 Nov 2016 16:54:12 +0100 Subject: [PATCH] cache: turn enable-writeback into a global commandline option Signed-off-by: Tobias Ulmer --- generic/cache.lua | 53 +++++++++++++++++++++++++++++++++++- generic/e2option.lua | 14 ++++++++++ global/e2-create-project.lua | 5 ++++ global/e2-fetch-project.lua | 5 ++++ global/e2-install-e2.lua | 5 ++++ local/e2-build.lua | 39 -------------------------- local/e2tool.lua | 5 ++++ 7 files changed, 86 insertions(+), 40 deletions(-) diff --git a/generic/cache.lua b/generic/cache.lua index b97512e..e3c2c57 100644 --- a/generic/cache.lua +++ b/generic/cache.lua @@ -27,6 +27,19 @@ local strict = require("strict") local transport = require("transport") local url = require("url") +--- Vector for keeping delayed flag options, +-- set to false once options are processed. +-- @field table containing the following fields: +-- @field server server name, validated later +-- @field flag operation name, currently only "writeback" +-- @field value value for operation +-- @see cache.setup_cache_apply_opts +-- @see cache.set_writeback +local _opts = { + -- { server=.., flag=.., value=.. }, + -- ... +} + --- Internal representation of a cache. This table is locked. -- @table cache -- @field _name Human readable name. @@ -136,6 +149,32 @@ function cache.setup_cache_local(c, project_root, project_location) return true end +--- Apply delayed commandline options once cache is set up and disable +-- the delayed mechanism +-- @param c cache object +-- @return True on success, false on error +-- @return Error object on failure +function cache.setup_cache_apply_opts(c) + local rc, re, opts + + opts = _opts + _opts = false -- stop delayed processing + + for _, opt in ipairs(opts) do + if opt.flag == "writeback" then + rc, re = cache.set_writeback(c, opt.server, opt.value) + if not rc then + return false, re + end + else + return false, + err.new("unknown delayed option: %s", opt.flag) + end + end + + return true +end + local _server_names = strict.lock({ dot = ".", -- the proj_storage server is equivalent to @@ -777,12 +816,24 @@ function cache.writeback_enabled(c, server, flags) end --- enable/disable writeback for a server --- @param c the cache data structure +-- @param c the cache data structure or nil when the cache is not yet set up -- @param server the server where the file is located -- @param value boolean: the new setting -- @return boolean -- @return an error object on failure function cache.set_writeback(c, server, value) + + if _opts then + e2lib.logf(3, "delaying cache.set_writeback(%s, %s, %s)", + tostring(c), tostring(server), tostring(value)) + table.insert(_opts, + { flag = "writeback", server = server, value = value }) + + return true + end + + assertIsTable(c) + if type(value) ~= "boolean" then return false, err.new( "cache.set_writeback(): value is not boolean") diff --git a/generic/e2option.lua b/generic/e2option.lua index fb950e6..013540a 100644 --- a/generic/e2option.lua +++ b/generic/e2option.lua @@ -28,6 +28,7 @@ local plugin = require("plugin") local err = require("err") local strict = require("strict") local tools = require("tools") +local cache = require("cache") local options = {} local aliases = {} @@ -96,6 +97,19 @@ local function defaultoptions() end, "FILE") + local function disable_writeback(server) + cache.set_writeback(nil, server, false) + end + + e2option.option("disable-writeback", "disable writeback for server", nil, + disable_writeback, "SERVER") + + local function enable_writeback(server) + cache.set_writeback(nil, server, true) + end + e2option.option("enable-writeback", "enable writeback for server", nil, + enable_writeback, "SERVER") + e2option.flag("quiet", "disable all log levels", function() e2lib.setlog(1, false) diff --git a/global/e2-create-project.lua b/global/e2-create-project.lua index 4296c49..b93ff67 100644 --- a/global/e2-create-project.lua +++ b/global/e2-create-project.lua @@ -121,6 +121,11 @@ local function e2_create_project(arg) error(e:cat(re)) end + rc, re = cache.setup_cache_apply_opts(scache) + if not rc then + error(e:cat(re)) + end + if #arguments ~= 1 then e2option.usage(1) end diff --git a/global/e2-fetch-project.lua b/global/e2-fetch-project.lua index 6dac88a..d5de693 100644 --- a/global/e2-fetch-project.lua +++ b/global/e2-fetch-project.lua @@ -60,6 +60,11 @@ local function e2_fetch_project(arg) error(e:cat(re)) end + rc, re = cache.setup_cache_apply_opts(scache) + if not rc then + error(e:cat(re)) + end + -- standard global tool setup finished if #arguments < 1 then diff --git a/global/e2-install-e2.lua b/global/e2-install-e2.lua index 4650947..66ea97c 100644 --- a/global/e2-install-e2.lua +++ b/global/e2-install-e2.lua @@ -64,6 +64,11 @@ local function e2_install_e2(arg) error(e:cat(re)) end + rc, re = cache.setup_cache_apply_opts(scache) + if not rc then + error(e:cat(re)) + end + -- standard global tool setup finished if #arguments > 0 then diff --git a/local/e2-build.lua b/local/e2-build.lua index 872541a..eb6ccd5 100644 --- a/local/e2-build.lua +++ b/local/e2-build.lua @@ -49,43 +49,6 @@ local function e2_build(arg) e2option.flag("playground", "prepare environment but do not build") e2option.flag("keep", "do not remove chroot environment after build") e2option.flag("buildid", "display buildids and exit") - -- cache is not yet initialized when parsing command line options, so - -- remember settings in order of appearance, and perform settings as soon - -- as the cache is initialized. - local writeback = {} - local function disable_writeback(server) - table.insert(writeback, { set = "disable", server = server }) - end - local function enable_writeback(server) - table.insert(writeback, { set = "enable", server = server }) - end - local function perform_writeback_settings(writeback) - local rc, re - local enable_msg = "enabling writeback for server '%s' [--enable-writeback]" - local disable_msg = - "disabling writeback for server '%s' [--disable-writeback]" - for _,set in ipairs(writeback) do - if set.set == "disable" then - e2lib.logf(3, disable_msg, set.server) - rc, re = cache.set_writeback(info.cache, set.server, false) - if not rc then - local e = err.new(disable_msg, set.server) - error(e:cat(re)) - end - elseif set.set == "enable" then - e2lib.logf(3, enable_msg, set.server) - rc, re = cache.set_writeback(info.cache, set.server, true) - if not rc then - local e = err.new(enable_msg, set.server) - error(e:cat(re)) - end - end - end - end - e2option.option("disable-writeback", "disable writeback for server", nil, - disable_writeback, "SERVER") - e2option.option("enable-writeback", "enable writeback for server", nil, - enable_writeback, "SERVER") local opts, arguments = e2option.parse(arg) if not opts then @@ -103,8 +66,6 @@ local function e2_build(arg) error(re) end - perform_writeback_settings(writeback) - -- apply the standard build mode and settings to all results for _,res in pairs(result.results) do res:build_mode(build_mode) diff --git a/local/e2tool.lua b/local/e2tool.lua index 127ab22..b1a98af 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -387,6 +387,11 @@ function e2tool.collect_project_info(info, skip_load_config) return false, e:cat(re) end + rc, re = cache.setup_cache_apply_opts(info.cache) + if not rc then + return false, e:cat(re) + end + local f = e2lib.join(info.root, e2lib.globals.e2version_file) local v, re = e2lib.parse_e2versionfile(f) if not v then -- 2.39.5