1
votes

I am working on a system that does not support lua File System Library. So i have to make my own directory iterator that gives me list of files with full path of particular format.

The system has a API the we can set for Directory path and another API to get list of file in that directory in lua table format.

The API:FileSetDir() sets the Directory to get file list. The API:FileList() gives table of list of file from the directory we have set using API:FileSetDir().

For example consider the following directory structure.

Movies/3d Movies/Avatar/Avatar.mp4
Movies/3d Movies/Avengers/Avengers.mkv
Movies/3d Movies/ironman.mp4
Movies/Horor/Ring/Ring.avi
Movies/Horor/A Nightmare On Elm Street/A Nightmare On Elm Street.iso
Movies/Barfi.mkv

My Code..

RootDirList1 = {}
RootDirList = {}
finalpath={}


function SetDIR(v)
    if v=="" then
        RootDir = '/mnt/media/net/192.168.1.40_usbshare1/Movies/'
    else
        RootDir = '/mnt/media/net/192.168.1.40_usbshare1/Movies/'..v
    end
    API:FileSetDir (RootDir)
    RootDirList1 = API:FileList ()
    CheckDirectory()
end

function CheckDirectory()
    for i,v in pairs(RootDirList1) do
        local RootDir = '/mnt/media/net/192.168.1.40_usbshare1/Movies/'..v
        API:FileSetDir (RootDir)
        local RootDirList = API:FileList ()
        if RootDirList == nil then
            print(v)
            print("Not Directory")
            if string.find(v,"%.mkv$") or string.find(v,"%.mp4$") then 
                finalpath[#finalpath+1] =RootDir.."/"..t
           print(RootDir.."/"..t)

            end
        else    
            CheckDirectory(v)
       end
    end
end 

SetDIR("")

The code gives me error with "Stack Overflow".

The SetDIR sets the Directory with API:FileSetDir(RootDir) and get the file list in RootDirList1 table. then i am calling function CheckDirectory() here i am running a for loop over RootDirList1 table if the table entry is file the it will be stored in to finalpath table. if table entry is Directory then it calls SetDIR to get list of file in that directory.

I know my approach is wrong to make this work any bright idea will be greatly appreciated.

2
check excess "or" in string " if string.find(v,"%.mkv$") or string.find(v,"%.mp4$") or then" - Mike V.
oh that was a typo the original code does not have this. - Prakash.DTI
function CheckDirectory() - neverending call without parameters - Mike V.
ok Noted. I am trying to achieve it more dynamically. Like when it runs first time it get the Master directory list then it iterate over each directory in master list until it finds all the files. - Prakash.DTI
What does CheckDirectory(v) mean? CheckDirectory does not have arguments! - Egor Skriptunoff

2 Answers

2
votes

Stack overflow (heh) means that, well, the stack is overflowing - it's full. A stack is basically a region of memory that is literally like an IRL stack of stuff: you can place something on top of it (push) or get it back (pop).

Lua has more than one stack. The important one here is the call stack: when you call a function, you push one value to it (where to resume after it returns). It is limited so that you can't consume infinite memory.

So, basically, your code causes infinite recursion. It calls itself and doesn't progress, so it just fills up memory and does nothing.

That being said, your error was in this line:

local RootDir = '/mnt/media/net/192.168.1.40_usbshare1/Movies/'..v

You're trying to list recursively, right? That's not recursive, at all. That line assumes that the file is in the root folder, "Movies", which generates infinite recursion, since it checks the same folder over and over and over and over, again and again. You should, instead, append to the current RootDir.

I think CheckDirectory(v) also plays a part in this. What's that supposed to mean? CheckDirectory has no arguments! (thanks Egor for pointing that out)

Below, I refactored your code, completely changing the operation to be more efficient, and fixing identation issues.

function CheckDirectory(dir)
    local result = {}

    -- set directory to dir, then list it
    API:FileSetDir(dir)
    local list = API:FileList()

    for i, v in pairs(list) do
        API:FileSetDir(dir .. v .. "/") -- fixed!

        local clist = API:FileList()

        -- don't compare to nil. Just use 'not clist'.
        -- And besides, we have an else, so I inverted everything
        -- and collapsed the else .. if into elseif.
        if clist then
            for i, v in ipairs(CheckDirectory(dir .. v .. "/")) do
                table.insert(result, v)
            end
        elseif string.find(v, "%.mkv$") or string.find(v, "%.mp4$") then
            table.insert(result, dir .. v)
            print(dir .. v) -- remove this line if you don't want to print
        end
    end

    return result
end 

local files = CheckDirectory('/mnt/media/net/192.168.1.40_usbshare1/Movies/')
0
votes

I emulated your API using lfs and write the code that works:

-- My block. Do not use in your project. ------
local lfs = require "lfs"
local API = { }

function API:FileSetDir(path)
        return lfs.chdir (path)
end
function API:FileList()
    local path= lfs.currentdir ()
    local t = {}
    for entry in lfs.dir(path) do
        t[#t+1] = entry
    end
    return t
end
--- End of my block ----------------------------------------------------
local finalpath={}
local RootDir = 'd:/temp'


function CheckDirectory(entry)
        local CurDir = entry
        local IsDir = API:FileSetDir(CurDir)  -- return nil when error
        local DirList = API:FileList()
        print(CurDir)
        if not  IsDir  then
                    print(CurDir .." - Not Directory")
        else    
                     for _,v in pairs(DirList) do
                                local tmp  = CurDir.."/".. v
                                if true or string.find(v,"%.mkv$") or string.find(v,"%.mp4$") then  -- true always  for me
                                        finalpath[#finalpath+1] = tmp
                                        print(' >> '..tmp)
                                end
                                if not (v=='.' or v=='..') then                             
                                  CheckDirectory(tmp)
                                end
                    end
        end     
end 

CheckDirectory(RootDir)