From df07f1108b04829fb276400e8892927d1553c817 Mon Sep 17 00:00:00 2001
From: "Dmitry E. Oboukhov" <unera@debian.org>
Date: Wed, 1 Oct 2014 15:47:22 +0400
Subject: [PATCH] Add some tests for fio.dirname, #491

Fix some bugs in the function (now it uses libc's (3) dirname ffi.call).
---
 src/lua/fio.lua       | 15 +++------------
 test/box/fio.result   | 21 +++++++++++++++++++++
 test/box/fio.test.lua |  8 ++++++++
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index 48687703bd..9f6b10d92f 100644
--- a/src/lua/fio.lua
+++ b/src/lua/fio.lua
@@ -5,6 +5,7 @@ local ffi = require('ffi')
 
 ffi.cdef[[
     int umask(int mask);
+    char *dirname(char *path);
 ]]
 
 local internal = fio.internal
@@ -209,18 +210,8 @@ fio.dirname = function(path)
         return nil
     end
     path = tostring(path)
-
-    while true do
-        if path == "" or string.sub(path, -1) == "/" then
-            break
-        end
-        path = string.sub(path, 1, -2)
-    end
-    if path == "" then
-        path = "."
-    end
-
-    return path
+    path = ffi.new('char[?]', #path, path)
+    return ffi.string(ffi.C.dirname(path))
 end
 
 fio.umask = function(umask)
diff --git a/test/box/fio.result b/test/box/fio.result
index 1485c63ad1..301874a6df 100644
--- a/test/box/fio.result
+++ b/test/box/fio.result
@@ -286,3 +286,24 @@ fio.unlink(nil)
 ---
 - false
 ...
+-- dirname
+fio.dirname('abc')
+---
+- .
+...
+fio.dirname('/abc')
+---
+- /
+...
+fio.dirname('/abc/cde')
+---
+- /abc
+...
+fio.dirname('/abc/cde/')
+---
+- /abc
+...
+fio.dirname('/')
+---
+- /
+...
diff --git a/test/box/fio.test.lua b/test/box/fio.test.lua
index c97c15965a..2a18ee6df2 100644
--- a/test/box/fio.test.lua
+++ b/test/box/fio.test.lua
@@ -97,3 +97,11 @@ fio.rmdir(tmpdir)
 
 fio.unlink()
 fio.unlink(nil)
+
+-- dirname
+
+fio.dirname('abc')
+fio.dirname('/abc')
+fio.dirname('/abc/cde')
+fio.dirname('/abc/cde/')
+fio.dirname('/')
-- 
GitLab