2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // MultiDLDb implementation.
23 #include <Security/multidldb.h>
24 #include <Security/securestorage.h>
25 #include <Security/cssmerrno.h>
32 using namespace CssmClient
;
38 // MultiDLDbDbCursorImpl declaration
40 class MultiDLDbDbCursorImpl
: public DbCursorImpl
43 MultiDLDbDbCursorImpl(const MultiDLDb
&parent
, const CSSM_QUERY
&query
, CssmAllocator
&allocator
);
44 MultiDLDbDbCursorImpl(const MultiDLDb
&parent
, uint32 capacity
, CssmAllocator
&allocator
);
45 virtual ~MultiDLDbDbCursorImpl();
47 bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
);
49 MultiDLDb
multiDLDb() { return parent
<MultiDLDb
>(); }
53 MultiDLDbImpl::ListRef mListRef
;
54 MultiDLDbImpl::List::const_iterator mNext
;
55 MultiDLDbImpl::List::const_iterator mEnd
;
59 } // end namespace CssmClient
61 } // end namespace Security
66 MultiDLDbImpl::MultiDLDbImpl(const vector
<DLDbIdentifier
> &list
, bool useSecureStorage
, const Cssm
&cssm
)
67 : ObjectImpl(cssm
), mListRef(list
), mUseSecureStorage(useSecureStorage
)
71 MultiDLDbImpl::MultiDLDbImpl(const vector
<DLDbIdentifier
> &list
, bool useSecureStorage
)
72 : ObjectImpl(Cssm::standard()), mListRef(list
), mUseSecureStorage(useSecureStorage
)
76 MultiDLDbImpl::~MultiDLDbImpl()
82 MultiDLDbImpl::database(const DLDbIdentifier
&dlDbIdentifier
)
84 StLock
<Mutex
> _(mLock
);
85 DbMap::const_iterator it
= mDbMap
.find(dlDbIdentifier
);
86 if (it
!= mDbMap
.end())
89 Module
module(dlDbIdentifier
.ssuid().guid(), cssm());
91 if (dlDbIdentifier
.ssuid().subserviceType() & CSSM_SERVICE_CSP
)
93 if (mUseSecureStorage
)
101 dl
->subserviceId(dlDbIdentifier
.ssuid().subserviceId());
102 dl
->version(dlDbIdentifier
.ssuid().version());
103 Db
db(dl
, dlDbIdentifier
.dbName());
104 if (find(mListRef
->begin(), mListRef
->end(), dlDbIdentifier
) != mListRef
->end())
105 mDbMap
.insert(DbMap::value_type(dlDbIdentifier
, db
));
111 MultiDLDbImpl::list(const vector
<DLDbIdentifier
> &list
)
113 StLock
<Mutex
> _(mLock
);
114 set
<DLDbIdentifier
> oldList(mListRef
->begin(), mListRef
->end());
115 mListRef
= ListRef(list
);
116 set
<DLDbIdentifier
> newList(mListRef
->begin(), mListRef
->end());
117 vector
<DLDbIdentifier
> obsolete
;
118 back_insert_iterator
<vector
<DLDbIdentifier
> > ii(obsolete
);
119 // Remove all db's from the map that were in oldList but are not in mListRef.
120 set_difference(oldList
.begin(), oldList
.end(), newList
.begin(), newList
.end(), ii
);
121 for (vector
<DLDbIdentifier
>::const_iterator it
= obsolete
.begin(); it
!= obsolete
.end(); ++it
)
126 MultiDLDbImpl::newDbCursor(const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
128 return new MultiDLDbDbCursorImpl(MultiDLDb(this), query
, allocator
);
132 MultiDLDbImpl::newDbCursor(uint32 capacity
, CssmAllocator
&allocator
)
134 return new MultiDLDbDbCursorImpl(MultiDLDb(this), capacity
, allocator
);
138 MultiDLDbImpl::activate()
143 MultiDLDbImpl::deactivate()
145 StLock
<Mutex
> _(mLock
);
146 mDbMap
.erase(mDbMap
.begin(), mDbMap
.end());
151 // MultiDLDbDbCursorImpl
153 MultiDLDbDbCursorImpl::MultiDLDbDbCursorImpl(const MultiDLDb
&parent
,
154 const CSSM_QUERY
&query
, CssmAllocator
&allocator
)
155 : DbCursorImpl(parent
, query
, allocator
)
159 MultiDLDbDbCursorImpl::MultiDLDbDbCursorImpl(const MultiDLDb
&parent
,
160 uint32 capacity
, CssmAllocator
&allocator
)
161 : DbCursorImpl(parent
, capacity
, allocator
)
165 MultiDLDbDbCursorImpl::~MultiDLDbDbCursorImpl()
175 MultiDLDbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
184 // This is how it ends.
189 mCursor
= DbCursor(multiDLDb()->database(*mNext
++), *this);
194 if (mCursor
->next(attributes
, data
, uniqueId
))
198 catch(const CssmCommonError
&err
)
200 OSStatus status
= err
.osStatus();
201 if(status
!= CSSMERR_DL_DATASTORE_DOESNOT_EXIST
)
207 mCursor
= DbCursor();
212 MultiDLDbDbCursorImpl::activate()
216 mListRef
= multiDLDb()->listRef();
217 mNext
= mListRef
->begin();
218 mEnd
= mListRef
->end();
224 MultiDLDbDbCursorImpl::deactivate()
229 mListRef
= MultiDLDbImpl::ListRef();
231 mCursor
= DbCursor();