Code

store: Let sdb_store_attribute() accept const arguments.
[sysdb.git] / src / include / core / store.h
1 /*
2  * SysDB - src/include/core/store.h
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #ifndef SDB_CORE_STORE_H
29 #define SDB_CORE_STORE_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/time.h"
34 #include "utils/llist.h"
36 #include <stdio.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 extern const sdb_type_t sdb_host_type;
43 extern const sdb_type_t sdb_attribute_type;
44 extern const sdb_type_t sdb_service_type;
46 typedef struct {
47         sdb_object_t super;
48         sdb_time_t last_update;
49 } sdb_store_obj_t;
50 #define SDB_STORE_OBJ_INIT(t) { SDB_OBJECT_TYPED_INIT(t), 0 }
51 #define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
52 #define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
54 typedef struct {
55         sdb_store_obj_t super;
57         char *hostname;
58 } sdb_service_t;
59 #define SDB_SVC_INIT { SDB_STORE_OBJ_INIT(sdb_service_type), NULL }
60 #define SDB_SVC(obj) ((sdb_service_t *)(obj))
61 #define SDB_CONST_SVC(obj) ((const sdb_service_t *)(obj))
63 typedef struct {
64         sdb_store_obj_t super;
66         char *attr_value;
67         char *hostname;
68 } sdb_attribute_t;
69 #define SDB_ATTR_INIT { SDB_STORE_OBJ_INIT(sdb_attribute_type), NULL, NULL }
70 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
71 #define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
73 typedef struct {
74         sdb_store_obj_t super;
76         sdb_llist_t *attributes;
77         sdb_llist_t *services;
78 } sdb_host_t;
79 #define SDB_HOST_INIT { SDB_STORE_OBJ_INIT(sdb_host_type), NULL, NULL }
80 #define SDB_HOST(obj) ((sdb_host_t *)(obj))
81 #define SDB_CONST_HOST(obj) ((const sdb_host_t *)(obj))
83 /* shortcuts for accessing the sdb_store_obj_t attributes of inheriting
84  * objects */
85 #define _last_update super.last_update
87 /*
88  * sdb_store_host:
89  * Add/update a host in the store. If the host, identified by its
90  * canonicalized name, already exists, it will be updated according to the
91  * specified name and timestamp. Else, a new entry will be created in the
92  * store. Any memory required for storing the entry will be allocated an
93  * managed by the store itself.
94  *
95  * Returns:
96  *  - 0 on success
97  *  - a positive value if the new entry is older than the currently stored
98  *    entry (in this case, no update will happen)
99  *  - a negative value on error
100  */
101 int
102 sdb_store_host(const char *name, sdb_time_t last_update);
104 _Bool
105 sdb_store_has_host(const char *name);
107 /*
108  * sdb_store_attribute:
109  * Add/update a host's attribute in the store. If the attribute, identified by
110  * its key, already exists for the specified host, it will be updated to the
111  * specified values. If the referenced host does not exist, an error will be
112  * reported. Else, a new entry will be created in the store. Any memory
113  * required for storing the entry will be allocated and managed by the store
114  * itself.
115  *
116  * Returns:
117  *  - 0 on success
118  *  - a positive value if the new entry is older than the currently stored
119  *    entry (in this case, no update will happen)
120  *  - a negative value on error
121  */
122 int
123 sdb_store_attribute(const char *hostname, const char *key, const char *value,
124                 sdb_time_t last_update);
126 /*
127  * sdb_store_service:
128  * Add/update a store in the store. If the service, identified by its name,
129  * already exists for the specified host, it will be updated according to the
130  * specified 'service' object. If the referenced host does not exist, an error
131  * will be reported. Else, a new entry will be created in the store. Any
132  * memory required for storing the entry will be allocated an managed by the
133  * store itself. The specified service-object will not be referenced or
134  * further accessed.
135  *
136  * Returns:
137  *  - 0 on success
138  *  - a positive value if the new entry is older than the currently stored
139  *    entry (in this case, no update will happen)
140  *  - a negative value on error
141  */
142 int
143 sdb_store_service(const sdb_service_t *svc);
145 int
146 sdb_store_dump(FILE *fh);
148 #ifdef __cplusplus
149 } /* extern "C" */
150 #endif
152 #endif /* ! SDB_CORE_STORE_H */
154 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */