Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * utils.c - Part of the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2002-2005 Richard Russon |
| 5 | * Copyright (c) 2003-2006 Anton Altaparmakov |
| 6 | * Copyright (c) 2003 Lode Leroy |
| 7 | * Copyright (c) 2005-2007 Yura Pakhuchiy |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 8 | * Copyright (c) 2014 Jean-Pierre Andre |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 9 | * |
| 10 | * A set of shared functions for ntfs utilities |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program (in the main directory of the Linux-NTFS |
| 24 | * distribution in the file COPYING); if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #ifdef HAVE_STDIO_H |
| 33 | #include <stdio.h> |
| 34 | #endif |
| 35 | #ifdef HAVE_STDARG_H |
| 36 | #include <stdarg.h> |
| 37 | #endif |
| 38 | #ifdef HAVE_ERRNO_H |
| 39 | #include <errno.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_SYS_TYPES_H |
| 42 | #include <sys/types.h> |
| 43 | #endif |
| 44 | #ifdef HAVE_SYS_STAT_H |
| 45 | #include <sys/stat.h> |
| 46 | #endif |
| 47 | #ifdef HAVE_UNISTD_H |
| 48 | #include <unistd.h> |
| 49 | #endif |
| 50 | #ifdef HAVE_STRING_H |
| 51 | #include <string.h> |
| 52 | #endif |
| 53 | #ifdef HAVE_LOCALE_H |
| 54 | #include <locale.h> |
| 55 | #endif |
| 56 | #ifdef HAVE_LIBINTL_H |
| 57 | #include <libintl.h> |
| 58 | #endif |
| 59 | #ifdef HAVE_STDLIB_H |
| 60 | #include <stdlib.h> |
| 61 | #endif |
| 62 | #ifdef HAVE_LIMITS_H |
| 63 | #include <limits.h> |
| 64 | #endif |
| 65 | #ifdef HAVE_CTYPE_H |
| 66 | #include <ctype.h> |
| 67 | #endif |
| 68 | |
| 69 | #include "utils.h" |
| 70 | #include "types.h" |
| 71 | #include "volume.h" |
| 72 | #include "debug.h" |
| 73 | #include "dir.h" |
| 74 | /* #include "version.h" */ |
| 75 | #include "logging.h" |
| 76 | #include "misc.h" |
| 77 | |
| 78 | const char *ntfs_bugs = "Developers' email address: "NTFS_DEV_LIST"\n"; |
| 79 | const char *ntfs_gpl = "This program is free software, released under the GNU " |
| 80 | "General Public License\nand you are welcome to redistribute it under " |
| 81 | "certain conditions. It comes with\nABSOLUTELY NO WARRANTY; for " |
| 82 | "details read the GNU General Public License to be\nfound in the file " |
| 83 | "\"COPYING\" distributed with this program, or online at:\n" |
| 84 | "http://www.gnu.org/copyleft/gpl.html\n"; |
| 85 | |
| 86 | static const char *invalid_ntfs_msg = |
| 87 | "The device '%s' doesn't have a valid NTFS.\n" |
| 88 | "Maybe you selected the wrong device? Or the whole disk instead of a\n" |
| 89 | "partition (e.g. /dev/hda, not /dev/hda1)? Or the other way around?\n"; |
| 90 | |
| 91 | static const char *corrupt_volume_msg = |
| 92 | "NTFS is inconsistent. Run chkdsk /f on Windows then reboot it TWICE!\n" |
| 93 | "The usage of the /f parameter is very IMPORTANT! No modification was\n" |
| 94 | "made to NTFS by this software.\n"; |
| 95 | |
| 96 | static const char *hibernated_volume_msg = |
| 97 | "The NTFS partition is hibernated. Please resume Windows and turned it \n" |
| 98 | "off properly, so mounting could be done safely.\n"; |
| 99 | |
| 100 | static const char *unclean_journal_msg = |
| 101 | "Access is denied because the NTFS journal file is unclean. Choices are:\n" |
| 102 | " A) Shutdown Windows properly.\n" |
| 103 | " B) Click the 'Safely Remove Hardware' icon in the Windows taskbar\n" |
| 104 | " notification area before disconnecting the device.\n" |
| 105 | " C) Use 'Eject' from Windows Explorer to safely remove the device.\n" |
| 106 | " D) If you ran chkdsk previously then boot Windows again which will\n" |
| 107 | " automatically initialize the journal.\n" |
| 108 | " E) Submit 'force' option (WARNING: This solution it not recommended).\n" |
| 109 | " F) ntfsmount: Mount the volume read-only by using the 'ro' mount option.\n"; |
| 110 | |
| 111 | static const char *opened_volume_msg = |
| 112 | "Access is denied because the NTFS volume is already exclusively opened.\n" |
| 113 | "The volume may be already mounted, or another software may use it which\n" |
| 114 | "could be identified for example by the help of the 'fuser' command.\n"; |
| 115 | |
| 116 | static const char *dirty_volume_msg = |
| 117 | "Volume is scheduled for check.\n" |
| 118 | "Please boot into Windows TWICE, or use the 'force' option.\n" |
| 119 | "NOTE: If you had not scheduled check and last time accessed this volume\n" |
| 120 | "using ntfsmount and shutdown system properly, then init scripts in your\n" |
| 121 | "distribution are broken. Please report to your distribution developers\n" |
| 122 | "(NOT to us!) that init scripts kill ntfsmount or mount.ntfs-fuse during\n" |
| 123 | "shutdown instead of proper umount.\n"; |
| 124 | |
| 125 | static const char *fakeraid_msg = |
| 126 | "You seem to have a SoftRAID/FakeRAID hardware and must use an activated,\n" |
| 127 | "different device under /dev/mapper, (e.g. /dev/mapper/nvidia_eahaabcc1)\n" |
| 128 | "to mount NTFS. Please see the 'dmraid' documentation for help.\n"; |
| 129 | |
| 130 | /** |
| 131 | * utils_set_locale |
| 132 | */ |
| 133 | int utils_set_locale(void) |
| 134 | { |
| 135 | const char *locale; |
| 136 | |
| 137 | locale = setlocale(LC_ALL, ""); |
| 138 | if (!locale) { |
| 139 | locale = setlocale(LC_ALL, NULL); |
| 140 | ntfs_log_error("Failed to set locale, using default '%s'.\n", |
| 141 | locale); |
| 142 | return 1; |
| 143 | } else { |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * linux-ntfs's ntfs_mbstoucs has different semantics, so we emulate it with |
| 150 | * ntfs-3g's. |
| 151 | */ |
| 152 | int ntfs_mbstoucs_libntfscompat(const char *ins, |
| 153 | ntfschar **outs, int outs_len) |
| 154 | { |
| 155 | if(!outs) { |
| 156 | errno = EINVAL; |
| 157 | return -1; |
| 158 | } |
| 159 | else if(*outs != NULL) { |
| 160 | /* Note: libntfs's mbstoucs implementation allows the caller to |
| 161 | * specify a preallocated buffer while libntfs-3g's always |
| 162 | * allocates the output buffer. |
| 163 | */ |
| 164 | ntfschar *tmpstr = NULL; |
| 165 | int tmpstr_len; |
| 166 | |
| 167 | tmpstr_len = ntfs_mbstoucs(ins, &tmpstr); |
| 168 | if(tmpstr_len >= 0) { |
| 169 | if((tmpstr_len + 1) > outs_len) { |
| 170 | /* Doing a realloc instead of reusing tmpstr |
| 171 | * because it emulates libntfs's mbstoucs more |
| 172 | * closely. */ |
| 173 | ntfschar *re_outs = realloc(*outs, |
| 174 | sizeof(ntfschar)*(tmpstr_len + 1)); |
| 175 | if(!re_outs) |
| 176 | tmpstr_len = -1; |
| 177 | else |
| 178 | *outs = re_outs; |
| 179 | } |
| 180 | |
| 181 | if(tmpstr_len >= 0) { |
| 182 | /* The extra character is the \0 terminator. */ |
| 183 | memcpy(*outs, tmpstr, |
| 184 | sizeof(ntfschar)*(tmpstr_len + 1)); |
| 185 | } |
| 186 | |
| 187 | free(tmpstr); |
| 188 | } |
| 189 | |
| 190 | return tmpstr_len; |
| 191 | } |
| 192 | else |
| 193 | return ntfs_mbstoucs(ins, outs); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * utils_valid_device - Perform some safety checks on the device, before start |
| 198 | * @name: Full pathname of the device/file to work with |
| 199 | * @force: Continue regardless of problems |
| 200 | * |
| 201 | * Check that the name refers to a device and that is isn't already mounted. |
| 202 | * These checks can be overridden by using the force option. |
| 203 | * |
| 204 | * Return: 1 Success, we can continue |
| 205 | * 0 Error, we cannot use this device |
| 206 | */ |
| 207 | int utils_valid_device(const char *name, int force) |
| 208 | { |
| 209 | unsigned long mnt_flags = 0; |
| 210 | struct stat st; |
| 211 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 212 | #if defined(HAVE_WINDOWS_H) | defined(__CYGWIN32__) |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 213 | /* FIXME: This doesn't work for Cygwin, so just return success. */ |
| 214 | return 1; |
| 215 | #endif |
| 216 | if (!name) { |
| 217 | errno = EINVAL; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | if (stat(name, &st) == -1) { |
| 222 | if (errno == ENOENT) |
| 223 | ntfs_log_error("The device %s doesn't exist\n", name); |
| 224 | else |
| 225 | ntfs_log_perror("Error getting information about %s", |
| 226 | name); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /* Make sure the file system is not mounted. */ |
| 231 | if (ntfs_check_if_mounted(name, &mnt_flags)) { |
| 232 | ntfs_log_perror("Failed to determine whether %s is mounted", |
| 233 | name); |
| 234 | if (!force) { |
| 235 | ntfs_log_error("Use the force option to ignore this " |
| 236 | "error.\n"); |
| 237 | return 0; |
| 238 | } |
| 239 | ntfs_log_warning("Forced to continue.\n"); |
| 240 | } else if (mnt_flags & NTFS_MF_MOUNTED) { |
| 241 | if (!force) { |
| 242 | ntfs_log_error("%s", opened_volume_msg); |
| 243 | ntfs_log_error("You can use force option to avoid this " |
| 244 | "check, but this is not recommended\n" |
| 245 | "and may lead to data corruption.\n"); |
| 246 | return 0; |
| 247 | } |
| 248 | ntfs_log_warning("Forced to continue.\n"); |
| 249 | } |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * utils_mount_volume - Mount an NTFS volume |
| 256 | */ |
| 257 | ntfs_volume * utils_mount_volume(const char *device, unsigned long flags) |
| 258 | { |
| 259 | ntfs_volume *vol; |
| 260 | |
| 261 | if (!device) { |
| 262 | errno = EINVAL; |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | /* Porting notes: |
| 267 | * |
| 268 | * libntfs-3g does not have the 'force' flag in ntfs_mount_flags. |
| 269 | * The 'force' flag in libntfs bypasses two safety checks when mounting |
| 270 | * read/write: |
| 271 | * 1. Do not mount when the VOLUME_IS_DIRTY flag in |
| 272 | * VOLUME_INFORMATION is set. |
| 273 | * 2. Do not mount when the logfile is unclean. |
| 274 | * |
| 275 | * libntfs-3g only has safety check number 2. The dirty flag is simply |
| 276 | * ignored because we are confident that we can handle a dirty volume. |
| 277 | * So we treat NTFS_MNT_RECOVER like NTFS_MNT_FORCE, knowing that the |
| 278 | * first check is always bypassed. |
| 279 | */ |
| 280 | |
| 281 | if (!utils_valid_device(device, flags & NTFS_MNT_RECOVER)) |
| 282 | return NULL; |
| 283 | |
| 284 | vol = ntfs_mount(device, flags); |
| 285 | if (!vol) { |
| 286 | ntfs_log_perror("Failed to mount '%s'", device); |
| 287 | if (errno == EINVAL) |
| 288 | ntfs_log_error(invalid_ntfs_msg, device); |
| 289 | else if (errno == EIO) |
| 290 | ntfs_log_error("%s", corrupt_volume_msg); |
| 291 | else if (errno == EPERM) |
| 292 | ntfs_log_error("%s", hibernated_volume_msg); |
| 293 | else if (errno == EOPNOTSUPP) |
| 294 | ntfs_log_error("%s", unclean_journal_msg); |
| 295 | else if (errno == EBUSY) |
| 296 | ntfs_log_error("%s", opened_volume_msg); |
| 297 | else if (errno == ENXIO) |
| 298 | ntfs_log_error("%s", fakeraid_msg); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | /* Porting notes: |
| 303 | * libntfs-3g does not record whether the volume log file was dirty |
| 304 | * before mount, so we can only warn if the VOLUME_IS_DIRTY flag is set |
| 305 | * in VOLUME_INFORMATION. */ |
| 306 | if (vol->flags & VOLUME_IS_DIRTY) { |
| 307 | if (!(flags & NTFS_MNT_RECOVER)) { |
| 308 | ntfs_log_error("%s", dirty_volume_msg); |
| 309 | ntfs_umount(vol, FALSE); |
| 310 | return NULL; |
| 311 | } |
| 312 | ntfs_log_error("WARNING: Dirty volume mount was forced by the " |
| 313 | "'force' mount option.\n"); |
| 314 | } |
| 315 | return vol; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * utils_parse_size - Convert a string representing a size |
| 320 | * @value: String to be parsed |
| 321 | * @size: Parsed size |
| 322 | * @scale: Whether or not to allow a suffix to scale the value |
| 323 | * |
| 324 | * Read a string and convert it to a number. Strings may be suffixed to scale |
| 325 | * them. Any number without a suffix is assumed to be in bytes. |
| 326 | * |
| 327 | * Suffix Description Multiple |
| 328 | * [tT] Terabytes 10^12 |
| 329 | * [gG] Gigabytes 10^9 |
| 330 | * [mM] Megabytes 10^6 |
| 331 | * [kK] Kilobytes 10^3 |
| 332 | * |
| 333 | * Notes: |
| 334 | * Only the first character of the suffix is read. |
| 335 | * The multipliers are decimal thousands, not binary: 1000, not 1024. |
| 336 | * If parse_size fails, @size will not be changed |
| 337 | * |
| 338 | * Return: 1 Success |
| 339 | * 0 Error, the string was malformed |
| 340 | */ |
| 341 | int utils_parse_size(const char *value, s64 *size, BOOL scale) |
| 342 | { |
| 343 | long long result; |
| 344 | char *suffix = NULL; |
| 345 | |
| 346 | if (!value || !size) { |
| 347 | errno = EINVAL; |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | ntfs_log_debug("Parsing size '%s'.\n", value); |
| 352 | |
| 353 | result = strtoll(value, &suffix, 0); |
| 354 | if (result < 0 || errno == ERANGE) { |
| 355 | ntfs_log_error("Invalid size '%s'.\n", value); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | if (!suffix) { |
| 360 | ntfs_log_error("Internal error, strtoll didn't return a suffix.\n"); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | if (scale) { |
| 365 | switch (suffix[0]) { |
| 366 | case 't': case 'T': result *= 1000; |
| 367 | case 'g': case 'G': result *= 1000; |
| 368 | case 'm': case 'M': result *= 1000; |
| 369 | case 'k': case 'K': result *= 1000; |
| 370 | case '-': case 0: |
| 371 | break; |
| 372 | default: |
| 373 | ntfs_log_error("Invalid size suffix '%s'. Use T, G, M, or K.\n", suffix); |
| 374 | return 0; |
| 375 | } |
| 376 | } else { |
| 377 | if ((suffix[0] != '-') && (suffix[0] != 0)) { |
| 378 | ntfs_log_error("Invalid number '%.*s'.\n", (int)(suffix - value + 1), value); |
| 379 | return 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | ntfs_log_debug("Parsed size = %lld.\n", result); |
| 384 | *size = result; |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * utils_parse_range - Convert a string representing a range of numbers |
| 390 | * @string: The string to be parsed |
| 391 | * @start: The beginning of the range will be stored here |
| 392 | * @finish: The end of the range will be stored here |
| 393 | * |
| 394 | * Read a string of the form n-m. If the lower end is missing, zero will be |
| 395 | * substituted. If the upper end is missing LONG_MAX will be used. If the |
| 396 | * string cannot be parsed correctly, @start and @finish will not be changed. |
| 397 | * |
| 398 | * Return: 1 Success, a valid string was found |
| 399 | * 0 Error, the string was not a valid range |
| 400 | */ |
| 401 | int utils_parse_range(const char *string, s64 *start, s64 *finish, BOOL scale) |
| 402 | { |
| 403 | s64 a, b; |
| 404 | char *middle; |
| 405 | |
| 406 | if (!string || !start || !finish) { |
| 407 | errno = EINVAL; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | middle = strchr(string, '-'); |
| 412 | if (string == middle) { |
| 413 | ntfs_log_debug("Range has no beginning, defaulting to 0.\n"); |
| 414 | a = 0; |
| 415 | } else { |
| 416 | if (!utils_parse_size(string, &a, scale)) |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | if (middle) { |
| 421 | if (middle[1] == 0) { |
| 422 | b = LONG_MAX; // XXX ULLONG_MAX |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 423 | ntfs_log_debug("Range has no end, defaulting to " |
| 424 | "%lld.\n", (long long)b); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 425 | } else { |
| 426 | if (!utils_parse_size(middle+1, &b, scale)) |
| 427 | return 0; |
| 428 | } |
| 429 | } else { |
| 430 | b = a; |
| 431 | } |
| 432 | |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 433 | ntfs_log_debug("Range '%s' = %lld - %lld\n", string, (long long)a, |
| 434 | (long long)b); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 435 | |
| 436 | *start = a; |
| 437 | *finish = b; |
| 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * find_attribute - Find an attribute of the given type |
| 443 | * @type: An attribute type, e.g. AT_FILE_NAME |
| 444 | * @ctx: A search context, created using ntfs_get_attr_search_ctx |
| 445 | * |
| 446 | * Using the search context to keep track, find the first/next occurrence of a |
| 447 | * given attribute type. |
| 448 | * |
| 449 | * N.B. This will return a pointer into @mft. As long as the search context |
| 450 | * has been created without an inode, it won't overflow the buffer. |
| 451 | * |
| 452 | * Return: Pointer Success, an attribute was found |
| 453 | * NULL Error, no matching attributes were found |
| 454 | */ |
| 455 | ATTR_RECORD * find_attribute(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx) |
| 456 | { |
| 457 | if (!ctx) { |
| 458 | errno = EINVAL; |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | if (ntfs_attr_lookup(type, NULL, 0, 0, 0, NULL, 0, ctx) != 0) { |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 463 | ntfs_log_debug("find_attribute didn't find an attribute of type: 0x%02x.\n", le32_to_cpu(type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 464 | return NULL; /* None / no more of that type */ |
| 465 | } |
| 466 | |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 467 | ntfs_log_debug("find_attribute found an attribute of type: 0x%02x.\n", le32_to_cpu(type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 468 | return ctx->attr; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * find_first_attribute - Find the first attribute of a given type |
| 473 | * @type: An attribute type, e.g. AT_FILE_NAME |
| 474 | * @mft: A buffer containing a raw MFT record |
| 475 | * |
| 476 | * Search through a raw MFT record for an attribute of a given type. |
| 477 | * The return value is a pointer into the MFT record that was supplied. |
| 478 | * |
| 479 | * N.B. This will return a pointer into @mft. The pointer won't stray outside |
| 480 | * the buffer, since we created the search context without an inode. |
| 481 | * |
| 482 | * Return: Pointer Success, an attribute was found |
| 483 | * NULL Error, no matching attributes were found |
| 484 | */ |
| 485 | ATTR_RECORD * find_first_attribute(const ATTR_TYPES type, MFT_RECORD *mft) |
| 486 | { |
| 487 | ntfs_attr_search_ctx *ctx; |
| 488 | ATTR_RECORD *rec; |
| 489 | |
| 490 | if (!mft) { |
| 491 | errno = EINVAL; |
| 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | ctx = ntfs_attr_get_search_ctx(NULL, mft); |
| 496 | if (!ctx) { |
| 497 | ntfs_log_error("Couldn't create a search context.\n"); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | rec = find_attribute(type, ctx); |
| 502 | ntfs_attr_put_search_ctx(ctx); |
| 503 | if (rec) |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 504 | ntfs_log_debug("find_first_attribute: found attr of type 0x%02x.\n", le32_to_cpu(type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 505 | else |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 506 | ntfs_log_debug("find_first_attribute: didn't find attr of type 0x%02x.\n", le32_to_cpu(type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 507 | return rec; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * utils_inode_get_name |
| 512 | * |
| 513 | * using inode |
| 514 | * get filename |
| 515 | * add name to list |
| 516 | * get parent |
| 517 | * if parent is 5 (/) stop |
| 518 | * get inode of parent |
| 519 | */ |
| 520 | #define max_path 20 |
| 521 | int utils_inode_get_name(ntfs_inode *inode, char *buffer, int bufsize) |
| 522 | { |
| 523 | // XXX option: names = posix/win32 or dos |
| 524 | // flags: path, filename, or both |
| 525 | |
| 526 | |
| 527 | ntfs_volume *vol; |
| 528 | ntfs_attr_search_ctx *ctx; |
| 529 | ATTR_RECORD *rec; |
| 530 | FILE_NAME_ATTR *attr; |
| 531 | int name_space; |
| 532 | MFT_REF parent = FILE_root; |
| 533 | char *names[max_path + 1];// XXX ntfs_malloc? and make max bigger? |
| 534 | int i, len, offset = 0; |
| 535 | |
| 536 | if (!inode || !buffer) { |
| 537 | errno = EINVAL; |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | vol = inode->vol; |
| 542 | |
| 543 | //ntfs_log_debug("sizeof(char*) = %d, sizeof(names) = %d\n", sizeof(char*), sizeof(names)); |
| 544 | memset(names, 0, sizeof(names)); |
| 545 | |
| 546 | for (i = 0; i < max_path; i++) { |
| 547 | |
| 548 | ctx = ntfs_attr_get_search_ctx(inode, NULL); |
| 549 | if (!ctx) { |
| 550 | ntfs_log_error("Couldn't create a search context.\n"); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | //ntfs_log_debug("i = %d, inode = %p (%lld)\n", i, inode, inode->mft_no); |
| 555 | |
| 556 | name_space = 4; |
| 557 | while ((rec = find_attribute(AT_FILE_NAME, ctx))) { |
| 558 | /* We know this will always be resident. */ |
| 559 | attr = (FILE_NAME_ATTR *) ((char *) rec + le16_to_cpu(rec->value_offset)); |
| 560 | |
| 561 | if (attr->file_name_type > name_space) { //XXX find the ... |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | name_space = attr->file_name_type; |
| 566 | parent = le64_to_cpu(attr->parent_directory); |
| 567 | |
| 568 | if (names[i]) { |
| 569 | free(names[i]); |
| 570 | names[i] = NULL; |
| 571 | } |
| 572 | |
| 573 | if (ntfs_ucstombs(attr->file_name, attr->file_name_length, |
| 574 | &names[i], 0) < 0) { |
| 575 | char *temp; |
| 576 | ntfs_log_error("Couldn't translate filename to current locale.\n"); |
| 577 | temp = ntfs_malloc(30); |
| 578 | if (!temp) |
| 579 | return 0; |
| 580 | snprintf(temp, 30, "<MFT%llu>", (unsigned |
| 581 | long long)inode->mft_no); |
| 582 | names[i] = temp; |
| 583 | } |
| 584 | |
| 585 | //ntfs_log_debug("names[%d] %s\n", i, names[i]); |
| 586 | //ntfs_log_debug("parent = %lld\n", MREF(parent)); |
| 587 | } |
| 588 | |
| 589 | ntfs_attr_put_search_ctx(ctx); |
| 590 | |
| 591 | if (i > 0) /* Don't close the original inode */ |
| 592 | ntfs_inode_close(inode); |
| 593 | |
| 594 | if (MREF(parent) == FILE_root) { /* The root directory, stop. */ |
| 595 | //ntfs_log_debug("inode 5\n"); |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | inode = ntfs_inode_open(vol, parent); |
| 600 | if (!inode) { |
| 601 | ntfs_log_error("Couldn't open inode %llu.\n", |
| 602 | (unsigned long long)MREF(parent)); |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (i >= max_path) { |
| 608 | /* If we get into an infinite loop, we'll end up here. */ |
| 609 | ntfs_log_error("The directory structure is too deep (over %d) nested directories.\n", max_path); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* Assemble the names in the correct order. */ |
| 614 | for (i = max_path; i >= 0; i--) { |
| 615 | if (!names[i]) |
| 616 | continue; |
| 617 | |
| 618 | len = snprintf(buffer + offset, bufsize - offset, "%c%s", PATH_SEP, names[i]); |
| 619 | if (len >= (bufsize - offset)) { |
| 620 | ntfs_log_error("Pathname was truncated.\n"); |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | offset += len; |
| 625 | } |
| 626 | |
| 627 | /* Free all the allocated memory */ |
| 628 | for (i = 0; i < max_path; i++) |
| 629 | free(names[i]); |
| 630 | |
| 631 | ntfs_log_debug("Pathname: %s\n", buffer); |
| 632 | |
| 633 | return 1; |
| 634 | } |
| 635 | #undef max_path |
| 636 | |
| 637 | /** |
| 638 | * utils_attr_get_name |
| 639 | */ |
| 640 | int utils_attr_get_name(ntfs_volume *vol, ATTR_RECORD *attr, char *buffer, int bufsize) |
| 641 | { |
| 642 | int len, namelen; |
| 643 | char *name; |
| 644 | ATTR_DEF *attrdef; |
| 645 | |
| 646 | // flags: attr, name, or both |
| 647 | if (!attr || !buffer) { |
| 648 | errno = EINVAL; |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | attrdef = ntfs_attr_find_in_attrdef(vol, attr->type); |
| 653 | if (attrdef) { |
| 654 | name = NULL; |
| 655 | namelen = ntfs_ucsnlen(attrdef->name, sizeof(attrdef->name)); |
| 656 | if (ntfs_ucstombs(attrdef->name, namelen, &name, 0) < 0) { |
| 657 | ntfs_log_error("Couldn't translate attribute type to " |
| 658 | "current locale.\n"); |
| 659 | // <UNKNOWN>? |
| 660 | return 0; |
| 661 | } |
| 662 | len = snprintf(buffer, bufsize, "%s", name); |
| 663 | } else { |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 664 | ntfs_log_error("Unknown attribute type 0x%02x\n", le32_to_cpu(attr->type)); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 665 | len = snprintf(buffer, bufsize, "<UNKNOWN>"); |
| 666 | } |
| 667 | |
| 668 | if (len >= bufsize) { |
| 669 | ntfs_log_error("Attribute type was truncated.\n"); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | if (!attr->name_length) { |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | buffer += len; |
| 678 | bufsize -= len; |
| 679 | |
| 680 | name = NULL; |
| 681 | namelen = attr->name_length; |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 682 | if (ntfs_ucstombs((ntfschar *)((char *)attr |
| 683 | + le16_to_cpu(attr->name_offset)), |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 684 | namelen, &name, 0) < 0) { |
| 685 | ntfs_log_error("Couldn't translate attribute name to current " |
| 686 | "locale.\n"); |
| 687 | // <UNKNOWN>? |
| 688 | len = snprintf(buffer, bufsize, "<UNKNOWN>"); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | len = snprintf(buffer, bufsize, "(%s)", name); |
| 693 | free(name); |
| 694 | |
| 695 | if (len >= bufsize) { |
| 696 | ntfs_log_error("Attribute name was truncated.\n"); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * utils_cluster_in_use - Determine if a cluster is in use |
| 705 | * @vol: An ntfs volume obtained from ntfs_mount |
| 706 | * @lcn: The Logical Cluster Number to test |
| 707 | * |
| 708 | * The metadata file $Bitmap has one binary bit representing each cluster on |
| 709 | * disk. The bit will be set for each cluster that is in use. The function |
| 710 | * reads the relevant part of $Bitmap into a buffer and tests the bit. |
| 711 | * |
| 712 | * This function has a static buffer in which it caches a section of $Bitmap. |
| 713 | * If the lcn, being tested, lies outside the range, the buffer will be |
| 714 | * refreshed. @bmplcn stores offset to the first bit (in bits) stored in the |
| 715 | * buffer. |
| 716 | * |
| 717 | * NOTE: Be very carefull with shifts by 3 everywhere in this function. |
| 718 | * |
| 719 | * Return: 1 Cluster is in use |
| 720 | * 0 Cluster is free space |
| 721 | * -1 Error occurred |
| 722 | */ |
| 723 | int utils_cluster_in_use(ntfs_volume *vol, long long lcn) |
| 724 | { |
| 725 | static unsigned char buffer[512]; |
| 726 | static long long bmplcn = -(sizeof(buffer) << 3); |
| 727 | int byte, bit; |
| 728 | ntfs_attr *attr; |
| 729 | |
| 730 | if (!vol) { |
| 731 | errno = EINVAL; |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | /* Does lcn lie in the section of $Bitmap we already have cached? */ |
| 736 | if ((lcn < bmplcn) |
| 737 | || (lcn >= (long long)(bmplcn + (sizeof(buffer) << 3)))) { |
| 738 | ntfs_log_debug("Bit lies outside cache.\n"); |
| 739 | attr = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, AT_UNNAMED, 0); |
| 740 | if (!attr) { |
| 741 | ntfs_log_perror("Couldn't open $Bitmap"); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | /* Mark the buffer as in use, in case the read is shorter. */ |
| 746 | memset(buffer, 0xFF, sizeof(buffer)); |
| 747 | bmplcn = lcn & (~((sizeof(buffer) << 3) - 1)); |
| 748 | |
| 749 | if (ntfs_attr_pread(attr, (bmplcn >> 3), sizeof(buffer), |
| 750 | buffer) < 0) { |
| 751 | ntfs_log_perror("Couldn't read $Bitmap"); |
| 752 | ntfs_attr_close(attr); |
| 753 | return -1; |
| 754 | } |
| 755 | |
| 756 | ntfs_log_debug("Reloaded bitmap buffer.\n"); |
| 757 | ntfs_attr_close(attr); |
| 758 | } |
| 759 | |
| 760 | bit = 1 << (lcn & 7); |
| 761 | byte = (lcn >> 3) & (sizeof(buffer) - 1); |
| 762 | ntfs_log_debug("cluster = %lld, bmplcn = %lld, byte = %d, bit = %d, " |
| 763 | "in use %d\n", lcn, bmplcn, byte, bit, buffer[byte] & |
| 764 | bit); |
| 765 | |
| 766 | return (buffer[byte] & bit); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * utils_mftrec_in_use - Determine if a MFT Record is in use |
| 771 | * @vol: An ntfs volume obtained from ntfs_mount |
| 772 | * @mref: MFT Reference (inode number) |
| 773 | * |
| 774 | * The metadata file $BITMAP has one binary bit representing each record in the |
| 775 | * MFT. The bit will be set for each record that is in use. The function |
| 776 | * reads the relevant part of $BITMAP into a buffer and tests the bit. |
| 777 | * |
| 778 | * This function has a static buffer in which it caches a section of $BITMAP. |
| 779 | * If the mref, being tested, lies outside the range, the buffer will be |
| 780 | * refreshed. |
| 781 | * |
| 782 | * Return: 1 MFT Record is in use |
| 783 | * 0 MFT Record is unused |
| 784 | * -1 Error occurred |
| 785 | */ |
| 786 | int utils_mftrec_in_use(ntfs_volume *vol, MFT_REF mref) |
| 787 | { |
| 788 | static u8 buffer[512]; |
| 789 | static s64 bmpmref = -(sizeof(buffer) << 3) - 1; /* Which bit of $BITMAP is in the buffer */ |
| 790 | int byte, bit; |
| 791 | |
| 792 | ntfs_log_trace("Entering.\n"); |
| 793 | |
| 794 | if (!vol) { |
| 795 | errno = EINVAL; |
| 796 | return -1; |
| 797 | } |
| 798 | |
| 799 | /* Does mref lie in the section of $Bitmap we already have cached? */ |
| 800 | if (((s64)MREF(mref) < bmpmref) |
| 801 | || ((s64)MREF(mref) >= (s64)(bmpmref + (sizeof(buffer) << 3)))) { |
| 802 | ntfs_log_debug("Bit lies outside cache.\n"); |
| 803 | |
| 804 | /* Mark the buffer as not in use, in case the read is shorter. */ |
| 805 | memset(buffer, 0, sizeof(buffer)); |
| 806 | bmpmref = mref & (~((sizeof(buffer) << 3) - 1)); |
| 807 | |
| 808 | if (ntfs_attr_pread(vol->mftbmp_na, (bmpmref>>3), sizeof(buffer), buffer) < 0) { |
| 809 | ntfs_log_perror("Couldn't read $MFT/$BITMAP"); |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | ntfs_log_debug("Reloaded bitmap buffer.\n"); |
| 814 | } |
| 815 | |
| 816 | bit = 1 << (mref & 7); |
| 817 | byte = (mref >> 3) & (sizeof(buffer) - 1); |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 818 | ntfs_log_debug("cluster = %lld, bmpmref = %lld, byte = %d, bit = %d, " |
| 819 | "in use %d\n", (long long) mref, (long long) bmpmref, |
| 820 | byte, bit, buffer[byte] & bit); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 821 | |
| 822 | return (buffer[byte] & bit); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * __metadata |
| 827 | */ |
| 828 | static int __metadata(ntfs_volume *vol, u64 num) |
| 829 | { |
| 830 | if (num <= FILE_UpCase) |
| 831 | return 1; |
| 832 | if (!vol) |
| 833 | return -1; |
| 834 | if ((vol->major_ver == 3) && (num == FILE_Extend)) |
| 835 | return 1; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * utils_is_metadata - Determine if an inode represents a metadata file |
| 842 | * @inode: An ntfs inode to be tested |
| 843 | * |
| 844 | * A handful of files in the volume contain filesystem data - metadata. |
| 845 | * They can be identified by their inode number (offset in MFT/$DATA) or by |
| 846 | * their parent. |
| 847 | * |
| 848 | * Return: 1 inode is a metadata file |
| 849 | * 0 inode is not a metadata file |
| 850 | * -1 Error occurred |
| 851 | */ |
| 852 | int utils_is_metadata(ntfs_inode *inode) |
| 853 | { |
| 854 | ntfs_volume *vol; |
| 855 | ATTR_RECORD *rec; |
| 856 | FILE_NAME_ATTR *attr; |
| 857 | MFT_RECORD *file; |
| 858 | u64 num; |
| 859 | |
| 860 | if (!inode) { |
| 861 | errno = EINVAL; |
| 862 | return -1; |
| 863 | } |
| 864 | |
| 865 | vol = inode->vol; |
| 866 | if (!vol) |
| 867 | return -1; |
| 868 | |
| 869 | num = inode->mft_no; |
| 870 | if (__metadata(vol, num) == 1) |
| 871 | return 1; |
| 872 | |
| 873 | file = inode->mrec; |
| 874 | if (file && (file->base_mft_record != 0)) { |
| 875 | num = MREF_LE(file->base_mft_record); |
| 876 | if (__metadata(vol, num) == 1) |
| 877 | return 1; |
| 878 | } |
| 879 | |
| 880 | rec = find_first_attribute(AT_FILE_NAME, inode->mrec); |
| 881 | if (!rec) |
| 882 | return -1; |
| 883 | |
| 884 | /* We know this will always be resident. */ |
| 885 | attr = (FILE_NAME_ATTR *)((char *)rec + le16_to_cpu(rec->value_offset)); |
| 886 | |
| 887 | num = MREF_LE(attr->parent_directory); |
| 888 | if ((num != FILE_root) && (__metadata(vol, num) == 1)) |
| 889 | return 1; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * utils_dump_mem - Display a block of memory in hex and ascii |
| 896 | * @buf: Buffer to be displayed |
| 897 | * @start: Offset into @buf to start from |
| 898 | * @length: Number of bytes to display |
| 899 | * @flags: Options to change the style of the output |
| 900 | * |
| 901 | * Display a block of memory in a tradition hex-dump manner. |
| 902 | * Optionally the ascii part can be turned off. |
| 903 | * |
| 904 | * The flags, described fully in utils.h, default to 0 (DM_DEFAULTS). |
| 905 | * Examples are: DM_INDENT (indent the output by one tab); DM_RED (colour the |
| 906 | * output); DM_NO_ASCII (only print the hex values). |
| 907 | */ |
| 908 | void utils_dump_mem(void *buf, int start, int length, int flags) |
| 909 | { |
| 910 | int off, i, s, e, col; |
| 911 | u8 *mem = buf; |
| 912 | |
| 913 | s = start & ~15; // round down |
| 914 | e = (start + length + 15) & ~15; // round up |
| 915 | |
| 916 | for (off = s; off < e; off += 16) { |
| 917 | col = 30; |
| 918 | if (flags & DM_RED) |
| 919 | col += 1; |
| 920 | if (flags & DM_GREEN) |
| 921 | col += 2; |
| 922 | if (flags & DM_BLUE) |
| 923 | col += 4; |
| 924 | if (flags & DM_INDENT) |
| 925 | ntfs_log_debug("\t"); |
| 926 | if (flags & DM_BOLD) |
| 927 | ntfs_log_debug("\e[01m"); |
| 928 | if (flags & (DM_RED | DM_BLUE | DM_GREEN | DM_BOLD)) |
| 929 | ntfs_log_debug("\e[%dm", col); |
| 930 | if (off == s) |
| 931 | ntfs_log_debug("%6.6x ", start); |
| 932 | else |
| 933 | ntfs_log_debug("%6.6x ", off); |
| 934 | |
| 935 | for (i = 0; i < 16; i++) { |
| 936 | if ((i == 8) && (!(flags & DM_NO_DIVIDER))) |
| 937 | ntfs_log_debug(" -"); |
| 938 | if (((off+i) >= start) && ((off+i) < (start+length))) |
| 939 | ntfs_log_debug(" %02X", mem[off+i]); |
| 940 | else |
| 941 | ntfs_log_debug(" "); |
| 942 | } |
| 943 | if (!(flags & DM_NO_ASCII)) { |
| 944 | ntfs_log_debug(" "); |
| 945 | for (i = 0; i < 16; i++) { |
| 946 | if (((off+i) < start) || ((off+i) >= (start+length))) |
| 947 | ntfs_log_debug(" "); |
| 948 | else if (isprint(mem[off + i])) |
| 949 | ntfs_log_debug("%c", mem[off + i]); |
| 950 | else |
| 951 | ntfs_log_debug("."); |
| 952 | } |
| 953 | } |
| 954 | if (flags & (DM_RED | DM_BLUE | DM_GREEN | DM_BOLD)) |
| 955 | ntfs_log_debug("\e[0m"); |
| 956 | ntfs_log_debug("\n"); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | |
| 961 | /** |
| 962 | * mft_get_search_ctx |
| 963 | */ |
| 964 | struct mft_search_ctx * mft_get_search_ctx(ntfs_volume *vol) |
| 965 | { |
| 966 | struct mft_search_ctx *ctx; |
| 967 | |
| 968 | if (!vol) { |
| 969 | errno = EINVAL; |
| 970 | return NULL; |
| 971 | } |
| 972 | |
| 973 | ctx = (struct mft_search_ctx*)calloc(1, sizeof *ctx); |
| 974 | |
| 975 | ctx->mft_num = -1; |
| 976 | ctx->vol = vol; |
| 977 | |
| 978 | return ctx; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * mft_put_search_ctx |
| 983 | */ |
| 984 | void mft_put_search_ctx(struct mft_search_ctx *ctx) |
| 985 | { |
| 986 | if (!ctx) |
| 987 | return; |
| 988 | if (ctx->inode) |
| 989 | ntfs_inode_close(ctx->inode); |
| 990 | free(ctx); |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * mft_next_record |
| 995 | */ |
| 996 | int mft_next_record(struct mft_search_ctx *ctx) |
| 997 | { |
| 998 | s64 nr_mft_records; |
| 999 | ATTR_RECORD *attr10 = NULL; |
| 1000 | ATTR_RECORD *attr20 = NULL; |
| 1001 | ATTR_RECORD *attr80 = NULL; |
| 1002 | ntfs_attr_search_ctx *attr_ctx; |
| 1003 | |
| 1004 | if (!ctx) { |
| 1005 | errno = EINVAL; |
| 1006 | return -1; |
| 1007 | } |
| 1008 | |
| 1009 | if (ctx->inode) { |
| 1010 | ntfs_inode_close(ctx->inode); |
| 1011 | ctx->inode = NULL; |
| 1012 | } |
| 1013 | |
| 1014 | nr_mft_records = ctx->vol->mft_na->initialized_size >> |
| 1015 | ctx->vol->mft_record_size_bits; |
| 1016 | |
| 1017 | for (ctx->mft_num++; (s64)ctx->mft_num < nr_mft_records; ctx->mft_num++) { |
| 1018 | int in_use; |
| 1019 | |
| 1020 | ctx->flags_match = 0; |
| 1021 | in_use = utils_mftrec_in_use(ctx->vol, (MFT_REF) ctx->mft_num); |
| 1022 | if (in_use == -1) { |
| 1023 | ntfs_log_error("Error reading inode %llu. Aborting.\n", |
| 1024 | (unsigned long long)ctx->mft_num); |
| 1025 | return -1; |
| 1026 | } |
| 1027 | |
| 1028 | if (in_use) { |
| 1029 | ctx->flags_match |= FEMR_IN_USE; |
| 1030 | |
| 1031 | ctx->inode = ntfs_inode_open(ctx->vol, (MFT_REF) ctx->mft_num); |
| 1032 | if (ctx->inode == NULL) { |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 1033 | MFT_RECORD *mrec; |
| 1034 | int r; |
| 1035 | MFT_REF base_inode; |
| 1036 | |
| 1037 | mrec = (MFT_RECORD*)NULL; |
| 1038 | r = ntfs_file_record_read(ctx->vol, |
| 1039 | (MFT_REF) ctx->mft_num, &mrec, NULL); |
| 1040 | if (r || !mrec || !mrec->base_mft_record) |
| 1041 | ntfs_log_error( |
| 1042 | "Error reading inode %lld.\n", |
| 1043 | (long long)ctx->mft_num); |
| 1044 | else { |
| 1045 | base_inode = le64_to_cpu( |
| 1046 | mrec->base_mft_record); |
| 1047 | ntfs_log_error("Inode %lld is an " |
| 1048 | "extent of inode %lld.\n", |
| 1049 | (long long)ctx->mft_num, |
| 1050 | (long long)MREF(base_inode)); |
| 1051 | } |
| 1052 | free (mrec); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | attr10 = find_first_attribute(AT_STANDARD_INFORMATION, ctx->inode->mrec); |
| 1057 | attr20 = find_first_attribute(AT_ATTRIBUTE_LIST, ctx->inode->mrec); |
| 1058 | attr80 = find_first_attribute(AT_DATA, ctx->inode->mrec); |
| 1059 | |
| 1060 | if (attr10) |
| 1061 | ctx->flags_match |= FEMR_BASE_RECORD; |
| 1062 | else |
| 1063 | ctx->flags_match |= FEMR_NOT_BASE_RECORD; |
| 1064 | |
| 1065 | if (attr20) |
| 1066 | ctx->flags_match |= FEMR_BASE_RECORD; |
| 1067 | |
| 1068 | if (attr80) |
| 1069 | ctx->flags_match |= FEMR_FILE; |
| 1070 | |
| 1071 | if (ctx->flags_search & FEMR_DIR) { |
| 1072 | attr_ctx = ntfs_attr_get_search_ctx(ctx->inode, NULL); |
| 1073 | if (attr_ctx) { |
| 1074 | if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, 0, 0, NULL, 0, attr_ctx) == 0) |
| 1075 | ctx->flags_match |= FEMR_DIR; |
| 1076 | |
| 1077 | ntfs_attr_put_search_ctx(attr_ctx); |
| 1078 | } else { |
| 1079 | ntfs_log_error("Couldn't create a search context.\n"); |
| 1080 | return -1; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | switch (utils_is_metadata(ctx->inode)) { |
| 1085 | case 1: ctx->flags_match |= FEMR_METADATA; break; |
| 1086 | case 0: ctx->flags_match |= FEMR_NOT_METADATA; break; |
| 1087 | default: |
| 1088 | ctx->flags_match |= FEMR_NOT_METADATA; break; |
| 1089 | //ntfs_log_error("Error reading inode %lld.\n", ctx->mft_num); |
| 1090 | //return -1; |
| 1091 | } |
| 1092 | |
| 1093 | } else { // !in_use |
| 1094 | ntfs_attr *mft; |
| 1095 | |
| 1096 | ctx->flags_match |= FEMR_NOT_IN_USE; |
| 1097 | |
| 1098 | ctx->inode = (ntfs_inode*)calloc(1, sizeof(*ctx->inode)); |
| 1099 | if (!ctx->inode) { |
| 1100 | ntfs_log_error("Out of memory. Aborting.\n"); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | |
| 1104 | ctx->inode->mft_no = ctx->mft_num; |
| 1105 | ctx->inode->vol = ctx->vol; |
| 1106 | ctx->inode->mrec = ntfs_malloc(ctx->vol->mft_record_size); |
| 1107 | if (!ctx->inode->mrec) { |
| 1108 | free(ctx->inode); // == ntfs_inode_close |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | mft = ntfs_attr_open(ctx->vol->mft_ni, AT_DATA, |
| 1113 | AT_UNNAMED, 0); |
| 1114 | if (!mft) { |
| 1115 | ntfs_log_perror("Couldn't open $MFT/$DATA"); |
| 1116 | // free / close |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | if (ntfs_attr_pread(mft, ctx->vol->mft_record_size * ctx->mft_num, ctx->vol->mft_record_size, ctx->inode->mrec) < ctx->vol->mft_record_size) { |
| 1121 | ntfs_log_perror("Couldn't read MFT Record %llu", |
| 1122 | (unsigned long long) ctx->mft_num); |
| 1123 | // free / close |
| 1124 | ntfs_attr_close(mft); |
| 1125 | return -1; |
| 1126 | } |
| 1127 | |
| 1128 | ntfs_attr_close(mft); |
| 1129 | } |
| 1130 | |
| 1131 | if (ctx->flags_match & ctx->flags_search) { |
| 1132 | break; |
| 1133 | } |
| 1134 | |
| 1135 | if (ntfs_inode_close(ctx->inode)) { |
| 1136 | ntfs_log_error("Error closing inode %llu.\n", |
| 1137 | (unsigned long long)ctx->mft_num); |
| 1138 | return -errno; |
| 1139 | } |
| 1140 | |
| 1141 | ctx->inode = NULL; |
| 1142 | } |
| 1143 | |
| 1144 | return (ctx->inode == NULL); |
| 1145 | } |
| 1146 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 1147 | #ifdef HAVE_WINDOWS_H |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1148 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 1149 | /* |
| 1150 | * Translate formats for older Windows |
| 1151 | * |
| 1152 | * Up to Windows XP, msvcrt.dll does not support long long format |
| 1153 | * specifications (%lld, %llx, etc). We have to translate them |
| 1154 | * to %I64. |
| 1155 | */ |
| 1156 | |
| 1157 | char *ntfs_utils_reformat(char *out, int sz, const char *fmt) |
| 1158 | { |
| 1159 | const char *f; |
| 1160 | char *p; |
| 1161 | int i; |
| 1162 | enum { F_INIT, F_PERCENT, F_FIRST } state; |
| 1163 | |
| 1164 | i = 0; |
| 1165 | f = fmt; |
| 1166 | p = out; |
| 1167 | state = F_INIT; |
| 1168 | while (*f && ((i + 3) < sz)) { |
| 1169 | switch (state) { |
| 1170 | case F_INIT : |
| 1171 | if (*f == '%') |
| 1172 | state = F_PERCENT; |
| 1173 | *p++ = *f++; |
| 1174 | i++; |
| 1175 | break; |
| 1176 | case F_PERCENT : |
| 1177 | if (*f == 'l') { |
| 1178 | state = F_FIRST; |
| 1179 | f++; |
| 1180 | } else { |
| 1181 | if (((*f < '0') || (*f > '9')) |
| 1182 | && (*f != '*') && (*f != '-')) |
| 1183 | state = F_INIT; |
| 1184 | *p++ = *f++; |
| 1185 | i++; |
| 1186 | } |
| 1187 | break; |
| 1188 | case F_FIRST : |
| 1189 | if (*f == 'l') { |
| 1190 | *p++ = 'I'; |
| 1191 | *p++ = '6'; |
| 1192 | *p++ = '4'; |
| 1193 | f++; |
| 1194 | i += 3; |
| 1195 | } else { |
| 1196 | *p++ = 'l'; |
| 1197 | *p++ = *f++; |
| 1198 | i += 2; |
| 1199 | } |
| 1200 | state = F_INIT; |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | *p++ = 0; |
| 1205 | return (out); |
| 1206 | } |
| 1207 | |
Steve Kondik | e68cb60 | 2016-08-28 00:45:36 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * Translate paths to files submitted from Windows |
| 1210 | * |
| 1211 | * Translate Windows directory separators to Unix ones |
| 1212 | * |
| 1213 | * Returns the translated path, to be freed by caller |
| 1214 | * NULL if there was an error, with errno set |
| 1215 | */ |
| 1216 | |
| 1217 | char *ntfs_utils_unix_path(const char *in) |
| 1218 | { |
| 1219 | char *out; |
| 1220 | int i; |
| 1221 | |
| 1222 | out = strdup(in); |
| 1223 | if (out) { |
| 1224 | for (i=0; in[i]; i++) |
| 1225 | if (in[i] == '\\') |
| 1226 | out[i] = '/'; |
| 1227 | } else |
| 1228 | errno = ENOMEM; |
| 1229 | return (out); |
| 1230 | } |
| 1231 | |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 1232 | #endif |