diff --git a/ChangeLog b/ChangeLog index 81bfe438..8c1a8726 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,8 +32,9 @@ xx/xx/2006 - 1.13.1-WIP - ntfsinfo: dump either a minimal (default) or the entire attribute header (--verbose) for all attributes types. Also removed a lot of redundant code and made some formatting corrections. (Szaka) - - libntfs: add ntfs_str2ucs(), ntfs_freeucs() and ntfs_mft_usn_dec() - functions, and convert all copy-pastes to use them. (Szaka) + - libntfs: add ntfs_str2ucs(), ntfs_freeucs(), ntfs_mft_usn_dec() + and ntfs_inode_badclus_bad() functions, and convert all copy-pastes + to use them. (Szaka) 27/02/2006 - 1.13.0 - Lots and lots and lots of fixes and enhancements. diff --git a/include/ntfs/inode.h b/include/ntfs/inode.h index b8bc5a71..032d0216 100644 --- a/include/ntfs/inode.h +++ b/include/ntfs/inode.h @@ -185,6 +185,8 @@ extern int ntfs_inode_add_attrlist(ntfs_inode *ni); extern int ntfs_inode_free_space(ntfs_inode *ni, int size); +extern int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *a); + #ifdef NTFS_RICH int ntfs_inode_close2(ntfs_inode *ni); diff --git a/libntfs/inode.c b/libntfs/inode.c index ca9d77f7..2fef1f6a 100644 --- a/libntfs/inode.c +++ b/libntfs/inode.c @@ -1105,6 +1105,50 @@ void ntfs_inode_update_time(ntfs_inode *ni) } } +/** + * ntfs_inode_badclus_bad - check for $Badclus:$Bad data attribute + * @mft_no: mft record number where @attr is present + * @attr: attribute record used to check for the $Bad attribute + * + * Check if the mft record given by @mft_no and @attr contains the bad sector + * list. Please note that mft record numbers describing $Badclus extent inodes + * will not match the current $Badclus:$Bad check. + * + * On success return 1 if the file is $Badclus:$Bad, otherwise return 0. + * On error return -1 with errno set to the error code. + */ +int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *attr) +{ + int len, ret = 0; + ntfschar *ustr; + + if (!attr) { + ntfs_log_error("Invalid argument.\n"); + errno = EINVAL; + return -1; + } + + if (mft_no != FILE_BadClus) + return 0; + + if (attr->type != AT_DATA) + return 0; + + if ((ustr = ntfs_str2ucs("$Bad", &len)) == NULL) { + ntfs_log_perror("Couldn't convert '$Bad' to Unicode"); + return -1; + } + + if (ustr && ntfs_names_are_equal(ustr, len, + (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)), + attr->name_length, 0, NULL, 0)) + ret = 1; + + ntfs_ucsfree(ustr); + + return ret; +} + #ifdef NTFS_RICH #include "rich.h" diff --git a/ntfsprogs/ntfsresize.c b/ntfsprogs/ntfsresize.c index 62e5657f..58758f12 100644 --- a/ntfsprogs/ntfsresize.c +++ b/ntfsprogs/ntfsresize.c @@ -638,29 +638,6 @@ static s64 nr_clusters_to_bitmap_byte_size(s64 nr_clusters) return bm_bsize; } -static int is_badclus_bad(u64 mft_no, ATTR_RECORD *a) -{ - int len, ret = 0; - ntfschar *ustr; - - if (mft_no != FILE_BadClus) - return 0; - - if ((ustr = ntfs_str2ucs("$Bad", &len)) == NULL) { - perr_printf("Couldn't convert '$Bad' to Unicode"); - return -1; - } - - if (ustr && ntfs_names_are_equal(ustr, len, - (ntfschar *)((u8 *)a + le16_to_cpu(a->name_offset)), - a->name_length, 0, NULL, 0)) - ret = 1; - - ntfs_ucsfree(ustr); - - return ret; -} - static void collect_resize_constraints(ntfs_resize_t *resize, runlist *rl) { s64 inode, last_lcn; @@ -675,9 +652,9 @@ static void collect_resize_constraints(ntfs_resize_t *resize, runlist *rl) flags = resize->ctx->attr->flags; atype = resize->ctx->attr->type; - if ((ret = is_badclus_bad(inode, resize->ctx->attr)) != 0) { + if ((ret = ntfs_inode_badclus_bad(inode, resize->ctx->attr)) != 0) { if (ret == -1) - exit(1); + perr_exit("Bad sector list check failed"); return; } @@ -1679,9 +1656,9 @@ static void relocate_attributes(ntfs_resize_t *resize, int do_mftdata) if (handle_mftdata(resize, do_mftdata) == 0) continue; - ret = is_badclus_bad(resize->mref, resize->ctx->attr); + ret = ntfs_inode_badclus_bad(resize->mref, resize->ctx->attr); if (ret == -1) - exit(1); + perr_exit("Bad sector list check failed"); else if (ret == 1) continue;