backend: misc refactor

- Following commit d01554400c, rename `attr_key_*` to `xattr_name_*`.
- Other misc changes.
This commit is contained in:
CismonX 2025-02-28 10:05:25 +08:00
parent cd648f9ef0
commit f952a35ddc
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
4 changed files with 58 additions and 59 deletions

View file

@ -2299,7 +2299,7 @@ Type of the @code{bookmark_get} function is defined as:
typedef int (bookmarkfs_bookmark_get_func) ( typedef int (bookmarkfs_bookmark_get_func) (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
bookmarkfs_bookmark_get_cb *callback, bookmarkfs_bookmark_get_cb *callback,
void *user_data, void *user_data,
void **cookie_ptr void **cookie_ptr
@ -2313,13 +2313,13 @@ Function arguments:
The pointer referring to the backend context. The pointer referring to the backend context.
@item id @item id
ID of the bookmark (could be a bookmark folder if @code{attr_key} ID of the bookmark (could be a bookmark folder if @code{xattr_name}
is not @code{NULL}). is not @code{NULL}).
@item attr_key @item xattr_name
Name of an extended attribute. Name of an extended attribute.
If not @code{NULL}, @code{attr_key} is guaranteed to be a NUL-terminated If not @code{NULL}, @code{xattr_name} is guaranteed to be a NUL-terminated
string, and the function should get the corresponding extended attribute value string, and the function should get the corresponding extended attribute value
instead of the bookmark content. instead of the bookmark content.
@ -2367,7 +2367,7 @@ Pointer to the ``cookie'' for the current @code{bookmark_get} operation.
@code{NULL} if not used by the caller. @code{NULL} if not used by the caller.
The “cookie” is an opaque pointer which stores internal states The “cookie” is an opaque pointer which stores internal states
for subsequent operations on the same bookmark (and @code{attr_key}) for subsequent operations on the same bookmark (and @code{xattr_name})
to determine whether the corresponding data has changed. to determine whether the corresponding data has changed.
If the value pointered to by @code{cookie_ptr} is @code{NULL}, If the value pointered to by @code{cookie_ptr} is @code{NULL},
@ -2459,7 +2459,7 @@ Type of the @code{bookmark_set} function is defined as:
typedef int (bookmarkfs_bookmark_set_func) ( typedef int (bookmarkfs_bookmark_set_func) (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
uint32_t flags, uint32_t flags,
void const *val, void const *val,
size_t val_len size_t val_len
@ -2476,10 +2476,10 @@ The pointer referring to the backend context.
ID of the object to update. ID of the object to update.
It could be a bookmark ID or tag ID, depending on the value of @code{flags}. It could be a bookmark ID or tag ID, depending on the value of @code{flags}.
@item attr_key @item xattr_name
Name of an extended attribute. Name of an extended attribute.
If not @code{NULL}, @code{attr_key} is guaranteed to be a NUL-terminated If not @code{NULL}, @code{xattr_name} is guaranteed to be a NUL-terminated
string, and the function should update the corresponding extended attribute string, and the function should update the corresponding extended attribute
value instead of the bookmark content. value instead of the bookmark content.
@ -2494,7 +2494,7 @@ the object.
The @code{val} argument points to an array of @code{struct timespec}, The @code{val} argument points to an array of @code{struct timespec},
the first element refers to the last access time, and the second element the first element refers to the last access time, and the second element
refers to the last modification time of the object. refers to the last modification time of the object.
Values of @code{attr_key} and @code{val_len} are unspecified. Values of @code{xattr_name} and @code{val_len} are unspecified.
The @code{tv_nsec} field of each timestamp may be @code{UTIME_OMIT}, The @code{tv_nsec} field of each timestamp may be @code{UTIME_OMIT},
which indicates that the timestamp should be left as-is. which indicates that the timestamp should be left as-is.

View file

@ -159,7 +159,7 @@ typedef int (bookmarkfs_bookmark_get_cb) (
typedef int (bookmarkfs_bookmark_get_func) ( typedef int (bookmarkfs_bookmark_get_func) (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
bookmarkfs_bookmark_get_cb *callback, bookmarkfs_bookmark_get_cb *callback,
void *user_data, void *user_data,
void **cookie_ptr void **cookie_ptr
@ -209,7 +209,7 @@ typedef int (bookmarkfs_bookmark_rename_func) (
typedef int (bookmarkfs_bookmark_set_func) ( typedef int (bookmarkfs_bookmark_set_func) (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
uint32_t flags, uint32_t flags,
void const *val, void const *val,
size_t val_len size_t val_len

View file

@ -228,8 +228,8 @@ static void free_maps (struct hashmap *, struct hashmap *,
struct hashmap *); struct hashmap *);
static int fsck_next (struct backend_ctx const *, uint64_t, json_t *, static int fsck_next (struct backend_ctx const *, uint64_t, json_t *,
size_t *, bookmarkfs_bookmark_check_cb *, void *); size_t *, bookmarkfs_bookmark_check_cb *, void *);
static int get_attr_type (char const *, uint32_t); static int get_xattr_id (char const *, uint32_t);
static int get_attr_val (json_t const *, char const *, uint32_t, json_t **); static int get_xattr_val (json_t const *, char const *, uint32_t, json_t **);
static int guidmap_comp (union hashmap_key, void const *); static int guidmap_comp (union hashmap_key, void const *);
static unsigned long static unsigned long
guidmap_hash (void const *); guidmap_hash (void const *);
@ -951,22 +951,22 @@ fsck_next (
} }
static int static int
get_attr_type ( get_xattr_id (
char const *key, char const *name,
uint32_t flags uint32_t flags
) { ) {
if (key == NULL) { if (name == NULL) {
return BM_XATTR_NULL; return BM_XATTR_NULL;
} }
if (0 == strcmp("date_added", key)) { if (0 == strcmp("date_added", name)) {
return BM_XATTR_DATE_ADDED; return BM_XATTR_DATE_ADDED;
} }
if (flags & BACKEND_FILENAME_GUID) { if (flags & BACKEND_FILENAME_GUID) {
if (0 == strcmp("title", key)) { if (0 == strcmp("title", name)) {
return BM_XATTR_TITLE; return BM_XATTR_TITLE;
} }
} else { } else {
if (0 == strcmp("guid", key)) { if (0 == strcmp("guid", name)) {
return BM_XATTR_GUID; return BM_XATTR_GUID;
} }
} }
@ -974,16 +974,16 @@ get_attr_type (
} }
static int static int
get_attr_val ( get_xattr_val (
json_t const *node, json_t const *node,
char const *attr_key, char const *xattr_name,
uint32_t flags, uint32_t flags,
json_t **value_ptr json_t **value_ptr
) { ) {
json_t *value; json_t *value;
int key_type = get_attr_type(attr_key, flags); int xattr_id = get_xattr_id(xattr_name, flags);
switch (key_type) { switch (xattr_id) {
case BM_XATTR_NULL: case BM_XATTR_NULL:
value = json_object_sget(node, "url"); value = json_object_sget(node, "url");
break; break;
@ -1004,13 +1004,13 @@ get_attr_val (
return -ENOATTR; return -ENOATTR;
} }
if (value == NULL) { if (value == NULL) {
if (unlikely(attr_key == NULL)) { if (unlikely(xattr_name == NULL)) {
return -EIO; return -EIO;
} }
return -EISDIR; return -EISDIR;
} }
*value_ptr = value; *value_ptr = value;
return key_type; return xattr_id;
} }
static int static int
@ -1933,7 +1933,7 @@ static int
bookmark_get ( bookmark_get (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
bookmarkfs_bookmark_get_cb *callback, bookmarkfs_bookmark_get_cb *callback,
void *user_data, void *user_data,
void **cookie_ptr void **cookie_ptr
@ -1952,7 +1952,7 @@ bookmark_get (
goto lookup; goto lookup;
} }
if (cookie->store != ctx->store) { if (cookie->store != ctx->store) {
if (attr_key != NULL) { if (xattr_name != NULL) {
goto lookup; goto lookup;
} }
// Checksum only applies to directory structure and URL values. // Checksum only applies to directory structure and URL values.
@ -1968,11 +1968,12 @@ bookmark_get (
return -ESTALE; return -ESTALE;
} }
if (entry->id == BOOKMARKS_ROOT_ID) { if (entry->id == BOOKMARKS_ROOT_ID) {
return attr_key == NULL ? -EISDIR : -ENOATTR; return xattr_name == NULL ? -EISDIR : -ENOATTR;
} }
json_t *value_node; json_t *value_node;
int status = get_attr_val(entry->node, attr_key, ctx->flags, &value_node); int status = get_xattr_val(entry->node, xattr_name, ctx->flags,
&value_node);
if (status < 0) { if (status < 0) {
return status; return status;
} }
@ -2600,7 +2601,7 @@ static int
bookmark_set ( bookmark_set (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
uint32_t flags, uint32_t flags,
void const *val, void const *val,
size_t val_len size_t val_len
@ -2637,7 +2638,8 @@ bookmark_set (
} }
json_t *val_node; json_t *val_node;
int key_type = get_attr_val(entry->node, attr_key, ctx->flags, &val_node); int key_type = get_xattr_val(entry->node, xattr_name, ctx->flags,
&val_node);
if (key_type < 0) { if (key_type < 0) {
return key_type; return key_type;
} }

View file

@ -347,7 +347,7 @@ static unsigned long
static void free_blcookie (struct bookmark_lcookie *); static void free_blcookie (struct bookmark_lcookie *);
static void free_dentmap (struct hashmap *); static void free_dentmap (struct hashmap *);
static void free_dentmap_entry (void *, void *); static void free_dentmap_entry (void *, void *);
static int get_attr_type (char const *, uint32_t); static int get_xattr_id (char const *, uint32_t);
static int64_t get_data_version (struct backend_ctx *); static int64_t get_data_version (struct backend_ctx *);
static bool is_valid_id (int64_t); static bool is_valid_id (int64_t);
static void msecs_to_timespec (struct timespec *, int64_t); static void msecs_to_timespec (struct timespec *, int64_t);
@ -2032,7 +2032,7 @@ static int
bookmark_do_get ( bookmark_do_get (
struct backend_ctx *ctx, struct backend_ctx *ctx,
uint64_t id, uint64_t id,
int attr_type, int xattr_id,
struct bookmark_get_ctx *qctx struct bookmark_get_ctx *qctx
) { ) {
#define BOOKMARK_GET_(cols, join) "SELECT CASE ? " cols "END " \ #define BOOKMARK_GET_(cols, join) "SELECT CASE ? " cols "END " \
@ -2050,7 +2050,7 @@ bookmark_do_get (
sqlite3_stmt **stmt_ptr = &ctx->stmts[STMT_BOOKMARK_GET_EX]; sqlite3_stmt **stmt_ptr = &ctx->stmts[STMT_BOOKMARK_GET_EX];
char const *sql = BOOKMARK_GET_EX; char const *sql = BOOKMARK_GET_EX;
if (attr_type >= MOZBM_XATTR_START) { if (xattr_id >= MOZBM_XATTR_START) {
stmt_ptr = &ctx->stmts[STMT_BOOKMARK_GET]; stmt_ptr = &ctx->stmts[STMT_BOOKMARK_GET];
sql = BOOKMARK_GET_WITH_GUID; sql = BOOKMARK_GET_WITH_GUID;
if (ctx->flags & BACKEND_FILENAME_GUID) { if (ctx->flags & BACKEND_FILENAME_GUID) {
@ -2063,7 +2063,7 @@ bookmark_do_get (
{ {
qctx->tags_root_id = ctx->tags_root_id; qctx->tags_root_id = ctx->tags_root_id;
}, },
DB_QUERY_BIND_INT64(attr_type), DB_QUERY_BIND_INT64(xattr_id),
DB_QUERY_BIND_INT64(id), DB_QUERY_BIND_INT64(id),
); );
if (nrows < 0) { if (nrows < 0) {
@ -2256,9 +2256,9 @@ bookmark_do_lookup (
bookmark_type >>= BOOKMARKFS_BOOKMARK_TYPE_SHIFT; bookmark_type >>= BOOKMARKFS_BOOKMARK_TYPE_SHIFT;
char const *sql_table[3][2] = { char const *sql_table[3][2] = {
{ BOOKMARK_LOOKUP_TITLE, BOOKMARK_LOOKUP_GUID }, { BOOKMARK_LOOKUP_TITLE, BOOKMARK_LOOKUP_GUID },
{ BOOKMARK_LOOKUP_TAG_TITLE, BOOKMARK_LOOKUP_TAG_GUID }, { BOOKMARK_LOOKUP_TAG_TITLE, BOOKMARK_LOOKUP_TAG_GUID },
{ BOOKMARK_LOOKUP_KEYWORD, BOOKMARK_LOOKUP_KEYWORD }, { BOOKMARK_LOOKUP_KEYWORD, BOOKMARK_LOOKUP_KEYWORD },
}; };
sql = sql_table[bookmark_type][filename_is_guid]; sql = sql_table[bookmark_type][filename_is_guid];
@ -2301,9 +2301,6 @@ bookmark_check_cb (
log_printf("bad bookmark ID %" PRIi64, id); log_printf("bad bookmark ID %" PRIi64, id);
goto fail; goto fail;
} }
if (unlikely((uint64_t)id == ctx->tags_root_id)) {
return 0;
}
int64_t position = sqlite3_column_int64(stmt, 1); int64_t position = sqlite3_column_int64(stmt, 1);
if (unlikely(position < 0)) { if (unlikely(position < 0)) {
@ -2559,25 +2556,25 @@ free_dentmap_entry (
} }
static int static int
get_attr_type ( get_xattr_id (
char const *key, char const *name,
uint32_t flags uint32_t flags
) { ) {
if (key == NULL) { if (name == NULL) {
return BM_XATTR_NULL; return BM_XATTR_NULL;
} }
if (0 == strcmp("date_added", key)) { if (0 == strcmp("date_added", name)) {
return BM_XATTR_DATE_ADDED; return BM_XATTR_DATE_ADDED;
} }
if (0 == strcmp("description", key)) { if (0 == strcmp("description", name)) {
return BM_XATTR_DESC; return BM_XATTR_DESC;
} }
if (flags & BACKEND_FILENAME_GUID) { if (flags & BACKEND_FILENAME_GUID) {
if (0 == strcmp("title", key)) { if (0 == strcmp("title", name)) {
return BM_XATTR_TITLE; return BM_XATTR_TITLE;
} }
} else { } else {
if (0 == strcmp("guid", key)) { if (0 == strcmp("guid", name)) {
return BM_XATTR_GUID; return BM_XATTR_GUID;
} }
} }
@ -3077,15 +3074,15 @@ static int
bookmark_get ( bookmark_get (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
bookmarkfs_bookmark_get_cb *callback, bookmarkfs_bookmark_get_cb *callback,
void *user_data, void *user_data,
void **cookie_ptr void **cookie_ptr
) { ) {
struct backend_ctx *ctx = backend_ctx; struct backend_ctx *ctx = backend_ctx;
int attr_type = get_attr_type(attr_key, ctx->flags); int xattr_id = get_xattr_id(xattr_name, ctx->flags);
if (attr_type < 0) { if (xattr_id < 0) {
return -ENOATTR; return -ENOATTR;
} }
@ -3107,7 +3104,7 @@ bookmark_get (
struct bookmark_get_ctx qctx; struct bookmark_get_ctx qctx;
qctx.callback = callback; qctx.callback = callback;
qctx.user_data = user_data; qctx.user_data = user_data;
int status = bookmark_do_get(ctx, id, attr_type, &qctx); int status = bookmark_do_get(ctx, id, xattr_id, &qctx);
if (status < 0) { if (status < 0) {
return status; return status;
} }
@ -3594,7 +3591,7 @@ static int
bookmark_set ( bookmark_set (
void *backend_ctx, void *backend_ctx,
uint64_t id, uint64_t id,
char const *attr_key, char const *xattr_name,
uint32_t flags, uint32_t flags,
void const *val, void const *val,
size_t val_len size_t val_len
@ -3612,17 +3609,17 @@ bookmark_set (
.last_visit_date = -1, .last_visit_date = -1,
}; };
int attr_type = MOZBM_XATTR_START; int xattr_id = MOZBM_XATTR_START;
if (flags & BOOKMARK_FLAG(SET_TIME)) { if (flags & BOOKMARK_FLAG(SET_TIME)) {
struct timespec const *times = val; struct timespec const *times = val;
place_cols.last_visit_date = timespec_to_msecs(&times[0]); place_cols.last_visit_date = timespec_to_msecs(&times[0]);
bm_cols.last_modified = timespec_to_msecs(&times[1]); bm_cols.last_modified = timespec_to_msecs(&times[1]);
if (place_cols.last_visit_date >= 0) { if (place_cols.last_visit_date >= 0) {
--attr_type; --xattr_id;
} }
} else { } else {
attr_type = get_attr_type(attr_key, ctx->flags); xattr_id = get_xattr_id(xattr_name, ctx->flags);
switch (attr_type) { switch (xattr_id) {
case BM_XATTR_NULL: case BM_XATTR_NULL:
place_cols.url = val; place_cols.url = val;
place_cols.url_len = val_len; place_cols.url_len = val_len;
@ -3658,7 +3655,7 @@ bookmark_set (
return -ENOATTR; return -ENOATTR;
} }
if (attr_type != BM_XATTR_NULL if (xattr_id != BM_XATTR_NULL
&& ctx->flags & BOOKMARKFS_BACKEND_CTIME && ctx->flags & BOOKMARKFS_BACKEND_CTIME
) { ) {
struct timespec now; struct timespec now;
@ -3679,7 +3676,7 @@ bookmark_set (
status = -EPERM; status = -EPERM;
goto fail; goto fail;
} }
if (attr_type >= MOZBM_XATTR_START) { if (xattr_id >= MOZBM_XATTR_START) {
goto end; goto end;
} }