From 27e0e7a0c7a37b7aa4995ae4d2a29d8ba623b98a Mon Sep 17 00:00:00 2001 From: "(none)!yura" <(none)!yura> Date: Sun, 12 Sep 2004 18:46:04 +0000 Subject: [PATCH] Add @stopped_at parameter to ntfs_mapping_pairs_build and adapt all callers. (Logical change 1.547) --- include/ntfs/runlist.h | 2 +- libntfs/attrib.c | 7 ++++--- libntfs/runlist.c | 39 +++++++++++++++++++++++++-------------- ntfsprogs/mkntfs.c | 4 ++-- ntfsprogs/ntfsmove.c | 2 +- ntfsprogs/ntfsresize.c | 2 +- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/include/ntfs/runlist.h b/include/ntfs/runlist.h index e79a1106..7a36258f 100644 --- a/include/ntfs/runlist.h +++ b/include/ntfs/runlist.h @@ -72,7 +72,7 @@ extern int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max, extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, const int dst_len, const runlist_element *rl, - const VCN start_vcn); + const VCN start_vcn, VCN *stopped_at); extern int ntfs_rl_truncate(runlist **rl, const VCN start_vcn); diff --git a/libntfs/attrib.c b/libntfs/attrib.c index 133fef75..5b3c6a3f 100644 --- a/libntfs/attrib.c +++ b/libntfs/attrib.c @@ -2513,7 +2513,7 @@ static int ntfs_attr_make_non_resident(ntfs_attr *na, /* Generate the mapping pairs array in the attribute record. */ if (ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs, arec_size - mp_ofs, - rl, 0) < 0) { + rl, 0, 0) < 0) { err = errno; // FIXME: Eeek! We need rollback! (AIA) Dprintf("%s(): Eeek! Failed to build mapping pairs. Leaving " @@ -3038,7 +3038,8 @@ static int ntfs_non_resident_attr_shrink(ntfs_attr *na, const s64 newsize) * correct destination, i.e. the attribute record itself. */ if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu( - a->mapping_pairs_offset), mp_size, na->rl, 0)) { + a->mapping_pairs_offset), mp_size, + na->rl, 0, 0)) { err = errno; // FIXME: Eeek! We need rollback! (AIA) Dprintf("%s(): Eeek! Mapping pairs build " @@ -3353,7 +3354,7 @@ static int ntfs_non_resident_attr_expand(ntfs_attr *na, const s64 newsize) */ if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp_size, - na->rl, sle64_to_cpu(a->lowest_vcn))) { + na->rl, sle64_to_cpu(a->lowest_vcn), 0)) { err = errno; Dprintf("%s(): BUG! Mapping pairs build " "failed. Please run chkdsk and if " diff --git a/libntfs/runlist.c b/libntfs/runlist.c index b2c7e8ac..f251e59a 100644 --- a/libntfs/runlist.c +++ b/libntfs/runlist.c @@ -1396,6 +1396,8 @@ err_out: * @dst_len: size of destination buffer @dst in bytes * @rl: runlist for which to build the mapping pairs array * @start_vcn: vcn at which to start the mapping pairs array + * @stopped_at: if function failed with ENOSPC error code @stopped_at contain + * first vcn outside destination buffer. * * Create the mapping pairs array from the runlist @rl, starting at vcn * @start_vcn and save the array in @dst. @dst_len is the size of @dst in @@ -1404,6 +1406,8 @@ err_out: * * If @rl is NULL, just write a single terminator byte to @dst. * + * @stopped_at can be NULL in case such information is not required. + * * Return 0 on success. On error, return -1 with errno set to the error code. * The following error codes are defined: * EINVAL - Run list contains unmapped elements. Make sure to only pass @@ -1414,7 +1418,7 @@ err_out: */ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, const int dst_len, const runlist_element *rl, - const VCN start_vcn) + const VCN start_vcn, VCN *stopped_at) { LCN prev_lcn; s8 *dst_max; @@ -1475,10 +1479,13 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, goto size_err; } else lcn_len = 0; - /* Update header byte. */ - *dst = lcn_len << 4 | len_len; - /* Position ourselves at next mapping pairs array element. */ - dst += 1 + len_len + lcn_len; + if (dst + len_len + lcn_len + 1 <= dst_max) { + /* Update header byte. */ + *dst = lcn_len << 4 | len_len; + /* Position at next mapping pairs array element. */ + dst += 1 + len_len + lcn_len; + } else + goto size_err; /* Go to next runlist element. */ rl++; } @@ -1509,17 +1516,21 @@ int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, prev_lcn = rl->lcn; } else lcn_len = 0; - /* Update header byte. */ - *dst = lcn_len << 4 | len_len; - /* Position ourselves at next mapping pairs array element. */ - dst += 1 + len_len + lcn_len; - } - if (dst <= dst_max) { - /* Terminator byte. */ - *dst = 0; - return 0; + if (dst + len_len + lcn_len + 1 <= dst_max) { + /* Update header byte. */ + *dst = lcn_len << 4 | len_len; + /* Position at next mapping pairs array element. */ + dst += 1 + len_len + lcn_len; + } else + goto size_err; } + /* Terminator byte. */ + *dst = 0; + return 0; size_err: + if (stopped_at) + *stopped_at = rl->vcn; + *dst = 0; errno = ENOSPC; return -1; val_err: diff --git a/ntfsprogs/mkntfs.c b/ntfsprogs/mkntfs.c index b2e3a23e..faa51bc4 100644 --- a/ntfsprogs/mkntfs.c +++ b/ntfsprogs/mkntfs.c @@ -1322,7 +1322,7 @@ static int insert_positioned_attr_in_mft_record(MFT_RECORD *m, Eprintf("Error writing non-resident attribute value." "\n"); err = ntfs_mapping_pairs_build(vol, (s8*)a + hdr_size + - ((name_len + 7) & ~7), mpa_size, rl, 0); + ((name_len + 7) & ~7), mpa_size, rl, 0, 0); } a->initialized_size = cpu_to_le64(inited_size); if (err < 0 || bw != val_len) { @@ -1512,7 +1512,7 @@ static int insert_non_resident_attr_in_mft_record(MFT_RECORD *m, Eprintf("Error writing non-resident attribute value." "\n"); err = ntfs_mapping_pairs_build(vol, (s8*)a + hdr_size + - ((name_len + 7) & ~7), mpa_size, rl, 0); + ((name_len + 7) & ~7), mpa_size, rl, 0, 0); } if (err < 0 || bw != val_len) { // FIXME: Handle error. diff --git a/ntfsprogs/ntfsmove.c b/ntfsprogs/ntfsmove.c index d805a94f..b4bd34a3 100644 --- a/ntfsprogs/ntfsmove.c +++ b/ntfsprogs/ntfsmove.c @@ -709,7 +709,7 @@ static s64 move_datarun (ntfs_volume *vol, ntfs_inode *ino, ATTR_RECORD *rec, // update data runs ntfs_mapping_pairs_build(vol, ((u8*)rec) + rec->mapping_pairs_offset, - need_to, from, 0); + need_to, from, 0, 0); // commit ntfs_inode_mark_dirty (ino); diff --git a/ntfsprogs/ntfsresize.c b/ntfsprogs/ntfsresize.c index 0de41876..73b42f2a 100644 --- a/ntfsprogs/ntfsresize.c +++ b/ntfsprogs/ntfsresize.c @@ -1157,7 +1157,7 @@ static void replace_attribute_runlist(ntfs_volume *vol, if (!(mp = calloc(1, mp_size))) perr_exit("Couldn't get memory"); - if (ntfs_mapping_pairs_build(vol, mp, mp_size, rl, 0)) + if (ntfs_mapping_pairs_build(vol, mp, mp_size, rl, 0, 0)) perr_exit("ntfs_mapping_pairs_build"); memmove((u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp, mp_size);