]> git.e2factory.org Git - e2factory.git/commitdiff
Replace read_line with eio.file_read_line()
authorTobias Ulmer <tu@emlix.com>
Thu, 24 Oct 2013 14:57:51 +0000 (16:57 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:01:23 +0000 (15:01 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
generic/eio.lua
generic/generic_git.lua
global/e2-fetch-project.lua
global/e2-install-e2.lua
local/e2tool.lua

index 41f17614aeba37e6b2ebe370ffdc7bc3cbcb0977..d72b508d0a739ae87084c2c394df2e331dd5d69f 100644 (file)
@@ -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.
index aba9017acfc6adc6c019a13a0c07755465eeec12..3b63c116afec2a9c44681505f860af05e9b4cf79 100644 (file)
@@ -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:
index fed8980441958bf8d75d531c2f190da8fb12c0d7..09e9c11888bef4210bc9ec1bfab729c395b3f8cb 100644 (file)
@@ -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)
index 783c82e71b909e8e148ddedb46cc7c2a14e10c5e..21d5d6f51319a978f5bf196673ec6695949b6166 100644 (file)
@@ -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
index 71f71738fd17e95f02948b16d4327a6f35553cd2..8adcf27c3d7041559ded894da22541d4eb59c64d 100644 (file)
@@ -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
index ac844798b5ed789048c7379663e8645c94732081..10721da5630ed9fed5f54e0665e16af2dfb37d44 100644 (file)
@@ -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