2 * ntfsinfo - Part of the Linux-NTFS project.
4 * Copyright (c) 2002 Matthew J. Fanto
5 * Copyright (c) 2002 Anton Altaparmakov
6 * Copyright (c) 2002-2003 Richard Russon
8 * This utility will dump a file's attributes.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the Linux-NTFS
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static const char *EXEC_NAME = "ntfsinfo";
41 static struct options {
42 char *device; /* Device/File to work with */
43 s64 inode; /* Info for this inode */
44 int quiet; /* Less output */
45 int verbose; /* Extra output */
46 int force; /* Override common sense */
49 GEN_PRINTF (Eprintf, stderr, NULL, FALSE)
50 GEN_PRINTF (Vprintf, stdout, &opts.verbose, TRUE)
51 GEN_PRINTF (Qprintf, stdout, &opts.quiet, FALSE)
54 * version - Print version information about the program
56 * Print a copyright statement and a brief description of the program.
62 printf ("\n%s v%s - Display information about an NTFS Volume.\n\n",
64 printf ("Copyright (c)\n");
65 printf (" 2002 Matthew J. Fanto\n");
66 printf (" 2002 Anton Altaparmakov\n");
67 printf (" 2002-2003 Richard Russon\n");
68 printf ("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
72 * usage - Print a list of the parameters to the program
74 * Print a list of the parameters and options for the program.
80 printf ("\nUsage: %s [options] device\n"
81 " -i num --inode num Display information about this inode\n"
83 " -f --force Use less caution\n"
84 " -q --quiet Less output\n"
85 " -v --verbose More output\n"
86 " -V --version Display version information\n"
87 " -h --help Display this help\n\n",
89 printf ("%s%s\n", ntfs_bugs, ntfs_home);
93 * parse_options - Read and validate the programs command line
95 * Read the command line, verify the syntax and parse the options.
96 * This function is very long, but quite simple.
99 * 0 Error, one or more problems
101 int parse_options (int argc, char *argv[])
103 static const char *sopt = "-fh?i:qvV";
104 static const struct option lopt[] = {
105 { "force", no_argument, NULL, 'f' },
106 { "help", no_argument, NULL, 'h' },
107 { "inode", required_argument, NULL, 'i' },
108 { "quiet", no_argument, NULL, 'q' },
109 { "verbose", no_argument, NULL, 'v' },
110 { "version", no_argument, NULL, 'V' },
111 { NULL, 0, NULL, 0 },
119 opterr = 0; /* We'll handle the errors, thank you. */
123 while ((c = getopt_long (argc, argv, sopt, lopt, NULL)) != -1) {
125 case 1: /* A non-option argument */
127 opts.device = argv[optind-1];
134 if ((opts.inode != -1) ||
135 (!utils_parse_size (argv[optind-1], &opts.inode, FALSE))) {
156 if ((optopt == 'i') && (!optarg)) {
157 Eprintf ("Option '%s' requires an argument.\n", argv[optind-1]);
159 Eprintf ("Unknown option '%s'.\n", argv[optind-1]);
169 if (opts.device == NULL) {
171 Eprintf ("You must specify exactly one device.\n");
175 if (opts.inode == -1) {
177 Eprintf ("You much specify an inode to learn about.\n");
181 if (opts.quiet && opts.verbose) {
182 Eprintf ("You may not use --quiet and --verbose at the same time.\n");
192 return (!err && !help && !ver);
197 * ntfs_dump_file_name_attribute
199 void ntfs_dump_file_name_attribute(ntfs_inode *inode, MFT_RECORD *mrec)
201 FILE_NAME_ATTR *file_name_attr = NULL;
202 ATTR_RECORD *attr = NULL;
203 ntfs_attr_search_ctx *ctx = NULL;
204 char *file_name = NULL;
207 ctx = ntfs_attr_get_search_ctx(inode, mrec);
209 if(ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) {
210 fprintf(stderr, "ntfsinfo error: cannot lookup attribute AT_FILE_NAME!\n");
216 file_name_attr = (FILE_NAME_ATTR*)((char *)attr + le16_to_cpu(attr->value_offset));
218 //need to convert the little endian unicode string to a multibyte string
219 ntfs_ucstombs(file_name_attr->file_name, file_name_attr->file_name_length,
220 &file_name, file_name_attr->file_name_length);
222 printf("Dumping $FILE_NAME (0x30)\n");
224 //basic stuff about the file
225 printf("File Name: \t\t %s\n",file_name);
226 printf("File Name Length: \t %d\n",file_name_attr->file_name_length);
227 printf("Allocated File Size: \t %lld\n", sle64_to_cpu(file_name_attr->allocated_size));
228 printf("Real File Size: \t %lld\n", sle64_to_cpu(file_name_attr->data_size));
230 //time conversion stuff
231 ntfs_time = ntfs2utc (sle64_to_cpu (file_name_attr->creation_time));
232 printf("File Creation Time: \t %s",ctime(&ntfs_time));
234 ntfs_time = ntfs2utc (sle64_to_cpu (file_name_attr->last_data_change_time));
235 printf("File Altered Time: \t %s",ctime(&ntfs_time));
237 ntfs_time = ntfs2utc (sle64_to_cpu (file_name_attr->last_mft_change_time));
238 printf("MFT Changed Time: \t %s",ctime(&ntfs_time));
240 ntfs_time = ntfs2utc (sle64_to_cpu (file_name_attr->last_access_time));
241 printf("Last Acced Time: \t %s",ctime(&ntfs_time));
248 * ntfs_dump_standard_information
250 void ntfs_dump_standard_information(ntfs_inode *inode, MFT_RECORD *mrec)
253 STANDARD_INFORMATION *standard_attr = NULL;
254 ATTR_RECORD *attr = NULL;
255 ntfs_attr_search_ctx *ctx = NULL;
257 ctx = ntfs_attr_get_search_ctx(inode, mrec);
259 if(ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) {
260 fprintf(stderr, "ntfsinfo error: cannot look up attribute AT_STANDARD_INFORMATION!\n");
266 standard_attr = (STANDARD_INFORMATION*)((char *)attr + le16_to_cpu(attr->value_offset));
268 printf("Dumping $STANDARD_INFORMATION (0x10)\n");
270 printf("Maximum Number of Versions: \t %d \n", le32_to_cpu (standard_attr->maximum_versions));
271 printf("Version Number: \t\t %d \n", le32_to_cpu (standard_attr->version_number));
272 printf("Class ID: \t\t\t %d \n", le32_to_cpu (standard_attr->class_id));
273 printf("User ID: \t\t\t %d \n", le32_to_cpu (standard_attr->owner_id));
274 printf("Security ID: \t\t\t %d \n", le32_to_cpu (standard_attr->security_id));
279 * ntfs_get_file_attributes
281 void ntfs_get_file_attributes(ntfs_volume *vol, s64 i)
284 MFT_RECORD *mrec = NULL;
285 //ntfs_attr_search_ctx *ctx = NULL;
286 ntfs_inode *inode = NULL;
290 inode = ntfs_inode_open(vol, mref);
292 if (ntfs_file_record_read(vol, mref, &mrec, NULL)) {
293 fprintf(stderr, "ntfsinfo error: error reading file record!\n");
297 //see flatcap.org/ntfs/info for what formatting should look like
298 //ntfs_dump_boot_sector_information(inode, mrec);
299 ntfs_dump_file_name_attribute(inode, mrec);
300 ntfs_dump_standard_information(inode, mrec);
308 * Return: 0 Success, the program worked
309 * 1 Error, something went wrong
311 int main(int argc, char **argv)
315 if (!parse_options (argc, argv))
320 vol = utils_mount_volume (opts.device, MS_RDONLY, opts.force);
324 ntfs_get_file_attributes (vol, opts.inode);
326 ntfs_umount (vol, FALSE);