libamxd  6.4.1
Data Model Manager
amxd_dm_get_function.c File Reference
#include <string.h>
#include <stdlib.h>
#include "amxd_priv.h"
#include <amxd/amxd_dm.h>
#include <amxd/amxd_object.h>
#include <amxd/amxd_path.h>
#include <amxd/amxd_dm_functions.h>
#include <amxd/amxd_transaction.h>
#include "amxd_assert.h"
#include "amxd_object_priv.h"
#include "amxd_dm_priv.h"

Go to the source code of this file.

Data Structures

struct  _amxd_get
 

Typedefs

typedef struct _amxd_get amxd_get_t
 

Functions

static bool amxd_get_filter (amxd_object_t *const object, UNUSED int32_t depth, void *priv)
 
static void amxd_get_subobject (amxd_object_t *const object, UNUSED int32_t depth, void *priv)
 
static amxd_status_t amxd_get_fill_response (amxd_dm_t *dm, bool key_path, bool template_only, amxc_llist_t *paths, amxc_var_t *args, amxc_var_t *ret)
 
amxd_status_t amxd_object_func_get (amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
 
amxd_status_t amxd_object_func_get_instances (amxd_object_t *object, UNUSED amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)
 

Typedef Documentation

◆ amxd_get_t

typedef struct _amxd_get amxd_get_t

Function Documentation

◆ amxd_get_fill_response()

static amxd_status_t amxd_get_fill_response ( amxd_dm_t dm,
bool  key_path,
bool  template_only,
amxc_llist_t *  paths,
amxc_var_t *  args,
amxc_var_t *  ret 
)
static

Definition at line 135 of file amxd_dm_get_function.c.

140  {
141  int32_t depth = amxc_var_dyncast(int32_t, GET_ARG(args, "depth"));
142  amxc_var_t* params = GET_ARG(args, "parameters");
143  bool has_params = !amxc_llist_is_empty(amxc_var_constcast(amxc_llist_t, params));
144  amxd_dm_access_t access = (amxd_dm_access_t) GET_UINT32(args, "access");
145 
146  amxd_get_t data = {
147  .response = ret,
148  .args = args,
149  .key_path = key_path,
150  .template_only = template_only,
151  .include_templates = GET_BOOL(args, "templates"),
152  .status = amxd_status_ok,
153  .has_params = has_params
154  };
155 
156  if(depth < 0) {
157  depth = INT32_MAX;
158  }
159 
160  amxc_llist_for_each(it, paths) {
161  amxd_object_t* object = NULL;
162  amxc_string_t* real_path = amxc_string_from_llist_it(it);
163  const char* str_rp = NULL;
164 
165  str_rp = amxc_string_get(real_path, 0);
166  object = amxd_dm_findf(dm, "%s", str_rp);
167 
168  if(template_only && (amxd_object_get_type(object) != amxd_object_template) &&
169  !data.include_templates) {
171  break;
172  }
173 
174  if(data.include_templates) {
175  // go up in hierarchy until highest template object is found
176  amxd_object_t* highest = object;
177  amxd_object_t* parent = amxd_object_get_parent(object);
178  while(parent != NULL) {
180  highest = parent;
181  }
182  parent = amxd_object_get_parent(parent);
183  }
184  object = highest;
185  }
186 
187  if(!amxd_action_verify_access(object, access)) {
189  break;
190  }
191 
192  if((amxd_object_get_type(object) == amxd_object_template)) {
193  if(has_params) {
195  break;
196  }
197  if(depth == 0) {
198  depth = 1;
199  }
200  }
201 
206  depth,
207  &data);
208  }
209 
210  return data.status;
211 }
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
static void amxd_get_subobject(amxd_object_t *const object, UNUSED int32_t depth, void *priv)
static bool amxd_get_filter(amxd_object_t *const object, UNUSED int32_t depth, void *priv)
bool amxd_action_verify_access(amxd_object_t *object, amxd_dm_access_t access)
@ amxd_status_parameter_not_found
Definition: amxd_types.h:82
@ amxd_status_invalid_path
Definition: amxd_types.h:99
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_direction_down
Definition: amxd_types.h:216
enum _amxd_dm_access amxd_dm_access_t
Access level.
@ amxd_object_template
Definition: amxd_types.h:183
amxd_object_t * amxd_object_get_parent(const amxd_object_t *const object)
Get the parent object.
void amxd_object_hierarchy_walk(amxd_object_t *const object, const amxd_direction_t direction, amxd_object_filter_fn_t filter, amxd_object_cb_fn_t cb, int32_t depth, void *priv)
Iterates over all objects in the data model tree.
static amxd_object_type_t amxd_object_get_type(const amxd_object_t *const object)
Returns the object type.
Definition: amxd_object.h:586
amxd_status_t status
amxc_var_t * response
static amxd_dm_t dm

◆ amxd_get_filter()

static bool amxd_get_filter ( amxd_object_t *const  object,
UNUSED int32_t  depth,
void *  priv 
)
static

Definition at line 80 of file amxd_dm_get_function.c.

82  {
83  amxd_object_t* parent = NULL;
84  amxd_get_t* data = (amxd_get_t*) priv;
85 
87  return true;
88  }
89  parent = amxd_object_get_parent(object);
91  !data->include_templates) {
92  return false;
93  }
94  return true;
95 }
@ amxd_object_instance
Definition: amxd_types.h:186

◆ amxd_get_subobject()

static void amxd_get_subobject ( amxd_object_t *const  object,
UNUSED int32_t  depth,
void *  priv 
)
static

Definition at line 97 of file amxd_dm_get_function.c.

99  {
100  amxd_get_t* data = (amxd_get_t*) priv;
101  amxc_var_t* sub_obj = NULL;
102  char* path = NULL;
103  amxd_dm_access_t access = (amxd_dm_access_t) GET_UINT32(data->args, "access");
104  uint32_t flags = data->key_path ? AMXD_OBJECT_NAMED : AMXD_OBJECT_INDEXED;
105 
106  when_true(amxd_object_get_type(object) == amxd_object_template &&
107  access == amxd_dm_access_public, exit);
108  when_true(data->template_only && amxd_object_get_type(object) != amxd_object_instance, exit);
109  when_false(amxd_action_can_access_object(amxd_object_get_attrs(object), access), exit);
110  path = amxd_object_get_path(object, AMXD_OBJECT_TERMINATE | flags);
111  sub_obj = amxc_var_add_key(amxc_htable_t, data->response, path, NULL);
113  if(data->has_params) {
114  amxc_var_delete(&sub_obj);
115  }
116  when_false(data->include_templates, exit);
117  }
118 
119  data->status = amxd_dm_invoke_action(object, NULL, action_object_read, data->args, sub_obj);
120  if(data->status != amxd_status_ok) {
121  amxc_var_delete(&sub_obj);
122  if(data->status == amxd_status_object_not_found) {
123  // when not found an empty set can be returned.
124  // it is also possible that the object is not accessible (protected/private)
125  // and there for should not be added to the result list, but it should
126  // not result in an error.
127  data->status = amxd_status_ok;
128  }
129  }
130 
131 exit:
132  free(path);
133 }
amxd_status_t amxd_dm_invoke_action(amxd_object_t *object, amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval)
Definition: amxd_dm.c:591
bool amxd_action_can_access_object(uint32_t object_attrs, amxd_dm_access_t access)
@ action_object_read
Definition: amxd_types.h:117
@ amxd_status_object_not_found
Definition: amxd_types.h:80
#define AMXD_OBJECT_INDEXED
Name and path format flag - use index for instance objects.
Definition: amxd_object.h:176
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
#define AMXD_OBJECT_TERMINATE
Path format flag - when set the object path is terminated with a dot.
Definition: amxd_object.h:214
@ amxd_dm_access_public
Definition: amxd_types.h:136
amxd_object_t amxd_status_t amxd_status_t char * amxd_object_get_path(const amxd_object_t *object, const uint32_t flags)
Get the full path of the object.
uint32_t amxd_object_get_attrs(const amxd_object_t *const object)
Gets the set attributes of an object.
Definition: amxd_object.c:334
amxc_var_t * args

◆ amxd_object_func_get()

amxd_status_t amxd_object_func_get ( amxd_object_t object,
UNUSED amxd_function_t func,
amxc_var_t *  args,
amxc_var_t *  ret 
)

Definition at line 213 of file amxd_dm_get_function.c.

