/** * ntfs_inode_badclus_bad - check for $Badclus:$Bad data attribute * @mft_no: mft record number where @attr is present * @attr: attribute record used to check for the $Bad attribute * * Check if the mft record given by @mft_no and @attr contains the bad sector * list. Please note that mft record numbers describing $Badclus extent inodes * will not match the current $Badclus:$Bad check. * * On success return 1 if the file is $Badclus:$Bad, otherwise return 0. * On error return -1 with errno set to the error code. */ int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *attr) { int len, ret = 0; ntfschar *ustr; if (!attr) { ntfs_log_error("Invalid argument.\n"); errno = EINVAL; return -1; } if (mft_no != FILE_BadClus) return 0; if (attr->type != AT_DATA) return 0; if ((ustr = ntfs_str2ucs("$Bad", &len)) == NULL) { ntfs_log_perror("Couldn't convert '$Bad' to Unicode"); return -1; } if (ustr && ntfs_names_are_equal(ustr, len, (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)), attr->name_length, 0, NULL, 0)) ret = 1; ntfs_ucsfree(ustr); return ret; }
/** * 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; }