From fbc392830c2b0af7258f57427b4c91c456e05ec0 Mon Sep 17 00:00:00 2001 From: szaka Date: Thu, 26 Feb 2009 23:41:32 +0000 Subject: [PATCH] ntfs_inode_free_space: factor out ntfs_attr_position --- include/ntfs-3g/attrib.h | 2 ++ libntfs-3g/attrib.c | 30 ++++++++++++++++++++++++++++++ libntfs-3g/inode.c | 28 ++++++---------------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/include/ntfs-3g/attrib.h b/include/ntfs-3g/attrib.h index eafa165a..36096239 100644 --- a/include/ntfs-3g/attrib.h +++ b/include/ntfs-3g/attrib.h @@ -92,6 +92,8 @@ extern int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name, const VCN lowest_vcn, const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx); +extern int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx); + extern ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol, const ATTR_TYPES type); diff --git a/libntfs-3g/attrib.c b/libntfs-3g/attrib.c index 777258a4..73747c8a 100644 --- a/libntfs-3g/attrib.c +++ b/libntfs-3g/attrib.c @@ -2350,6 +2350,36 @@ out: return ret; } +/** + * ntfs_attr_position - find given or next attribute type in an ntfs inode + * @type: attribute type to start lookup + * @ctx: search context with mft record and attribute to search from + * + * Find an attribute type in an ntfs inode or the next attribute which is not + * the AT_END attribute. Please see more details at ntfs_attr_lookup. + * + * Return 0 if the search was successful and -1 if not, with errno set to the + * error code. + * + * The following error codes are defined: + * EINVAL Invalid arguments. + * EIO I/O error or corrupt data structures found. + * ENOMEM Not enough memory to allocate necessary buffers. + * ENOSPC No attribute was found after 'type', only AT_END. + */ +int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx) +{ + if (ntfs_attr_lookup(type, NULL, 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (errno != ENOENT) + return -1; + if (ctx->attr->type == AT_END) { + errno = ENOSPC; + return -1; + } + } + return 0; +} + /** * ntfs_attr_init_search_ctx - initialize an attribute search context * @ctx: attribute search context to initialize diff --git a/libntfs-3g/inode.c b/libntfs-3g/inode.c index 69c59d81..3d026c32 100644 --- a/libntfs-3g/inode.c +++ b/libntfs-3g/inode.c @@ -1022,17 +1022,9 @@ int ntfs_inode_free_space(ntfs_inode *ni, int size) * $STANDARD_INFORMATION and $ATTRIBUTE_LIST must stay in the base MFT * record, so position search context on the first attribute after them. */ - if (ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL, - 0, ctx)) { - if (errno != ENOENT) { - err = errno; - ntfs_log_perror("%s: attr lookup failed #2", __FUNCTION__); - goto put_err_out; - } - if (ctx->attr->type == AT_END) { - err = ENOSPC; - goto put_err_out; - } + if (ntfs_attr_position(AT_FILE_NAME, ctx)) { + err = errno; + goto put_err_out; } while (1) { @@ -1081,17 +1073,9 @@ retry: * $ATTRIBUTE_LIST. */ ntfs_attr_reinit_search_ctx(ctx); - if (ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, - NULL, 0, ctx)) { - if (errno != ENOENT) { - err = errno; - ntfs_log_perror("Attr lookup #2 failed"); - break; - } - if (ctx->attr->type == AT_END) { - err = ENOSPC; - break; - } + if (ntfs_attr_position(AT_FILE_NAME, ctx)) { + err = errno; + break; } } put_err_out: