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 static struct osl_column_description blob_cols
[] = {
20 .storage_type
= OSL_MAPPED_STORAGE
,
21 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
24 .compare_function
= uint32_compare
27 .storage_type
= OSL_MAPPED_STORAGE
,
28 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
30 .compare_function
= string_compare
33 .storage_type
= OSL_DISK_STORAGE
,
39 /** \cond doxygen isn't smart enough to recognize these */
40 INIT_BLOB_TABLE(lyrics
);
41 INIT_BLOB_TABLE(images
);
42 INIT_BLOB_TABLE(moods
);
43 INIT_BLOB_TABLE(playlists
);
46 /** Flags that may be passed to the \p ls functions of each blob type. */
48 /** List both id and name. */
49 BLOB_LS_FLAG_LONG
= 1,
50 /** Reverse sort order. */
51 BLOB_LS_FLAG_REVERSE
= 2,
52 /** Sort by id instead of name. */
53 BLOB_LS_FLAG_SORT_BY_ID
= 4,
56 /** Structure passed to the \p print_blob function. */
57 struct lsblob_action_data
{
58 /** The flags given at the command line. */
60 /** Message buffer. */
61 struct para_buffer pb
;
64 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
65 const char *name
, void *data
)
67 struct lsblob_action_data
*lbad
= data
;
68 struct osl_object obj
;
72 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
73 return para_printf(&lbad
->pb
, "%s\n", name
);
74 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
76 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
79 id
= *(uint32_t *)obj
.data
;
80 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
83 static void com_lsblob_callback(struct osl_table
*table
,
84 int fd
, const struct osl_object
*query
)
86 struct lsblob_action_data lbad
= {
87 .flags
= *(uint32_t *)query
->data
,
91 .max_size_handler
= pass_buffer_as_shm
94 struct pattern_match_data pmd
= {
96 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
97 .size
= query
->size
- sizeof(uint32_t)},
98 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
99 .match_col_num
= BLOBCOL_NAME
,
101 .action
= print_blob
,
105 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
106 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
107 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
108 pmd
.loop_col_num
= BLOBCOL_NAME
;
110 pmd
.loop_col_num
= BLOBCOL_ID
;
111 ret
= for_each_matching_row(&pmd
);
113 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
115 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
119 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
122 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
125 for (i
= 1; i
< argc
; i
++) {
126 const char *arg
= argv
[i
];
129 if (!strcmp(arg
, "--")) {
133 if (!strcmp(arg
, "-l")) {
134 flags
|= BLOB_LS_FLAG_LONG
;
137 if (!strcmp(arg
, "-i")) {
138 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
141 if (!strcmp(arg
, "-r")) {
142 flags
|= BLOB_LS_FLAG_REVERSE
;
148 // return -E_BLOB_SYNTAX;
149 return send_option_arg_callback_request(&options
, argc
- i
,
150 argv
+ i
, f
, send_result
, &fd
);
153 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
154 __a_unused
const char *name
, void *data
)
157 struct osl_object obj
;
159 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
163 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
164 ret2
= osl_close_disk_object(&obj
);
165 return (ret
< 0)? ret
: ret2
;
168 static void com_catblob_callback(struct osl_table
*table
, int fd
,
169 const struct osl_object
*query
)
171 struct pattern_match_data pmd
= {
174 .loop_col_num
= BLOBCOL_NAME
,
175 .match_col_num
= BLOBCOL_NAME
,
176 .pm_flags
= PM_SKIP_EMPTY_NAME
,
180 for_each_matching_row(&pmd
);
183 static int com_catblob(callback_function
*f
, int fd
, int argc
,
184 char * const * const argv
)
187 return -E_BLOB_SYNTAX
;
188 return send_standard_callback_request(argc
- 1, argv
+ 1, f
, send_result
, &fd
);
191 /** Used for removing rows from a blob table. */
193 /** Message buffer. */
194 struct para_buffer pb
;
195 /** Number of removed blobs. */
196 unsigned num_removed
;
199 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
200 const char *name
, void *data
)
202 struct rmblob_data
*rmbd
= data
;
203 int ret
= osl_del_row(table
, row
);
205 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
212 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
213 const struct osl_object
*query
)
216 struct rmblob_data rmbd
= {
221 .max_size_handler
= pass_buffer_as_shm
224 struct pattern_match_data pmd
= {
227 .loop_col_num
= BLOBCOL_NAME
,
228 .match_col_num
= BLOBCOL_NAME
,
229 .pm_flags
= PM_SKIP_EMPTY_NAME
,
231 .action
= remove_blob
233 ret
= for_each_matching_row(&pmd
);
235 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
239 if (!rmbd
.num_removed
)
240 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
242 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
243 afs_event(BLOB_RENAME
, NULL
, table
);
246 if (ret2
>= 0 && rmbd
.pb
.offset
)
247 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
251 static int com_rmblob(callback_function
*f
, int fd
, int argc
,
252 char * const * const argv
)
255 return -E_MOOD_SYNTAX
;
256 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
260 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
261 const struct osl_object
*query
)
263 struct osl_object objs
[NUM_BLOB_COLUMNS
];
264 char *name
= query
->data
;
265 size_t name_len
= strlen(name
) + 1;
270 ret
= osl_get_num_rows(table
, &num_rows
);
273 if (!num_rows
) { /* this is the first entry ever added */
274 /* insert dummy row containing the id */
275 id
= 2; /* this entry will be entry #1, so 2 is the next */
276 objs
[BLOBCOL_ID
].data
= &id
;
277 objs
[BLOBCOL_ID
].size
= sizeof(id
);
278 objs
[BLOBCOL_NAME
].data
= "";
279 objs
[BLOBCOL_NAME
].size
= 1;
280 objs
[BLOBCOL_DEF
].data
= "";
281 objs
[BLOBCOL_DEF
].size
= 1;
282 ret
= osl_add_row(table
, objs
);
286 /* check if name already exists */
288 struct osl_object obj
= {.data
= name
, .size
= name_len
};
289 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
290 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
292 if (ret
>= 0) { /* we already have a blob with this name */
293 obj
.data
= name
+ name_len
;
294 obj
.size
= query
->size
- name_len
;
295 ret
= osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
);
298 /* new blob, get id of the dummy row and increment it */
301 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
304 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
307 id
= *(uint32_t *)obj
.data
+ 1;
309 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
314 objs
[BLOBCOL_ID
].data
= &id
;
315 objs
[BLOBCOL_ID
].size
= sizeof(id
);
316 objs
[BLOBCOL_NAME
].data
= name
;
317 objs
[BLOBCOL_NAME
].size
= name_len
;
318 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
319 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
320 ret
= osl_add_row(table
, objs
);
323 afs_event(BLOB_ADD
, NULL
, table
);
326 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
329 static int com_addblob(callback_function
*f
, int fd
, int argc
,
330 char * const * const argv
)
332 struct osl_object arg_obj
;
335 return -E_BLOB_SYNTAX
;
336 if (!*argv
[1]) /* empty name is reserved for the dummy row */
337 return -E_BLOB_SYNTAX
;
338 arg_obj
.size
= strlen(argv
[1]) + 1;
339 arg_obj
.data
= (char *)argv
[1];
340 return stdin_command(fd
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
343 /* FIXME: Print output to client, not to log file */
344 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
345 const struct osl_object
*query
)
347 char *src
= (char *) query
->data
;
348 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
349 char *dest
= src
+ obj
.size
;
351 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
356 obj
.size
= strlen(dest
) + 1;
357 ret
= osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
360 afs_event(BLOB_RENAME
, NULL
, table
);
363 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
366 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
367 int argc
, char * const * const argv
)
370 return -E_MOOD_SYNTAX
;
371 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
375 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
376 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
378 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
380 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
382 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
385 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
389 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
395 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
398 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
401 *name
= (char *)obj
.data
;
405 /** Define the \p get_name_by_id function for this blob type. */
406 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
407 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
409 return blob_get_name_by_id(table_name ## _table, id, name); \
413 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
414 struct osl_object
*def
)
417 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
423 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
426 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
429 /** Define the \p get_def_by_id function for this blob type. */
430 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
431 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
433 return blob_get_def_by_name(table_name ## _table, name, def); \
436 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
437 struct osl_object
*def
)
440 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
446 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
449 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
452 /** Define the \p get_def_by_id function for this blob type. */
453 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
454 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
456 return blob_get_def_by_id(table_name ## _table, id, def); \
459 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
460 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
462 struct osl_object obj
;
463 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
467 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
469 /** Define the \p get_name_and_def_by_row function for this blob type. */
470 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
471 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
472 char **name, struct osl_object *def) \
474 return blob_get_name_and_def_by_row(table_name ## _table, \
478 /** Define the \p close function for this blob type. */
479 #define DEFINE_BLOB_CLOSE(table_name) \
480 void table_name ## _close(void) \
482 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
483 table_name ## _table = NULL; \
486 /** Define the \p create function for this blob type. */
487 #define DEFINE_BLOB_CREATE(table_name) \
488 int table_name ## _create(const char *dir) \
490 table_name ## _table_desc.dir = dir; \
491 return osl_create_table(&table_name ## _table_desc); \
494 static int blob_open(struct osl_table
**table
,
495 struct osl_table_description
*desc
,
500 ret
= osl_open_table(desc
, table
);
504 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
509 #define DEFINE_BLOB_OPEN(table_name) \
510 int table_name ## _open(const char *dir) \
512 return blob_open(&table_name ## _table, \
513 &table_name ## _table_desc, dir); \
517 /** Define the \p init function for this blob type. */
518 #define DEFINE_BLOB_INIT(table_name) \
519 void table_name ## _init(struct afs_table *t) \
521 t->name = table_name ## _table_desc.name; \
522 t->open = table_name ## _open; \
523 t->close = table_name ## _close; \
524 t->create = table_name ## _create;\
525 t->event_handler = table_name ##_event_handler; \
526 table_name ## _table = NULL; \
530 /** Define all functions for this blob type. */
531 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
532 DEFINE_BLOB_OPEN(table_name) \
533 DEFINE_BLOB_CLOSE(table_name) \
534 DEFINE_BLOB_CREATE(table_name) \
535 DEFINE_BLOB_INIT(table_name) \
536 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
537 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
538 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
539 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
540 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
541 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
542 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
543 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
544 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
546 /** \cond doxygen isn't smart enough to recognize these */
547 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
548 DEFINE_BLOB_FUNCTIONS(images
, img
);
549 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
550 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);