Commit c0d6b4e0 authored by Renato Figueiro Maia's avatar Renato Figueiro Maia

[OPENBUS-2189] Remover as dependências da biblioteca OpenBusLua com as...

[OPENBUS-2189] Remover as dependências da biblioteca OpenBusLua com as bibliotecas LuaFileSystem e 'luuid' de Lua
- Remoção da dependência de LuaFileSystem (lfs), que agora apenas o busservices utiliza.

git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/trunk@137389 ae0415b3-e90b-0410-900d-d0be9363c56b
parent ca1b5887
-- $Id$
local _G = require "_G"
local assert = _G.assert
local ipairs = _G.ipairs
local loadfile = _G.loadfile
local pairs = _G.pairs
local pcall = _G.pcall
local setmetatable = _G.setmetatable
local os = require "os"
local removefile = os.remove
local renamefile = os.rename
local tmpname = os.tmpname
local io = require "io"
local open = io.open
local lfs = require "lfs"
local listdir = lfs.dir
local getattribute = lfs.attributes
local makedir = lfs.mkdir
local removedir = lfs.rmdir
local Viewer = require "loop.debug.Viewer"
local oo = require "openbus.util.oo"
local class = oo.class
local Serializer = Viewer{ nolabels = true }
local function createpath(path)
local result, errmsg = getattribute(path, "mode")
if result == "directory" then
result, errmsg = true, nil
elseif result ~= nil then
result, errmsg = false, "'"..path..
"' expected to be directory (got "..result..")"
elseif errmsg:match("^cannot obtain information") then
result, errmsg = makedir(path)
if not result then
errmsg = "unable to create directory '"..path.."' ("..errmsg..")"
end
end
return result, errmsg
end
local function removepath(path)
for filename in listdir(path) do
local filepath = path..filename
if filename:find("%.lua$")
and getattribute(filepath, "mode") == "file" then
local ok, errmsg = removefile(filepath)
if ok == nil then
return nil, "unable to remove file '"..filepath.."' ("..errmsg..")"
end
end
end
local ok, errmsg = removedir(path)
if ok == nil then
return nil, "unable to remove directory '"..path.."' ("..errmsg..")"
end
return true
end
local function loadfrom_cont(path, ok, ...)
if ok then return ... end
local errmsg = ...
return nil, "corrupted file '"..path.."' ("..errmsg..")"
end
local function loadfrom(path)
local result, errmsg = loadfile(path, "t", {})
if result == nil then
if errmsg:find("No such file or directory") then
return
end
return nil, "unable to load file '"..path.."' ("..errmsg..")"
end
return loadfrom_cont(path, pcall(result))
end
local function saveto(path, ...)
local temp = path..".tmp" -- must be in the same path
-- of the final file because
-- 'os.rename' can only
-- rename files in the same
-- file system.
local result, errmsg = open(temp, "w")
if result == nil then
errmsg = "unable to create temporary file '"..temp.."' ("..errmsg..")"
else
local file = result
result, errmsg = file:write("return ", Serializer:tostring(...))
file:close()
if result == nil then
errmsg = "unable to write temporary file '"..temp.."' ("..errmsg..")"
else
result, errmsg = renamefile(temp, path)
if result == nil then
errmsg = "unable to replace file '"..path.."' (with file "..errmsg..")"
end
end
removefile(temp)
end
return result, errmsg
end
local function closeobject(obj)
obj.path = nil
setmetatable(obj, nil)
end
local Table = class()
function Table:__init()
function self.iterator(next)
local file = next()
while file ~= nil do
local path = self.path..file
if getattribute(path, "mode") == "file" then
return file:match("^(.+)%.lua$"), assert(loadfrom(path))
end
file = next()
end
end
end
function Table:getentry(key)
return loadfrom(self.path..key..".lua")
end
function Table:setentry(key, ...)
return saveto(self.path..key..".lua", ...)
end
function Table:setentryfield(key, field, ...)
local path = self.path..key..".lua"
local result, errmsg = loadfrom(path)
if result == nil then
if errmsg ~= nil then
return result, errmsg
end
result = {}
end
local count = select("#", ...)
local value = select(count, ...)
for i = 1, count-1 do
local field = select(i, ...)
local value = result[field]
if value == nil then value = {} end
result[field] = value
result = value
end
result[field] = value
result, errmsg = saveto(path, result)
return result, errmsg
end
function Table:removeentry(key)
local path = self.path..key..".lua"
local ok, errmsg = removefile(path)
if ok == nil then
return nil, "unable to remove file '"..path.."' ("..errmsg..")"
end
return true
end
function Table:ientries()
return self.iterator, listdir(self.path)
end
function Table:remove()
local result, errmsg = removepath(self.path)
if result then
self.base.tables[self.name] = nil
self.base, self.name = nil, nil
closeobject(self)
end
return result, errmsg
end
local TableNamePat = "[^.][^/\\?*]*"
local DataBase = class()
function DataBase:__init()
local path = self.path
local tables = {}
for name in listdir(path) do
local tablepath = path..name.."/"
if name:match(TableNamePat)
and getattribute(tablepath, "mode") == "directory" then
tables[name] = Table{ base = self, name = name, path = tablepath }
end
end
self.tables = tables
end
function DataBase:gettable(name)
local table = self.tables[name]
if table == nil then
local path = self.path..name
local result, errmsg = createpath(path)
if not result then
return nil, errmsg
end
table = Table{ base = self, name = name, path = path.."/" }
self.tables[name] = table
end
return table
end
function DataBase:itables()
return pairs(self.tables)
end
function DataBase:close()
for name, table in pairs(self.tables) do
closeobject(table)
end
closeobject(self)
return true
end
local module = {}
function module.open(path)
local result, errmsg = createpath(path)
if not result then return nil, errmsg end
return DataBase{ path = path.."/" }
end
return module
......@@ -8,12 +8,11 @@ OPENBUSLIB= ${OPENBUS_HOME}/lib
SRC= console.c
LIBS:= lce lfs luavararg luastruct luasocket loop luatuple \
luacoroutine luacothread luainspector luaidl oil luascs luaopenbus lua5.1
LIBS:= lce luavararg luastruct luasocket loop luatuple \
luacoroutine luacothread luainspector luaidl oil luascs luaopenbus
INCLUDES+= . $(SRCLUADIR) \
$(OPENBUSINC)/lce \
$(OPENBUSINC)/luafilesystem \
$(OPENBUSINC)/luavararg \
$(OPENBUSINC)/luastruct \
$(OPENBUSINC)/luasocket2 \
......
......@@ -18,7 +18,6 @@
#include "lualib.h"
#include "lce.h"
#include "lfs.h"
#include "luavararg.h"
#include "luastruct.h"
#include "luasocket.h"
......@@ -407,7 +406,6 @@ static int pmain (lua_State *L) {
/* preload binded C libraries */
luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 1);
lua_pushcfunction(L,luaopen_lfs);lua_setfield(L,-2,"lfs");
lua_pushcfunction(L,luaopen_vararg);lua_setfield(L,-2,"vararg");
lua_pushcfunction(L,luaopen_struct);lua_setfield(L,-2,"struct");
lua_pushcfunction(L,luaopen_socket_core);lua_setfield(L,-2,"socket.core");
......
......@@ -18,7 +18,6 @@ LUASRC= \
$(LUADIR)/openbus/util/argcheck.lua \
$(LUADIR)/openbus/util/autotable.lua \
$(LUADIR)/openbus/util/corba.lua \
$(LUADIR)/openbus/util/database.lua \
$(LUADIR)/openbus/util/logger.lua \
$(LUADIR)/openbus/util/messages.lua \
$(LUADIR)/openbus/util/oo.lua \
......@@ -27,6 +26,8 @@ LUASRC= \
$(LUADIR)/openbus/util/tickets.lua \
$(LUADIR)/openbus.lua
SCSIDL= $(OPENBUS_SCSIDL)/scs.idl
LIBIDL= $(OPENBUS_LIBIDL)/openbus.idl
NEWIDL= \
......@@ -40,14 +41,14 @@ OLDIDL= \
include ${OIL_HOME}/openbus/base.mak
$(LUADIR)/openbus/idl/parsed.lua: $(IDL2LUA) $(LIBIDL) $(NEWIDL)
$(OILBIN) $(IDL2LUA) -I $(OPENBUS_NEWIDL) -o $@ $(LIBIDL)
$(LUADIR)/openbus/idl/parsed.lua: $(IDL2LUA) $(LIBIDL) $(NEWIDL) $(SCSIDL)
$(OILBIN) $(IDL2LUA) -I $(OPENBUS_SCSIDL) -I $(OPENBUS_NEWIDL) -o $@ $(LIBIDL)
$(LUADIR)/openbus/core/idl/parsed.lua: $(IDL2LUA) $(NEWIDL)
$(OILBIN) $(IDL2LUA) -o $@ $(NEWIDL)
$(LUADIR)/openbus/core/idl/parsed.lua: $(IDL2LUA) $(NEWIDL) $(SCSIDL)
$(OILBIN) $(IDL2LUA) -I $(OPENBUS_SCSIDL) -o $@ $(NEWIDL)
$(LUADIR)/openbus/core/legacy/parsed.lua: $(IDL2LUA) $(OLDIDL)
$(OILBIN) $(IDL2LUA) -o $@ $(OLDIDL)
$(LUADIR)/openbus/core/legacy/parsed.lua: $(IDL2LUA) $(OLDIDL) $(SCSIDL)
$(OILBIN) $(IDL2LUA) -I $(OPENBUS_SCSIDL) -o $@ $(OLDIDL)
$(PRELOAD_DIR)/$(LIBNAME).c: $(LUAPRELOADER) $(LUASRC)
$(LOOPBIN) $(LUAPRELOADER) -m \
......
......@@ -34,14 +34,6 @@ local OpenBusContext = orb.OpenBusContext
assert(OpenBusContext.orb == orb)
local conns = {}
local checkuuid do
local used = {}
function checkuuid(id)
assert(used[id] == nil)
used[id] = true
end
end
local function catcherr(...)
local ok, err = pcall(...)
assert(not ok)
......@@ -81,8 +73,8 @@ local function assertlogged(conn)
-- check logged in only attributes
assert(conn.login ~= nil)
assert(conn.login.entity == entity)
checkuuid(conn.login.id)
checkuuid(conn.busid)
assert(conn.login.id ~= nil)
assert(conn.busid ~= nil)
local loginid = conn.login.id
local busid = conn.busid
-- check the attempt to login again
......
local Matcher = require "loop.debug.Matcher"
local Viewer = require("loop.debug.Viewer"){linebreak=" ",identation=""}
local Data = {
Users = {
JohnDoe = {name="John Doe",age=34,male=true},
JaneDoe = {name="Jane Doe",age=24,male=false},
BabyDoe = {name="Baby Doe",age=0.2},
},
Certifiers = {
JohnDoe = "4ABA81F8-7A42-11E0-A71F-001FF3565357",
JaneDoe = "5C9D8EB0-7A42-11E0-A71F-001FF3565357",
BabyDoe = "5D809AC0-7A42-11E0-A71F-001FF3565357",
},
}
local database = require "openbus.util.database"
do
local db = assert(database.open("test.db"))
for name, contents in pairs(Data) do
local table = db:gettable(name)
for key, value in pairs(contents) do
assert(table:setentry(key, value))
end
end
end
do
local function checktable(name, table)
local contents = Data[name]
for key, value in pairs(contents) do
assert(Matcher():match(table:getentry(key), value))
end
for key, value in table:ientries() do
assert(Matcher():match(value, contents[key]))
end
end
local db = assert(database.open("test.db"))
for name, table in db:itables() do
checktable(name, table)
end
for name in pairs(Data) do
local table = db:gettable(name)
checktable(name, table)
assert(table:remove())
end
end
do
assert(os.execute("chmod 000 test.db") == 0)
local ok, err = pcall(database.open, "test.db")
assert(ok == false)
assert(err:find("cannot open test.db/: Permission denied", 1, true))
assert(os.execute("chmod 755 test.db") == 0)
local db = assert(database.open("test.db"))
assert(io.open("test.db/ErrorTable", "w")):close()
local ok, err = db:gettable("ErrorTable")
assert(ok == nil)
assert(err == "'test.db/ErrorTable' expected to be directory (got file)")
local db = assert(database.open("test.db"))
local ok, err = db:gettable("ErrorTable")
assert(ok == nil)
assert(err == "'test.db/ErrorTable' expected to be directory (got file)")
local ok, err = db:gettable("ErrorTable/")
assert(ok == nil)
assert(err:match("unable to create directory 'test%.db/ErrorTable/' %(.-%)"))
os.remove("test.db/ErrorTable")
end
do
local db = assert(database.open("test.db"))
local table = assert(db:gettable("Table"))
local file = assert(io.open("test.db/Table/error.lua", "w"))
assert(file:write("illegal Lua code") == true)
file:close()
local entry, err = table:getentry("missing")
assert(entry == nil)
assert(err == nil)
local entry, err = table:getentry("error")
assert(entry == nil)
assert(err == "unable to load file 'test.db/Table/error.lua' (test.db/Table/error.lua:1: '=' expected near 'Lua')")
assert(os.execute("chmod 000 test.db/Table") == 0)
local entry, err = table:getentry("error")
assert(entry == nil)
assert(err == "unable to load file 'test.db/Table/error.lua' (cannot open test.db/Table/error.lua: Permission denied)")
local ok, err = table:setentry("error", {"vazio"})
assert(not ok)
assert(err:match("unable to create temporary file 'test%.db/Table/error%.lua%.tmp' %(.-: Permission denied%)"))
local ok, err = table:removeentry("error")
assert(not ok)
assert(err:match("unable to remove file 'test%.db/Table/error%.lua' %(.-: Permission denied%)"))
assert(os.execute("chmod 755 test.db/Table") == 0)
assert(os.execute("chmod 555 test.db") == 0)
local ok, err = table:remove()
assert(not ok)
assert(err == "unable to remove directory 'test.db/Table/' (Permission denied)")
assert(os.execute("chmod 755 test.db") == 0)
assert(table:remove())
end
do
local lfs = require "lfs"
assert(lfs.rmdir("test.db"))
end
......@@ -11,7 +11,6 @@ fi
LUACASES="\
openbus/test/util/tickets \
openbus/test/util/database \
openbus/test/LoginLogout \
openbus/test/Concurrency \
openbus/test/NoLoginIceptorCalls \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment