cec651ea311442844deb85ba99b3fb272e299c68
[reactos.git] / reactos / base / applications / cmdutils / fsutil / dirty.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS FS utility tool
4 * FILE: base/applications/cmdutils/dirty.c
5 * PURPOSE: FSutil dirty bit handling
6 * PROGRAMMERS: Pierre Schweitzer <pierre@reactos.org>
7 */
8
9 #include "fsutil.h"
10 #include <winioctl.h>
11
12 /* Add handlers here for subcommands */
13 static int QueryMain(int argc, const TCHAR *argv[]);
14 static int SetMain(int argc, const TCHAR *argv[]);
15 static HandlerItem HandlersList[] =
16 {
17 /* Proc, name, help */
18 { QueryMain, _T("query"), _T("Show the dirty bit") },
19 { SetMain, _T("set"), _T("Set the dirty bit") },
20 };
21
22 static int
23 QueryMain(int argc, const TCHAR *argv[])
24 {
25 HANDLE Volume;
26 TCHAR VolumeID[PATH_MAX];
27 ULONG VolumeStatus, BytesRead;
28
29 /* We need a volume (letter or GUID) */
30 if (argc < 2)
31 {
32 _ftprintf(stderr, _T("Usage: fsutil dirty query <volume>\n"));
33 _ftprintf(stderr, _T("\tFor example: fsutil dirty query c:\n"));
34 return 1;
35 }
36
37 /* Create full name */
38 _stprintf(VolumeID, _T("\\\\.\\%s"), argv[1]);
39
40 /* Open the volume */
41 Volume = CreateFile(VolumeID, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
42 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
43 if (Volume == INVALID_HANDLE_VALUE)
44 {
45 _ftprintf(stderr, _T("Error: %d\n"), GetLastError());
46 return 1;
47 }
48
49 /* And query the dirty status */
50 if (DeviceIoControl(Volume, FSCTL_IS_VOLUME_DIRTY, NULL, 0, &VolumeStatus,
51 sizeof(ULONG), &BytesRead, NULL) == FALSE)
52 {
53 _ftprintf(stderr, _T("Error: %d\n"), GetLastError());
54 CloseHandle(Volume);
55 return 1;
56 }
57
58 CloseHandle(Volume);
59
60 /* Print the status */
61 _ftprintf(stdout, _T("The %s volume is %s\n"), argv[1], (VolumeStatus & VOLUME_IS_DIRTY ? _T("dirty") : _T("clean")));
62
63 return 1;
64 }
65
66 static int
67 SetMain(int argc, const TCHAR *argv[])
68 {
69 /* FIXME */
70 _ftprintf(stderr, _T("Not implemented\n"));
71 return 1;
72 }
73
74 static void
75 PrintUsage(const TCHAR * Command)
76 {
77 int i;
78
79 /* If we were given a command, print it's not supported */
80 if (Command != NULL)
81 {
82 _ftprintf(stderr, _T("Unhandled DIRTY command: %s\n"), Command);
83 }
84
85 /* And dump any available command */
86 _ftprintf(stderr, _T("---- Handled DIRTY commands ----\n\n"));
87 for (i = 0; i < (sizeof(HandlersList) / sizeof(HandlersList[0])); ++i)
88 {
89 _ftprintf(stderr, _T("%s\t%s\n"), HandlersList[i].Command, HandlersList[i].Desc);
90 }
91 }
92
93 int
94 DirtyMain(int argc, const TCHAR *argv[])
95 {
96 return FindHandler(argc, argv, (HandlerItem *)&HandlersList,
97 (sizeof(HandlersList) / sizeof(HandlersList[0])),
98 PrintUsage);
99 }