2 * Copyright (c) 2000-2001,2011-2012,2014 Apple 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_cdsa_client/multidldb.h>
24 #include <security_cdsa_client/securestorage.h>
31 using namespace CssmClient
;
37 // MultiDLDbDbCursorImpl declaration
39 class MultiDLDbDbCursorImpl
: public DbCursorImpl
42 MultiDLDbDbCursorImpl(const MultiDLDb
&parent
, const CSSM_QUERY
&query
, Allocator
&allocator
);
43 MultiDLDbDbCursorImpl(const MultiDLDb
&parent
, uint32 capacity
, Allocator
&allocator
);
44 virtual ~MultiDLDbDbCursorImpl();
46 bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
);
48 MultiDLDb
multiDLDb() { return parent
<MultiDLDb
>(); }
52 MultiDLDbImpl::ListRef mListRef
;
53 MultiDLDbImpl::List::const_iterator mNext
;
54 MultiDLDbImpl::List::const_iterator mEnd
;
58 } // end namespace CssmClient
60 } // end namespace Security
65 MultiDLDbImpl::MultiDLDbImpl(const vector
<DLDbIdentifier
> &list
, bool useSecureStorage
, const Cssm
&cssm
)
66 : ObjectImpl(cssm
), mListRef(list
), mUseSecureStorage(useSecureStorage
)
70 MultiDLDbImpl::MultiDLDbImpl(const vector
<DLDbIdentifier
> &list
, bool useSecureStorage
)
71 : ObjectImpl(Cssm::standard()), mListRef(list
), mUseSecureStorage(useSecureStorage
)
75 MultiDLDbImpl::~MultiDLDbImpl()
81 MultiDLDbImpl::database(const DLDbIdentifier
&dlDbIdentifier
)
83 StLock
<Mutex
> _(mLock
);
84 DbMap::const_iterator it
= mDbMap
.find(dlDbIdentifier
);
85 if (it
!= mDbMap
.end())
88 Module
module(dlDbIdentifier
.ssuid().guid(), cssm());
90 if (dlDbIdentifier
.ssuid().subserviceType() & CSSM_SERVICE_CSP
)
92 if (mUseSecureStorage
)
100 dl
->subserviceId(dlDbIdentifier
.ssuid().subserviceId());
101 dl
->version(dlDbIdentifier
.ssuid().version());
102 Db
db(dl
, dlDbIdentifier
.dbName());
103 if (find(mListRef
->begin(), mListRef
->end(), dlDbIdentifier
) != mListRef
->end())
104 mDbMap
.insert(DbMap::value_type(dlDbIdentifier
, db
));
110 MultiDLDbImpl::list(const vector
<DLDbIdentifier
> &list
)
112 StLock
<Mutex
> _(mLock
);
113 set
<DLDbIdentifier
> oldList(mListRef
->begin(), mListRef
->end());
114 mListRef
= ListRef(list
);
115 set
<DLDbIdentifier
> newList(mListRef
->begin(), mListRef
->end());
116 vector
<DLDbIdentifier
> obsolete
;
117 back_insert_iterator
<vector
<DLDbIdentifier
> > ii(obsolete
);
118 // Remove all db's from the map that were in oldList but are not in mListRef.
119 set_difference(oldList
.begin(), oldList
.end(), newList
.begin(), newList
.end(), ii
);
120 for (vector
<DLDbIdentifier
>::const_iterator it
= obsolete
.begin(); it
!= obsolete
.end(); ++it
)
125 MultiDLDbImpl::newDbCursor(const CSSM_QUERY
&query
, Allocator
&allocator
)
127 return new MultiDLDbDbCursorImpl(MultiDLDb(this), query
, allocator
);
131 MultiDLDbImpl::newDbCursor(uint32 capacity
, Allocator
&allocator
)
133 return new MultiDLDbDbCursorImpl(MultiDLDb(this), capacity
, allocator
);
137 MultiDLDbImpl::activate()
142 MultiDLDbImpl::deactivate()
144 StLock
<Mutex
> _(mLock
);
145 mDbMap
.erase(mDbMap
.begin(), mDbMap
.end());
150 // MultiDLDbDbCursorImpl
152 MultiDLDbDbCursorImpl::MultiDLDbDbCursorImpl(const MultiDLDb
&parent
,
153 const CSSM_QUERY
&query
, Allocator
&allocator
)
154 : DbCursorImpl(parent
, query
, allocator
)
158 MultiDLDbDbCursorImpl::MultiDLDbDbCursorImpl(const MultiDLDb
&parent
,
159 uint32 capacity
, Allocator
&allocator
)
160 : DbCursorImpl(parent
, capacity
, allocator
)
164 MultiDLDbDbCursorImpl::~MultiDLDbDbCursorImpl()
174 MultiDLDbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
183 // This is how it ends.
188 mCursor
= DbCursor(multiDLDb()->database(*mNext
++), *this);
193 if (mCursor
->next(attributes
, data
, uniqueId
))
197 catch(const CommonError
&err
)
199 OSStatus status
= err
.osStatus();
200 if(status
!= CSSMERR_DL_DATASTORE_DOESNOT_EXIST
)
206 mCursor
= DbCursor();
211 MultiDLDbDbCursorImpl::activate()
213 StLock
<Mutex
> _(mActivateMutex
);
216 mListRef
= multiDLDb()->listRef();
217 mNext
= mListRef
->begin();
218 mEnd
= mListRef
->end();
224 MultiDLDbDbCursorImpl::deactivate()
226 StLock
<Mutex
> _(mActivateMutex
);
230 mListRef
= MultiDLDbImpl::ListRef();
232 mCursor
= DbCursor();