2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file blob.c Macros and functions for blob handling. */
18 #include "portable_io.h"
21 * Compare two osl objects pointing to unsigned integers of 32 bit size.
23 * \param obj1 Pointer to the first integer.
24 * \param obj2 Pointer to the second integer.
26 * \return The values required for an osl compare function.
28 * \sa osl_compare_func, osl_hash_compare().
30 static int uint32_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
32 uint32_t d1
= read_u32((const char *)obj1
->data
);
33 uint32_t d2
= read_u32((const char *)obj2
->data
);
42 static struct osl_column_description blob_cols
[] = {
44 .storage_type
= OSL_MAPPED_STORAGE
,
45 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
48 .compare_function
= uint32_compare
51 .storage_type
= OSL_MAPPED_STORAGE
,
52 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
54 .compare_function
= string_compare
57 .storage_type
= OSL_DISK_STORAGE
,
63 /** \cond doxygen isn't smart enough to recognize these */
64 INIT_BLOB_TABLE(lyrics
);
65 INIT_BLOB_TABLE(images
);
66 INIT_BLOB_TABLE(moods
);
67 INIT_BLOB_TABLE(playlists
);
70 /** Flags that may be passed to the \p ls functions of each blob type. */
72 /** List both id and name. */
73 BLOB_LS_FLAG_LONG
= 1,
74 /** Reverse sort order. */
75 BLOB_LS_FLAG_REVERSE
= 2,
76 /** Sort by id instead of name. */
77 BLOB_LS_FLAG_SORT_BY_ID
= 4,
80 /** Structure passed to the \p print_blob function. */
81 struct lsblob_action_data
{
82 /** The flags given at the command line. */
84 /** Message buffer. */
85 struct para_buffer pb
;
88 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
89 const char *name
, void *data
)
91 struct lsblob_action_data
*lbad
= data
;
92 struct osl_object obj
;
96 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
97 return para_printf(&lbad
->pb
, "%s\n", name
);
98 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
100 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
103 id
= *(uint32_t *)obj
.data
;
104 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
107 static void com_lsblob_callback(struct osl_table
*table
,
108 int fd
, const struct osl_object
*query
)
110 struct lsblob_action_data lbad
= {
111 .flags
= *(uint32_t *)query
->data
,
115 .max_size_handler
= pass_buffer_as_shm
118 struct pattern_match_data pmd
= {
120 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
121 .size
= query
->size
- sizeof(uint32_t)},
122 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
123 .match_col_num
= BLOBCOL_NAME
,
125 .action
= print_blob
,
129 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
130 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
131 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
132 pmd
.loop_col_num
= BLOBCOL_NAME
;
134 pmd
.loop_col_num
= BLOBCOL_ID
;
135 ret
= for_each_matching_row(&pmd
);
137 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
139 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
143 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
146 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
149 for (i
= 1; i
< argc
; i
++) {
150 const char *arg
= argv
[i
];
153 if (!strcmp(arg
, "--")) {
157 if (!strcmp(arg
, "-l")) {
158 flags
|= BLOB_LS_FLAG_LONG
;
161 if (!strcmp(arg
, "-i")) {
162 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
165 if (!strcmp(arg
, "-r")) {
166 flags
|= BLOB_LS_FLAG_REVERSE
;
172 // return -E_BLOB_SYNTAX;
173 return send_option_arg_callback_request(&options
, argc
- i
,
174 argv
+ i
, f
, send_result
, &fd
);
177 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
178 __a_unused
const char *name
, void *data
)
181 struct osl_object obj
;
183 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
187 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
188 ret2
= osl(osl_close_disk_object(&obj
));
189 return (ret
< 0)? ret
: ret2
;
192 static void com_catblob_callback(struct osl_table
*table
, int fd
,
193 const struct osl_object
*query
)
195 struct pattern_match_data pmd
= {
198 .loop_col_num
= BLOBCOL_NAME
,
199 .match_col_num
= BLOBCOL_NAME
,
200 .pm_flags
= PM_SKIP_EMPTY_NAME
,
204 for_each_matching_row(&pmd
);
207 static int com_catblob(callback_function
*f
, int fd
, int argc
,
208 char * const * const argv
)
211 return -E_BLOB_SYNTAX
;
212 return send_standard_callback_request(argc
- 1, argv
+ 1, f
, send_result
, &fd
);
215 /** Used for removing rows from a blob table. */
217 /** Message buffer. */
218 struct para_buffer pb
;
219 /** Number of removed blobs. */
220 unsigned num_removed
;
223 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
224 const char *name
, void *data
)
226 struct rmblob_data
*rmbd
= data
;
227 int ret
= osl(osl_del_row(table
, row
));
229 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
236 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
237 const struct osl_object
*query
)
240 struct rmblob_data rmbd
= {
245 .max_size_handler
= pass_buffer_as_shm
248 struct pattern_match_data pmd
= {
251 .loop_col_num
= BLOBCOL_NAME
,
252 .match_col_num
= BLOBCOL_NAME
,
253 .pm_flags
= PM_SKIP_EMPTY_NAME
,
255 .action
= remove_blob
257 ret
= for_each_matching_row(&pmd
);
259 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
263 if (!rmbd
.num_removed
)
264 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
266 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
267 afs_event(BLOB_RENAME
, NULL
, table
);
270 if (ret2
>= 0 && rmbd
.pb
.offset
)
271 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
275 static int com_rmblob(callback_function
*f
, int fd
, int argc
,
276 char * const * const argv
)
279 return -E_MOOD_SYNTAX
;
280 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
284 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
285 const struct osl_object
*query
)
287 struct osl_object objs
[NUM_BLOB_COLUMNS
];
288 char *name
= query
->data
;
289 size_t name_len
= strlen(name
) + 1;
294 ret
= osl(osl_get_num_rows(table
, &num_rows
));
297 if (!num_rows
) { /* this is the first entry ever added */
298 /* insert dummy row containing the id */
299 id
= 2; /* this entry will be entry #1, so 2 is the next */
300 objs
[BLOBCOL_ID
].data
= &id
;
301 objs
[BLOBCOL_ID
].size
= sizeof(id
);
302 objs
[BLOBCOL_NAME
].data
= "";
303 objs
[BLOBCOL_NAME
].size
= 1;
304 objs
[BLOBCOL_DEF
].data
= "";
305 objs
[BLOBCOL_DEF
].size
= 1;
306 ret
= osl(osl_add_row(table
, objs
));
310 /* check if name already exists */
312 struct osl_object obj
= {.data
= name
, .size
= name_len
};
313 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
314 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
316 if (ret
>= 0) { /* we already have a blob with this name */
317 obj
.data
= name
+ name_len
;
318 obj
.size
= query
->size
- name_len
;
319 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
322 /* new blob, get id of the dummy row and increment it */
325 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
328 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
331 id
= *(uint32_t *)obj
.data
+ 1;
333 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
338 objs
[BLOBCOL_ID
].data
= &id
;
339 objs
[BLOBCOL_ID
].size
= sizeof(id
);
340 objs
[BLOBCOL_NAME
].data
= name
;
341 objs
[BLOBCOL_NAME
].size
= name_len
;
342 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
343 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
344 ret
= osl(osl_add_row(table
, objs
));
347 afs_event(BLOB_ADD
, NULL
, table
);
350 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
353 static int com_addblob(callback_function
*f
, int fd
, int argc
,
354 char * const * const argv
)
356 struct osl_object arg_obj
;
359 return -E_BLOB_SYNTAX
;
360 if (!*argv
[1]) /* empty name is reserved for the dummy row */
361 return -E_BLOB_SYNTAX
;
362 arg_obj
.size
= strlen(argv
[1]) + 1;
363 arg_obj
.data
= (char *)argv
[1];
364 return stdin_command(fd
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
367 /* FIXME: Print output to client, not to log file */
368 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
369 const struct osl_object
*query
)
371 char *src
= (char *) query
->data
;
372 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
373 char *dest
= src
+ obj
.size
;
375 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
380 obj
.size
= strlen(dest
) + 1;
381 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
384 afs_event(BLOB_RENAME
, NULL
, table
);
387 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
390 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
391 int argc
, char * const * const argv
)
394 return -E_MOOD_SYNTAX
;
395 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
399 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
400 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
402 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
404 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
406 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
409 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
413 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
419 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
422 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
425 *name
= (char *)obj
.data
;
429 /** Define the \p get_name_by_id function for this blob type. */
430 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
431 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
433 return blob_get_name_by_id(table_name ## _table, id, name); \
437 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
438 struct osl_object
*def
)
441 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
447 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
450 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
453 /** Define the \p get_def_by_id function for this blob type. */
454 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
455 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
457 return blob_get_def_by_name(table_name ## _table, name, def); \
460 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
461 struct osl_object
*def
)
464 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
470 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
473 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
476 /** Define the \p get_def_by_id function for this blob type. */
477 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
478 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
480 return blob_get_def_by_id(table_name ## _table, id, def); \
483 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
484 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
486 struct osl_object obj
;
487 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
491 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
493 /** Define the \p get_name_and_def_by_row function for this blob type. */
494 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
495 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
496 char **name, struct osl_object *def) \
498 return blob_get_name_and_def_by_row(table_name ## _table, \
502 /** Define the \p close function for this blob type. */
503 #define DEFINE_BLOB_CLOSE(table_name) \
504 void table_name ## _close(void) \
506 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
507 table_name ## _table = NULL; \
510 /** Define the \p create function for this blob type. */
511 #define DEFINE_BLOB_CREATE(table_name) \
512 int table_name ## _create(const char *dir) \
514 table_name ## _table_desc.dir = dir; \
515 return osl_create_table(&table_name ## _table_desc); \
518 static int blob_open(struct osl_table
**table
,
519 struct osl_table_description
*desc
,
524 ret
= osl(osl_open_table(desc
, table
));
528 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
533 #define DEFINE_BLOB_OPEN(table_name) \
534 int table_name ## _open(const char *dir) \
536 return blob_open(&table_name ## _table, \
537 &table_name ## _table_desc, dir); \
541 /** Define the \p init function for this blob type. */
542 #define DEFINE_BLOB_INIT(table_name) \
543 void table_name ## _init(struct afs_table *t) \
545 t->name = table_name ## _table_desc.name; \
546 t->open = table_name ## _open; \
547 t->close = table_name ## _close; \
548 t->create = table_name ## _create;\
549 t->event_handler = table_name ##_event_handler; \
550 table_name ## _table = NULL; \
554 /** Define all functions for this blob type. */
555 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
556 DEFINE_BLOB_OPEN(table_name) \
557 DEFINE_BLOB_CLOSE(table_name) \
558 DEFINE_BLOB_CREATE(table_name) \
559 DEFINE_BLOB_INIT(table_name) \
560 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
561 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
562 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
563 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
564 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
565 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
566 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
567 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
568 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
570 /** \cond doxygen isn't smart enough to recognize these */
571 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
572 DEFINE_BLOB_FUNCTIONS(images
, img
);
573 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
574 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);