216  {
218  amxd_dm_t* dm = amxd_object_get_dm(object);
219  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
220  const char* rel_path = GET_CHAR(var_rel_path, NULL);
221  amxc_var_t* params = GET_ARG(args, "parameters");
222  amxd_path_t path;
223  const char* object_path = NULL;
224  const char* parameter = NULL;
225  amxc_llist_t paths;
226 
227  amxc_llist_init(&paths);
229 
230  retval = amxd_path_init(&path, rel_path);
231  when_failed(retval, exit);
232 
233  parameter = amxd_path_get_param(&path);
234  object_path = amxd_path_get(&path, AMXD_OBJECT_TERMINATE);
235  if((parameter != NULL) && (parameter[0] != 0)) {
236  if(params == NULL) {
237  params = amxc_var_add_key(amxc_llist_t, args, "parameters", NULL);
238  }
239  amxc_var_add(cstring_t, params, parameter);
240  }
241 
242  amxc_var_set_type(ret, AMXC_VAR_ID_HTABLE);
243  retval = amxd_object_resolve_pathf(object, &paths, "%s", object_path);
244  when_failed(retval, exit);
245  amxc_var_take_it(var_rel_path);
246  retval = amxd_get_fill_response(dm, false, false, &paths, args, ret);
247 
248  if(retval == amxd_status_ok) {
249  const amxc_htable_t* hresult = amxc_var_constcast(amxc_htable_t, ret);
250  if(amxc_htable_is_empty(hresult)) {
251  if(!amxd_path_is_search_path(&path)) {
252  object = amxd_object_findf(object, "%s", object_path);
253  if((object == NULL) || (amxd_object_get_type(object) != amxd_object_template)) {
254  retval = amxd_status_invalid_path;
255  }
256  } else {
257  if(!amxd_object_is_supported(object, rel_path)) {
258  retval = amxd_status_invalid_path;
259  }
260  }
261  }
262  } else {
263  if(retval == amxd_status_parameter_not_found) {
264  const amxc_htable_t* hresult = amxc_var_constcast(amxc_htable_t, ret);
265  if((hresult != NULL) && !amxc_htable_is_empty(hresult)) {
266  retval = amxd_status_ok;
267  }
268  }
269  }
270 
271 exit:
272  if(retval != amxd_status_ok) {
273  amxc_var_clean(ret);
274  }
275  amxc_var_delete(&var_rel_path);
276  amxd_path_clean(&path);
277  amxc_llist_clean(&paths, amxc_string_list_it_free);
278  return retval;
279 }
static amxd_status_t amxd_get_fill_response(amxd_dm_t *dm, bool key_path, bool template_only, amxc_llist_t *paths, amxc_var_t *args, amxc_var_t *ret)
void PRIVATE amxd_def_funcs_remove_args(amxc_var_t *args)
enum _amxd_status amxd_status_t
@ amxd_status_unknown_error
Definition: amxd_types.h:79
amxd_object_t * amxd_object_findf(amxd_object_t *object, const char *rel_path,...) __attribute__((format(printf
Find an object in the data model tree, starting from an object.
amxd_dm_t * amxd_object_get_dm(const amxd_object_t *const object)
Get the data model.
amxd_object_t amxd_status_t amxd_object_resolve_pathf(amxd_object_t *object, amxc_llist_t *paths, const char *rel_path,...) __attribute__((format(printf
Resolves a search path into a list of matching object paths.
bool amxd_object_is_supported(amxd_object_t *object, const char *rel_path)
Checks if a path is in the supported data model.
amxd_status_t amxd_path_init(amxd_path_t *path, const char *object_path)
Initializes an amxd_path_t structure.
Definition: amxd_path.c:328
const char * amxd_path_get_param(amxd_path_t *path)
Gets the parameter name.
Definition: amxd_path.c:497
static bool amxd_path_is_search_path(amxd_path_t *path)
Checks if the path is a search path.
Definition: amxd_path.h:544
const char * amxd_path_get(amxd_path_t *path, int flags)
Returns the path stored in the amxd_path_t structure.
Definition: amxd_path.c:470
void amxd_path_clean(amxd_path_t *path)
Cleans an amxd_path_t structure.
Definition: amxd_path.c:351

◆ amxd_object_func_get_instances()

amxd_status_t amxd_object_func_get_instances ( amxd_object_t object,
UNUSED amxd_function_t func,
amxc_var_t *  args,
amxc_var_t *  ret 
)

Definition at line 281 of file amxd_dm_get_function.c.

284  {
286  amxd_dm_t* dm = amxd_object_get_dm(object);
287  amxc_var_t* var_rel_path = GET_ARG(args, "rel_path");
288  const char* rel_path = GET_CHAR(var_rel_path, NULL);
289  amxd_path_t path;
290  const char* object_path = NULL;
291  amxc_llist_t paths;
292  amxc_var_t* attrs = NULL;
293 
294  amxc_llist_init(&paths);
296 
297  retval = amxd_path_init(&path, rel_path);
298  when_failed(retval, exit);
299  if(!amxd_object_is_supported(object, rel_path)) {
300  retval = amxd_status_invalid_path;
301  goto exit;
302  }
303 
304  if(amxd_path_get_param(&path) != NULL) {
305  retval = amxd_status_invalid_path;
306  goto exit;
307  }
308 
309  object_path = amxd_path_get(&path, AMXD_OBJECT_TERMINATE);
310 
311  amxc_var_set_type(ret, AMXC_VAR_ID_HTABLE);
312  retval = amxd_object_resolve_pathf(object, &paths, "%s", object_path);
313  when_failed(retval, exit);
314  attrs = amxc_var_add_key(uint32_t, args, "attributes", SET_BIT(amxd_pattr_key));
315  amxc_var_take_it(var_rel_path);
316  retval = amxd_get_fill_response(dm, false, true, &paths, args, ret);
317 
318 exit:
319  if(retval != amxd_status_ok) {
320  amxc_var_clean(ret);
321  }
322  amxc_var_delete(&attrs);
323  amxc_var_delete(&var_rel_path);
324  amxd_path_clean(&path);
325  amxc_llist_clean(&paths, amxc_string_list_it_free);
326  return retval;
327 }
#define SET_BIT(x)
Definition: amxd_common.h:65
@ amxd_pattr_key
Definition: amxd_types.h:362