Added option --check to ntfsresize (from fedora)

This commit is contained in:
Jean-Pierre André 2011-03-23 09:50:12 +01:00
parent 71488598d7
commit 67f07ac3ad
2 changed files with 61 additions and 23 deletions

View File

@ -116,6 +116,11 @@ is equivalent to
.BR "\-f \-v" . .BR "\-f \-v" .
Long named options can be abbreviated to any unique prefix of their name. Long named options can be abbreviated to any unique prefix of their name.
.TP .TP
\fB\-c\fR, \fB\-\-check\fR
By using this option ntfsresize will only check the device to ensure that it
is ready to be resized. If not, it will print any errors detected.
If the device is fine, nothing will be printed.
.TP
\fB\-i\fR, \fB\-\-info\fR \fB\-i\fR, \fB\-\-info\fR
By using this option ntfsresize will determine the theoretically smallest By using this option ntfsresize will determine the theoretically smallest
shrunken filesystem size supported. Most of the time the result is the space shrunken filesystem size supported. Most of the time the result is the space

View File

@ -136,6 +136,7 @@ static struct {
int infombonly; int infombonly;
int show_progress; int show_progress;
int badsectors; int badsectors;
int check;
s64 bytes; s64 bytes;
char *volume; char *volume;
} opt; } opt;
@ -312,6 +313,7 @@ static void usage(void)
printf("\nUsage: %s [OPTIONS] DEVICE\n" printf("\nUsage: %s [OPTIONS] DEVICE\n"
" Resize an NTFS volume non-destructively, safely move any data if needed.\n" " Resize an NTFS volume non-destructively, safely move any data if needed.\n"
"\n" "\n"
" -c, --check Check to ensure that the device is ready for resize\n"
" -i, --info Estimate the smallest shrunken size possible\n" " -i, --info Estimate the smallest shrunken size possible\n"
" -m, --info-mb-only Estimate the smallest shrunken size possible, output size in MB only\n" " -m, --info-mb-only Estimate the smallest shrunken size possible, output size in MB only\n"
" -s, --size SIZE Resize volume to SIZE[k|M|G] bytes\n" " -s, --size SIZE Resize volume to SIZE[k|M|G] bytes\n"
@ -439,9 +441,10 @@ static s64 get_new_volume_size(char *s)
*/ */
static int parse_options(int argc, char **argv) static int parse_options(int argc, char **argv)
{ {
static const char *sopt = "-bdfhimnPs:vV"; static const char *sopt = "-bcdfhimnPs:vV";
static const struct option lopt[] = { static const struct option lopt[] = {
{ "bad-sectors",no_argument, NULL, 'b' }, { "bad-sectors",no_argument, NULL, 'b' },
{ "check", no_argument, NULL, 'c' },
#ifdef DEBUG #ifdef DEBUG
{ "debug", no_argument, NULL, 'd' }, { "debug", no_argument, NULL, 'd' },
#endif #endif
@ -476,6 +479,9 @@ static int parse_options(int argc, char **argv)
case 'b': case 'b':
opt.badsectors++; opt.badsectors++;
break; break;
case 'c':
opt.check++;
break;
case 'd': case 'd':
opt.debug++; opt.debug++;
break; break;
@ -2242,6 +2248,44 @@ static void print_num_of_relocations(ntfs_resize_t *resize)
rounded_up_division(relocations, NTFS_MBYTE)); rounded_up_division(relocations, NTFS_MBYTE));
} }
static ntfs_volume *check_volume(void)
{
ntfs_volume *myvol = NULL;
/*
* Pass MS_FORENSIC so that the mount process does not modify the
* volume at all. We will do the logfile emptying and dirty setting
* later if needed.
*/
if (!(myvol = ntfs_mount(opt.volume, opt.ro_flag | MS_FORENSIC))) {
int err = errno;
perr_printf("Opening '%s' as NTFS failed", opt.volume);
switch (err) {
case EINVAL :
printf(invalid_ntfs_msg, opt.volume);
break;
case EIO :
printf("%s", corrupt_volume_msg);
break;
case EPERM :
printf("%s", hibernated_volume_msg);
break;
case EOPNOTSUPP :
printf("%s", unclean_journal_msg);
break;
case EBUSY :
printf("%s", opened_volume_msg);
break;
default :
break;
}
exit(1);
}
return myvol;
}
/** /**
* mount_volume * mount_volume
* *
@ -2268,27 +2312,7 @@ static ntfs_volume *mount_volume(void)
err_exit("Device '%s' is mounted. " err_exit("Device '%s' is mounted. "
"You must 'umount' it first.\n", opt.volume); "You must 'umount' it first.\n", opt.volume);
} }
/* vol = check_volume();
* Pass MS_FORENSIC so that the mount process does not modify the
* volume at all. We will do the logfile emptying and dirty setting
* later if needed.
*/
if (!(vol = ntfs_mount(opt.volume, opt.ro_flag | MS_FORENSIC))) {
int err = errno;
perr_printf("Opening '%s' as NTFS failed", opt.volume);
if (err == EINVAL)
printf(invalid_ntfs_msg, opt.volume);
else if (err == EIO)
printf("%s", corrupt_volume_msg);
else if (err == EPERM)
printf("%s", hibernated_volume_msg);
else if (err == EOPNOTSUPP)
printf("%s", unclean_journal_msg);
else if (err == EBUSY)
printf("%s", opened_volume_msg);
exit(1);
}
if (vol->flags & VOLUME_IS_DIRTY) if (vol->flags & VOLUME_IS_DIRTY)
if (opt.force-- <= 0) if (opt.force-- <= 0)
@ -2417,7 +2441,7 @@ int main(int argc, char **argv)
ntfs_resize_t resize; ntfs_resize_t resize;
s64 new_size = 0; /* in clusters; 0 = --info w/o --size */ s64 new_size = 0; /* in clusters; 0 = --info w/o --size */
s64 device_size; /* in bytes */ s64 device_size; /* in bytes */
ntfs_volume *vol; ntfs_volume *vol = NULL;
ntfs_log_set_handler(ntfs_log_handler_outerr); ntfs_log_set_handler(ntfs_log_handler_outerr);
@ -2428,6 +2452,15 @@ int main(int argc, char **argv)
utils_set_locale(); utils_set_locale();
/*
* If we're just checking the device, we'll do it first,
* and exit out, no matter what we find.
*/
if (opt.check) {
vol = check_volume();
exit(0);
}
if (!(vol = mount_volume())) if (!(vol = mount_volume()))
err_exit("Couldn't open volume '%s'!\n", opt.volume); err_exit("Couldn't open volume '%s'!\n", opt.volume);