Merge 0.9->trunk
authorMatthew Wild <mwild1@gmail.com>
Mon, 2 Sep 2013 23:20:28 +0000 (00:20 +0100)
committerMatthew Wild <mwild1@gmail.com>
Mon, 2 Sep 2013 23:20:28 +0000 (00:20 +0100)
1  2 
core/configmanager.lua
util-src/pposix.c
util/set.lua

diff --combined core/configmanager.lua
index d73bafa4e2bc2ff126b86056af72dd8f0c416106,71f60c81952f32260b483ece8951255ee4c47123..d92120d03493e95175a6c06209c636613779bcf0
@@@ -1,7 -1,7 +1,7 @@@
  -- Prosody IM
  -- Copyright (C) 2008-2010 Matthew Wild
  -- Copyright (C) 2008-2010 Waqas Hussain
- -- 
+ --
  -- This project is MIT/X11 licensed. Please see the
  -- COPYING file in the source package for more information.
  --
@@@ -73,11 -73,11 +73,11 @@@ d
                        -- Some normalization
                        parent_path = parent_path:gsub("%"..path_sep.."+$", "");
                        path = path:gsub("^%.%"..path_sep.."+", "");
-                       
                        local is_relative;
                        if path_sep == "/" and path:sub(1,1) ~= "/" then
                                is_relative = true;
 -                      elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" or path:sub(2,3) ~= ":/")) then
 +                      elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
                                is_relative = true;
                        end
                        if is_relative then
@@@ -85,7 -85,7 +85,7 @@@
                        end
                end
                return path;
-       end     
+       end
  end
  
  -- Helper function to convert a glob to a Lua pattern
@@@ -167,7 -167,7 +167,7 @@@ d
                                        set(config, env.__currenthost or "*", k, v);
                                end
                });
-               
                rawset(env, "__currenthost", "*") -- Default is global
                function env.VirtualHost(name)
                        if rawget(config, name) and rawget(config[name], "component_module") then
                        end;
                end
                env.Host, env.host = env.VirtualHost, env.VirtualHost;
-               
                function env.Component(name)
                        if rawget(config, name) and rawget(config[name], "defined") and not rawget(config[name], "component_module") then
                                error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
                                        set(config, name or "*", option_name, option_value);
                                end
                        end
-       
                        return function (module)
                                        if type(module) == "string" then
                                                set(config, name, "component_module", module);
                                end
                end
                env.component = env.Component;
-               
                function env.Include(file)
                        if file:match("[*?]") then
                                local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
                        end
                end
                env.include = env.Include;
-               
                function env.RunScript(file)
                        return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
                end
-               
                local chunk, err = envload(data, "@"..config_file, env);
-               
                if not chunk then
                        return nil, err;
                end
-               
                local ok, err = pcall(chunk);
-               
                if not ok then
                        return nil, err;
                end
-               
                return true;
        end
-       
  end
  
  return _M;
diff --combined util-src/pposix.c
index 7f64038beb3bd07bd353396ee47de4502e1db0b8,2e1de97171dea2ce0f4d902e664f357ada77ec90..8ad167b72d25ad5d0e0c3f619207302c09d05c1f
@@@ -36,7 -36,7 +36,7 @@@
  #include "lauxlib.h"
  
  #include <fcntl.h>
 -#if defined(_GNU_SOURCE)
 +#if defined(__linux__) && defined(_GNU_SOURCE)
  #include <linux/falloc.h>
  #endif
  
@@@ -491,11 -491,24 +491,24 @@@ int string2resource(const char *s) 
        return -1;
  }
  
+ unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) {
+       switch(lua_type(L, idx)) {
+       case LUA_TSTRING:
+               if(strcmp(lua_tostring(L, idx), "unlimited") == 0)
+                       return RLIM_INFINITY;
+       case LUA_TNUMBER:
+               return lua_tointeger(L, idx);
+       case LUA_TNONE:
+       case LUA_TNIL:
+               return current;
+       default:
+               return luaL_argerror(L, idx, "unexpected type");
+       }
+ }
  int lc_setrlimit(lua_State *L) {
+       struct rlimit lim;
        int arguments = lua_gettop(L);
-       int softlimit = -1;
-       int hardlimit = -1;
-       const char *resource = NULL;
        int rid = -1;
        if(arguments < 1 || arguments > 3) {
                lua_pushboolean(L, 0);
                return 2;
        }
  
-       resource = luaL_checkstring(L, 1);
-       softlimit = luaL_checkinteger(L, 2);
-       hardlimit = luaL_checkinteger(L, 3);
+       rid = string2resource(luaL_checkstring(L, 1));
+       if (rid == -1) {
+               lua_pushboolean(L, 0);
+               lua_pushstring(L, "invalid-resource");
+               return 2;
+       }
  
-       rid = string2resource(resource);
-       if (rid != -1) {
-               struct rlimit lim;
-               struct rlimit lim_current;
-               if (softlimit < 0 || hardlimit < 0) {
-                       if (getrlimit(rid, &lim_current)) {
-                               lua_pushboolean(L, 0);
-                               lua_pushstring(L, "getrlimit-failed");
-                               return 2;
-                       }
-               }
+       /* Fetch current values to use as defaults */
+       if (getrlimit(rid, &lim)) {
+               lua_pushboolean(L, 0);
+               lua_pushstring(L, "getrlimit-failed");
+               return 2;
+       }
  
-               if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur;
-                       else lim.rlim_cur = softlimit;
-               if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max;
-                       else lim.rlim_max = hardlimit;
+       lim.rlim_cur = arg_to_rlimit(L, 2, lim.rlim_cur);
+       lim.rlim_max = arg_to_rlimit(L, 3, lim.rlim_max);
  
-               if (setrlimit(rid, &lim)) {
-                       lua_pushboolean(L, 0);
-                       lua_pushstring(L, "setrlimit-failed");
-                       return 2;
-               }
-       } else {
-               /* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */
+       if (setrlimit(rid, &lim)) {
                lua_pushboolean(L, 0);
-               lua_pushstring(L, "invalid-resource");
+               lua_pushstring(L, "setrlimit-failed");
                return 2;
        }
        lua_pushboolean(L, 1);
@@@ -552,6 -554,8 +554,8 @@@ int lc_getrlimit(lua_State *L) 
                return 2;
        }
  
        resource = luaL_checkstring(L, 1);
        rid = string2resource(resource);
        if (rid != -1) {
                return 2;
        }
        lua_pushboolean(L, 1);
-       lua_pushnumber(L, lim.rlim_cur);
-       lua_pushnumber(L, lim.rlim_max);
+       if(lim.rlim_cur == RLIM_INFINITY)
+               lua_pushstring(L, "unlimited");
+       else
+               lua_pushnumber(L, lim.rlim_cur);
+       if(lim.rlim_max == RLIM_INFINITY)
+               lua_pushstring(L, "unlimited");
+       else
+               lua_pushnumber(L, lim.rlim_max);
        return 3;
  }
  
@@@ -670,7 -680,7 +680,7 @@@ int lc_fallocate(lua_State* L
        offset = luaL_checkinteger(L, 2);
        len = luaL_checkinteger(L, 3);
  
 -#if defined(_GNU_SOURCE)
 +#if defined(__linux__) && defined(_GNU_SOURCE)
        if(fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len) == 0)
        {
                lua_pushboolean(L, 1);
diff --combined util/set.lua
index b9e9ef217c25d4f8d55e37853d0788ee863eb061,e9dfec1bca729b509040e785248389187dca8abd..a2df669c05e97706b8d6120cddece71a8a5d73b7
@@@ -1,7 -1,7 +1,7 @@@
  -- Prosody IM
  -- Copyright (C) 2008-2010 Matthew Wild
  -- Copyright (C) 2008-2010 Waqas Hussain
- -- 
+ --
  -- This project is MIT/X11 licensed. Please see the
  -- COPYING file in the source package for more information.
  --
@@@ -40,13 -40,13 +40,13 @@@ function set_mt.__eq(set1, set2
                        return false;
                end
        end
-       
        for item in pairs(set2) do
                if not set1[item] then
                        return false;
                end
        end
-       
        return true;
  end
  function set_mt.__tostring(set)
@@@ -65,23 -65,23 +65,23 @@@ en
  function new(list)
        local items = setmetatable({}, items_mt);
        local set = { _items = items };
-       
        function set:add(item)
                items[item] = true;
        end
-       
        function set:contains(item)
                return items[item];
        end
-       
        function set:items()
                return items;
        end
-       
        function set:remove(item)
                items[item] = nil;
        end
-       
        function set:add_list(list)
                if list then
                        for _, item in ipairs(list) do
                        end
                end
        end
-       
        function set:include(otherset)
 -              for item in pairs(otherset) do
 +              for item in otherset do
                        items[item] = true;
                end
        end
  
        function set:exclude(otherset)
 -              for item in pairs(otherset) do
 +              for item in otherset do
                        items[item] = nil;
                end
        end
-       
        function set:empty()
                return not next(items);
        end
-       
        if list then
                set:add_list(list);
        end
-       
        return setmetatable(set, set_mt);
  end
  
  function union(set1, set2)
        local set = new();
        local items = set._items;
-       
        for item in pairs(set1._items) do
                items[item] = true;
        end
        for item in pairs(set2._items) do
                items[item] = true;
        end
-       
        return set;
  end
  
  function difference(set1, set2)
        local set = new();
        local items = set._items;
-       
        for item in pairs(set1._items) do
                items[item] = (not set2._items[item]) or nil;
        end
@@@ -142,13 -142,13 +142,13 @@@ en
  function intersection(set1, set2)
        local set = new();
        local items = set._items;
-       
        set1, set2 = set1._items, set2._items;
-       
        for item in pairs(set1) do
                items[item] = (not not set2[item]) or nil;
        end
-       
        return set;
  end