ReactOS  0.4.12-dev-18-gf469aca
volume.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS FS utility tool
4  * FILE: base/applications/cmdutils/volume.c
5  * PURPOSE: FSutil volumes management
6  * PROGRAMMERS: Pierre Schweitzer <pierre@reactos.org>
7  */
8 
9 #include "fsutil.h"
10 
11 /* Add handlers here for subcommands */
15 {
16  /* Proc, name, help */
17  { DismountMain, _T("dismount"), _T("Dismounts a volume") },
18  { DiskFreeMain, _T("diskfree"), _T("Prints free space on a volume") },
19 };
20 
21 static int
23 {
26 
27  Operation = (Lock ? FSCTL_LOCK_VOLUME : FSCTL_UNLOCK_VOLUME);
28  if (DeviceIoControl(Volume, Operation, NULL, 0, NULL, 0,
29  &BytesRead, NULL) == FALSE)
30  {
32  return 1;
33  }
34 
35  return 0;
36 }
37 
38 static int
39 DismountMain(int argc, const TCHAR *argv[])
40 {
41  HANDLE Volume;
43 
44  /* We need a volume (letter or GUID) */
45  if (argc < 2)
46  {
47  _ftprintf(stderr, _T("Usage: fsutil volume dismount <volume>\n"));
48  _ftprintf(stderr, _T("\tFor example: fsutil volume dismount d:\n"));
49  return 1;
50  }
51 
52  /* Get a handle for the volume */
53  Volume = OpenVolume(argv[1], FALSE, FALSE);
54  if (Volume == INVALID_HANDLE_VALUE)
55  {
56  return 1;
57  }
58 
59  /* Attempt to lock the volume */
60  if (LockOrUnlockVolume(Volume, TRUE))
61  {
62  CloseHandle(Volume);
63  return 1;
64  }
65 
66  /* Issue the dismount command */
68  0, &BytesRead, NULL) == FALSE)
69  {
71  LockOrUnlockVolume(Volume, FALSE);
72  CloseHandle(Volume);
73  return 1;
74  }
75 
76  /* Unlock and quit */
77  LockOrUnlockVolume(Volume, FALSE);
78  CloseHandle(Volume);
79 
80  _ftprintf(stdout, _T("The %s volume has been dismounted\n"), argv[1]);
81 
82  return 0;
83 }
84 
85 static int
86 DiskFreeMain(int argc, const TCHAR *argv[])
87 {
88  ULARGE_INTEGER FreeAvail, TotalNumber, TotalNumberFree;
89 
90  /* We need a path */
91  if (argc < 2)
92  {
93  _ftprintf(stderr, _T("Usage: fsutil volume diskfree <volume path>\n"));
94  _ftprintf(stderr, _T("\tFor example: fsutil volume diskfree c:\n"));
95  return 1;
96  }
97 
98  if (!GetDiskFreeSpaceEx(argv[1], &FreeAvail, &TotalNumber, &TotalNumberFree))
99  {
101  return 1;
102  }
103 
104  _ftprintf(stdout, _T("Total free bytes\t\t: %I64i\n"), TotalNumberFree.QuadPart);
105  _ftprintf(stdout, _T("Total bytes\t\t\t: %I64i\n"), TotalNumber.QuadPart);
106  _ftprintf(stdout, _T("Total free available bytes\t: %I64i\n"), FreeAvail.QuadPart);
107 
108  return 0;
109 }
110 
111 static void
113 {
114  PrintDefaultUsage(_T(" VOLUME "), Command, (HandlerItem *)&HandlersList,
115  (sizeof(HandlersList) / sizeof(HandlersList[0])));
116 }
117 
118 int
119 VolumeMain(int argc, const TCHAR *argv[])
120 {
121  return FindHandler(argc, argv, (HandlerItem *)&HandlersList,
122  (sizeof(HandlersList) / sizeof(HandlersList[0])),
123  PrintUsage);
124 }
static int argc
Definition: ServiceArgs.c:12
#define FSCTL_UNLOCK_VOLUME
Definition: nt_native.h:833
#define TRUE
Definition: types.h:120
static int LockOrUnlockVolume(HANDLE Volume, BOOLEAN Lock)
Definition: volume.c:22
#define CloseHandle
Definition: compat.h:398
int VolumeMain(int argc, const TCHAR *argv[])
Definition: volume.c:119
void PrintDefaultUsage(const TCHAR *Command, const TCHAR *SubCommand, HandlerItem *HandlersList, int HandlerListCount)
Definition: common.c:102
static HandlerProc DiskFreeMain
Definition: volume.c:13
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
Definition: shell.h:41
static HandlerProc DismountMain
Definition: volume.c:12
static char ** argv
Definition: ServiceArgs.c:11
FILE * stdout
int FindHandler(int argc, const TCHAR *argv[], HandlerItem *HandlersList, int HandlerListCount, void(*UsageHelper)(const TCHAR *))
Definition: common.c:11
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define _ftprintf
Definition: tchar.h:518
#define FSCTL_DISMOUNT_VOLUME
Definition: nt_native.h:834
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
static HandlerItem HandlersList[]
Definition: volume.c:14
BOOL OpenVolume(LPCTSTR lpszVolumeName)
Definition: volume.c:28
unsigned long DWORD
Definition: ntddk_ex.h:95
static void PrintUsage(const TCHAR *Command)
Definition: volume.c:112
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment IN PNDIS_RW_LOCK Lock
Definition: CrNtStubs.h:75
int() HandlerProc(int argc, const TCHAR *argv[])
Definition: fsutil.h:8
#define GetDiskFreeSpaceEx
Definition: winbase.h:3622
static VOID PrintErrorMessage(DWORD dwError)
Definition: at.c:308
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
#define FSCTL_LOCK_VOLUME
Definition: nt_native.h:832
unsigned int ULONG
Definition: retypes.h:1
FILE * stderr
_In_ FLT_SET_CONTEXT_OPERATION Operation
Definition: fltkernel.h:1468
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255