isleafdir

function isleafdir( path ) --> res

Description

Checks if path directs to a directory without sub directories.

Parameters

path

Path that should be checked.

Return Values

res

true if the path is to directory without sub directories, otherwise false.

Code

local lfs = require( "lfs" ) --ZREQ-lfs
--ZFUNC-isleafdir-v1
local function isleafdir( path ) --> res
   --ZFUNC-isdodd-v1
   local function isdodd( e )
      if e == "." or e == ".." then
         return true
      end

      return false
   end

   local mode = lfs.attributes( path, "mode" )
   if mode ~= "directory" then return false end

   for entry in lfs.dir( path ) do
      local entrypath = path.."/"..entry
      local mode = lfs.attributes( entrypath, "mode" )
      if mode == "directory" and not isdodd( entry ) then
         return false
      end
   end

   return true
end

return isleafdir

Examples

local t = require( "tapered" )
local isleafdir = require( "isleafdir" )
-- util functions
local mkdirtree = require( "mkdirtree" )
local rmdirtree = require( "rmdirtree" )

-- setup
t.ok( mkdirtree{
   [ "middir" ] = {
      [ "leafdir" ] = {
         [ "tmpfile.txt" ] = ""
      }
   }
} )

-- test
t.ok( isleafdir( "middir/leafdir" ) )
t.nok( isleafdir( "middir" ) )

-- teardown
t.ok( rmdirtree( "middir" ) )

t.done()

See also