catalog subsystem should cope with being called with different lua states 0.3.3
authorJ. Ali Harlow <ali@juiblex.co.uk>
Fri May 20 11:20:22 2016 +0100 (2016-05-20)
changeset 1830dd8888a271
parent 17 a3325f438881
child 19 a659f35ffb54
catalog subsystem should cope with being called with different lua states

whelk 0.3.2 was assuming that lua state would always be the same. If it
wasn't then there would be no cat admin metatable registered in the second
lua state but whelk would just assume that there was causing things to go
wrong. We now use luaL_newmetatable() to test for whether a metatable was
already registered and take the appropriate actions if it wasn't.
.hgtags
configure.ac
whelk/_whelk.h
whelk/catalog.c
     1.1 --- a/.hgtags	Thu Aug 25 20:41:08 2011 +0100
     1.2 +++ b/.hgtags	Fri May 20 11:20:22 2016 +0100
     1.3 @@ -1,1 +1,2 @@
     1.4  579831f324c72818382f1eeacfe54b5b9dace0e1 0.3.2
     1.5 +36ce85e5c7c0fe2fb7b140656021b5ef28ab5b7f 0.3.2.50
     2.1 --- a/configure.ac	Thu Aug 25 20:41:08 2011 +0100
     2.2 +++ b/configure.ac	Fri May 20 11:20:22 2016 +0100
     2.3 @@ -1,7 +1,7 @@
     2.4  #                                               -*- Autoconf -*-
     2.5  # Process this file with autoconf to produce a configure script.
     2.6  
     2.7 -AC_INIT([whelk],[0.3.2],[ali@juiblex.co.uk])
     2.8 +AC_INIT([whelk],[0.3.3],[ali@juiblex.co.uk])
     2.9  AC_PREREQ(2.59)
    2.10  AC_CONFIG_AUX_DIR([config])
    2.11  AC_CONFIG_SRCDIR([whelk/whelk.c])
    2.12 @@ -27,7 +27,7 @@
    2.13  # See http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91 for details
    2.14  #
    2.15  LT_CURRENT=2
    2.16 -LT_REVISION=0
    2.17 +LT_REVISION=1
    2.18  LT_AGE=2
    2.19  AC_SUBST(LT_CURRENT)
    2.20  AC_SUBST(LT_REVISION)
     3.1 --- a/whelk/_whelk.h	Thu Aug 25 20:41:08 2011 +0100
     3.2 +++ b/whelk/_whelk.h	Fri May 20 11:20:22 2016 +0100
     3.3 @@ -56,6 +56,7 @@
     3.4  
     3.5  int whelk_crypt_cat_admin_new(lua_State *L);
     3.6  
     3.7 +int whelk_win32_error(lua_State *L,DWORD err);
     3.8  int whelk_setup_copy_oem_inf(lua_State *L);
     3.9  int whelk_setup_uninstall_oem_inf(lua_State *L);
    3.10  void whelk_open_setup(lua_State *L);
     4.1 --- a/whelk/catalog.c	Thu Aug 25 20:41:08 2011 +0100
     4.2 +++ b/whelk/catalog.c	Fri May 20 11:20:22 2016 +0100
     4.3 @@ -132,26 +132,39 @@
     4.4      { NULL }
     4.5  };
     4.6  
     4.7 -static struct whelk_cat_admin_class *whelk_cat_admin_class_new(lua_State *L)
     4.8 +static struct whelk_cat_admin_class *whelk_cat_admin_class_new(lua_State *L,
     4.9 +  DWORD *err)
    4.10  {
    4.11      struct whelk_cat_admin_class *klass;
    4.12      klass=calloc(sizeof(struct whelk_cat_admin_class),1);
    4.13 -    klass->wintrust=LoadLibrary("wintrust.dll");
    4.14 +    klass->wintrust=LoadLibraryA("wintrust.dll");
    4.15      if (!klass->wintrust)
    4.16      {
    4.17 +	if (err)
    4.18 +	    *err=GetLastError();
    4.19  	free(klass);
    4.20  	return NULL;
    4.21      }
    4.22 -    klass->acquire_context=
    4.23 +    klass->acquire_context=(BOOL (WINAPI *)())
    4.24        GetProcAddress(klass->wintrust,"CryptCATAdminAcquireContext");
    4.25 +    if (err && !klass->acquire_context)
    4.26 +	*err=GetLastError();
    4.27      klass->add_catalog=(HCATINFO (WINAPI *)())
    4.28        GetProcAddress(klass->wintrust,"CryptCATAdminAddCatalog");
    4.29 -    klass->remove_catalog=
    4.30 +    if (err && !klass->add_catalog)
    4.31 +	*err=GetLastError();
    4.32 +    klass->remove_catalog=(BOOL (WINAPI *)())
    4.33        GetProcAddress(klass->wintrust,"CryptCATAdminRemoveCatalog");
    4.34 -    klass->release_catalog_context=
    4.35 +    if (err && !klass->remove_catalog)
    4.36 +	*err=GetLastError();
    4.37 +    klass->release_catalog_context=(BOOL (WINAPI *)())
    4.38        GetProcAddress(klass->wintrust,"CryptCATAdminReleaseCatalogContext");
    4.39 -    klass->release_context=
    4.40 +    if (err && !klass->release_catalog_context)
    4.41 +	*err=GetLastError();
    4.42 +    klass->release_context=(BOOL (WINAPI *)())
    4.43        GetProcAddress(klass->wintrust,"CryptCATAdminReleaseContext");
    4.44 +    if (err && !klass->release_context)
    4.45 +	*err=GetLastError();
    4.46      if (!klass->acquire_context || !klass->add_catalog ||
    4.47        !klass->release_catalog_context || !klass->release_context)
    4.48      {
    4.49 @@ -159,33 +172,39 @@
    4.50  	free(klass);
    4.51  	return NULL;
    4.52      }
    4.53 -    luaL_newmetatable(L,WHELK_TYPE_CAT_ADMIN);
    4.54 -    lua_pushvalue(L,-1);
    4.55 -    lua_setfield(L,-2,"__index");
    4.56 -    luaL_register(L,NULL,whelk_cat_admin_methods);
    4.57      return klass;
    4.58  }
    4.59  
    4.60 -static struct whelk_cat_admin *newcatadmin(lua_State *L)
    4.61 +static struct whelk_cat_admin *newcatadmin(lua_State *L,DWORD *err)
    4.62  {
    4.63      struct whelk_cat_admin *ca;
    4.64      static struct whelk_cat_admin_class *klass=NULL;
    4.65      if (!klass)
    4.66 -	klass=whelk_cat_admin_class_new(L);
    4.67 +	klass=whelk_cat_admin_class_new(L,err);
    4.68 +    if (!klass)
    4.69 +	return NULL;
    4.70      ca=lua_newuserdata(L,sizeof(struct whelk_cat_admin));
    4.71      ca->klass=klass;
    4.72      ca->handle=INVALID_HANDLE_VALUE;
    4.73 -    luaL_getmetatable(L,WHELK_TYPE_CAT_ADMIN);
    4.74 +    if (luaL_newmetatable(L,WHELK_TYPE_CAT_ADMIN))
    4.75 +    {
    4.76 +	lua_pushvalue(L,-1);
    4.77 +	lua_setfield(L,-2,"__index");
    4.78 +	luaL_register(L,NULL,whelk_cat_admin_methods);
    4.79 +    }
    4.80      lua_setmetatable(L,-2);
    4.81      return ca;
    4.82  }
    4.83  
    4.84  int whelk_crypt_cat_admin_new(lua_State *L)
    4.85  {
    4.86 +    DWORD err=0;
    4.87      struct whelk_cat_admin *ca;
    4.88      static GUID subsystem=DRIVER_ACTION_VERIFY;
    4.89 -    ca=newcatadmin(L);
    4.90 +    ca=newcatadmin(L,&err);
    4.91 +    if (!ca)
    4.92 +	return whelk_win32_error(L,err);
    4.93      if (!ca->klass->acquire_context(&ca->handle,&subsystem,0))
    4.94 -	lua_pushnil(L);
    4.95 +	return whelk_win32_error(L,GetLastError());
    4.96      return 1;
    4.97  }