From fc967a60cab417522c46bfa5e06913fa4a905ebf Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Tue, 3 Dec 2013 19:19:27 +0100 Subject: [PATCH] parse_server_location: improve doc, lock table, fix return errors Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 25 +++++++++++++++++-------- global/e2-fetch-project.lua | 2 +- local/e2-new-source.lua | 4 ++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 220f714..4284e0f 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -2104,28 +2104,37 @@ function e2lib.parentdirs(path) return t end - ---- parse a server:location string, taking a default server into account +--- Parse a server:location string, taking a default server into account. -- @param serverloc string: the string to parse --- @param default_server string: the default server name --- @return a table with fields server and location, nil on error --- @return nil, an error string on error +-- @param default_server Default server name used when serverloc contains none. +-- @return Locked server location table, false on error +-- @return Error object on failure. +-- @see server_location function e2lib.parse_server_location(serverloc, default_server) assert(type(serverloc) == 'string' and type(default_server) == 'string') + + --- Server location table. + -- @table server_location + -- @field server Server name, defaults to default_server if not supplied. + -- @field location Path location relative to the server. + -- @see parse_server_location local sl = {} + sl.server, sl.location = serverloc:match("(%S+):(%S+)") if not (sl.server and sl.location) then sl.location = serverloc:match("(%S+)") if not (sl.location and default_server) then - return nil, "can't parse location" + return false, err.new("can't parse location in %q", serverloc) end sl.server = default_server end + if sl.location:match("[.][.]") or sl.location:match("^/") then - return nil, "invalid location" + return false, err.new("invalid location: %q", serverloc) end - return sl + + return strict.lock(sl) end --- setup cache from the global server configuration diff --git a/global/e2-fetch-project.lua b/global/e2-fetch-project.lua index 0c7e4d2..98d803b 100644 --- a/global/e2-fetch-project.lua +++ b/global/e2-fetch-project.lua @@ -75,7 +75,7 @@ local function e2_fetch_project(arg) end local sl, re = e2lib.parse_server_location(arguments[1], - e2lib.globals.default_projects_server) + e2lib.globals.default_projects_server) if not sl then return false, e:cat(re) end diff --git a/local/e2-new-source.lua b/local/e2-new-source.lua index 9c10d82..a49ff58 100644 --- a/local/e2-new-source.lua +++ b/local/e2-new-source.lua @@ -270,9 +270,9 @@ local function e2_new_source(arg) end local location = arguments[1] - local sl, e = e2lib.parse_server_location(location, info.default_files_server) + local sl, re = e2lib.parse_server_location(location, info.default_files_server) if not sl then - return false, e + return false, re end local server = sl.server local location = sl.location -- 2.39.5