ntfscat.c revision 9663:ace9a2ac3683
1/** 2 * ntfscat - Part of the Linux-NTFS project. 3 * 4 * Copyright (c) 2003-2005 Richard Russon 5 * Copyright (c) 2003-2005 Anton Altaparmakov 6 * Copyright (c) 2003-2005 Szabolcs Szakacsits 7 * Copyright (c) 2007 Yura Pakhuchiy 8 * 9 * This utility will concatenate files and print on the standard output. 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program (in the main directory of the Linux-NTFS 23 * distribution in the file COPYING); if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 */ 26 27#include "config.h" 28 29#ifdef HAVE_STDIO_H 30#include <stdio.h> 31#endif 32#ifdef HAVE_GETOPT_H 33#include <getopt.h> 34#endif 35#ifdef HAVE_STDLIB_H 36#include <stdlib.h> 37#endif 38#ifdef HAVE_STRING_H 39#include <string.h> 40#endif 41 42#include "types.h" 43#include "attrib.h" 44#include "utils.h" 45#include "volume.h" 46#include "debug.h" 47#include "dir.h" 48#include "ntfscat.h" 49#include "version.h" 50 51static const char *EXEC_NAME = "ntfscat"; 52static struct options opts; 53 54/** 55 * version - Print version information about the program 56 * 57 * Print a copyright statement and a brief description of the program. 58 * 59 * Return: none 60 */ 61static void version(void) 62{ 63 ntfs_log_info("\n%s v%s (libntfs %s) - Concatenate files and print " 64 "on the standard output.\n\n", EXEC_NAME, VERSION, 65 ntfs_libntfs_version()); 66 ntfs_log_info("Copyright (c) 2003-2005 Richard Russon\n"); 67 ntfs_log_info("Copyright (c) 2003-2005 Anton Altaparmakov\n"); 68 ntfs_log_info("Copyright (c) 2003-2005 Szabolcs Szakacsits\n"); 69 ntfs_log_info("Copyright (c) 2007 Yura Pakhuchiy\n"); 70 ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); 71} 72 73/** 74 * usage - Print a list of the parameters to the program 75 * 76 * Print a list of the parameters and options for the program. 77 * 78 * Return: none 79 */ 80static void usage(void) 81{ 82 ntfs_log_info("\nUsage: %s [options] device [file]\n\n" 83 " -a, --attribute TYPE Display this attribute type\n" 84 " -n, --attribute-name NAME Display this attribute name\n" 85 " -i, --inode NUM Display this inode\n\n" 86 " -f, --force Use less caution\n" 87 " -h, --help Print this help\n" 88 " -q, --quiet Less output\n" 89 " -V, --version Version information\n" 90 " -v, --verbose More output\n\n", 91// Does not work for compressed files at present so leave undocumented... 92// " -r --raw Display the raw data (e.g. for compressed or encrypted file)", 93 EXEC_NAME); 94 ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); 95} 96 97/** 98 * parse_attribute - Read an attribute name, or number 99 * @value: String to be parsed 100 * @attr: Resulting attribute id (on success) 101 * 102 * Read a string representing an attribute. It may be a decimal, octal or 103 * hexadecimal number, or the attribute name in full. The leading $ sign is 104 * optional. 105 * 106 * Return: 1 Success, a valid attribute name or number 107 * 0 Error, not an attribute name or number 108 */ 109static int parse_attribute(const char *value, ATTR_TYPES *attr) 110{ 111 static const char *attr_name[] = { 112 "$STANDARD_INFORMATION", 113 "$ATTRIBUTE_LIST", 114 "$FILE_NAME", 115 "$OBJECT_ID", 116 "$SECURITY_DESCRIPTOR", 117 "$VOLUME_NAME", 118 "$VOLUME_INFORMATION", 119 "$DATA", 120 "$INDEX_ROOT", 121 "$INDEX_ALLOCATION", 122 "$BITMAP", 123 "$REPARSE_POINT", 124 "$EA_INFORMATION", 125 "$EA", 126 "$PROPERTY_SET", 127 "$LOGGED_UTILITY_STREAM", 128 NULL 129 }; 130 131 int i; 132 long num; 133 134 for (i = 0; attr_name[i]; i++) { 135 if ((strcmp(value, attr_name[i]) == 0) || 136 (strcmp(value, attr_name[i] + 1) == 0)) { 137 *attr = (ATTR_TYPES)cpu_to_le32((i + 1) * 16); 138 return 1; 139 } 140 } 141 142 num = strtol(value, NULL, 0); 143 if ((num > 0) && (num < 257)) { 144 *attr = (ATTR_TYPES)cpu_to_le32(num); 145 return 1; 146 } 147 148 return 0; 149} 150 151/** 152 * parse_options - Read and validate the programs command line 153 * 154 * Read the command line, verify the syntax and parse the options. 155 * This function is very long, but quite simple. 156 * 157 * Return: 1 Success 158 * 0 Error, one or more problems 159 */ 160static int parse_options(int argc, char **argv) 161{ 162 static const char *sopt = "-a:fh?i:n:qVvr"; 163 static const struct option lopt[] = { 164 { "attribute", required_argument, NULL, 'a' }, 165 { "attribute-name", required_argument, NULL, 'n' }, 166 { "force", no_argument, NULL, 'f' }, 167 { "help", no_argument, NULL, 'h' }, 168 { "inode", required_argument, NULL, 'i' }, 169 { "quiet", no_argument, NULL, 'q' }, 170 { "version", no_argument, NULL, 'V' }, 171 { "verbose", no_argument, NULL, 'v' }, 172 { "raw", no_argument, NULL, 'r' }, 173 { NULL, 0, NULL, 0 } 174 }; 175 176 int c = -1; 177 int err = 0; 178 int ver = 0; 179 int help = 0; 180 int levels = 0; 181 ATTR_TYPES attr = AT_UNUSED; 182 183 opterr = 0; /* We'll handle the errors, thank you. */ 184 185 opts.inode = -1; 186 opts.attr = cpu_to_le32(-1); 187 opts.attr_name = NULL; 188 opts.attr_name_len = 0; 189 190 while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { 191 switch (c) { 192 case 1: /* A non-option argument */ 193 if (!opts.device) { 194 opts.device = argv[optind - 1]; 195 } else if (!opts.file) { 196 opts.file = argv[optind - 1]; 197 } else { 198 ntfs_log_error("You must specify exactly one " 199 "file.\n"); 200 err++; 201 } 202 break; 203 case 'a': 204 if (opts.attr != cpu_to_le32(-1)) { 205 ntfs_log_error("You must specify exactly one " 206 "attribute.\n"); 207 } else if (parse_attribute(optarg, &attr) > 0) { 208 opts.attr = attr; 209 break; 210 } else { 211 ntfs_log_error("Couldn't parse attribute.\n"); 212 } 213 err++; 214 break; 215 case 'f': 216 opts.force++; 217 break; 218 case 'h': 219 case '?': 220 if (strncmp (argv[optind-1], "--log-", 6) == 0) { 221 if (!ntfs_log_parse_option (argv[optind-1])) 222 err++; 223 break; 224 } 225 help++; 226 break; 227 case 'i': 228 if (opts.inode != -1) 229 ntfs_log_error("You must specify exactly one inode.\n"); 230 else if (utils_parse_size(optarg, &opts.inode, FALSE)) 231 break; 232 else 233 ntfs_log_error("Couldn't parse inode number.\n"); 234 err++; 235 break; 236 237 case 'n': 238 opts.attr_name_len = ntfs_mbstoucs(optarg, 239 &opts.attr_name, 0); 240 if (opts.attr_name_len < 0) { 241 ntfs_log_perror("Invalid attribute name '%s'", 242 optarg); 243 usage(); 244 } 245 246 case 'q': 247 opts.quiet++; 248 ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); 249 break; 250 case 'V': 251 ver++; 252 break; 253 case 'v': 254 opts.verbose++; 255 ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); 256 break; 257 case 'r': 258 opts.raw = TRUE; 259 break; 260 default: 261 ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); 262 err++; 263 break; 264 } 265 } 266 267 /* Make sure we're in sync with the log levels */ 268 levels = ntfs_log_get_levels(); 269 if (levels & NTFS_LOG_LEVEL_VERBOSE) 270 opts.verbose++; 271 if (!(levels & NTFS_LOG_LEVEL_QUIET)) 272 opts.quiet++; 273 274 if (help || ver) { 275 opts.quiet = 0; 276 } else { 277 if (opts.device == NULL) { 278 ntfs_log_error("You must specify a device.\n"); 279 err++; 280 281 } else if (opts.file == NULL && opts.inode == -1) { 282 ntfs_log_error("You must specify a file or inode " 283 "with the -i option.\n"); 284 err++; 285 286 } else if (opts.file != NULL && opts.inode != -1) { 287 ntfs_log_error("You can't specify both a file and inode.\n"); 288 err++; 289 } 290 291 if (opts.quiet && opts.verbose) { 292 ntfs_log_error("You may not use --quiet and --verbose at the " 293 "same time.\n"); 294 err++; 295 } 296 } 297 298 if (ver) 299 version(); 300 if (help || err) 301 usage(); 302 303 return (!err && !help && !ver); 304} 305 306/** 307 * index_get_size - Find the INDX block size from the index root 308 * @inode: Inode of the directory to be checked 309 * 310 * Find the size of a directory's INDX block from the INDEX_ROOT attribute. 311 * 312 * Return: n Success, the INDX blocks are n bytes in size 313 * 0 Error, not a directory 314 */ 315static int index_get_size(ntfs_inode *inode) 316{ 317 ATTR_RECORD *attr90; 318 INDEX_ROOT *iroot; 319 320 attr90 = find_first_attribute(AT_INDEX_ROOT, inode->mrec); 321 if (!attr90) 322 return 0; // not a directory 323 324 iroot = (INDEX_ROOT*)((u8*)attr90 + le16_to_cpu(attr90->u.res.value_offset)); 325 return le32_to_cpu(iroot->index_block_size); 326} 327 328/** 329 * cat 330 */ 331static int cat(ntfs_volume *vol, ntfs_inode *inode, ATTR_TYPES type, 332 ntfschar *name, int namelen) 333{ 334 const int bufsize = 4096; 335 char *buffer; 336 ntfs_attr *attr; 337 s64 bytes_read, written; 338 s64 offset; 339 u32 block_size; 340 341 buffer = malloc(bufsize); 342 if (!buffer) 343 return 1; 344 345 attr = ntfs_attr_open(inode, type, name, namelen); 346 if (!attr) { 347 ntfs_log_error("Cannot find attribute type 0x%x.\n", 348 le32_to_cpu(type)); 349 free(buffer); 350 return 1; 351 } 352 353 if ((inode->mft_no < 2) && (attr->type == AT_DATA)) 354 block_size = vol->mft_record_size; 355 else if (attr->type == AT_INDEX_ALLOCATION) 356 block_size = index_get_size(inode); 357 else 358 block_size = 0; 359 360 offset = 0; 361 for (;;) { 362 if (!opts.raw && block_size > 0) { 363 // These types have fixup 364 bytes_read = ntfs_attr_mst_pread(attr, offset, 1, block_size, buffer); 365 if (bytes_read > 0) 366 bytes_read *= block_size; 367 } else { 368 bytes_read = ntfs_attr_pread(attr, offset, bufsize, buffer); 369 } 370 //ntfs_log_info("read %lld bytes\n", bytes_read); 371 if (bytes_read == -1) { 372 ntfs_log_perror("ERROR: Couldn't read file"); 373 break; 374 } 375 if (!bytes_read) 376 break; 377 378 written = fwrite(buffer, 1, bytes_read, stdout); 379 if (written != bytes_read) { 380 ntfs_log_perror("ERROR: Couldn't output all data!"); 381 break; 382 } 383 offset += bytes_read; 384 } 385 386 ntfs_attr_close(attr); 387 free(buffer); 388 return 0; 389} 390 391/** 392 * main - Begin here 393 * 394 * Start from here. 395 * 396 * Return: 0 Success, the program worked 397 * 1 Error, something went wrong 398 */ 399int main(int argc, char *argv[]) 400{ 401 ntfs_volume *vol; 402 ntfs_inode *inode; 403 ATTR_TYPES attr; 404 int result = 1; 405 406 ntfs_log_set_handler(ntfs_log_handler_stderr); 407 408 if (!parse_options(argc, argv)) 409 return 1; 410 411 utils_set_locale(); 412 413 vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | 414 (opts.force ? NTFS_MNT_FORCE : 0)); 415 if (!vol) { 416 ntfs_log_perror("ERROR: couldn't mount volume"); 417 return 1; 418 } 419 420 if (opts.inode != -1) 421 inode = ntfs_inode_open(vol, opts.inode); 422 else 423 inode = ntfs_pathname_to_inode(vol, NULL, opts.file); 424 425 if (!inode) { 426 ntfs_log_perror("ERROR: Couldn't open inode"); 427 return 1; 428 } 429 430 attr = AT_DATA; 431 if (opts.attr != cpu_to_le32(-1)) 432 attr = opts.attr; 433 434 result = cat(vol, inode, attr, opts.attr_name, opts.attr_name_len); 435 436 ntfs_inode_close(inode); 437 ntfs_umount(vol, FALSE); 438 439 return result; 440} 441