diff --git a/README.md b/README.md index 561eb9f..0d4b462 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,6 @@ Dependencies ======= - [pp](https://github.com/luapower/pp) -- [lfs](http://keplerproject.github.io/luafilesystem/) All above libraries can be found in [LuaPower](https://luapower.com/). diff --git a/flatdb.lua b/flatdb.lua index c85c8d6..91f5282 100644 --- a/flatdb.lua +++ b/flatdb.lua @@ -1,12 +1,23 @@ -local lfs = require("lfs") local pp = require("pp") local function isFile(path) - return lfs.attributes(path, "mode") == "file" + local f = io.open(path, "r") + if f then + f:close() + return true + end + return false end local function isDir(path) - return lfs.attributes(path, "mode") == "directory" + local tmp = path.."/"..os.tmpname() + local f = io.open(tmp, "w") + if f then + f:close() + os.remove(tmp) + return true + end + return false end local function load_page(path) @@ -38,7 +49,9 @@ local db_funcs = { end end for p, page in pairs(db) do - store_page(pool[db].."/"..p, page) + if not store_page(pool[db].."/"..p, page) then + return false + end end return true end @@ -59,8 +72,8 @@ pool.hack = db_funcs return setmetatable(pool, { __mode = "kv", __call = function(pool, path) + assert(isDir(path), path.." is not a directory.") if pool[path] then return pool[path] end - if not isDir(path) then return end local db = {} setmetatable(db, mt) pool[path] = db