Changes by: mattjf
Update of /cvsroot/linux-ntfs/linux-ntfs/ntfstools
In directory usw-pr-cvs1:/tmp/cvs-serv30059/ntfstools
Modified Files:
ntfslabel.c
Log Message:
Changes to ntfslabel - still doesn't work yet
Index: ntfslabel.c
===================================================================
RCS file: /cvsroot/linux-ntfs/linux-ntfs/ntfstools/ntfslabel.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -U2 -r1.8 -r1.9
--- ntfslabel.c 23 Apr 2002 23:27:33 -0000 1.8
+++ ntfslabel.c 25 Apr 2002 18:07:06 -0000 1.9
@@ -1,2 +1,3 @@
+
/*
* $Id$
@@ -43,8 +44,6 @@
#include "attrib.h"
-
-
-const char *EXEC_NAME = "ntfslabel";
-const char *AUTHOR = "Matthew Fanto";
+const char *EXEC_NAME = "ntfslabel";
+const char *AUTHOR = "Matthew Fanto";
ntfs_volume *vol;
@@ -52,15 +51,18 @@
void change_label(const char *dev, const char *label);
-int resize_resident_attribute_value(MFT_RECORD *m, ATTR_RECORD *a, const __u32 new_vsize);
+int resize_resident_attribute_value(MFT_RECORD * m, ATTR_RECORD * a,
+ const __u32 new_vsize);
void print_label(const char *dev);
-int insert_resident_attr_in_mft_record(MFT_RECORD *m, const ATTR_TYPES type,
- const char *name, __u32 name_len, const IGNORE_CASE_BOOL ic,
- const ATTR_FLAGS flags, const RESIDENT_ATTR_FLAGS res_flags,
+int modify_resident_attr_in_mft_record(MFT_RECORD * m, const ATTR_TYPES type,
+ const char *name, __u32 name_len,
+ const IGNORE_CASE_BOOL ic,
+ const ATTR_FLAGS flags,
+ const RESIDENT_ATTR_FLAGS res_flags,
const char *val, const __u32 val_len);
-
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
{
char *locale;
@@ -80,5 +82,5 @@
print_label(argv[1]);
else if (argc == 3)
- change_label(argv[1], argv[2]);
+ change_label(argv[1],argv[2]);
else {
fprintf(stderr, "%s v%s - %s\n", EXEC_NAME, VERSION, AUTHOR);
@@ -89,6 +91,4 @@
}
-
-
/**
* print_label - display the current label on a device
@@ -99,33 +99,30 @@
* it is not.
**/
- void print_label(const char *dev){
+void
+print_label(const char *dev)
+{
- int err;
- unsigned long mnt_flags;
-
- /* We should first make sure it is a valid block device */
- err = ntfs_check_if_mounted(dev, &mnt_flags);
- if (err){
- if(errno == ENODEV){
- printf("Error: %s is not a valid block device!\n",dev);
- exit(1);
- }
- }
-
-
- vol = ntfs_mount(dev, MS_RDONLY);
-
- if (!vol){
- printf("ntfs_mount() on device %s failed!\n",dev);
- exit(1);
- }
+ int err;
+ unsigned long mnt_flags;
- printf("%s\n",vol->vol_name);
- ntfs_umount(vol, 0);
-}
+ /* We should first make sure it is a valid block device */
+ err = ntfs_check_if_mounted(dev, &mnt_flags);
+ if (err) {
+ if (errno == ENODEV) {
+ printf("Error: %s is not a valid block device!\n", dev);
+ exit(1);
+ }
+ }
+ vol = ntfs_mount(dev, MS_RDONLY);
-
+ if (!vol) {
+ printf("ntfs_mount() on device %s failed!\n", dev);
+ exit(1);
+ }
+ printf("%s\n", vol->vol_name);
+ ntfs_umount(vol, 0);
+}
/**
@@ -136,151 +133,173 @@
* change_label will first verify the device is a valid block
* device, and will exit with error INVALID_BLOCK_DEVICE if
- * it is not. it will also verify the device is not currently
+r * it is not. it will also verify the device is not currently
* mounted and exit with error NTFS_MF_MOUNTED if it is
**/
-void change_label(const char *dev, const char *label)
+void
+change_label(const char *dev, const char *label)
{
- MFT_RECORD *mrec = NULL;
- MFT_REF mref;
- ATTR_RECORD *a;
- ntfs_attr_search_ctx *ctx = NULL;
- int err;
- unsigned long mnt_flags;
-
- printf("now changing label - FIXME: Not implemented yet!\n");
+ MFT_RECORD *mrec = NULL;
+ MFT_REF mref;
+ ATTR_RECORD *a;
+ ntfs_attr_search_ctx *ctx = NULL;
+ int err;
+ unsigned long mnt_flags;
+ uchar_t *new_label;
+ int label_len;
+
+ printf("now changing label - FIXME: Not implemented yet!\n");
- /*First we need to make sure the device is not mounted*/
+ /*First we need to make sure the device is not mounted */
err = ntfs_check_if_mounted(dev, &mnt_flags);
-
- if (err){
- if(errno == ENODEV){
- printf("Error: %s is not a valid block device!\n",dev);
- exit(1);
- }
- } else if (mnt_flags & NTFS_MF_MOUNTED){
- printf("Error: %s is already mounted!\n",dev);
+
+ if (err) {
+ if (errno == ENODEV) {
+ printf("Error: %s is not a valid block device!\n", dev);
+ exit(1);
+ }
+
+ }
+ if(mnt_flags == NTFS_MF_MOUNTED)
+ {
+ printf("Error: Device already mounted!!\n");
exit(1);
}
-
- vol = ntfs_mount(dev,MS_RDONLY);
- if (!vol)
- printf("ntfs_mount() failed!\n");
-
- if (strlen(vol->vol_name) != strlen(label) ){
-
- mref = (MFT_REF)FILE_Volume;
-
- if ((err = ntfs_read_file_record(vol,mref,&mrec,NULL))) {
- printf("Error reading file record\n");
- exit(1);
- }
-
- ctx = ntfs_get_attr_search_ctx(NULL,mrec);
-
- if (!ctx) {
- printf("Failed to allocate attribute search context!\n");
- exit(1);
- }
-
- if (ntfs_lookup_attr(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) {
- printf("$VOLUME_NAME attribute not found?!?\n");
- exit(1);
- }
-
- a = ctx->attr;
- if (a->non_resident){
- printf("Error: Attribute $VOLUME_NAME must be resident!\n");
- exit(1);
- }
-
- if (resize_resident_attribute_value(mrec, a, strlen(label))){
- printf("Error: Cannot resize resident attribute!\n");
- exit(1);
- }
-
- if (insert_resident_attr_in_mft_record(mrec,AT_VOLUME_NAME,NULL,0,0,0,0,
- (char*)label,strlen(label))){
- printf("Error: Cannot modify resident attribute!\n");
- exit(1);
- }
-
-
- if (ntfs_write_mft_record(vol,mref,mrec)){
- printf("Error: Cannot write MFT Record to disk!\n");
-
- if(errno == EINVAL)
- printf("EINVAL\n");
- if(errno == EBADF)
- printf("EBADF\n");
- if(errno == ESPIPE)
- printf("ESPIPE\n");
- if(errno == EIO)
- printf("EIO\n");
-
- exit(1);
- }
+ vol = ntfs_mount(dev, 0);
+ if (!vol) {
+ printf("ntfs_mount() failed!\n");
+ exit(1);
}
-
-
-}
+ label_len = strlen(label);
+ mref = (MFT_REF) FILE_Volume;
+ if ((err = ntfs_read_file_record(vol, mref, &mrec, NULL))) {
+ printf("Error reading file record\n");
+ exit(1);
+ }
+
+
+
+ ctx = ntfs_get_attr_search_ctx(NULL, mrec);
+
+ if (!ctx) {
+ printf
+ ("Failed to allocate attribute search context!\n");
+ exit(1);
+ }
+
+
+
+ if (ntfs_lookup_attr
+ (AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) {
+ printf("$VOLUME_NAME attribute not found?!?\n");
+ exit(1);
+ }
+
+
+
+ a = ctx->attr;
+ if (a->non_resident) {
+ printf("Error: Attribute $VOLUME_NAME must be resident!\n");
+ exit(1);
+ }
+
+
+ stoucs(new_label,label,label_len);
+ label_len = sizeof(char) * label_len;
+
+ if (resize_resident_attribute_value(mrec, a, label_len)) {
+ printf("Error: Cannot resize resident attribute!\n");
+ exit(1);
+ }
+
+
+
+
+ if (modify_resident_attr_in_mft_record(mrec,
+ AT_VOLUME_NAME, NULL, 0, 0, 0, 0, (char *)label,label_len)) {
+
+ printf("Error: Cannot modify resident attribute!\n");
+ exit(1);
+ }
+
+
+ if (ntfs_write_mft_record(vol, mref, mrec)) {
+ printf("Error: Cannot write MFT Record to disk!\n");
+
+ if (errno == EINVAL)
+ printf("EINVAL\n");
+ if (errno == EBADF)
+ printf("EBADF\n");
+ if (errno == ESPIPE)
+ printf("ESPIPE\n");
+ if (errno == EIO)
+ printf("EIO\n");
+
+ exit(1);
+ }
+
+
+
+}
+
+
+
+
/* Return 0 on success and -errno on error. */
-int resize_resident_attribute_value(MFT_RECORD *m, ATTR_RECORD *a,
- const __u32 new_vsize)
+int
+resize_resident_attribute_value(MFT_RECORD * m, ATTR_RECORD * a,
+ const __u32 new_vsize)
{
- int new_alen, new_muse;
+
+ int new_alen, new_muse;
- /* New attribute length and mft record bytes used. */
- new_alen = (le32_to_cpu(a->length) - le32_to_cpu(a->value_length) +
- new_vsize + 7) & ~7;
- new_muse = le32_to_cpu(m->bytes_in_use) - le32_to_cpu(a->length) +
- new_alen;
- /* Check for sufficient space. */
- if (new_muse > le32_to_cpu(m->bytes_allocated) ) {
- // Aarrgghh! Need to make space. Probably want generic function
- // for this as we need to call it from other places, too.
- return -ENOTSUP;
- }
- /* Move attributes behind @a to their new location. */
- memmove((char*)a + new_alen, (char*)a + le32_to_cpu(a->length),
- le32_to_cpu(m->bytes_in_use) - ((char*)a - (char*)m) -
- le32_to_cpu(a->length));
- /* Adjust @m to reflect change in used space. */
- m->bytes_in_use = cpu_to_le32(new_muse);
- /* Adjust @a to reflect new value size. */
- a->length = cpu_to_le32(new_alen);
- a->value_length = cpu_to_le32(new_vsize);
- return 0;
+ /* New attribute length and mft record bytes used. */
+ new_alen = (le32_to_cpu(a->length) - le32_to_cpu(a->value_length) +
+ new_vsize + 7) & ~7;
+ new_muse = le32_to_cpu(m->bytes_in_use) - le32_to_cpu(a->length) +
+ new_alen;
+ /* Check for sufficient space. */
+ if (new_muse > le32_to_cpu(m->bytes_allocated)) {
+ // Aarrgghh! Need to make space. Probably want generic function
+ // for this as we need to call it from other places, too.
+ return -ENOTSUP;
+ }
+ /* Move attributes behind @a to their new location. */
+ memmove((char *) a + new_alen, (char *) a + le32_to_cpu(a->length),
+ le32_to_cpu(m->bytes_in_use) - ((char *) a - (char *) m) -
+ le32_to_cpu(a->length));
+ /* Adjust @m to reflect change in used space. */
+ m->bytes_in_use = cpu_to_le32(new_muse);
+ /* Adjust @a to reflect new value size. */
+ a->length = cpu_to_le32(new_alen);
+ a->value_length = cpu_to_le32(new_vsize);
+ return 0;
}
-
-
-
-int stoucs(uchar_t *dest, const char *src, int maxlen)
+int
+stoucs(uchar_t * dest, const char *src, int maxlen)
{
- char c;
- int i;
+ char c;
+ int i;
- /* Need two bytes for null terminator. */
- maxlen -= 2;
- for (i = 0; i < maxlen; i++) {
- c = src[i];
- if (!c)
- break;
- dest[i] = cpu_to_le16(c);
- }
- dest[i] = cpu_to_le16('\0');
- return i;
+ /* Need two bytes for null terminator. */
+ maxlen -= 2;
+ for (i = 0; i < maxlen; i++) {
+ c = src[i];
+ if (!c)
+ break;
+ dest[i] = cpu_to_le16(c);
+ }
+ dest[i] = cpu_to_le16('\0');
+ return i;
}
-
-
/**
* make_room_for_attribute - make room for an attribute inside an mft record
@@ -299,44 +318,48 @@
* the input parameters were faulty.
*/
-int make_room_for_attribute(MFT_RECORD *m, char *pos, const __u32 size)
+int
+make_room_for_attribute(MFT_RECORD * m, char *pos, const __u32 size)
{
- __u32 biu;
+ __u32 biu;
- if (!size)
- return 0;
+ if (!size)
+ return 0;
#ifdef DEBUG
- /*
- * Rigorous consistency checks. Always return -EINVAL even if more
- * appropriate codes exist for simplicity of parsing the return value.
- */
- if (size != ((size + 7) & ~7)) {
- fprintf(stderr, "make_room_for_attribute() received non "
- "8-byte aligned size.\n");
- return -EINVAL;
- }
- if (!m || !pos)
- return -EINVAL;
- if (pos < (char*)m || pos + size < (char*)m ||
- pos > (char*)m + le32_to_cpu(m->bytes_allocated) ||
- pos + size > (char*)m + le32_to_cpu(m->bytes_allocated))
- return -EINVAL;
- /* The -8 is for the attribute terminator. */
- if (pos - (char*)m > le32_to_cpu(m->bytes_in_use) - 8)
- return -EINVAL;
+ /*
+ * Rigorous consistency checks. Always return -EINVAL even if more
+ * appropriate codes exist for simplicity of parsing the return value.
+ */
+ if (size != ((size + 7) & ~7)) {
+ fprintf(stderr, "make_room_for_attribute() received non "
+ "8-byte aligned size.\n");
+ return -EINVAL;
+ }
+ if (!m || !pos)
+ return -EINVAL;
+ if (pos < (char *) m || pos + size < (char *) m ||
+ pos > (char *) m + le32_to_cpu(m->bytes_allocated) ||
+ pos + size > (char *) m + le32_to_cpu(m->bytes_allocated))
+ return -EINVAL;
+ /* The -8 is for the attribute terminator. */
+ if (pos - (char *) m > le32_to_cpu(m->bytes_in_use) - 8)
+ return -EINVAL;
#endif
- biu = le32_to_cpu(m->bytes_in_use);
- /* Do we have enough space? */
- if (biu + size > le32_to_cpu(m->bytes_allocated))
- return -ENOSPC;
- /* Move everything after pos to pos + size. */
- memmove(pos + size, pos, biu - (pos - (char*)m));
- /* Update mft record. */
- m->bytes_in_use = cpu_to_le32(biu + size);
- return 0;
+ biu = le32_to_cpu(m->bytes_in_use);
+ /* Do we have enough space? */
+ if (biu + size > le32_to_cpu(m->bytes_allocated))
+ return -ENOSPC;
+ /* Move everything after pos to pos + size. */
+ memmove(pos + size, pos, biu - (pos - (char *) m));
+ /* Update mft record. */
+ m->bytes_in_use = cpu_to_le32(biu + size);
+ return 0;
}
+
+
+
/* Return 0 on success and -errno on error. */
-int insert_resident_attr_in_mft_record(MFT_RECORD *m, const ATTR_TYPES type,
+int modify_resident_attr_in_mft_record(MFT_RECORD *m, const ATTR_TYPES type,
const char *name, __u32 name_len, const IGNORE_CASE_BOOL ic,
const ATTR_FLAGS flags, const RESIDENT_ATTR_FLAGS res_flags,
@@ -360,4 +383,6 @@
} else
uname = NULL;
+
+
/* Check if the attribute is already there. */
ctx = ntfs_get_attr_search_ctx(NULL, m);
@@ -372,9 +397,5 @@
goto err_out;
}
- if (!ntfs_lookup_attr(type, uname, name_len, ic, 0, val, val_len,
- ctx)) {
- err = -EEXIST;
- goto err_out;
- }
+
if (errno != ENOENT) {
printf("Corrupt inode.\n");
@@ -383,4 +404,5 @@
}
a = ctx->attr;
+ asize = val_len;
/* sizeof(resident attribute record header) == 24 */
asize = ((24 + ((name_len + 7) & ~7) + val_len) + 7) & ~7;
@@ -404,4 +426,6 @@
goto err_out;
}
+
+
#ifdef DEBUG
if (err == -EINVAL) {
@@ -411,6 +435,6 @@
goto err_out;
}
-#endif
- a->type = type;
+#endif
+ a->type = type;
a->length = cpu_to_le32(asize);
a->non_resident = 0;
@@ -425,8 +449,11 @@
a->resident_flags = res_flags;
a->reservedR = 0;
+
+
if (name_len)
memcpy((char*)a + 24, uname, name_len << 1);
if (val_len)
memcpy((char*)a + le16_to_cpu(a->value_offset), val, val_len);
+
err_out:
if (ctx)
@@ -436,2 +463,4 @@
return err;
}
+
+
|