2 * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <common/defaults.h>
20 #include <common/error.h>
21 #include <common/macros.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <lttng/tracker-internal.h>
28 struct lttng_tracker_id
*lttng_tracker_id_create(void)
30 struct lttng_tracker_id
*id
;
32 id
= zmalloc(sizeof(*id
));
37 id
->type
= LTTNG_ID_UNKNOWN
;
42 lttng_tracker_id_destroy(id
);
46 enum lttng_tracker_id_status
lttng_tracker_id_set_value(
47 struct lttng_tracker_id
*id
, int value
)
52 return LTTNG_TRACKER_ID_STATUS_INVALID
;
55 id
->type
= LTTNG_ID_VALUE
;
57 return LTTNG_TRACKER_ID_STATUS_OK
;
60 enum lttng_tracker_id_status
lttng_tracker_id_set_string(
61 struct lttng_tracker_id
*id
, const char *value
)
66 id
->type
= LTTNG_ID_STRING
;
67 id
->string
= strdup(value
);
68 if (id
->string
== NULL
) {
73 return LTTNG_TRACKER_ID_STATUS_OK
;
75 return LTTNG_TRACKER_ID_STATUS_INVALID
;
78 enum lttng_tracker_id_status
lttng_tracker_id_set_all(
79 struct lttng_tracker_id
*id
)
83 id
->type
= LTTNG_ID_ALL
;
85 return LTTNG_TRACKER_ID_STATUS_OK
;
88 static void lttng_tracker_id_reset(struct lttng_tracker_id
*id
)
94 if (id
->string
!= NULL
) {
99 id
->type
= LTTNG_ID_UNKNOWN
;
103 void lttng_tracker_id_destroy(struct lttng_tracker_id
*id
)
109 lttng_tracker_id_reset(id
);
114 enum lttng_tracker_id_type
lttng_tracker_id_get_type(
115 const struct lttng_tracker_id
*id
)
121 enum lttng_tracker_id_status
lttng_tracker_id_get_value(
122 const struct lttng_tracker_id
*id
, int *value
)
125 if (id
->type
== LTTNG_ID_UNKNOWN
) {
126 return LTTNG_TRACKER_ID_STATUS_UNSET
;
129 if (id
->type
!= LTTNG_ID_VALUE
) {
130 return LTTNG_TRACKER_ID_STATUS_INVALID
;
134 return LTTNG_TRACKER_ID_STATUS_OK
;
137 bool lttng_tracker_id_is_equal(const struct lttng_tracker_id
*left
,
138 const struct lttng_tracker_id
*right
)
140 if (left
->type
!= right
->type
) {
144 switch (left
->type
) {
148 if (left
->value
!= right
->value
) {
152 case LTTNG_ID_STRING
:
153 if (strcmp(left
->string
, right
->string
) != 0) {
159 * Normally this should return true, but comparing unset tracker
167 int lttng_tracker_id_copy(struct lttng_tracker_id
*dest
,
168 const struct lttng_tracker_id
*orig
)
171 enum lttng_tracker_id_status status
;
176 switch (orig
->type
) {
178 status
= lttng_tracker_id_set_all(dest
);
181 status
= lttng_tracker_id_set_value(dest
, orig
->value
);
183 case LTTNG_ID_STRING
:
184 status
= lttng_tracker_id_set_string(dest
, orig
->string
);
187 status
= LTTNG_TRACKER_ID_STATUS_OK
;
191 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
199 struct lttng_tracker_id
*lttng_tracker_id_duplicate(
200 const struct lttng_tracker_id
*orig
)
203 struct lttng_tracker_id
*copy
= NULL
;
205 copy
= lttng_tracker_id_create();
210 ret
= lttng_tracker_id_copy(copy
, orig
);
217 lttng_tracker_id_destroy(copy
);
221 enum lttng_tracker_id_status
lttng_tracker_id_get_string(
222 const struct lttng_tracker_id
*id
, const char **value
)
225 if (id
->type
== LTTNG_ID_UNKNOWN
) {
227 return LTTNG_TRACKER_ID_STATUS_UNSET
;
230 if (id
->type
!= LTTNG_ID_STRING
) {
232 return LTTNG_TRACKER_ID_STATUS_INVALID
;
236 return LTTNG_TRACKER_ID_STATUS_OK
;
239 struct lttng_tracker_ids
*lttng_tracker_ids_create(unsigned int count
)
241 struct lttng_tracker_ids
*ids
= NULL
;
243 ids
= zmalloc(sizeof(*ids
));
248 ids
->id_array
= zmalloc(sizeof(struct lttng_tracker_id
) * count
);
249 if (!ids
->id_array
) {
262 struct lttng_tracker_id
*lttng_tracker_ids_get_pointer_of_index(
263 const struct lttng_tracker_ids
*ids
, unsigned int index
)
266 if (index
>= ids
->count
) {
270 return &ids
->id_array
[index
];
273 const struct lttng_tracker_id
*lttng_tracker_ids_get_at_index(
274 const struct lttng_tracker_ids
*ids
, unsigned int index
)
277 return lttng_tracker_ids_get_pointer_of_index(ids
, index
);
280 int lttng_tracker_ids_get_count(const struct lttng_tracker_ids
*ids
, unsigned int *count
)
283 enum lttng_tracker_id_status status
= LTTNG_ROTATION_STATUS_OK
;
285 if (!ids
|| !count
) {
286 status
= LTTNG_ROTATION_STATUS_INVALID
;
295 void lttng_tracker_ids_destroy(struct lttng_tracker_ids
*ids
)
301 for (int i
= 0; i
< ids
->count
; i
++) {
302 lttng_tracker_id_reset(&ids
->id_array
[i
]);