Changes by: flatcap
Update of /cvsroot/linux-ntfs/ntfsprogs/libntfs
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21510/libntfs
Modified Files:
Makefile.am bitmap.c dir.c index.c inode.c mft.c volume.c
Added Files:
rich.c tree.c
Log Message:
ntfsrm has been split up and merged into libntfs.
currently it's #ifdef'd out.
tree.c - code for handling directory trees
rich.c - a few helpers without a home (this file will go away soon)
Note: ntfsrm isn't in the build any more (you need to ./configure --enable-rich; make extras). When rm is less intrusive, again, I'll put it back in the build.
--- NEW FILE ---
/**
* rich.c - Temporary junk file. Part of the Linux-NTFS project.
*
* Copyright (c) 2004-2005 Richard Russon
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program/include file is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the Linux-NTFS
* distribution in the file COPYING); if not, write to the Free Software
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef NTFS_RICH
#include <stdlib.h>
#include "rich.h"
#include "layout.h"
/**
* find_attribute - Find an attribute of the given type
* @type: An attribute type, e.g. AT_FILE_NAME
* @ctx: A search context, created using ntfs_get_attr_search_ctx
*
* Using the search context to keep track, find the first/next occurrence of a
* given attribute type.
*
* N.B. This will return a pointer into @mft. As long as the search context
* has been created without an inode, it won't overflow the buffer.
*
* Return: Pointer Success, an attribute was found
* NULL Error, no matching attributes were found
*/
ATTR_RECORD * find_attribute (const ATTR_TYPES type, ntfs_attr_search_ctx *ctx)
{
if (!ctx) {
errno = EINVAL;
return NULL;
}
if (ntfs_attr_lookup(type, NULL, 0, 0, 0, NULL, 0, ctx) != 0) {
Dprintf ("find_attribute didn't find an attribute of type: 0x%02x.\n", type);
return NULL; /* None / no more of that type */
}
Dprintf ("find_attribute found an attribute of type: 0x%02x.\n", type);
return ctx->attr;
}
/**
* find_first_attribute - Find the first attribute of a given type
* @type: An attribute type, e.g. AT_FILE_NAME
* @mft: A buffer containing a raw MFT record
*
* Search through a raw MFT record for an attribute of a given type.
* The return value is a pointer into the MFT record that was supplied.
*
* N.B. This will return a pointer into @mft. The pointer won't stray outside
* the buffer, since we created the search context without an inode.
*
* Return: Pointer Success, an attribute was found
* NULL Error, no matching attributes were found
*/
ATTR_RECORD * find_first_attribute (const ATTR_TYPES type, MFT_RECORD *mft)
{
ntfs_attr_search_ctx *ctx;
ATTR_RECORD *rec;
if (!mft) {
errno = EINVAL;
return NULL;
}
ctx = ntfs_attr_get_search_ctx (NULL, mft);
if (!ctx) {
//XXX Eprintf ("Couldn't create a search context.\n");
return NULL;
}
rec = find_attribute (type, ctx);
ntfs_attr_put_search_ctx (ctx);
if (rec)
Dprintf ("find_first_attribute: found attr of type 0x%02x.\n", type);
else
Dprintf ("find_first_attribute: didn't find attr of type 0x%02x.\n", type);
return rec;
}
/**
* ntfs_name_print
*/
void ntfs_name_print (ntfschar *name, int name_len)
{
char *buffer = NULL;
if (name_len) {
ntfs_ucstombs (name, name_len, &buffer, 0);
printf ("%s", buffer);
free (buffer);
} else {
printf ("!");
}
}
/**
* utils_free_non_residents3
*/
int utils_free_non_residents3 (struct ntfs_bmp *bmp, ntfs_inode *inode, ATTR_RECORD *attr)
{
ntfs_attr *na;
runlist_element *rl;
LCN size;
LCN count;
if (!bmp)
return 1;
if (!inode)
return 1;
if (!attr)
return 1;
if (!attr->non_resident)
return 0;
na = ntfs_attr_open (inode, attr->type, NULL, 0);
if (!na)
return 1;
ntfs_attr_map_whole_runlist (na);
rl = na->rl;
size = na->allocated_size >> inode->vol->cluster_size_bits;
for (count = 0; count < size; count += rl->length, rl++) {
if (ntfs_bmp_set_range (bmp, rl->lcn, rl->length, 0) < 0) {
printf (RED "set range : %lld - %lld FAILED\n" END, rl->lcn, rl->lcn+rl->length-1);
}
}
ntfs_attr_close (na);
return 0;
}
/**
* utils_free_non_residents2
*/
int utils_free_non_residents2 (ntfs_inode *inode, struct ntfs_bmp *bmp)
{
ntfs_attr_search_ctx *ctx;
if (!inode)
return -1;
if (!bmp)
return -1;
ctx = ntfs_attr_get_search_ctx (NULL, inode->mrec);
if (!ctx) {
printf ("can't create a search context\n");
return -1;
}
while (ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx) == 0) {
utils_free_non_residents3 (bmp, inode, ctx->attr);
}
ntfs_attr_put_search_ctx (ctx);
return 0;
}
#endif /* NTFS_RICH */
--- NEW FILE ---
/**
* tree.c - Directory tree handling code. Part of the Linux-NTFS project.
*
* Copyright (c) 2004-2005 Richard Russon
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program/include file is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the Linux-NTFS
* distribution in the file COPYING); if not, write to the Free Software
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
[...2080 lines suppressed...]
// ie = insert
// child = child
// suc = successor
// suc_num = insert point
ie = med_ie;
suc = suc->parent;
suc_num = 0;
printf ("\n");
printf (BOLD YELLOW "Ascend\n" END);
goto ascend;
done:
return 0;
}
#endif /* NTFS_RICH */
Index: Makefile.am
===================================================================
RCS file: /cvsroot/linux-ntfs/ntfsprogs/libntfs/Makefile.am,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -p -r1.33 -r1.34
--- Makefile.am 16 Oct 2005 21:57:00 -0000 1.33
+++ Makefile.am 16 Oct 2005 23:33:04 -0000 1.34
@@ -60,6 +60,10 @@ libntfs_la_SOURCES = \
version.c \
volume.c
+if ENABLE_RICH
+libntfs_la_SOURCES += rich.c tree.c
+endif
+
if ENABLE_GNOME_VFS
gnomevfsmoduleslibdir = $(libdir)/gnome-vfs-2.0/modules
Index: bitmap.c
===================================================================
RCS file: /cvsroot/linux-ntfs/ntfsprogs/libntfs/bitmap.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -p -r1.13 -r1.14
--- bitmap.c 28 Sep 2005 13:47:48 -0000 1.13
+++ bitmap.c 16 Oct 2005 23:33:04 -0000 1.14
@@ -2,6 +2,7 @@
* bitmap.c - Bitmap handling code. Part of the Linux-NTFS project.
*
* Copyright (c) 2002-2004 Anton Altaparmakov
+ * Copyright (c) 2004-2005 Richard Russon
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
@@ -220,3 +221,388 @@ int ntfs_bitmap_clear_run(ntfs_attr *na,
{
return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0);
}
+
+
+#ifdef NTFS_RICH
+
+#include <stdlib.h>
+
+#include "layout.h"
+#include "volume.h"
+#include "bitmap.h"
+#include "rich.h"
+
+/**
+ * ntfs_bmp_rollback
+ */
+int ntfs_bmp_rollback (struct ntfs_bmp *bmp)
+{
+ int i;
+
+ if ((!bmp) || (bmp->count == 0))
+ return 0;
+
+ for (i = 0; i < bmp->count; i++)
+ free (bmp->data[i]);
+
+ free (bmp->data);
+ free (bmp->data_vcn);
+ bmp->data = NULL;
+ bmp->data_vcn = NULL;
+ bmp->count = 0;
+
+ return 0;
+}
+
+/**
+ * ntfs_bmp_commit
+ */
+int ntfs_bmp_commit (struct ntfs_bmp *bmp)
+{
+ int i;
+ u32 cs;
+#ifdef RM_WRITE
+ u32 ws; // write size
+#endif
+
+ if (!bmp)
+ return 0;
+ if (bmp->count == 0)
+ return 0;
+
+#if 0
+ printf ("attr = 0x%02X\n", bmp->attr->type);
+ printf ("resident = %d\n", !NAttrNonResident (bmp->attr));
+ printf ("\ta size = %lld\n", bmp->attr->allocated_size);
+ printf ("\td size = %lld\n", bmp->attr->data_size);
+ printf ("\ti size = %lld\n", bmp->attr->initialized_size);
+#endif
+
+ printf ("commit bmp inode %lld, 0x%02X (%sresident)\n", bmp->attr->ni->mft_no, bmp->attr->type, NAttrNonResident (bmp->attr) ? "non-" : "");
+
+ if (NAttrNonResident (bmp->attr)) {
+ cs = bmp->vol->cluster_size;
+
+ // non-resident
+ for (i = 0; i < bmp->count; i++) {
+#ifdef RM_WRITE
+ if (((bmp->data_vcn[i]+1) * cs) < bmp->attr->data_size)
+ ws = cs;
+ else
+ ws = bmp->attr->data_size & (cs - 1);
+ //printf ("writing %d bytes\n", ws);
+ ntfs_attr_pwrite (bmp->attr, bmp->data_vcn[i] * cs, ws, bmp->data[i]); // XXX retval
+#endif
+ printf (RED "\tntfs_attr_pwrite (vcn %lld)\n" END, bmp->data_vcn[i]);
+ }
+ } else {
+ // resident
+#ifdef RM_WRITE
+ ntfs_attr_pwrite (bmp->attr, bmp->data_vcn[0], bmp->attr->data_size, bmp->data[0]); // XXX retval
+#endif
+ printf (RED "\tntfs_attr_pwrite resident (%lld)\n" END, bmp->attr->data_size);
+ }
+
+ ntfs_bmp_rollback (bmp);
+
+ return 0;
+}
+
+/**
+ * ntfs_bmp_free
+ */
+void ntfs_bmp_free (struct ntfs_bmp *bmp)
+{
+ if (!bmp)
+ return;
+
+ ntfs_bmp_rollback (bmp);
+
+ ntfs_attr_close (bmp->attr);
+
+ free (bmp);
+}
+
+/**
+ * ntfs_bmp_create
+ */
+struct ntfs_bmp * ntfs_bmp_create (ntfs_inode *inode, ATTR_TYPES type, ntfschar *name, int name_len)
+{
+ struct ntfs_bmp *bmp;
+ ntfs_attr *attr;
+
+ if (!inode)
+ return NULL;
+
+ attr = ntfs_attr_open (inode, type, name, name_len);
+ if (!attr)
+ return NULL;
+
+ bmp = calloc (1, sizeof (*bmp));
+ if (!bmp) {
+ ntfs_attr_close (attr);
+ return NULL;
+ }
+
+ bmp->vol = inode->vol;
+ bmp->attr = attr;
+ bmp->data = NULL;
+ bmp->data_vcn = NULL;
+ bmp->count = 0;
+
+ return bmp;
+}
+
+/**
+ * ntfs_bmp_add_data
+ */
+int ntfs_bmp_add_data (struct ntfs_bmp *bmp, VCN vcn, u8 *data)
+{
+ int i = 0;
+ int old;
+ int new;
+
+ if (!bmp || !data)
+ return -1;
+
+ old = ROUND_UP (bmp->count, 16);
+ bmp->count++;
+ new = ROUND_UP (bmp->count, 16);
+
+ if (old != new) {
+ bmp->data = realloc (bmp->data, new * sizeof (*bmp->data));
+ bmp->data_vcn = realloc (bmp->data_vcn , new * sizeof (*bmp->data_vcn));
+ }
+
+ for (i = 0; i < bmp->count-1; i++)
+ if (bmp->data_vcn[i] > vcn)
+ break;
+
+ if ((bmp->count-i) > 0) {
+ memmove (&bmp->data[i+1], &bmp->data[i], (bmp->count-i) * sizeof (*bmp->data));
+ memmove (&bmp->data_vcn[i+1], &bmp->data_vcn[i], (bmp->count-i) * sizeof (*bmp->data_vcn));
+ }
+
+ bmp->data[i] = data;
+ bmp->data_vcn[i] = vcn;
+
+ return bmp->count;
+}
+
+/**
+ * ntfs_bmp_get_data
+ */
+u8 * ntfs_bmp_get_data (struct ntfs_bmp *bmp, VCN vcn)
+{
+ u8 *buffer;
+ int i;
+ int cs;
+ int cb;
+
+ if (!bmp)
+ return NULL;
+
+ cs = bmp->vol->cluster_size;
+ cb = bmp->vol->cluster_size_bits;
+
+ // XXX range check against vol,attr
+ // never compressed, so data = init
+
+ vcn >>= (cb + 3); // convert to bitmap clusters
+
+ for (i = 0; i < bmp->count; i++) {
+ if (vcn == bmp->data_vcn[i]) {
+ //printf ("reusing bitmap cluster %lld\n", vcn);
+ return bmp->data[i];
+ }
+ }
+
+ buffer = calloc (1, cs); // XXX could be smaller if attr size < cluster size
+ if (!buffer)
+ return NULL;
+
+ //printf ("loading from bitmap cluster %lld\n", vcn);
+ //printf ("loading from bitmap byte %lld\n", vcn<<cb);
+ if (ntfs_attr_pread (bmp->attr, vcn<<cb, cs, buffer) < 0) {
+ free (buffer);
+ return NULL;
+ }
+
+ ntfs_bmp_add_data (bmp, vcn, buffer); // XXX retval
+ return buffer;
+}
+
+/**
+ * ntfs_bmp_set_range
+ */
+int ntfs_bmp_set_range (struct ntfs_bmp *bmp, VCN vcn, s64 length, int value)
+{
+ // shouldn't all the vcns be lcns?
+ s64 i;
+ u8 *buffer;
+ int csib; // cluster size in bits
+
+ int block_start, block_finish; // rename to c[sf] (rename to clust_)
+ int vcn_start, vcn_finish; // rename to v[sf]
+ int byte_start, byte_finish; // rename to b[sf]
+ u8 mask_start, mask_finish; // rename to m[sf]
+
+ s64 a,b;
+
+ if (!bmp)
+ return -1;
+
+ if (value)
+ value = 0xFF;
+
+ csib = bmp->vol->cluster_size << 3;
+
+ vcn_start = vcn;
+ vcn_finish = vcn + length - 1;
+
+ //printf ("vcn_start = %d, vcn_finish = %d\n", vcn_start, vcn_finish);
+ a = ROUND_DOWN (vcn_start, csib);
+ b = ROUND_DOWN (vcn_finish, csib) + 1;
+
+ //printf ("a = %lld, b = %lld\n", a, b);
+
+ for (i = a; i < b; i += csib) {
+ //printf ("ntfs_bmp_get_data %lld\n", i);
+ buffer = ntfs_bmp_get_data (bmp, i);
+ if (!buffer)
+ return -1;
+
+ block_start = i;
+ block_finish = block_start + csib - 1;
+
+ mask_start = (0xFF << (vcn_start & 7));
+ mask_finish = (0xFF >> (7 - (vcn_finish & 7)));
+
+ if ((vcn_start >= block_start) && (vcn_start <= block_finish)) {
+ byte_start = (vcn_start - block_start) >> 3;
+ } else {
+ byte_start = 0;
+ mask_start = 0xFF;
+ }
+
+ if ((vcn_finish >= block_start) && (vcn_finish <= block_finish)) {
+ byte_finish = (vcn_finish - block_start) >> 3;
+ } else {
+ byte_finish = bmp->vol->cluster_size - 1;
+ mask_finish = 0xFF;
+ }
+
+ if ((byte_finish - byte_start) > 1) {
+ memset (buffer+byte_start+1, value, byte_finish-byte_start-1);
+ } else if (byte_finish == byte_start) {
+ mask_start &= mask_finish;
+ mask_finish = 0x00;
+ }
+
+ if (value) {
+ buffer[byte_start] |= mask_start;
+ buffer[byte_finish] |= mask_finish;
+ } else {
+ buffer[byte_start] &= (~mask_start);
+ buffer[byte_finish] &= (~mask_finish);
+ }
+ }
+
+#if 1
+ printf (GREEN "Modified: inode %lld, ", bmp->attr->ni->mft_no);
+ switch (bmp->attr->type) {
+ case AT_BITMAP: printf ("$BITMAP"); break;
+ case AT_DATA: printf ("$DATA"); break;
+ default: break;
+ }
+ printf (" vcn %lld-%lld\n" END, vcn>>12, (vcn+length-1)>>12);
+#endif
+ return 1;
+}
+
+/**
+ * ntfs_bmp_find_last_set
+ */
+s64 ntfs_bmp_find_last_set (struct ntfs_bmp *bmp)
+{
+ s64 clust_count;
+ s64 byte_count;
+ s64 clust;
+ int byte;
+ int bit;
+ int note;
+ u8 *buffer;
+
+ if (!bmp)
+ return -2;
+
+ // find byte size of bmp
+ // find cluster size of bmp
+
+ byte_count = bmp->attr->data_size;
+ clust_count = ROUND_UP (byte_count, bmp->vol->cluster_size) >> bmp->vol->cluster_size_bits;
+
+ //printf ("bitmap = %lld bytes\n", byte_count);
+ //printf ("bitmap = %lld buffers\n", clust_count);
+
+ // for each cluster backwards
+ for (clust = clust_count-1; clust >= 0; clust--) {
+ //printf ("cluster %lld\n", clust);
+ //printf ("get vcn %lld\n", clust << (bmp->vol->cluster_size_bits + 3));
+ buffer = ntfs_bmp_get_data (bmp, clust << (bmp->vol->cluster_size_bits + 3));
+ //utils_dump_mem (buffer, 0, 8, DM_NO_ASCII);
+ if (!buffer)
+ return -2;
+ if ((clust == (clust_count-1) && ((byte_count % bmp->vol->cluster_size) != 0))) {
+ byte = byte_count % bmp->vol->cluster_size;
+ } else {
+ byte = bmp->vol->cluster_size;
+ }
+ //printf ("start byte = %d\n", byte);
+ // for each byte backward
+ for (byte--; byte >= 0; byte--) {
+ //printf ("\tbyte %d (%d)\n", byte, buffer[byte]);
+ // for each bit shift up
+ note = -1;
+ for (bit = 7; bit >= 0; bit--) {
+ //printf ("\t\tbit %d (%d)\n", (1<<bit), buffer[byte] & (1<<bit));
+ if (buffer[byte] & (1<<bit)) {
+ // if set, keep note
+ note = bit;
+ break;
+ }
+ }
+ if (note >= 0) {
+ // if note, return value
+ //printf ("match %lld (c=%lld,b=%d,n=%d)\n", (((clust << bmp->vol->cluster_size_bits) + byte) << 3) + note, clust, byte, note);
+ return ((((clust << bmp->vol->cluster_size_bits) + byte) << 3) + note);
+ }
+ }
+ }
+
+ return -1;
+}
+
+/**
+ * ntfs_bmp_find_space
+ */
+int ntfs_bmp_find_space (struct ntfs_bmp *bmp, LCN start, long size)
+{
+ if (!bmp)
+ return 0;
+
+ start = 0;
+ size = 0;
+
+ /*
+ bmp find space - uncached bmp's
+ $Bitmap/$DATA free space on volume
+ dir/$BITMAP free index record
+ $MFT/$BITMAP free record in mft
+ */
+ return 0;
+}
+
+
+#endif /* NTFS_RICH */
+
Index: dir.c
===================================================================
RCS file: /cvsroot/linux-ntfs/ntfsprogs/libntfs/dir.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -p -r1.28 -r1.29
--- dir.c 16 Oct 2005 19:21:47 -0000 1.28
+++ dir.c 16 Oct 2005 23:33:04 -0000 1.29
@@ -3,6 +3,7 @@
*
* Copyright (c) 2002-2005 Anton Altaparmakov
* Copyright (c) 2005 Yura Pakhuchiy
+ * Copyright (c) 2004-2005 Richard Russon
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
@@ -1506,3 +1507,391 @@ err_out:
errno = err;
return -1;
}
+
+
+#ifdef NTFS_RICH
+
+#include <stdlib.h>
+
+#include "layout.h"
+#include "volume.h"
+#include "inode.h"
+#include "dir.h"
+#include "tree.h"
+#include "bitmap.h"
+#include "index.h"
+#include "rich.h"
+
+/**
+ * ntfs_dir_rollback
+ */
+int ntfs_dir_rollback (struct ntfs_dir *dir)
+{
+ int i;
+
+ if (!dir)
+ return -1;
+
+ if (ntfs_dt_rollback (dir->index) < 0)
+ return -1;
+
+ if (ntfs_bmp_rollback (dir->bitmap) < 0)
+ return -1;
+
+ for (i = 0; i < dir->child_count; i++) {
+ if (ntfs_dir_rollback (dir->children[i]) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+/**
+ * ntfs_dir_truncate
+ */
+int ntfs_dir_truncate (ntfs_volume *vol, struct ntfs_dir *dir)
+{
+ //int i;
+ //u8 *buffer;
+ //int buf_count;
+ s64 last_bit;
+ INDEX_ENTRY *ie;
+
+ if (!vol || !dir)
+ return -1;
+
+ if ((dir->ialloc == NULL) || (dir->bitmap == NULL))
+ return 0;
+
+#if 0
+ buf_count = ROUND_UP (dir->bitmap->attr->allocated_size, vol->cluster_size) >> vol->cluster_size_bits;
+ printf ("alloc = %lld bytes\n", dir->ialloc->allocated_size);
+ printf ("alloc = %lld clusters\n", dir->ialloc->allocated_size >> vol->cluster_size_bits);
+ printf ("bitmap bytes 0 to %lld\n", ((dir->ialloc->allocated_size >> vol->cluster_size_bits)-1)>>3);
+ printf ("bitmap = %p\n", dir->bitmap);
+ printf ("bitmap = %lld bytes\n", dir->bitmap->attr->allocated_size);
+ printf ("bitmap = %d buffers\n", buf_count);
+#endif
+
+ last_bit = ntfs_bmp_find_last_set (dir->bitmap);
+ if (dir->ialloc->allocated_size == (dir->index_size * (last_bit + 1))) {
+ //printf ("nothing to do\n");
+ return 0;
+ }
+
+ printf (BOLD YELLOW "Truncation needed\n" END);
+
+#if 0
+ printf ("\tlast bit = %lld\n", last_bit);
+ printf ("\tactual IALLOC size = %lld\n", dir->ialloc->allocated_size);
+ printf ("\tshould IALLOC size = %lld\n", dir->index_size * (last_bit + 1));
+#endif
+
+ if ((dir->index_size * (last_bit + 1)) == 0) {
+ printf ("root dt %d, vcn = %lld\n", dir->index->changed, dir->index->vcn);
+ //rollback all dts
+ //ntfs_dt_rollback (dir->index);
+ //dir->index = NULL;
+ // What about the ROOT dt?
+
+ ie = ntfs_ie_copy (dir->index->children[0]);
+ if (!ie) {
+ printf (RED "IE copy failed\n" END);
+ return -1;
+ }
+
+ ie = ntfs_ie_remove_vcn (ie);
+ if (!ie) {
+ printf (RED "IE remove vcn failed\n" END);
+ return -1;
+ }
+
+ //utils_dump_mem (dir->index->data, 0, dir->index->data_len, DM_DEFAULTS); printf ("\n");
+ //utils_dump_mem (ie, 0, ie->length, DM_DEFAULTS); printf ("\n");
+ ntfs_dt_root_replace (dir->index, 0, dir->index->children[0], ie);
+ //utils_dump_mem (dir->index->data, 0, dir->index->data_len, DM_DEFAULTS); printf ("\n");
+ //printf ("root dt %d, vcn = %lld\n", dir->index->changed, dir->index->vcn);
+
+ ntfs_ie_free (ie);
+ ie = NULL;
+
+ //index flags remove LARGE_INDEX
+ dir->index->header->flags = 0;
+
+ //rollback dir's bmp
+ ntfs_bmp_free (dir->bitmap);
+ dir->bitmap = NULL;
+
+ /*
+ for (i = 0; i < dir->index->child_count; i++) {
+ ntfs_dt_rollback (dir->index->sub_nodes[i]);
+ dir->index->sub_nodes[i] = NULL;
+ }
+ */
+
+ //printf ("dir->index->inodes[0] = %p\n", dir->index->inodes[0]);
+
+ //remove 0xA0 attribute
+ ntfs_mft_remove_attr (vol->private_bmp2, dir->inode, AT_INDEX_ALLOCATION);
+
+ //remove 0xB0 attribute
+ ntfs_mft_remove_attr (vol->private_bmp2, dir->inode, AT_BITMAP);
+ } else {
+ printf (RED "Cannot shrink directory\n" END);
+ //ntfs_dir_shrink_alloc
+ //ntfs_dir_shrink_bitmap
+ //make bitmap resident?
+ }
+
+ /*
+ * Remove
+ * dt -> dead
+ * bitmap updated
+ * rollback dead dts
+ * commit bitmap
+ * commit dts
+ * commit dir
+ */
+ /*
+ * Reuse
+ * search for lowest dead
+ * update bitmap
+ * init dt
+ * remove from dead
+ * insert into tree
+ * init INDX
+ */
+
+#if 0
+ buffer = ntfs_bmp_get_data (dir->bitmap, 0);
+ if (!buffer)
+ return -1;
+
+ utils_dump_mem (buffer, 0, 8, DM_NO_ASCII);
+ for (i = buf_count-1; i >= 0; i--) {
+ if (buffer[i]) {
+ printf ("alloc in use\n");
+ return 0;
+ }
+ }
+#endif
+
+ // <dir>/$BITMAP($I30)
+ // <dir>/$INDEX_ALLOCATION($I30)
+ // $Bitmap
+
+ // Find the highest set bit in the directory bitmap
+ // can we free any clusters of the alloc?
+ // if yes, resize attribute
+
+ // Are *any* bits set?
+ // If not remove ialloc
+
+ return 0;
+}
+
+/**
+ * ntfs_dir_commit
+ */
+int ntfs_dir_commit (struct ntfs_dir *dir)
+{
+ int i;
+
+ if (!dir)
+ return 0;
+
+ printf ("commit dir inode %llu\n", dir->inode->mft_no);
+ if (NInoDirty (dir->inode)) {
+#ifdef RM_WRITE
+ ntfs_inode_sync (dir->inode);
+#endif
+ printf (RED "\tntfs_inode_sync %llu\n" END, dir->inode->mft_no);
+ }
+
+ if (ntfs_dt_commit (dir->index) < 0)
+ return -1;
+
+ if (ntfs_bmp_commit (dir->bitmap) < 0)
+ return -1;
+
+ for (i = 0; i < dir->child_count; i++) {
+ if (ntfs_dir_commit (dir->children[i]) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+/**
+ * ntfs_dir_free
+ */
+void ntfs_dir_free (struct ntfs_dir *dir)
+{
+ struct ntfs_dir *parent;
+ int i;
+
+ if (!dir)
+ return;
+
+ ntfs_dir_rollback (dir);
+
+ parent = dir->parent;
+ if (parent) {
+ for (i = 0; i < parent->child_count; i++) {
+ if (parent->children[i] == dir) {
+ parent->children[i] = NULL;
+ }
+ }
+ }
+
+ ntfs_attr_close (dir->iroot);
+ ntfs_attr_close (dir->ialloc);
+ ntfs_inode_close2 (dir->inode);
+
+ ntfs_dt_free (dir->index);
+ ntfs_bmp_free (dir->bitmap);
+
+ for (i = 0; i < dir->child_count; i++)
+ ntfs_dir_free (dir->children[i]);
+
+ free (dir->name);
+ free (dir->children);
+ free (dir);
+}
+
+/**
+ * ntfs_dir_create
+ */
+struct ntfs_dir * ntfs_dir_create (ntfs_volume *vol, MFT_REF mft_num)
+{
+ struct ntfs_dir *dir = NULL;
+ ntfs_inode *inode = NULL;
+ ATTR_RECORD *rec = NULL;
+ INDEX_ROOT *ir = NULL;
+ FILE_NAME_ATTR *name = NULL;
+
+ if (!vol)
+ return NULL;
+
+ //printf ("ntfs_dir_create %lld\n", MREF (mft_num));
+ inode = ntfs_inode_open2 (vol, mft_num);
+ if (!inode)
+ return NULL;
+
+ dir = calloc (1, sizeof (*dir));
+ if (!dir) {
+ ntfs_inode_close2 (inode);
+ return NULL;
+ }
+
+ dir->inode = inode;
+ dir->iroot = ntfs_attr_open (inode, AT_INDEX_ROOT, I30, 4);
+ dir->ialloc = ntfs_attr_open (inode, AT_INDEX_ALLOCATION, I30, 4);
+
+ if (!dir->iroot) {
+ ntfs_dir_free (dir);
+ return NULL;
+ }
+
+ dir->vol = vol;
+ dir->parent = NULL;
+ dir->name = NULL;
+ dir->name_len = 0;
+ dir->index = NULL;
+ dir->children = NULL;
+ dir->child_count = 0;
+ dir->mft_num = mft_num;
+
+ // This may not exist
+ dir->bitmap = ntfs_bmp_create (inode, AT_BITMAP, I30, 4);
+
+ if (dir->iroot) {
+ rec = find_first_attribute (AT_INDEX_ROOT, inode->mrec);
+ ir = (INDEX_ROOT*) ((u8*)rec + rec->value_offset);
+ dir->index_size = ir->index_block_size;
+ } else {
+ // XXX !iroot?
+ dir->index_size = 0;
+ }
+
+ // Finally, find the dir's name
+ rec = find_first_attribute (AT_FILE_NAME, inode->mrec);
+ name = (FILE_NAME_ATTR*) ((u8*)rec + rec->value_offset);
+
+ dir->name_len = name->file_name_length;
+ dir->name = malloc (sizeof (ntfschar) * dir->name_len);
+ memcpy (dir->name, name->file_name, sizeof (ntfschar) * dir->name_len);
+
+ return dir;
+}
+
+/**
+ * ntfs_dir_add
+ */
+void ntfs_dir_add (struct ntfs_dir *parent, struct ntfs_dir *child)
+{
+ if (!parent || !child)
+ return;
+
+ parent->child_count++;
+ //printf ("child count = %d\n", parent->child_count);
+ parent->children = realloc (parent->children, parent->child_count * sizeof (struct ntfs_dir*));
+ child->parent = parent;
+
+ parent->children[parent->child_count-1] = child;
+}
+
+/**
+ * ntfs_dir_find2
+ */
+struct ntfs_dir * ntfs_dir_find2 (struct ntfs_dir *dir, ntfschar *name, int name_len)
+{
+ int i;
+ struct ntfs_dir *child = NULL;
+ struct ntfs_dt *dt = NULL;
+ int dt_num = 0;
+ INDEX_ENTRY *ie;
+ MFT_REF mft_num;
+
+ if (!dir || !name)
+ return NULL;
+
+ if (!dir->index) { // XXX when will this happen?
+ printf ("ntfs_dir_find2 - directory has no index\n");
+ return NULL;
+ }
+
+ for (i = 0; i < dir->child_count; i++) {
+ if (0 == ntfs_names_collate (name, name_len,
+ dir->children[i]->name,
+ dir->children[i]->name_len,
+ 2, IGNORE_CASE,
+ dir->vol->upcase,
+ dir->vol->upcase_len))
+ return dir->children[i];
+ }
+
+ dt = ntfs_dt_find2 (dir->index, name, name_len, &dt_num);
+ if (!dt) {
+ printf ("can't find name in dir\n");
+ return NULL;
+ }
+
+ ie = dt->children[dt_num];
+
+ mft_num = ie->indexed_file;
+
+ child = ntfs_dir_create (dir->vol, mft_num);
+ if (!child)
+ return NULL;
+
+ child->index = ntfs_dt_create (child, NULL, -1);
+
+ ntfs_dir_add (dir, child);
+
+ return child;
+}
+
+
+#endif /* NTFS_RICH */
+
Index: index.c
===================================================================
RCS file: /cvsroot/linux-ntfs/ntfsprogs/libntfs/index.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -p -r1.14 -r1.15
--- index.c 16 Oct 2005 00:07:14 -0000 1.14
+++ index.c 16 Oct 2005 23:33:04 -0000 1.15
@@ -3,6 +3,7 @@
*
* Copyright (c) 2004-2005 Anton Altaparmakov
* Copyright (c) 2005 Yura Pakhuchiy
+ * Copyright (c) 2004-2005 Richard Russon
*
* This program/include file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
@@ -642,3 +643,228 @@ err_out:
errno = err;
return -1;
}
+
+
+#ifdef NTFS_RICH
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "index.h"
+#include "rich.h"
+
+/**
+ * ntfs_ie_free
+ */
+void ntfs_ie_free (INDEX_ENTRY *ie)
+{
+ free (ie);
+}
+
+/**
+ * ntfs_ie_create
+ */
+INDEX_ENTRY * ntfs_ie_create (void)
+{
+ int length;
+ INDEX_ENTRY *ie;
+
+ length = 16;
+ ie = calloc (1, length);
+ if (!ie)
+ return NULL;
+
+ ie->indexed_file = 0;
+ ie->length = length;
+ ie->key_length = 0;
+ ie->flags = INDEX_ENTRY_END;
+ ie->reserved = 0;
+ return ie;
+}
+
+/**
+ * ntfs_ie_get_vcn
+ */
+VCN ntfs_ie_get_vcn (INDEX_ENTRY *ie)
+{
+ if (!ie)
+ return -1;
+ if (!(ie->flags & INDEX_ENTRY_NODE))
+ return -1;
+
+ return *((VCN*) ((u8*) ie + ie->length - 8));
+}
+
+/**
+ * ntfs_ie_copy
+ */
+INDEX_ENTRY * ntfs_ie_copy (INDEX_ENTRY *ie)
+{
+ INDEX_ENTRY *copy = NULL;
+
+ if (!ie)
+ return NULL;
+
+ copy = malloc (ie->length);
+ if (!copy)
+ return NULL;
+ memcpy (copy, ie, ie->length);
+
+ return copy;
+}
+
+/**
+ * ntfs_ie_set_vcn
+ */
+INDEX_ENTRY * ntfs_ie_set_vcn (INDEX_ENTRY *ie, VCN vcn)
+{
+ if (!ie)
+ return 0;
+
+ if (!(ie->flags & INDEX_ENTRY_NODE)) {
+ ie->length += 8;
+ ie = realloc (ie, ie->length);
+ if (!ie)
+ return NULL;
+
+ ie->flags |= INDEX_ENTRY_NODE;
+ }
+
+ *((VCN*) ((u8*) ie + ie->length - 8)) = vcn;
+ return ie;
+}
+
+/**
+ * ntfs_ie_remove_vcn
+ */
+INDEX_ENTRY * ntfs_ie_remove_vcn (INDEX_ENTRY *ie)
+{
+ if (!ie)
+ return NULL;
+ if (!(ie->flags & INDEX_ENTRY_NODE))
+ return ie;
+
+ ie->length -= 8;
+ ie->flags &= ~INDEX_ENTRY_NODE;
+ ie = realloc (ie, ie->length);
+ return ie;
+}
+
+/**
+ * ntfs_ie_set_name
+ */
+INDEX_ENTRY * ntfs_ie_set_name (INDEX_ENTRY *ie, ntfschar *name, int namelen, FILE_NAME_TYPE_FLAGS nametype)
+{
+ FILE_NAME_ATTR *file;
+ int need;
+ BOOL wipe = FALSE;
+ VCN vcn = 0;
+
+ if (!ie || !name)
+ return NULL;
+
+ /*
+ * INDEX_ENTRY
+ * MFT_REF indexed_file;
+ * u16 length;
+ * u16 key_length;
+ * INDEX_ENTRY_FLAGS flags;
+ * u16 reserved;
+ *
+ * FILENAME
+ * MFT_REF parent_directory;
+ * s64 creation_time;
+ * s64 last_data_change_time;
+ * s64 last_mft_change_time;
+ * s64 last_access_time;
+ * s64 allocated_size;
+ * s64 data_size;
+ * FILE_ATTR_FLAGS file_attributes;
+ * u32 reserved;
+ * u8 file_name_length;
+ * FILE_NAME_TYPE_FLAGS file_name_type;
+ * ntfschar file_name[l];
+ * u8 reserved[n]
+ *
+ * VCN vcn;
+ */
+
+ //printf ("key length = 0x%02X\n", ie->key_length);
+ //printf ("new name length = %d\n", namelen);
+ if (ie->key_length > 0) {
+ file = &ie->key.file_name;
+ //printf ("filename, length %d\n", file->file_name_length);
+ need = ATTR_SIZE (namelen * sizeof (ntfschar) + 2) -
+ ATTR_SIZE (file->file_name_length * sizeof (ntfschar) + 2);
+ } else {
+ //printf ("no filename\n");
+ need = ATTR_SIZE (sizeof (FILE_NAME_ATTR) + (namelen * sizeof (ntfschar)));
+ wipe = TRUE;
+ }
+
+ //printf ("need 0x%02X bytes\n", need);
+
+ if (need != 0) {
+ if (ie->flags & INDEX_ENTRY_NODE)
+ vcn = ntfs_ie_get_vcn (ie);
+
+ ie->length += need;
+ ie->key_length += need;
+
+ //printf ("realloc 0x%02X\n", ie->length);
+ ie = realloc (ie, ie->length);
+ if (!ie)
+ return NULL;
+
+ if (ie->flags & INDEX_ENTRY_NODE)
+ ie = ntfs_ie_set_vcn (ie, vcn);
+
+ if (wipe)
+ memset (&ie->key.file_name, 0, sizeof (FILE_NAME_ATTR));
+ if (need > 0)
+ memset ((u8*)ie + ie->length - need, 0, need);
+ }
+
+ memcpy (ie->key.file_name.file_name, name, namelen * sizeof (ntfschar));
+
+ ie->key.file_name.file_name_length = namelen;
+ ie->key.file_name.file_name_type = nametype;
+ ie->flags &= ~INDEX_ENTRY_END;
+
+ //printf ("ie->length = 0x%02X\n", ie->length);
+ //printf ("ie->key_length = 0x%02X\n", ie->key_length);
+
+ return ie;
+}
+
+/**
+ * ntfs_ie_remove_n...
[truncated message content] |