Add memtest support.
[syslinux-debian/hramrach.git] / win / ntfstest.c
blob1fc271823d274fe7afeb44570c82142631e88265
1 /* -------------------------------------------------------------------------- *
3 * Copyright 2011 Shao Miller - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ------------------------------------------------------------------------- */
13 /****
14 * ntfstest.c
16 * (C) Shao Miller, 2011
18 * Tests ntfssect.c functions
20 * With special thanks to Mark Roddy for his article:
21 * http://www.wd-3.com/archive/luserland.htm
23 #include <windows.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "ntfssect.h"
31 /*** Object types */
33 /*** Function declarations */
34 static void show_usage(void);
35 static void show_last_err(void);
36 static void show_err(DWORD);
38 /*** Struct/union definitions */
40 /*** Function definitions */
42 /** Program entry-point */
43 int main(int argc, char ** argv) {
44 int rc;
45 DWORD err;
46 S_NTFSSECT_VOLINFO vol_info;
47 HANDLE file;
48 LARGE_INTEGER vcn, lba;
49 S_NTFSSECT_EXTENT extent;
50 LONGLONG len;
51 BOOL ok;
53 if (argc != 2) {
54 rc = EXIT_FAILURE;
55 show_usage();
56 goto err_args;
59 /* Get volume info */
60 err = NtfsSectGetVolumeInfoFromFileName(argv[1], &vol_info);
61 if (err != ERROR_SUCCESS) {
62 show_err(err);
63 goto err_vol_info;
65 printf(
66 "Volume has %d bytes per sector, %d sectors per cluster\n",
67 vol_info.BytesPerSector,
68 vol_info.SectorsPerCluster
71 /* Open the file for reading */
72 file = CreateFile(
73 argv[1],
74 GENERIC_READ,
75 FILE_SHARE_READ,
76 NULL,
77 OPEN_EXISTING,
79 NULL
81 if (file == INVALID_HANDLE_VALUE) {
82 rc = EXIT_FAILURE;
83 show_last_err();
84 goto err_file;
87 /* For each extent */
88 for (
89 vcn.QuadPart = 0;
90 NtfsSectGetFileVcnExtent(file, &vcn, &extent) == ERROR_SUCCESS;
91 vcn = extent.NextVcn
92 ) {
93 len = extent.NextVcn.QuadPart - extent.FirstVcn.QuadPart;
94 printf("Extent @ VCN #%lld,", extent.FirstVcn.QuadPart);
95 printf(" %lld clusters long:\n", len);
96 printf(" VCN #%lld -", extent.FirstVcn.QuadPart);
97 printf(" #%lld\n", extent.FirstVcn.QuadPart + len - 1);
98 printf(" LCN #%lld -", extent.FirstLcn.QuadPart);
99 printf(" #%lld\n", extent.FirstLcn.QuadPart + len - 1);
100 err = NtfsSectLcnToLba(
101 &vol_info,
102 &extent.FirstLcn,
103 &lba
105 if (err == ERROR_SUCCESS) {
106 printf(" LBA #%lld -", lba.QuadPart);
107 printf(
108 " #%lld\n",
109 lba.QuadPart + len * vol_info.SectorsPerCluster
112 continue;
115 rc = EXIT_SUCCESS;
117 CloseHandle(file);
118 err_file:
120 CloseHandle(vol_info.Handle);
121 err_vol_info:
123 err_args:
125 return rc;
128 /** Display usage */
129 static void show_usage(void) {
130 static const char usage_text[] = "\
131 File sector info . . . . . . . . . . . . . . . . . . . . Shao Miller, 2011\n\
133 Usage: NTFSTEST.EXE <filename>\n\
135 Attempts to dump cluster and sector info for <filename>.\n";
137 printf(usage_text);
138 return;
141 static void show_last_err(void) {
142 show_err(GetLastError());
143 return;
146 /** Display an error */
147 static void show_err(DWORD err_code) {
148 void * buf;
150 FormatMessage(
151 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
152 NULL,
153 err_code,
154 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
155 (LPTSTR) &buf,
157 NULL
159 fprintf(stderr, "Error: %s\n", buf);
160 LocalFree(buf);
161 return;