Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * ntfsls - Part of the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2003 Lode Leroy |
| 5 | * Copyright (c) 2003-2005 Anton Altaparmakov |
| 6 | * Copyright (c) 2003 Richard Russon |
| 7 | * Copyright (c) 2004 Carmelo Kintana |
| 8 | * Copyright (c) 2004 Giang Nguyen |
| 9 | * |
| 10 | * This utility will list a directory's files. |
| 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 | #include "config.h" |
| 28 | |
| 29 | #ifdef HAVE_STDIO_H |
| 30 | #include <stdio.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_STDLIB_H |
| 33 | #include <stdlib.h> |
| 34 | #endif |
| 35 | #ifdef HAVE_TIME_H |
| 36 | #include <time.h> |
| 37 | #endif |
| 38 | #ifdef HAVE_GETOPT_H |
| 39 | #include <getopt.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_STRING_H |
| 42 | #include <string.h> |
| 43 | #endif |
| 44 | |
| 45 | #include "types.h" |
| 46 | #include "mft.h" |
| 47 | #include "attrib.h" |
| 48 | #include "layout.h" |
| 49 | #include "inode.h" |
| 50 | #include "utils.h" |
| 51 | #include "dir.h" |
| 52 | #include "list.h" |
| 53 | #include "ntfstime.h" |
| 54 | /* #include "version.h" */ |
| 55 | #include "logging.h" |
| 56 | |
| 57 | static const char *EXEC_NAME = "ntfsls"; |
| 58 | |
| 59 | /** |
| 60 | * To hold sub-directory information for recursive listing. |
| 61 | * @depth: the level of this dir relative to opts.path |
| 62 | */ |
| 63 | struct dir { |
| 64 | struct ntfs_list_head list; |
| 65 | ntfs_inode *ni; |
| 66 | char name[MAX_PATH]; |
| 67 | int depth; |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * path_component - to store path component strings |
| 72 | * |
| 73 | * @name: string pointer |
| 74 | * |
| 75 | * NOTE: @name is not directly allocated memory. It simply points to the |
| 76 | * character array name in struct dir. |
| 77 | */ |
| 78 | struct path_component { |
| 79 | struct ntfs_list_head list; |
| 80 | const char *name; |
| 81 | }; |
| 82 | |
| 83 | /* The list of sub-dirs is like a "horizontal" tree. The root of |
| 84 | * the tree is opts.path, but it is not part of the list because |
| 85 | * that's not necessary. The rules of the list are (in order of |
| 86 | * precedence): |
| 87 | * 1. directories immediately follow their parent. |
| 88 | * 2. siblings are next to one another. |
| 89 | * |
| 90 | * For example, if: |
| 91 | * 1. opts.path is / |
| 92 | * 2. / has 2 sub-dirs: dir1 and dir2 |
| 93 | * 3. dir1 has 2 sub-dirs: dir11 and dir12 |
| 94 | * 4. dir2 has 0 sub-dirs |
| 95 | * then the list will be: |
| 96 | * dummy head -> dir1 -> dir11 -> dir12 -> dir2 |
| 97 | * |
| 98 | * dir_list_insert_pos keeps track of where to insert a sub-dir |
| 99 | * into the list. |
| 100 | */ |
| 101 | static struct ntfs_list_head *dir_list_insert_pos = NULL; |
| 102 | |
| 103 | /* The global depth relative to opts.path. |
| 104 | * ie: opts.path has depth 0, a sub-dir of opts.path has depth 1 |
| 105 | */ |
| 106 | static int depth = 0; |
| 107 | |
| 108 | static struct options { |
| 109 | char *device; /* Device/File to work with */ |
| 110 | int quiet; /* Less output */ |
| 111 | int verbose; /* Extra output */ |
| 112 | int force; /* Override common sense */ |
| 113 | int all; |
| 114 | int system; |
| 115 | int dos; |
| 116 | int lng; |
| 117 | int inode; |
| 118 | int classify; |
| 119 | int recursive; |
| 120 | const char *path; |
| 121 | } opts; |
| 122 | |
| 123 | typedef struct { |
| 124 | ntfs_volume *vol; |
| 125 | } ntfsls_dirent; |
| 126 | |
| 127 | static int list_dir_entry(ntfsls_dirent * dirent, const ntfschar * name, |
| 128 | const int name_len, const int name_type, |
| 129 | const s64 pos, const MFT_REF mref, |
| 130 | const unsigned dt_type); |
| 131 | |
| 132 | /** |
| 133 | * version - Print version information about the program |
| 134 | * |
| 135 | * Print a copyright statement and a brief description of the program. |
| 136 | * |
| 137 | * Return: none |
| 138 | */ |
| 139 | static void version(void) |
| 140 | { |
| 141 | printf("\n%s v%s (libntfs-3g) - Display information about an NTFS " |
| 142 | "Volume.\n\n", EXEC_NAME, VERSION); |
| 143 | printf("Copyright (c) 2003 Lode Leroy\n"); |
| 144 | printf("Copyright (c) 2003-2005 Anton Altaparmakov\n"); |
| 145 | printf("Copyright (c) 2003 Richard Russon\n"); |
| 146 | printf("Copyright (c) 2004 Carmelo Kintana\n"); |
| 147 | printf("Copyright (c) 2004 Giang Nguyen\n"); |
| 148 | printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * usage - Print a list of the parameters to the program |
| 153 | * |
| 154 | * Print a list of the parameters and options for the program. |
| 155 | * |
| 156 | * Return: none |
| 157 | */ |
| 158 | static void usage(void) |
| 159 | { |
| 160 | printf("\nUsage: %s [options] device\n" |
| 161 | "\n" |
| 162 | " -a, --all Display all files\n" |
| 163 | " -F, --classify Display classification\n" |
| 164 | " -f, --force Use less caution\n" |
| 165 | " -h, --help Display this help\n" |
| 166 | " -i, --inode Display inode numbers\n" |
| 167 | " -l, --long Display long info\n" |
| 168 | " -p, --path PATH Directory whose contents to list\n" |
| 169 | " -q, --quiet Less output\n" |
| 170 | " -R, --recursive Recursively list subdirectories\n" |
| 171 | " -s, --system Display system files\n" |
| 172 | " -V, --version Display version information\n" |
| 173 | " -v, --verbose More output\n" |
| 174 | " -x, --dos Use short (DOS 8.3) names\n" |
| 175 | "\n", |
| 176 | EXEC_NAME); |
| 177 | |
| 178 | printf("NOTE: If neither -a nor -s is specified, the program defaults to -a.\n\n"); |
| 179 | |
| 180 | printf("%s%s\n", ntfs_bugs, ntfs_home); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * parse_options - Read and validate the programs command line |
| 185 | * |
| 186 | * Read the command line, verify the syntax and parse the options. |
| 187 | * This function is very long, but quite simple. |
| 188 | * |
| 189 | * Return: 1 Success |
| 190 | * 0 Error, one or more problems |
| 191 | */ |
| 192 | static int parse_options(int argc, char *argv[]) |
| 193 | { |
| 194 | static const char *sopt = "-aFfh?ilp:qRsVvx"; |
| 195 | static const struct option lopt[] = { |
| 196 | { "all", no_argument, NULL, 'a' }, |
| 197 | { "classify", no_argument, NULL, 'F' }, |
| 198 | { "force", no_argument, NULL, 'f' }, |
| 199 | { "help", no_argument, NULL, 'h' }, |
| 200 | { "inode", no_argument, NULL, 'i' }, |
| 201 | { "long", no_argument, NULL, 'l' }, |
| 202 | { "path", required_argument, NULL, 'p' }, |
| 203 | { "recursive", no_argument, NULL, 'R' }, |
| 204 | { "quiet", no_argument, NULL, 'q' }, |
| 205 | { "system", no_argument, NULL, 's' }, |
| 206 | { "version", no_argument, NULL, 'V' }, |
| 207 | { "verbose", no_argument, NULL, 'v' }, |
| 208 | { "dos", no_argument, NULL, 'x' }, |
| 209 | { NULL, 0, NULL, 0 }, |
| 210 | }; |
| 211 | |
| 212 | int c = -1; |
| 213 | int err = 0; |
| 214 | int ver = 0; |
| 215 | int help = 0; |
| 216 | int levels = 0; |
| 217 | |
| 218 | opterr = 0; /* We'll handle the errors, thank you. */ |
| 219 | |
| 220 | memset(&opts, 0, sizeof(opts)); |
| 221 | opts.device = NULL; |
| 222 | opts.path = "/"; |
| 223 | |
| 224 | while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { |
| 225 | switch (c) { |
| 226 | case 1: |
| 227 | if (!opts.device) |
| 228 | opts.device = optarg; |
| 229 | else |
| 230 | err++; |
| 231 | break; |
| 232 | case 'p': |
| 233 | opts.path = optarg; |
| 234 | break; |
| 235 | case 'f': |
| 236 | opts.force++; |
| 237 | break; |
| 238 | case 'h': |
| 239 | case '?': |
| 240 | if (strncmp (argv[optind-1], "--log-", 6) == 0) { |
| 241 | if (!ntfs_log_parse_option (argv[optind-1])) |
| 242 | err++; |
| 243 | break; |
| 244 | } |
| 245 | help++; |
| 246 | break; |
| 247 | case 'q': |
| 248 | opts.quiet++; |
| 249 | ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); |
| 250 | break; |
| 251 | case 'v': |
| 252 | opts.verbose++; |
| 253 | ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); |
| 254 | break; |
| 255 | case 'V': |
| 256 | ver++; |
| 257 | break; |
| 258 | case 'x': |
| 259 | opts.dos = 1; |
| 260 | break; |
| 261 | case 'l': |
| 262 | opts.lng++; |
| 263 | break; |
| 264 | case 'i': |
| 265 | opts.inode++; |
| 266 | break; |
| 267 | case 'F': |
| 268 | opts.classify++; |
| 269 | break; |
| 270 | case 'a': |
| 271 | opts.all++; |
| 272 | break; |
| 273 | case 's': |
| 274 | opts.system++; |
| 275 | break; |
| 276 | case 'R': |
| 277 | opts.recursive++; |
| 278 | break; |
| 279 | default: |
| 280 | ntfs_log_error("Unknown option '%s'.\n", argv[optind - 1]); |
| 281 | err++; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* Make sure we're in sync with the log levels */ |
| 287 | levels = ntfs_log_get_levels(); |
| 288 | if (levels & NTFS_LOG_LEVEL_VERBOSE) |
| 289 | opts.verbose++; |
| 290 | if (!(levels & NTFS_LOG_LEVEL_QUIET)) |
| 291 | opts.quiet++; |
| 292 | |
| 293 | /* defaults to -a if -s is not specified */ |
| 294 | if (!opts.system) |
| 295 | opts.all++; |
| 296 | |
| 297 | if (help || ver) |
| 298 | opts.quiet = 0; |
| 299 | else { |
| 300 | if (opts.device == NULL) { |
| 301 | if (argc > 1) |
| 302 | ntfs_log_error("You must specify exactly one " |
| 303 | "device.\n"); |
| 304 | err++; |
| 305 | } |
| 306 | |
| 307 | if (opts.quiet && opts.verbose) { |
| 308 | ntfs_log_error("You may not use --quiet and --verbose at the " |
| 309 | "same time.\n"); |
| 310 | err++; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (ver) |
| 315 | version(); |
| 316 | if (help || err) |
| 317 | usage(); |
| 318 | |
| 319 | return (!err && !help && !ver); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * free_dir - free one dir |
| 324 | * @tofree: the dir to free |
| 325 | * |
| 326 | * Close the inode and then free the dir |
| 327 | */ |
| 328 | static void free_dir(struct dir *tofree) |
| 329 | { |
| 330 | if (tofree) { |
| 331 | if (tofree->ni) { |
| 332 | ntfs_inode_close(tofree->ni); |
| 333 | tofree->ni = NULL; |
| 334 | } |
| 335 | free(tofree); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * free_dirs - walk the list of dir's and free each of them |
| 341 | * @dir_list: the ntfs_list_head of any entry in the list |
| 342 | * |
| 343 | * Iterate over @dir_list, calling free_dir on each entry |
| 344 | */ |
| 345 | static void free_dirs(struct ntfs_list_head *dir_list) |
| 346 | { |
| 347 | struct dir *tofree = NULL; |
| 348 | struct ntfs_list_head *walker = NULL; |
| 349 | |
| 350 | if (dir_list) { |
| 351 | ntfs_list_for_each(walker, dir_list) { |
| 352 | free_dir(tofree); |
| 353 | tofree = ntfs_list_entry(walker, struct dir, list); |
| 354 | } |
| 355 | |
| 356 | free_dir(tofree); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * readdir_recursive - list a directory and sub-directories encountered |
| 362 | * @ni: ntfs inode of the directory to list |
| 363 | * @pos: current position in directory |
| 364 | * @dirent: context for filldir callback supplied by the caller |
| 365 | * |
| 366 | * For each directory, print its path relative to opts.path. List a directory, |
| 367 | * then list each of its sub-directories. |
| 368 | * |
| 369 | * Returns 0 on success or -1 on error. |
| 370 | * |
| 371 | * NOTE: Assumes recursive option. Currently no limit on the depths of |
| 372 | * recursion. |
| 373 | */ |
| 374 | static int readdir_recursive(ntfs_inode * ni, s64 * pos, ntfsls_dirent * dirent) |
| 375 | { |
| 376 | /* list of dirs to "ls" recursively */ |
| 377 | static struct dir dirs = { |
| 378 | .list = NTFS_LIST_HEAD_INIT(dirs.list), |
| 379 | .ni = NULL, |
| 380 | .name = {0}, |
| 381 | .depth = 0 |
| 382 | }; |
| 383 | |
| 384 | static struct path_component paths = { |
| 385 | .list = NTFS_LIST_HEAD_INIT(paths.list), |
| 386 | .name = NULL |
| 387 | }; |
| 388 | |
| 389 | static struct path_component base_comp; |
| 390 | |
| 391 | struct dir *subdir = NULL; |
| 392 | struct dir *tofree = NULL; |
| 393 | struct path_component comp; |
| 394 | struct path_component *tempcomp = NULL; |
| 395 | struct ntfs_list_head *dir_walker = NULL; |
| 396 | struct ntfs_list_head *comp_walker = NULL; |
| 397 | s64 pos2 = 0; |
| 398 | int ni_depth = depth; |
| 399 | int result = 0; |
| 400 | |
| 401 | if (ntfs_list_empty(&dirs.list)) { |
| 402 | base_comp.name = opts.path; |
| 403 | ntfs_list_add(&base_comp.list, &paths.list); |
| 404 | dir_list_insert_pos = &dirs.list; |
| 405 | printf("%s:\n", opts.path); |
| 406 | } |
| 407 | |
| 408 | depth++; |
| 409 | |
| 410 | result = ntfs_readdir(ni, pos, dirent, (ntfs_filldir_t) list_dir_entry); |
| 411 | |
| 412 | if (result == 0) { |
| 413 | ntfs_list_add_tail(&comp.list, &paths.list); |
| 414 | |
| 415 | /* for each of ni's sub-dirs: list in this iteration, then |
| 416 | free at the top of the next iteration or outside of loop */ |
| 417 | ntfs_list_for_each(dir_walker, &dirs.list) { |
| 418 | if (tofree) { |
| 419 | free_dir(tofree); |
| 420 | tofree = NULL; |
| 421 | } |
| 422 | subdir = ntfs_list_entry(dir_walker, struct dir, list); |
| 423 | |
| 424 | /* subdir is not a subdir of ni */ |
| 425 | if (subdir->depth != ni_depth + 1) |
| 426 | break; |
| 427 | |
| 428 | pos2 = 0; |
| 429 | dir_list_insert_pos = &dirs.list; |
| 430 | if (!subdir->ni) { |
| 431 | subdir->ni = |
| 432 | ntfs_pathname_to_inode(ni->vol, ni, |
| 433 | subdir->name); |
| 434 | |
| 435 | if (!subdir->ni) { |
| 436 | ntfs_log_error |
| 437 | ("ntfsls::readdir_recursive(): cannot get inode from pathname.\n"); |
| 438 | result = -1; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | puts(""); |
| 443 | |
| 444 | comp.name = subdir->name; |
| 445 | |
| 446 | /* print relative path header */ |
| 447 | ntfs_list_for_each(comp_walker, &paths.list) { |
| 448 | tempcomp = |
| 449 | ntfs_list_entry(comp_walker, |
| 450 | struct path_component, list); |
| 451 | printf("%s", tempcomp->name); |
| 452 | if (tempcomp != &comp |
| 453 | && *tempcomp->name != PATH_SEP |
| 454 | && (!opts.classify |
| 455 | || tempcomp == &base_comp)) |
| 456 | putchar(PATH_SEP); |
| 457 | } |
| 458 | puts(":"); |
| 459 | |
| 460 | result = readdir_recursive(subdir->ni, &pos2, dirent); |
| 461 | |
| 462 | if (result) |
| 463 | break; |
| 464 | |
| 465 | tofree = subdir; |
| 466 | ntfs_list_del(dir_walker); |
| 467 | } |
| 468 | |
| 469 | ntfs_list_del(&comp.list); |
| 470 | } |
| 471 | |
| 472 | if (tofree) |
| 473 | free_dir(tofree); |
| 474 | |
| 475 | /* if at the outer-most readdir_recursive, then clean up */ |
| 476 | if (ni_depth == 0) { |
| 477 | free_dirs(&dirs.list); |
| 478 | } |
| 479 | |
| 480 | depth--; |
| 481 | |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * list_dir_entry |
| 487 | * |
| 488 | * FIXME: Should we print errors as we go along? (AIA) |
| 489 | */ |
| 490 | static int list_dir_entry(ntfsls_dirent * dirent, const ntfschar * name, |
| 491 | const int name_len, const int name_type, |
| 492 | const s64 pos __attribute__((unused)), |
| 493 | const MFT_REF mref, const unsigned dt_type) |
| 494 | { |
| 495 | char *filename = NULL; |
| 496 | int result = 0; |
| 497 | |
| 498 | struct dir *dir = NULL; |
| 499 | |
| 500 | filename = calloc(1, MAX_PATH); |
| 501 | if (!filename) |
| 502 | return -1; |
| 503 | |
| 504 | if (ntfs_ucstombs(name, name_len, &filename, MAX_PATH) < 0) { |
| 505 | ntfs_log_error("Cannot represent filename in current locale.\n"); |
| 506 | goto free; |
| 507 | } |
| 508 | |
| 509 | result = 0; // These are successful |
| 510 | if ((MREF(mref) < FILE_first_user) && (!opts.system)) |
| 511 | goto free; |
| 512 | if (name_type == FILE_NAME_POSIX && !opts.all) |
| 513 | goto free; |
| 514 | if (((name_type & FILE_NAME_WIN32_AND_DOS) == FILE_NAME_WIN32) && |
| 515 | opts.dos) |
| 516 | goto free; |
| 517 | if (((name_type & FILE_NAME_WIN32_AND_DOS) == FILE_NAME_DOS) && |
| 518 | !opts.dos) |
| 519 | goto free; |
| 520 | if (dt_type == NTFS_DT_DIR && opts.classify) |
| 521 | sprintf(filename + strlen(filename), "/"); |
| 522 | |
| 523 | if (dt_type == NTFS_DT_DIR && opts.recursive |
| 524 | && strcmp(filename, ".") && strcmp(filename, "./") |
| 525 | && strcmp(filename, "..") && strcmp(filename, "../")) |
| 526 | { |
| 527 | dir = (struct dir *)calloc(1, sizeof(struct dir)); |
| 528 | |
| 529 | if (!dir) { |
| 530 | ntfs_log_error("Failed to allocate for subdir.\n"); |
| 531 | result = -1; |
| 532 | goto free; |
| 533 | } |
| 534 | |
| 535 | strcpy(dir->name, filename); |
| 536 | dir->ni = NULL; |
| 537 | dir->depth = depth; |
| 538 | } |
| 539 | |
| 540 | if (!opts.lng) { |
| 541 | if (!opts.inode) |
| 542 | printf("%s\n", filename); |
| 543 | else |
| 544 | printf("%7llu %s\n", (unsigned long long)MREF(mref), |
| 545 | filename); |
| 546 | result = 0; |
| 547 | } else { |
| 548 | s64 filesize = 0; |
| 549 | ntfs_inode *ni; |
| 550 | ntfs_attr_search_ctx *ctx = NULL; |
| 551 | FILE_NAME_ATTR *file_name_attr; |
| 552 | ATTR_RECORD *attr; |
| 553 | struct timespec change_time; |
| 554 | char t_buf[26]; |
| 555 | |
| 556 | result = -1; // Everything else is bad |
| 557 | |
| 558 | ni = ntfs_inode_open(dirent->vol, mref); |
| 559 | if (!ni) |
| 560 | goto release; |
| 561 | |
| 562 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 563 | if (!ctx) |
| 564 | goto release; |
| 565 | |
| 566 | if (ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL, |
| 567 | 0, ctx)) |
| 568 | goto release; |
| 569 | attr = ctx->attr; |
| 570 | |
| 571 | file_name_attr = (FILE_NAME_ATTR *)((char *)attr + |
| 572 | le16_to_cpu(attr->value_offset)); |
| 573 | if (!file_name_attr) |
| 574 | goto release; |
| 575 | |
| 576 | change_time = ntfs2timespec(file_name_attr->last_data_change_time); |
| 577 | strcpy(t_buf, ctime(&change_time.tv_sec)); |
| 578 | memmove(t_buf+16, t_buf+19, 5); |
| 579 | t_buf[21] = '\0'; |
| 580 | |
| 581 | if (dt_type != NTFS_DT_DIR) { |
| 582 | if (!ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, |
| 583 | NULL, 0, ctx)) |
| 584 | filesize = ntfs_get_attribute_value_length( |
| 585 | ctx->attr); |
| 586 | } |
| 587 | |
| 588 | if (opts.inode) |
| 589 | printf("%7llu %8lld %s %s\n", |
| 590 | (unsigned long long)MREF(mref), |
| 591 | (long long)filesize, t_buf + 4, |
| 592 | filename); |
| 593 | else |
| 594 | printf("%8lld %s %s\n", (long long)filesize, t_buf + 4, |
| 595 | filename); |
| 596 | |
| 597 | if (dir) { |
| 598 | dir->ni = ni; |
| 599 | ni = NULL; /* so release does not close inode */ |
| 600 | } |
| 601 | |
| 602 | result = 0; |
| 603 | release: |
| 604 | /* Release attribute search context and close the inode. */ |
| 605 | if (ctx) |
| 606 | ntfs_attr_put_search_ctx(ctx); |
| 607 | if (ni) |
| 608 | ntfs_inode_close(ni); |
| 609 | } |
| 610 | |
| 611 | if (dir) { |
| 612 | if (result == 0) { |
| 613 | ntfs_list_add(&dir->list, dir_list_insert_pos); |
| 614 | dir_list_insert_pos = &dir->list; |
| 615 | } else { |
| 616 | free(dir); |
| 617 | dir = NULL; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | free: |
| 622 | free(filename); |
| 623 | return result; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * main - Begin here |
| 628 | * |
| 629 | * Start from here. |
| 630 | * |
| 631 | * Return: 0 Success, the program worked |
| 632 | * 1 Error, parsing mount options failed |
| 633 | * 2 Error, mount attempt failed |
| 634 | * 3 Error, failed to open root directory |
| 635 | * 4 Error, failed to open directory in search path |
| 636 | */ |
| 637 | int main(int argc, char **argv) |
| 638 | { |
| 639 | s64 pos; |
| 640 | ntfs_volume *vol; |
| 641 | ntfs_inode *ni; |
| 642 | ntfsls_dirent dirent; |
| 643 | |
| 644 | ntfs_log_set_handler(ntfs_log_handler_outerr); |
| 645 | |
| 646 | if (!parse_options(argc, argv)) { |
| 647 | // FIXME: Print error... (AIA) |
| 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | utils_set_locale(); |
| 652 | |
| 653 | vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | |
| 654 | (opts.force ? NTFS_MNT_RECOVER : 0)); |
| 655 | if (!vol) { |
| 656 | // FIXME: Print error... (AIA) |
| 657 | return 2; |
| 658 | } |
| 659 | |
| 660 | ni = ntfs_pathname_to_inode(vol, NULL, opts.path); |
| 661 | if (!ni) { |
| 662 | // FIXME: Print error... (AIA) |
| 663 | ntfs_umount(vol, FALSE); |
| 664 | return 3; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * We now are at the final path component. If it is a file just |
| 669 | * list it. If it is a directory, list its contents. |
| 670 | */ |
| 671 | pos = 0; |
| 672 | memset(&dirent, 0, sizeof(dirent)); |
| 673 | dirent.vol = vol; |
| 674 | if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { |
| 675 | if (opts.recursive) |
| 676 | readdir_recursive(ni, &pos, &dirent); |
| 677 | else |
| 678 | ntfs_readdir(ni, &pos, &dirent, |
| 679 | (ntfs_filldir_t) list_dir_entry); |
| 680 | // FIXME: error checking... (AIA) |
| 681 | } else { |
| 682 | ATTR_RECORD *rec; |
| 683 | FILE_NAME_ATTR *attr; |
| 684 | ntfs_attr_search_ctx *ctx; |
| 685 | int space = 4; |
| 686 | ntfschar *name = NULL; |
| 687 | int name_len = 0;; |
| 688 | |
| 689 | ctx = ntfs_attr_get_search_ctx(ni, NULL); |
| 690 | if (!ctx) |
| 691 | return -1; |
| 692 | |
| 693 | while ((rec = find_attribute(AT_FILE_NAME, ctx))) { |
| 694 | /* We know this will always be resident. */ |
| 695 | attr = (FILE_NAME_ATTR *) ((char *) rec + le16_to_cpu(rec->value_offset)); |
| 696 | |
| 697 | if (attr->file_name_type < space) { |
| 698 | name = attr->file_name; |
| 699 | name_len = attr->file_name_length; |
| 700 | space = attr->file_name_type; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | list_dir_entry(&dirent, name, name_len, space, pos, ni->mft_no, |
| 705 | NTFS_DT_REG); |
| 706 | // FIXME: error checking... (AIA) |
| 707 | |
| 708 | ntfs_attr_put_search_ctx(ctx); |
| 709 | } |
| 710 | |
| 711 | /* Finished with the inode; release it. */ |
| 712 | ntfs_inode_close(ni); |
| 713 | |
| 714 | ntfs_umount(vol, FALSE); |
| 715 | return 0; |
| 716 | } |
| 717 | |