From 5e360c246f549432c3e1b8a60ea35ef2230dd1c7 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Thu, 24 Oct 2013 16:57:51 +0200 Subject: [PATCH] Replace read_line with eio.file_read_line() Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 22 ------------------- generic/eio.lua | 37 +++++++++++++++++++++++++++++++ generic/generic_git.lua | 4 +++- global/e2-fetch-project.lua | 3 ++- global/e2-install-e2.lua | 15 ++++++++----- local/e2tool.lua | 44 +++++++++++-------------------------- 6 files changed, 64 insertions(+), 61 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 41f1761..d72b508 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -796,28 +796,6 @@ function e2lib.tartype_by_suffix(filename) return tartype end ---- Read the first line from the given file and return it. --- Beware that end of file is considered an error. --- @param path Path to file (string). --- @return The first line or false on error. --- @return Error object on failure. -function e2lib.read_line(path) - local f, msg = io.open(path) - if not f then - return false, err.new("%s: %s", path, msg) - end - local l, msg = f:read("*l") - f:close() - if not l then - if not msg then - msg = "end of file" - end - - return false, err.new("%s: %s", path, msg) - end - return l -end - --- Use the global parameters from the global configuration. -- @return True on success, false on error. -- @return Error object on failure. diff --git a/generic/eio.lua b/generic/eio.lua index aba9017..3b63c11 100644 --- a/generic/eio.lua +++ b/generic/eio.lua @@ -348,6 +348,43 @@ function eio.cloexec(something, set) return leio.cloexec(something, set) end + +--- Read the first line from file pointed to by filename. End of file is +-- considered to be an error. +-- @param filename File name. +-- @return First line of text, up to but not including the new-line character. +-- False on error. +-- @return Error object on failure. +function eio.file_read_line(filename) + local file, re, line, rc + + file, re = eio.fopen(filename, "r") + if not file then + return false, re + end + + line, re = eio.readline(file) + if not line then + eio.fclose(file) + return false, re + end + + rc, re = eio.fclose(file) + if not rc then + return false, re + end + + if line == "" then + return false, err.new("unexpected end of file in %q", filename) + end + + if string.sub(line, -1) == "\n" then + line = string.sub(line, 1, -2) + end + + return line +end + return strict.lock(eio) -- vim:sw=4:sts=4:et: diff --git a/generic/generic_git.lua b/generic/generic_git.lua index fed8980..09e9c11 100644 --- a/generic/generic_git.lua +++ b/generic/generic_git.lua @@ -33,6 +33,7 @@ local generic_git = {} local e2lib = require("e2lib") +local eio = require("eio") local cache = require("cache") local url = require("url") local tools = require("tools") @@ -417,8 +418,9 @@ function generic_git.git_config(gitdir, query) e:append("git config failed") return nil, e end - local git_output = e2lib.read_line(tmpfile) + local git_output, re = eio.file_read_line(tmpfile) if not git_output then + e:cat(re) return nil, e:append("can't read git output from temporary file") end e2lib.rmtempfile(tmpfile) diff --git a/global/e2-fetch-project.lua b/global/e2-fetch-project.lua index 783c82e..21d5d6f 100644 --- a/global/e2-fetch-project.lua +++ b/global/e2-fetch-project.lua @@ -30,6 +30,7 @@ local e2lib = require("e2lib") local e2option = require("e2option") +local eio = require("eio") local generic_git = require("generic_git") local cache = require("cache") local err = require("err") @@ -122,7 +123,7 @@ local function e2_fetch_project(arg) -- read the version from the first line local version_file = string.format("%s/version", tmpdir) - local line, re = e2lib.read_line(version_file) + local line, re = eio.file_read_line(version_file) if not line then return false, e:cat(re) end diff --git a/global/e2-install-e2.lua b/global/e2-install-e2.lua index 71f7173..8adcf27 100644 --- a/global/e2-install-e2.lua +++ b/global/e2-install-e2.lua @@ -30,6 +30,7 @@ local e2lib = require("e2lib") local e2option = require("e2option") +local eio = require("eio") local generic_git = require("generic_git") local err = require("err") local buildconfig = require("buildconfig") @@ -50,10 +51,10 @@ local function e2_install_e2(arg) return false, err.new("can't locate project root.") end - -- try to get project specific config file paht - local config_file_config = string.format("%s/%s", root, e2lib.globals.e2config) - local config_file = e2lib.read_line(config_file_config) + -- try to get project specific config file path -- don't care if this succeeds, the parameter is optional. + local config_file_config = e2lib.join(root, e2lib.globals.e2config) + local config_file = eio.file_read_line(config_file_config) local rc, e = e2lib.read_global_config(config_file) if not rc then @@ -97,7 +98,8 @@ local function e2_install_e2(arg) end -- read the version from the first line - local line, re = e2lib.read_line(e2lib.globals.global_interface_version_file) + local line, re = eio.file_read_line( + e2lib.globals.global_interface_version_file) if not line then return false, e:cat(re) end @@ -132,10 +134,11 @@ local function e2_install_e2(arg) extensions = {} -- empty list end - local s = e2lib.read_line(".e2/e2version") + local s, re = eio.file_read_line(".e2/e2version") local branch, tag = s:match("(%S+) (%S+)") if not branch or not tag then - return false, e:append("cannot parse e2 version") + e:cat(re) + return false, e:cat(err.new("cannot parse e2 version")) end local ref if tag == "^" then diff --git a/local/e2tool.lua b/local/e2tool.lua index ac84479..10721da 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -30,6 +30,7 @@ local e2tool = {} local e2lib = require("e2lib") +local eio = require("eio") local err = require("err") local scm = require("scm") local tools = require("tools") @@ -754,7 +755,7 @@ end -- @return an error object on failure local function check_config_syntax_compat(info) local e = err.new("checking configuration syntax compatibilitly failed") - local l, re = e2lib.read_line(info.config_syntax_file) + local l, re = eio.file_read_line(info.config_syntax_file) if not l then return false, e:cat(re) end @@ -765,16 +766,16 @@ local function check_config_syntax_compat(info) end end local s = [[ - Your configuration syntax is incompatible with this tool version. - Please read the configuration Changelog, update your project configuration - and finally insert the new configuration syntax version into %s +Your configuration syntax is incompatible with this tool version. +Please read the configuration Changelog, update your project configuration +and finally insert the new configuration syntax version into %s - Configuration syntax versions supported by this version of the tools are: - ]] +Configuration syntax versions supported by this version of the tools are:]] e2lib.logf(2, s, info.config_syntax_file) for _,m in ipairs(info.config_syntax_compat) do - e2lib.logf(2, " %s", m) + e2lib.logf(2, "%s", m) end + e2lib.logf(2, "Currently configured configuration syntax is: %q", l) return false, e:append("configuration syntax mismatch") end @@ -1271,10 +1272,10 @@ function e2tool.collect_project_info(info, skip_load_config) e2lib.finish(1) end - -- try to get project specific config file paht - local config_file_config = e2lib.join(info.root, e2lib.globals.e2config) - local config_file = e2lib.read_line(config_file_config) + -- try to get project specific config file path -- don't care if this succeeds, the parameter is optional. + local config_file_config = e2lib.join(info.root, e2lib.globals.e2config) + local config_file = eio.file_read_line(config_file_config) local rc, re = e2lib.read_global_config(config_file) if not rc then @@ -1424,7 +1425,7 @@ function e2tool.collect_project_info(info, skip_load_config) -- read .e2/proj-location info.project_location_config = e2lib.join(info.root, ".e2/project-location") - local line, re = e2lib.read_line(info.project_location_config) + local line, re = eio.file_read_line(info.project_location_config) if not line then return false, e:cat(re) end @@ -1438,7 +1439,7 @@ function e2tool.collect_project_info(info, skip_load_config) -- read global interface version and check if this version of the local -- tools supports the version used for the project - local line, re = e2lib.read_line(e2lib.globals.global_interface_version_file) + local line, re = eio.file_read_line(e2lib.globals.global_interface_version_file) if not line then return false, e:cat(re) end @@ -1828,25 +1829,6 @@ function e2tool.dsort(info) return e2tool.dlist_recursive(info, info.project.default_results) end ---- read hash file. -local function read_hash_file(info, server, location) - local e = err.new("error reading hash file") - local cs = nil - local cache_flags = { cache = true } - local rc, re = info.cache:cache_file(server, location, cache_flags) - if not rc then - return nil, e:cat(re) - end - local path = info.cache:file_path(server, location, cache_flags) - if path then - cs = e2lib.read_line(path) - if cs then - return cs, nil - end - end - return nil, e:append("can't open checksum file") -end - --- hash a file -- @param path string: path to a file -- @return string the hash value, nil on error -- 2.39.5