]> git.e2factory.org Git - e2factory.git/commitdiff
Clean up pathname operations
authorTobias Ulmer <tu@emlix.com>
Wed, 28 Nov 2012 17:31:06 +0000 (18:31 +0100)
committerTobias Ulmer <tu@emlix.com>
Tue, 26 Feb 2013 18:07:12 +0000 (19:07 +0100)
* document dirname() in luadoc style
* document basename() in luadoc style. Drop the optional extension
  stripping capability, since nothing seems to use it.
* there are no users of splitpath(), drop it.

Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua

index b3393496673fec64f56f9238f5ff06cfeaec1f94..1db0ec3a84562b99560bb40a2a2b22bb01813abe 100644 (file)
@@ -636,54 +636,33 @@ function e2lib.finish(returncode)
     os.exit(returncode)
 end
 
-
--- Pathname operations
---
---   dirname(PATH) -> STRING
---
---     Returns the directory part of the string PATH.
---
---   basename(PATH, [EXTENSION]) -> STRING
---
---     Returns the filename part of PATH by stripping the directory part.
---     if EXTENSION is given and if it matches the file-extension of PATH,
---     then the extension part is also removed.
---
---   splitpath(PATH) -> DIR, BASE, TYPE
---
---     Checks PATH for trailing "/" for a directory,
---     splits up the real path into dir and base to ensure that
---     DIR .. BASE will address the file, as DIR always ends in "/"
---     TYPE is set to stat.type. return nil for non existing file
---
-
+--- Returns the "directory" part of a path
+-- @param path string: a path with components seperated by slashes.
+-- @return all but the last component of the path, or "." if none could be found.
 function e2lib.dirname(path)
+    assert(type(path) == "string")
+
     local s, e, dir = string.find(path, "^(.*)/[^/]*$")
-    if dir == "" then return "/"
-    else return dir or "." end
+    if dir == "" then
+        return "/"
+    end
+
+    return dir or "."
 end
 
-function e2lib.basename(path, ext)
+--- Returns the "filename" part of a path.
+-- @param path string: a path with components seperated by slashes.
+-- @return returns the last (right-most) component of a path, or the path
+-- itself if no component could be found.
+function e2lib.basename(path)
+    assert(type(path) == "string")
+
     local s, e, base = string.find(path, "^.*/([^/]+)[/]?$")
-    if not base then base = path end
-    if ext then
-        if string.sub(base, -#ext) == ext then
-            return string.sub(base, 1, -#ext - 1)
-        end
+    if not base then
+        base = path
     end
-    return base
-end
 
-function e2lib.splitpath(path)
-    local p = e2util.realpath(path)
-    if not p then return nil, "path does not exist" end
-    local st = e2util.stat(p)
-    local sf = string.sub(path, -1) ~= "/"
-    if (st.type == "directory") == sf then
-        return nil, "is " .. (sf and "" or "not ") .. "a directory"
-    end
-    local s, e, d, b = string.find(p, "^(.*/)([^/]*)$")
-    return d, b == "" and "." or b, st.type
+    return base
 end
 
 function e2lib.is_backup_file(path)