Changes by: szaka
Update of /cvsroot/linux-ntfs/linux-ntfs/ntfstools
In directory usw-pr-cvs1:/tmp/cvs-serv4048/ntfstools
Modified Files:
ntfsresize.c
Log Message:
Shrink volume to size given in byte[K|M|G]
Index: ntfsresize.c
===================================================================
RCS file: /cvsroot/linux-ntfs/linux-ntfs/ntfstools/ntfsresize.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -U2 -r1.17 -r1.18
--- ntfsresize.c 16 Jul 2002 13:26:42 -0000 1.17
+++ ntfsresize.c 17 Jul 2002 04:37:48 -0000 1.18
@@ -52,4 +52,5 @@
int info;
s64 size;
+ s64 bytes;
char *volume;
} opt;
@@ -110,16 +111,52 @@
void usage(char *s)
{
- printf ("Usage: %s [-fhinv] [-c clusters] device\n", s);
- printf (" -c clusters Shrink volume to new size given in clusters\n");
- Dprintf(" -d Show debug information\n");
- printf (" -f Force to progress (DANGEROUS)\n");
- printf (" -h This help text\n");
- printf (" -i Calculate the smallest shrinked volume size\n");
- printf (" -n No write operations (read-only mode)\n");
- printf (" -v Verbose operation\n");
+ printf ("Usage: %s [-fhin] [-c clusters] [-s byte[K|M|G]] device\n", s);
+ printf (" -c clusters Shrink volume to size given in NTFS clusters\n");
+ Dprintf(" -d Show debug information\n");
+ printf (" -f Force to progress (DANGEROUS)\n");
+ printf (" -h This help text\n");
+ printf (" -i Calculate the smallest shrinked volume size supported\n");
+ printf (" -n No write operations (mount volume read-only)\n");
+ printf (" -s byte[K|M|G] Shrink volume to size given in byte[K|M|G]\n");
+/* printf (" -v Verbose operation\n"); */
exit(1);
}
+s64 get_new_volume_size(char *s, char **argv)
+{
+ s64 size;
+ char *suffix;
+
+ size = strtoll(s, &suffix, 10);
+ if (size <= 0 || errno == ERANGE)
+ err_exit("Illegal new volume size\n");
+
+ if (!*suffix)
+ return size;
+
+ if (strlen(suffix) > 1)
+ usage(argv[0]);
+
+ /* FIXME: check for overflow */
+ switch (*suffix) {
+ case 'G':
+ case 'g':
+ size *= 1024;
+ case 'M':
+ case 'm':
+ size *= 1024;
+ case 'K':
+ case 'k':
+ size *= 1024;
+ break;
+ default:
+ usage(argv[0]);
+ }
+
+ return size;
+}
+
+
void parse_options(int argc, char **argv)
{
@@ -129,10 +166,10 @@
memset(&opt, 0, sizeof(opt));
- while ((i = getopt(argc, argv, "c:dfhinv")) != EOF)
+ while ((i = getopt(argc, argv, "c:dfhins:")) != EOF)
switch (i) {
case 'c':
opt.size = strtoll(optarg, &s, 0);
- if (*s || opt.size < 0 || errno == ERANGE)
- err_exit("Size of clusters is out of range\n");
+ if (*s || opt.size <= 0 || errno == ERANGE)
+ err_exit("Illegal number of clusters!\n");
break;
case 'd':
@@ -150,4 +187,7 @@
opt.ro_flag = MS_RDONLY;
break;
+ case 's':
+ opt.bytes = get_new_volume_size(optarg, argv);
+ break;
case 'v':
opt.verbose++;
@@ -167,12 +207,19 @@
perr_exit("Couldn't open /dev/null");
+
+ if (opt.size && opt.bytes) {
+ printf(NERR_PREFIX "It makes no sense to use "
+ "-c and -s together.\n");
+ usage(argv[0]);
+ }
+
/* If no '-c clusters' then estimate smallest shrinked volume size */
- if (!opt.size)
+ if (!opt.size && !opt.bytes)
opt.info = 1;
if (opt.info) {
- if (opt.size) {
- printf(NERR_PREFIX "It makes no sense to use both "
- "-i and -c at the same time.\n");
+ if (opt.size || opt.bytes) {
+ printf(NERR_PREFIX "It makes no sense to use -i and "
+ "-%c together.\n", opt.size ? 'c' : 's');
usage(argv[0]);
}
@@ -350,5 +397,5 @@
if (opt.info)
printf("The volume end is fragmented. "
- "This case isn't yet supported.\n");
+ "This case is not yet supported.\n");
exit(1);
}
@@ -632,4 +679,11 @@
+void print_volume_size(char *str, ntfs_volume *v, s64 nr_clusters)
+{
+ printf("%s: %lld clusters (%lld MB)\n",
+ str, nr_clusters, (nr_clusters * v->cluster_size) >> 20);
+}
+
+
void mount_volume()
{
@@ -661,5 +715,5 @@
printf("Cluster size : %u\n", vol->cluster_size);
- printf("Number of clusters : %lld\n", vol->nr_clusters);
+ print_volume_size("Current volume size", vol, vol->nr_clusters);
}
@@ -688,5 +742,5 @@
{
struct bitmap on_disk_lcn_bitmap;
- s64 new_volume_size; /* in clusters */
+ s64 new_volume_size = 0; /* in clusters */
int i;
@@ -695,4 +749,11 @@
mount_volume();
+ if (opt.size || opt.bytes) {
+ new_volume_size = opt.bytes / vol->cluster_size;
+ if (opt.size)
+ new_volume_size = opt.size;
+ print_volume_size("New volume size ", vol, new_volume_size);
+ }
+
setup_lcn_bitmap();
@@ -703,7 +764,8 @@
free(on_disk_lcn_bitmap.bm);
- /* FIXME: check opt.size validity */
+ if (opt.info)
+ advise_on_resize();
- new_volume_size = opt.size;
+ /* FIXME: check opt.size validity */
/* Backup boot sector at the end of device isn't counted in NTFS
@@ -717,7 +779,4 @@
else if (new_volume_size == vol->nr_clusters)
exit(0);
-
- if (opt.info)
- advise_on_resize();
for (i = new_volume_size; i < vol->nr_clusters; i++)
|