2 OpenChange Storage Abstraction Layer library
6 Note: init and load functions have been copied from
7 samba4/source4/param/util.c initially written by Jelmer.
9 Copyright (C) Jelmer Vernooij 2005-2007
10 Copyright (C) Julien Kerihuel 2009-2011
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 \file mapistore_backend_public.c
29 \brief Provides a public API mapistore backends can use to interact
30 with mapistore internals. Some functions are just wrappers over
31 existing mapistore functions.
34 #include "mapistore_errors.h"
35 #include "mapistore.h"
36 #include "mapistore_common.h"
37 #include "mapistore_private.h"
38 #include "mapistore_backend.h"
42 \details Open a wrapped context to a LDB database
44 This function is a wrapper to mapistore_ldb_wrap_connect which
45 helps keeping mapistore_backend_context opaque to backends.
47 \param ctx pointer to the mapistore backend opaque context
48 \param path the path to the database (mapistore.ldb) to open
50 \return valid LDB context pointer on success, otherwise NULL
52 struct ldb_context *mapistore_public_ldb_connect(struct mapistore_backend_context *ctx,
55 struct mapistore_context *mstore_ctx = (struct mapistore_context *)ctx;
56 struct tevent_context *ev;
59 if (!path || !mstore_ctx) {
63 ev = tevent_context_init(mstore_ctx);
66 return mapistore_ldb_wrap_connect(mstore_ctx, ev, path, 0);
71 \details Let a backend checks if a message of folder is already
72 indexed in mapistore.ldb
74 \param ctx pointer to the mapistore backend opaque context
75 \param username the username where to look for the URI
76 \param mapistore_uri the mapistore URI to lookup
78 \return MAPISTORE_ERR_EXIST if the URI was found,
79 MAPISTORE_ERR_NOT_FOUND if it wasn't, other MAPISTORE error
81 enum MAPISTORE_ERROR mapistore_exist(struct mapistore_backend_context *ctx,
83 const char *mapistore_uri)
85 enum MAPISTORE_ERROR retval;
86 struct mapistore_context *mstore_ctx;
89 MAPISTORE_RETVAL_IF(!ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
90 MAPISTORE_RETVAL_IF(!ctx->mstore_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
91 MAPISTORE_RETVAL_IF(!mapistore_uri, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
93 mstore_ctx = ctx->mstore_ctx;
95 /* Step 1. Create an indexing context */
96 retval = mapistore_indexing_context_add(mstore_ctx,
98 &(mstore_ctx->mapistore_indexing_list));
99 MAPISTORE_RETVAL_IF(retval, retval, NULL);
101 /* Step 2. Search the URI */
102 retval = mapistore_indexing_record_search_uri(mstore_ctx->mapistore_indexing_list, mapistore_uri);
108 \details Let backends register a folder and index it within
109 mapistore indexing database
111 \param ctx pointer to the mapistore backend opaque context
112 \param username the username used to register the folder
113 \param parent_uri the mapistore URI of the parent folder
114 \param mapistore_uri the mapistore URI to register
115 \param range the number of message IDs we want to reserve for this
118 \return MAPISTORE success on success, otherwise MAPISTORE error
120 enum MAPISTORE_ERROR mapistore_register_folder(struct mapistore_backend_context *ctx,
121 const char *username,
122 const char *parent_uri,
123 const char *mapistore_uri,
126 enum MAPISTORE_ERROR retval;
127 struct mapistore_context *mstore_ctx;
134 MAPISTORE_RETVAL_IF(!ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
135 MAPISTORE_RETVAL_IF(!ctx->mstore_ctx, MAPISTORE_ERR_NOT_INITIALIZED, NULL);
136 MAPISTORE_RETVAL_IF(!username, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
137 MAPISTORE_RETVAL_IF(!mapistore_uri, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
140 range = MAPISTORE_INDEXING_DFLT_ALLOC_RANGE_VAL;
143 mstore_ctx = ctx->mstore_ctx;
145 /* Step 1. Ensure the URI doesn't exist */
146 retval = mapistore_exist(ctx, username, mapistore_uri);
147 MAPISTORE_RETVAL_IF(retval != MAPISTORE_ERR_NOT_FOUND, retval, NULL);
149 /* Step 2. Create or retrieve an indexing context */
150 retval = mapistore_indexing_context_add(mstore_ctx, username, &(mstore_ctx->mapistore_indexing_list));
151 MAPISTORE_RETVAL_IF(retval, retval, NULL);
153 /* Step 3. Ensure the parent_fid URI exists and retrieve its
154 * folder identifier value */
155 retval = mapistore_indexing_get_record_fmid_by_uri(mstore_ctx->mapistore_indexing_list, parent_uri, &parent_fid);
156 if (retval) goto finish;
158 /* Step 4. Ask for a new FID */
159 retval = mapistore_get_new_fmid(mstore_ctx->processing_ctx, username, &fid);
160 if (retval) goto finish;
162 /* Step 5. Register the folder within the indexing database */
163 retval = mapistore_indexing_add_fmid_record(mstore_ctx->mapistore_indexing_list, fid,
164 mapistore_uri, parent_fid,
165 MAPISTORE_INDEXING_FOLDER);
166 if (retval) goto finish;
168 /* Step 6. Request an allocation range for messages */
169 retval = mapistore_get_new_allocation_range(mstore_ctx->processing_ctx,
170 username, range, &rstart, &rend);
171 if (retval) goto finish;
173 /* Step 7. Set the allocation range for the folder */
174 retval = mapistore_indexing_add_folder_record_allocation_range(mstore_ctx->mapistore_indexing_list,
176 if (retval) goto finish;
178 /* Step 8. Delete the indexing context */
180 retval = mapistore_indexing_context_del(mstore_ctx, username);