Add a new OCPF error OCPF_E_EXIST returned if a context already exist.
authorJulien Kerihuel <j.kerihuel@openchange.org>
Thu, 3 Jun 2010 22:44:16 +0000 (22:44 +0000)
committerJulien Kerihuel <j.kerihuel@openchange.org>
Thu, 3 Jun 2010 22:44:16 +0000 (22:44 +0000)
Prevent ocpf_new_context from adding a context to the list if it
already existed

libocpf/ocpf.h
libocpf/ocpf_context.c
libocpf/ocpf_public.c

index af6b6ba43ee0c8ac5d6c404906c40b8bf797c6f9..52ce667896462c4f75e519ea7f0a9417cc91c22e 100644 (file)
@@ -25,6 +25,7 @@
 
 #define        OCPF_SUCCESS    0x0
 #define        OCPF_ERROR      0x1
+#define        OCPF_E_EXIST    0x2
 
 #define        OCPF_FLAGS_RDWR                 0
 #define        OCPF_FLAGS_READ                 1
@@ -32,6 +33,5 @@
 #define        OCPF_FLAGS_CREATE               3
 
 extern struct ocpf     *ocpf;
-/* extern unsigned int lineno; */
 
 #endif /* ! __OCPF_H_ */
index b549a259b2ecfa677e6f0d6ed1126c5e52078f48..0d7e810cbc1b576cca2cea9332c02760732a99de 100644 (file)
@@ -114,13 +114,16 @@ struct ocpf_context *ocpf_context_init(TALLOC_CTX *mem_ctx,
    \param filename pointer to the 
    \param context_id pointer to the context_id the function returns
    \param flags Flags controlling how the OCPF should be opened
+   \param existing boolean returned by the function to specify if the
+   context was already existing or not
 
    \return valid ocpf context pointer on success, otherwise NULL
  */
 struct ocpf_context *ocpf_context_add(struct ocpf *ocpf_ctx, 
                                      const char *filename,
                                      uint32_t *context_id,
-                                     uint8_t flags)
+                                     uint8_t flags,
+                                     bool *existing)
 {
        struct ocpf_context     *el;
        struct ocpf_freeid      *elf;
@@ -136,6 +139,7 @@ struct ocpf_context *ocpf_context_add(struct ocpf *ocpf_ctx,
                if (el->filename && !strcmp(el->filename, filename)) {
                        *context_id = el->context_id;
                        el->ref_count += 1;
+                       *existing = true;
                        return el;
                }
        }
@@ -158,6 +162,7 @@ struct ocpf_context *ocpf_context_add(struct ocpf *ocpf_ctx,
        }
 
        /* Initialize the new context */
+       *existing = false;
        el = ocpf_context_init(ocpf_ctx->mem_ctx, filename, flags, *context_id);
        /* handle the case where file couldn't be opened */
 
index 94eb14e86cc97d4480903f37e20377641a6c23d0..26181d2a8ed2517b8b2606aa17b64afb878fb33c 100644 (file)
@@ -98,17 +98,21 @@ _PUBLIC_ int ocpf_release(void)
 _PUBLIC_ int ocpf_new_context(const char *filename, uint32_t *context_id, uint8_t flags)
 {
        struct ocpf_context     *ctx;
+       bool                    existing = false;
 
        OCPF_RETVAL_IF(!ocpf || !ocpf->mem_ctx, NULL, OCPF_NOT_INITIALIZED, NULL);
 
-       ctx = ocpf_context_add(ocpf, filename, context_id, flags);
+       ctx = ocpf_context_add(ocpf, filename, context_id, flags, &existing);
        if (!ctx) {
                return OCPF_ERROR;
        }
 
-       DLIST_ADD_END(ocpf->context, ctx, struct ocpf_context *);
+       if (existing == false) {
+               DLIST_ADD_END(ocpf->context, ctx, struct ocpf_context *);
+               return OCPF_SUCCESS;
+       } 
 
-       return OCPF_SUCCESS;
+       return OCPF_E_EXIST;
 }