erofs-utils: lib: add match_base_prefix() helper
authorJingbo Xu <jefflexu@linux.alibaba.com>
Thu, 17 Aug 2023 07:14:53 +0000 (15:14 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 17 Aug 2023 11:21:08 +0000 (19:21 +0800)
Since the introduction of long xattr name prefix, match_prefix() will
search among the long xattr name prefixes first and return the matched
prefix, while erofs_getxattr() expects a base prefix even when the
queried xattr name matches a long prefix.

Thus introduce match_base_prefix() helper to do this.

Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
Link: https://lore.kernel.org/r/20230817071455.12040-2-jefflexu@linux.alibaba.com
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
lib/xattr.c

index 25487504e5a6f05c94cefd19f9f90efd1d6a933a..4091fe6c926f5e9046af2b48a20756526d4d6aec 100644 (file)
@@ -137,27 +137,34 @@ static struct xattr_item *get_xattritem(u8 prefix, char *kvbuf,
        return item;
 }
 
-static bool match_prefix(const char *key, u8 *index, u16 *len)
+static bool match_base_prefix(const char *key, u8 *index, u16 *len)
 {
        struct xattr_prefix *p;
-       struct ea_type_node *tnode;
 
-       list_for_each_entry(tnode, &ea_name_prefixes, list) {
-               p = &tnode->type;
+       for (p = xattr_types; p < xattr_types + ARRAY_SIZE(xattr_types); ++p) {
                if (p->prefix && !strncmp(p->prefix, key, p->prefix_len)) {
                        *len = p->prefix_len;
-                       *index = tnode->index;
+                       *index = p - xattr_types;
                        return true;
                }
        }
-       for (p = xattr_types; p < xattr_types + ARRAY_SIZE(xattr_types); ++p) {
+       return false;
+}
+
+static bool match_prefix(const char *key, u8 *index, u16 *len)
+{
+       struct xattr_prefix *p;
+       struct ea_type_node *tnode;
+
+       list_for_each_entry(tnode, &ea_name_prefixes, list) {
+               p = &tnode->type;
                if (p->prefix && !strncmp(p->prefix, key, p->prefix_len)) {
                        *len = p->prefix_len;
-                       *index = p - xattr_types;
+                       *index = tnode->index;
                        return true;
                }
        }
-       return false;
+       return match_base_prefix(key, index, len);
 }
 
 static struct xattr_item *parse_one_xattr(const char *path, const char *key,
@@ -1198,7 +1205,7 @@ int erofs_getxattr(struct erofs_inode *vi, const char *name, char *buffer,
        if (ret)
                return ret;
 
-       if (!match_prefix(name, &prefix, &prefixlen))
+       if (!match_base_prefix(name, &prefix, &prefixlen))
                return -ENODATA;
 
        it.it.sbi = vi->sbi;