Changes by: szaka
Update of /cvsroot/linux-ntfs/linux-ntfs/ntfstools
In directory usw-pr-cvs1:/tmp/cvs-serv4222/ntfstools
Modified Files:
ntfsresize.c
Log Message:
ntfsresize: cosmetic cleanup, progress bar added
Index: ntfsresize.c
===================================================================
RCS file: /cvsroot/linux-ntfs/linux-ntfs/ntfstools/ntfsresize.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -U2 -r1.5 -r1.6
--- ntfsresize.c 9 Jul 2002 19:17:49 -0000 1.5
+++ ntfsresize.c 10 Jul 2002 10:56:26 -0000 1.6
@@ -58,23 +58,49 @@
};
+struct progress_bar {
+ u64 start;
+ u64 stop;
+ int resolution;
+ float unit;
+};
+
ntfs_volume *vol = NULL;
struct bitmap lcn_bitmap;
+#define ERR_PREFIX "==> ERROR"
+#define PERR_PREFIX ERR_PREFIX "(%d): "
+#define NERR_PREFIX ERR_PREFIX ": "
+
#define rounded_up_division(a, b) (((a) + (b - 1)) / (b))
-#define perr_exit(a) err_exit(a ": %s\n", strerror(errno))
int err_exit(const char *fmt, ...)
{
- va_list ap;
-
- fprintf(stdout, "ERROR: ");
- va_start(ap, fmt);
- vfprintf(stdout, fmt, ap);
- va_end(ap);
+ va_list ap;
+
+ fprintf(stdout, NERR_PREFIX);
+ va_start(ap, fmt);
+ vfprintf(stdout, fmt, ap);
+ va_end(ap);
fflush(stdout);
fflush(stderr);
- exit(1);
+ exit(1);
+}
+
+
+int perr_exit(const char *fmt, ...)
+{
+ va_list ap;
+ int eo = errno;
+
+ fprintf(stdout, PERR_PREFIX, eo);
+ va_start(ap, fmt);
+ vfprintf(stdout, fmt, ap);
+ va_end(ap);
+ printf(": %s\n", strerror(eo));
+ fflush(stdout);
+ fflush(stderr);
+ exit(1);
}
@@ -83,8 +109,8 @@
{
printf ("Usage: %s [-nv] [-c clusters] device\n", s);
+ printf (" -n No write operations (read-only mode)\n");
+ printf (" -v Verbose operation\n");
printf (" -c clusters New NTFS size in clusters\n");
Dprintf(" -d Show debug information\n");
- printf (" -v Verbose operation\n");
- printf (" -n No write operations (read-only mode)\n");
exit(1);
}
@@ -98,15 +124,12 @@
memset(&opt, 0, sizeof(opt));
- while ((i = getopt(argc, argv, "vdnc:")) != EOF)
+ while ((i = getopt(argc, argv, "nvc:d")) != EOF)
switch (i) {
- case 'v':
- opt.verbose++;
- break;
- case 'd':
- opt.debug = 1;
- break;
case 'n':
opt.ro_flag = MS_RDONLY;
break;
+ case 'v':
+ opt.verbose++;
+ break;
case 'c':
opt.size = strtoll(optarg, &s, 0);
@@ -114,4 +137,7 @@
err_exit("Size of clusters is out of range\n");
break;
+ case 'd':
+ opt.debug = 1;
+ break;
default:
usage(argv[0]);
@@ -139,5 +165,5 @@
/* Needs to be multiple of 8 bytes */
bm_bsize = (bm_bsize + 7) & ~7;
- Dprintf("Bitmap byte size : %lld (%lld clusters)\n",
+ printf("Bitmap byte size : %lld (%lld clusters)\n",
bm_bsize, rounded_up_division(bm_bsize, vol->cluster_size));
@@ -230,16 +256,47 @@
+void progress_init(struct progress_bar *p, u64 start, u64 stop, int res)
+{
+ p->start = start;
+ p->stop = stop;
+ p->unit = 100.0 / (stop - start);
+ p->resolution = res;
+}
+
+
+void progress_update(struct progress_bar *p, u64 current)
+{
+ float percent = p->unit * current;
+
+ if (current != p->stop) {
+ if ((current != p->start) && (current % p->resolution))
+ return;
+ printf("%6.2f percent completed\r", percent);
+ } else
+ printf("100.00 percent completed\n");
+ fflush(stdout);
+}
+
void walk_inodes()
{
- s32 inode;
+ s32 inode = 0;
+ s64 last_mft_rec;
MFT_REF mref;
MFT_RECORD *mrec = NULL;
+ struct progress_bar progress;
+
+ printf("Scanning volume ...\n");
+
+ last_mft_rec = vol->nr_mft_records - 1;
+ progress_init(&progress, inode, last_mft_rec, 100);
+
+ for (; inode <= last_mft_rec; inode++) {
+ progress_update(&progress, inode);
- for (inode = 0; inode < vol->nr_mft_records; inode++) {
mref = (MFT_REF)inode;
if (ntfs_read_file_record(vol, mref, &mrec, NULL)) {
if (errno == EIO)
continue;
- perr_exit("ntfs_read_file_record");
+ perr_exit("Reading inode %ld failed", inode);
}
if (!(mrec->flags & MFT_RECORD_IN_USE))
@@ -257,6 +314,6 @@
u64 i;
- printf("ERROR: Fragmented volume not yet supported. "
- "Try to defragment it before resize.\n");
+ printf(NERR_PREFIX "Fragmented volume not yet supported. "
+ "Defragment it before resize.\n");
for (i = vol->nr_clusters - 1; i > 0; i--)
@@ -378,5 +435,5 @@
u64 k = (u64)rl[i].lcn + j;
ntfs_set_bit(lcn_bitmap.bm, k, 0);
- printf("Unallocate cluster "
+ printf("Unallocate cluster: "
"%llu (%llx)\n", k, k);
}
@@ -468,4 +525,6 @@
ntfs_attr_search_ctx *ctx = NULL;
+ printf("Updating $BadClust file ...\n");
+
lookup_data_attr((MFT_REF)FILE_BadClus, "$Bad", &ctx);
look_for_bad_sector(ctx->attr);
@@ -486,4 +545,6 @@
ntfs_attr_search_ctx *ctx = NULL;
+ printf("Updating $Bitmap file ...\n");
+
lookup_data_attr((MFT_REF)FILE_Bitmap, NULL, &ctx);
/* FIXME: sanity_check_attr(ctx->attr); */
@@ -542,4 +603,7 @@
perr_exit("ntfs_mount failed");
+ printf("Cluster size : %u\n", vol->cluster_size);
+ printf("Number of clusters: %lld\n", vol->nr_clusters);
+
setup_lcn_bitmap();
@@ -550,7 +614,4 @@
free(on_disk_lcn_bitmap.bm);
- printf("Number of clusters: %lld\n", vol->nr_clusters);
- printf("Cluster size : %u\n", vol->cluster_size);
-
/* FIXME: check/adjust opt.size validity */
@@ -573,4 +634,6 @@
/* FIXME: create backup boot sector -- if possible*/
/* FIXME: "call" ntfsfix: set the volume dirty, delete log, etc */
+
+ printf("Volume had been resized. Please run 'ntfsfix' on it.\n");
return 0;
|