From 2c942cb3f4464321e3e75a5fe838b81b9ca648ee Mon Sep 17 00:00:00 2001 From: flatcap Date: Wed, 13 Jul 2005 22:55:01 +0000 Subject: [PATCH] tidy up dt rollback and commit --- ntfsprogs/ntfsrm.c | 97 +++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/ntfsprogs/ntfsrm.c b/ntfsprogs/ntfsrm.c index 55651471..b6e4384d 100644 --- a/ntfsprogs/ntfsrm.c +++ b/ntfsprogs/ntfsrm.c @@ -1045,6 +1045,28 @@ static int ntfs_ie_test (void) } +/** + * ntfs_dt_free + */ +static void ntfs_dt_free (struct ntfs_dt *dt) +{ + int i; + + if (!dt) + return; + + for (i = 0; i < dt->child_count; i++) { + ntfs_dt_free (dt->sub_nodes[i]); + ntfs_inode_close2 (dt->inodes[i]); + } + + free (dt->sub_nodes); + free (dt->children); + free (dt->inodes); + free (dt->data); // XXX is this always ours? + free (dt); +} + /** * ntfs_dt_rollback */ @@ -1053,28 +1075,41 @@ static int ntfs_dt_rollback (struct ntfs_dt *dt) int i; if (!dt) - return -1; + return 0; + if (dt->child_count == 0) // No children or nothing mapped + return 0; - return 0; // TEMP - - for (i = 0; i < dt->child_count; i++) { - if (dt->sub_nodes) - ntfs_dt_rollback (dt->sub_nodes[i]); - if (dt->inodes) + if (dt->changed) { + // We can't trust anything below us in the tree + for (i = 0; i < dt->child_count; i++) { + ntfs_dt_free (dt->sub_nodes[i]); ntfs_inode_close2 (dt->inodes[i]); + } + + dt->child_count = 0; + + free (dt->data); + free (dt->children); + free (dt->sub_nodes); + free (dt->inodes); + + dt->data = NULL; + dt->children = NULL; + dt->sub_nodes = NULL; + dt->inodes = NULL; + } else { + // This node is OK, check the su-nodes + for (i = 0; i < dt->child_count; i++) { + if (ntfs_dt_rollback (dt->sub_nodes[i])) { + ntfs_inode_close2 (dt->inodes[i]); + // Child was changed so unmap it + dt->sub_nodes[i] = NULL; + dt->inodes[i] = NULL; + } + } } - free (dt->data); - free (dt->children); - free (dt->sub_nodes); - free (dt->inodes); - - dt->data = NULL; - dt->children = NULL; - dt->sub_nodes = NULL; - dt->inodes = NULL; - - return 0; + return (dt->child_count == 0); } /** @@ -1136,32 +1171,6 @@ static int ntfs_dt_commit (struct ntfs_dt *dt) return 0; } -/** - * ntfs_dt_free - */ -static void ntfs_dt_free (struct ntfs_dt *dt) -{ - int i; - - if (!dt) - return; - - ntfs_dt_rollback (dt); - - for (i = 0; i < dt->child_count; i++) { - //if (dt->sub_nodes) - ntfs_dt_free (dt->sub_nodes[i]); - //if (dt->inodes) - ntfs_inode_close2 (dt->inodes[i]); - } - - free (dt->sub_nodes); - free (dt->children); - free (dt->inodes); - free (dt->data); // XXX is this always ours? - free (dt); -} - /** * ntfs_dt_alloc_children */