Exemple #1
0
static int ntfs_open(const char *device)
{
	ntfs_volume *vol;
	unsigned long flags = 0;
	int ret = NTFS_VOLUME_OK;
	
	if (opts.probetype == PROBE_READONLY)
		flags |= MS_RDONLY;

	vol = ntfs_mount(device, flags);
	if (!vol)
		ret = ntfs_volume_error(errno);

	ntfs_umount(vol, FALSE);

	return ret;
}
Exemple #2
0
/**
 * main
 */
int main(int argc, char **argv)
{
	unsigned long mnt_flags, ul;
	int err;
	ntfs_inode *ni;
	ntfs_volume *vol;
#ifdef HAVE_WINDOWS_H
	char *unix_name;
#endif

	vol = (ntfs_volume*)NULL;
	ntfs_log_set_handler(ntfs_log_handler_outerr);

	/* Initialize opts to zero / required values. */
	memset(&opts, 0, sizeof(opts));

	/* Parse command line options. */
	parse_options(argc, argv);

	utils_set_locale();

	/* Make sure the file system is not mounted. */
	if (ntfs_check_if_mounted(dev_name, &mnt_flags))
		ntfs_log_perror("Failed to determine whether %s is mounted",
				dev_name);
	else if (mnt_flags & NTFS_MF_MOUNTED) {
		ntfs_log_error("%s is mounted.\n", dev_name);
		if (!opts.force)
			err_exit((ntfs_volume*)NULL, "Refusing to run!\n");
		fprintf(stderr, "ntfsfallocate forced anyway. Hope /etc/mtab "
				"is incorrect.\n");
	}

	/* Mount the device. */
	if (opts.no_action) {
		ntfs_log_quiet("Running in READ-ONLY mode!\n");
		ul = NTFS_MNT_RDONLY;
	} else
		if (opts.force)
			ul = NTFS_MNT_RECOVER;
		else
			ul = 0;
	vol = ntfs_mount(dev_name, ul);
	if (!vol)
		err_exit(vol, "Failed to mount %s: %s\n", dev_name,
			strerror(errno));

	if ((vol->flags & VOLUME_IS_DIRTY) && !opts.force)
		err_exit(vol, "Volume is dirty, please run chkdsk.\n");

	if (ntfs_volume_get_free_space(vol))
		err_exit(vol, "Failed to get free clusters %s: %s\n",
					dev_name, strerror(errno));

	/* Open the specified inode. */
#ifdef HAVE_WINDOWS_H
	unix_name = (char*)malloc(strlen(file_name) + 1);
	if (unix_name) {
		int i;
		for (i=0; file_name[i]; i++)
			if (file_name[i] == '\\')
				unix_name[i] = '/';
			else
				unix_name[i] = file_name[i];
		unix_name[i] = 0;
		ni = ntfs_pathname_to_inode(vol, NULL, unix_name);
		free(unix_name);
	} else
		ni = (ntfs_inode*)NULL;
#else
	ni = ntfs_pathname_to_inode(vol, NULL, file_name);
#endif
	if (!ni)
		err_exit(vol, "Failed to open file \"%s\": %s\n", file_name,
				strerror(errno));
	if (!opts.no_action)
		err = ntfs_fallocate(ni, opt_alloc_offs, opt_alloc_len);

	/* Close the inode. */
	if (ntfs_inode_close(ni)) {
		err = -1;
		err_exit(vol, "Failed to close inode \"%s\" : %s\n", file_name,
				strerror(errno));
	}

	/* Unmount the volume. */
	err = ntfs_umount(vol, 0);
	vol = (ntfs_volume*)NULL;
	if (err)
		ntfs_log_perror("Warning: Failed to umount %s", dev_name);

	/* Free the attribute name if it exists. */
	if (attr_name_len)
		ntfs_ucsfree(attr_name);

	ntfs_log_quiet("ntfsfallocate completed successfully. Have a nice day.\n");
	return 0;
}