Back to index

lightning-sunbird  0.9+nobinonly
find.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: find.c,v $ $Revision: 1.3.28.1 $ $Date: 2006/03/02 22:45:14 $";
00039 #endif /* DEBUG */
00040 
00041 #include "ckdbm.h"
00042 
00043 static void
00044 nss_dbm_mdFindObjects_Final
00045 (
00046   NSSCKMDFindObjects *mdFindObjects,
00047   NSSCKFWFindObjects *fwFindObjects,
00048   NSSCKMDSession *mdSession,
00049   NSSCKFWSession *fwSession,
00050   NSSCKMDToken *mdToken,
00051   NSSCKFWToken *fwToken,
00052   NSSCKMDInstance *mdInstance,
00053   NSSCKFWInstance *fwInstance
00054 )
00055 {
00056   nss_dbm_find_t *find = (nss_dbm_find_t *)mdFindObjects->etc;
00057 
00058   /* Locks might have system resources associated */
00059   (void)NSSCKFWMutex_Destroy(find->list_lock);
00060   (void)NSSArena_Destroy(find->arena);
00061 }
00062 
00063 
00064 static NSSCKMDObject *
00065 nss_dbm_mdFindObjects_Next
00066 (
00067   NSSCKMDFindObjects *mdFindObjects,
00068   NSSCKFWFindObjects *fwFindObjects,
00069   NSSCKMDSession *mdSession,
00070   NSSCKFWSession *fwSession,
00071   NSSCKMDToken *mdToken,
00072   NSSCKFWToken *fwToken,
00073   NSSCKMDInstance *mdInstance,
00074   NSSCKFWInstance *fwInstance,
00075   NSSArena *arena,
00076   CK_RV *pError
00077 )
00078 {
00079   nss_dbm_find_t *find = (nss_dbm_find_t *)mdFindObjects->etc;
00080   struct nss_dbm_dbt_node *node;
00081   nss_dbm_object_t *object;
00082   NSSCKMDObject *rv;
00083 
00084   while(1) {
00085     /* Lock */
00086     {
00087       *pError = NSSCKFWMutex_Lock(find->list_lock);
00088       if( CKR_OK != *pError ) {
00089         return (NSSCKMDObject *)NULL;
00090       }
00091       
00092       node = find->found;
00093       if( (struct nss_dbm_dbt_node *)NULL != node ) {
00094         find->found = node->next;
00095       }
00096       
00097       *pError = NSSCKFWMutex_Unlock(find->list_lock);
00098       if( CKR_OK != *pError ) {
00099         /* screwed now */
00100         return (NSSCKMDObject *)NULL;
00101       }
00102     }
00103 
00104     if( (struct nss_dbm_dbt_node *)NULL == node ) {
00105       break;
00106     }
00107 
00108     if( nss_dbm_db_object_still_exists(node->dbt) ) {
00109       break;
00110     }
00111   }
00112 
00113   if( (struct nss_dbm_dbt_node *)NULL == node ) {
00114     *pError = CKR_OK;
00115     return (NSSCKMDObject *)NULL;
00116   }
00117 
00118   object = nss_ZNEW(arena, nss_dbm_object_t);
00119   if( (nss_dbm_object_t *)NULL == object ) {
00120     *pError = CKR_HOST_MEMORY;
00121     return (NSSCKMDObject *)NULL;
00122   }
00123 
00124   object->arena = arena;
00125   object->handle = nss_ZNEW(arena, nss_dbm_dbt_t);
00126   if( (nss_dbm_dbt_t *)NULL == object->handle ) {
00127     *pError = CKR_HOST_MEMORY;
00128     return (NSSCKMDObject *)NULL;
00129   }
00130 
00131   object->handle->my_db = node->dbt->my_db;
00132   object->handle->dbt.size = node->dbt->dbt.size;
00133   object->handle->dbt.data = nss_ZAlloc(arena, node->dbt->dbt.size);
00134   if( (void *)NULL == object->handle->dbt.data ) {
00135     *pError = CKR_HOST_MEMORY;
00136     return (NSSCKMDObject *)NULL;
00137   }
00138 
00139   (void)memcpy(object->handle->dbt.data, node->dbt->dbt.data, node->dbt->dbt.size);
00140 
00141   rv = nss_dbm_mdObject_factory(object, pError);
00142   if( (NSSCKMDObject *)NULL == rv ) {
00143     return (NSSCKMDObject *)NULL;
00144   }
00145 
00146   return rv;
00147 }
00148 
00149 NSS_IMPLEMENT NSSCKMDFindObjects *
00150 nss_dbm_mdFindObjects_factory
00151 (
00152   nss_dbm_find_t *find,
00153   CK_RV *pError
00154 )
00155 {
00156   NSSCKMDFindObjects *rv;
00157 
00158   rv = nss_ZNEW(find->arena, NSSCKMDFindObjects);
00159   if( (NSSCKMDFindObjects *)NULL == rv ) {
00160     *pError = CKR_HOST_MEMORY;
00161     return (NSSCKMDFindObjects *)NULL;
00162   }
00163 
00164   rv->etc = (void *)find;
00165   rv->Final = nss_dbm_mdFindObjects_Final;
00166   rv->Next = nss_dbm_mdFindObjects_Next;
00167 
00168   return rv;
00169 }