2 * Copyright (C) 2007-2008 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. */
17 static struct osl_column_description blob_cols
[] = {
19 .storage_type
= OSL_MAPPED_STORAGE
,
20 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
23 .compare_function
= uint32_compare
26 .storage_type
= OSL_MAPPED_STORAGE
,
27 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
29 .compare_function
= string_compare
32 .storage_type
= OSL_DISK_STORAGE
,
38 /** \cond doxygen isn't smart enough to recognize these */
39 INIT_BLOB_TABLE(lyrics
);
40 INIT_BLOB_TABLE(images
);
41 INIT_BLOB_TABLE(moods
);
42 INIT_BLOB_TABLE(playlists
);
45 /** Flags that may be passed to the \p ls functions of each blob type. */
47 /** List both id and name. */
48 BLOB_LS_FLAG_LONG
= 1,
49 /** Reverse sort order. */
50 BLOB_LS_FLAG_REVERSE
= 2,
51 /** Sort by id instead of name. */
52 BLOB_LS_FLAG_SORT_BY_ID
= 4,
55 /** Structure passed to the \p print_blob function. */
56 struct lsblob_action_data
{
57 /* The flags given at the command line. */
59 /** Message buffer. */
60 struct para_buffer pb
;
63 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
64 const char *name
, void *data
)
66 struct lsblob_action_data
*lbad
= data
;
67 struct osl_object obj
;
71 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
)) {
72 para_printf(&lbad
->pb
, "%s\n", name
);
75 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
77 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
80 id
= *(uint32_t *)obj
.data
;
81 para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
85 static int com_lsblob_callback(struct osl_table
*table
,
86 const struct osl_object
*query
, struct osl_object
*result
)
88 struct lsblob_action_data lbad
= {.flags
= *(uint32_t *)query
->data
};
89 struct pattern_match_data pmd
= {
91 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
92 .size
= query
->size
- sizeof(uint32_t)},
93 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
94 .match_col_num
= BLOBCOL_NAME
,
100 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
101 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
102 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
103 pmd
.loop_col_num
= BLOBCOL_NAME
;
105 pmd
.loop_col_num
= BLOBCOL_ID
;
106 ret
= for_each_matching_row(&pmd
);
108 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
111 result
->data
= lbad
.pb
.buf
;
112 result
->size
= lbad
.pb
.offset
;
116 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
119 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
122 for (i
= 1; i
< argc
; i
++) {
123 const char *arg
= argv
[i
];
126 if (!strcmp(arg
, "--")) {
130 if (!strcmp(arg
, "-l")) {
131 flags
|= BLOB_LS_FLAG_LONG
;
134 if (!strcmp(arg
, "-i")) {
135 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
138 if (!strcmp(arg
, "-r")) {
139 flags
|= BLOB_LS_FLAG_REVERSE
;
145 // return -E_BLOB_SYNTAX;
146 return send_option_arg_callback_request(&options
, argc
- i
,
147 argv
+ i
, f
, send_result
, &fd
);
150 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
151 __a_unused
const char *name
, void *data
)
154 struct osl_object
*blobs
= data
;
155 struct osl_object obj
;
157 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
161 blobs
->data
= para_realloc(blobs
->data
, blobs
->size
+ obj
.size
);
162 memcpy(blobs
->data
+ blobs
->size
, obj
.data
, obj
.size
);
163 blobs
->size
+= obj
.size
;
165 return osl_close_disk_object(&obj
);
168 static int com_catblob_callback(struct osl_table
*table
,
169 const struct osl_object
*query
, struct osl_object
*result
)
172 struct pattern_match_data pmd
= {
175 .loop_col_num
= BLOBCOL_NAME
,
176 .match_col_num
= BLOBCOL_NAME
,
177 .pm_flags
= PM_SKIP_EMPTY_NAME
,
182 ret
= for_each_matching_row(&pmd
);
185 return (ret
> 0)? 0 : ret
;
188 static int com_catblob(callback_function
*f
, int fd
, int argc
,
189 char * const * const argv
)
192 return -E_BLOB_SYNTAX
;
193 return send_standard_callback_request(argc
- 1, argv
+ 1, f
, send_result
, &fd
);
196 /** Used for removing rows from a blob table. */
198 /** Message buffer. */
199 struct para_buffer pb
;
200 /** Number of removed blobs. */
201 unsigned num_removed
;
204 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
205 const char *name
, void *data
)
207 struct rmblob_data
*rmbd
= data
;
208 int ret
= osl_del_row(table
, row
);
210 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
214 return 1; /* return success to remove other matching blobs. */
217 static int com_rmblob_callback(struct osl_table
*table
,
218 const struct osl_object
*query
,
219 struct osl_object
*result
)
222 struct rmblob_data rmbd
= {.num_removed
= 0};
223 struct pattern_match_data pmd
= {
226 .loop_col_num
= BLOBCOL_NAME
,
227 .match_col_num
= BLOBCOL_NAME
,
228 .pm_flags
= PM_SKIP_EMPTY_NAME
,
230 .action
= remove_blob
233 ret
= for_each_matching_row(&pmd
);
235 para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
236 if (!rmbd
.num_removed
)
237 para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
239 para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
240 afs_event(BLOB_RENAME
, NULL
, table
);
242 result
->data
= rmbd
.pb
.buf
;
243 result
->size
= rmbd
.pb
.offset
;
247 static int com_rmblob(callback_function
*f
, int fd
, int argc
,
248 char * const * const argv
)
251 return -E_MOOD_SYNTAX
;
252 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
256 static int com_addblob_callback(struct osl_table
*table
,
257 const struct osl_object
*query
,
258 __a_unused
struct osl_object
*result
)
260 struct osl_object objs
[NUM_BLOB_COLUMNS
];
261 char *name
= query
->data
;
262 size_t name_len
= strlen(name
) + 1;
267 ret
= osl_get_num_rows(table
, &num_rows
);
270 if (!num_rows
) { /* this is the first entry ever added */
271 /* insert dummy row containing the id */
272 id
= 2; /* this entry will be entry #1, so 2 is the next */
273 objs
[BLOBCOL_ID
].data
= &id
;
274 objs
[BLOBCOL_ID
].size
= sizeof(id
);
275 objs
[BLOBCOL_NAME
].data
= "";
276 objs
[BLOBCOL_NAME
].size
= 1;
277 objs
[BLOBCOL_DEF
].data
= "";
278 objs
[BLOBCOL_DEF
].size
= 1;
279 ret
= osl_add_row(table
, objs
);
283 /* check if name already exists */
285 struct osl_object obj
= {.data
= name
, .size
= name_len
};
286 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
287 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
289 if (ret
>= 0) { /* we already have a blob with this name */
290 obj
.data
= name
+ name_len
;
291 obj
.size
= query
->size
- name_len
;
292 return osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
);
294 /* new blob, get id of the dummy row and increment it */
297 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
300 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
303 id
= *(uint32_t *)obj
.data
+ 1;
305 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
310 objs
[BLOBCOL_ID
].data
= &id
;
311 objs
[BLOBCOL_ID
].size
= sizeof(id
);
312 objs
[BLOBCOL_NAME
].data
= name
;
313 objs
[BLOBCOL_NAME
].size
= name_len
;
314 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
315 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
316 ret
= osl_add_row(table
, objs
);
319 afs_event(BLOB_ADD
, NULL
, table
);
323 static int com_addblob(callback_function
*f
, int fd
, int argc
,
324 char * const * const argv
)
326 struct osl_object arg_obj
;
329 return -E_BLOB_SYNTAX
;
330 if (!*argv
[1]) /* empty name is reserved for the dummy row */
331 return -E_BLOB_SYNTAX
;
332 PARA_NOTICE_LOG("argv[1]: %s\n", argv
[1]);
333 arg_obj
.size
= strlen(argv
[1]) + 1;
334 arg_obj
.data
= (char *)argv
[1];
335 return stdin_command(fd
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
338 static int com_mvblob_callback(struct osl_table
*table
,
339 const struct osl_object
*query
,
340 __a_unused
struct osl_object
*result
)
342 char *src
= (char *) query
->data
;
343 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
344 char *dest
= src
+ obj
.size
;
346 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
351 obj
.size
= strlen(dest
) + 1;
352 ret
= osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
355 afs_event(BLOB_RENAME
, NULL
, table
);
359 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
360 int argc
, char * const * const argv
)
363 return -E_MOOD_SYNTAX
;
364 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
368 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
369 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
370 struct osl_object *output) \
372 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
374 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
376 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
379 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
383 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
389 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
392 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
395 *name
= (char *)obj
.data
;
399 /** Define the \p get_name_by_id function for this blob type. */
400 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
401 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
403 return blob_get_name_by_id(table_name ## _table, id, name); \
407 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
408 struct osl_object
*def
)
411 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
417 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
420 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
423 /** Define the \p get_def_by_id function for this blob type. */
424 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
425 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
427 return blob_get_def_by_name(table_name ## _table, name, def); \
430 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
431 struct osl_object
*def
)
434 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
440 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
443 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
446 /** Define the \p get_def_by_id function for this blob type. */
447 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
448 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
450 return blob_get_def_by_id(table_name ## _table, id, def); \
453 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
454 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
456 struct osl_object obj
;
457 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
461 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
463 /** Define the \p get_name_and_def_by_row function for this blob type. */
464 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
465 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
466 char **name, struct osl_object *def) \
468 return blob_get_name_and_def_by_row(table_name ## _table, \
472 /** Define the \p close function for this blob type. */
473 #define DEFINE_BLOB_CLOSE(table_name) \
474 void table_name ## _close(void) \
476 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
477 table_name ## _table = NULL; \
480 /** Define the \p create function for this blob type. */
481 #define DEFINE_BLOB_CREATE(table_name) \
482 int table_name ## _create(const char *dir) \
484 table_name ## _table_desc.dir = dir; \
485 return osl_create_table(&table_name ## _table_desc); \
488 static int blob_open(struct osl_table
**table
,
489 struct osl_table_description
*desc
,
494 ret
= osl_open_table(desc
, table
);
498 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
503 #define DEFINE_BLOB_OPEN(table_name) \
504 int table_name ## _open(const char *dir) \
506 return blob_open(&table_name ## _table, \
507 &table_name ## _table_desc, dir); \
511 /** Define the \p init function for this blob type. */
512 #define DEFINE_BLOB_INIT(table_name) \
513 void table_name ## _init(struct afs_table *t) \
515 t->name = table_name ## _table_desc.name; \
516 t->open = table_name ## _open; \
517 t->close = table_name ## _close; \
518 t->create = table_name ## _create;\
519 t->event_handler = table_name ##_event_handler; \
520 table_name ## _table = NULL; \
524 /** Define all functions for this blob type. */
525 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
526 DEFINE_BLOB_OPEN(table_name) \
527 DEFINE_BLOB_CLOSE(table_name) \
528 DEFINE_BLOB_CREATE(table_name) \
529 DEFINE_BLOB_INIT(table_name) \
530 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
531 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
532 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
533 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
534 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
535 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
536 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
537 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
538 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
540 /** \cond doxygen isn't smart enough to recognize these */
541 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
542 DEFINE_BLOB_FUNCTIONS(images
, img
);
543 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
544 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);