From 32c57ab52a2232386b4507a53435c97d197d57dc Mon Sep 17 00:00:00 2001 From: Etienne Brateau Date: Sun, 23 Jan 2022 05:17:12 +0100 Subject: ext2fs: refactor add a method for dupplicated code * xattr: introduce xattr_header_valid method Message-Id: <20220123041715.19402-14-etienne.brateau@gmail.com> --- ext2fs/xattr.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ext2fs/xattr.c b/ext2fs/xattr.c index ab6f5600..31af3b25 100644 --- a/ext2fs/xattr.c +++ b/ext2fs/xattr.c @@ -413,6 +413,16 @@ xattr_entry_replace (struct ext2_xattr_header *header, } +/* Given a xattr header, return 1 if the header is a valid xattr header + * and 0 if the header is not valid. + */ +static int +xattr_header_valid(struct ext2_xattr_header *header) +{ + return header->h_magic != htole32 (EXT2_XATTR_BLOCK_MAGIC) + || header->h_blocks != htole32 (1); +} + /* * Given a node, free extended attributes block associated with @@ -450,8 +460,7 @@ ext2_free_xattr_block (struct node *np) block = disk_cache_block_ref (blkno); header = EXT2_XATTR_HEADER (block); - if (header->h_magic != htole32 (EXT2_XATTR_BLOCK_MAGIC) - || header->h_blocks != htole32 (1)) + if (xattr_header_valid(header)) { ext2_warning ("Invalid extended attribute block."); err = EIO; @@ -536,8 +545,7 @@ ext2_list_xattr (struct node *np, char *buffer, size_t *len) block = disk_cache_block_ref (blkno); header = EXT2_XATTR_HEADER (block); - if (header->h_magic != htole32 (EXT2_XATTR_BLOCK_MAGIC) - || header->h_blocks != htole32 (1)) + if (xattr_header_valid(header)) { ext2_warning ("Invalid extended attribute block."); err = EIO; @@ -611,8 +619,7 @@ ext2_get_xattr (struct node *np, const char *name, char *value, size_t *len) dino_deref (ei); header = EXT2_XATTR_HEADER (block); - if (header->h_magic != htole32 (EXT2_XATTR_BLOCK_MAGIC) - || header->h_blocks != htole32 (1)) + if (xattr_header_valid(header)) { ext2_warning ("Invalid extended attribute block."); err = EIO; @@ -724,8 +731,7 @@ ext2_set_xattr (struct node *np, const char *name, const char *value, { block = disk_cache_block_ref (blkno); header = EXT2_XATTR_HEADER (block); - if (header->h_magic != htole32 (EXT2_XATTR_BLOCK_MAGIC) - || header->h_blocks != htole32 (1)) + if (xattr_header_valid(header)) { ext2_warning ("Invalid extended attribute block."); err = EIO; -- cgit v1.2.3