ReactOS 0.4.16-dev-112-g52265ae
balance.cpp File Reference
#include "shellext.h"
#include "balance.h"
#include "resource.h"
#include "../btrfsioctl.h"
#include <shlobj.h>
#include <uxtheme.h>
#include <stdio.h>
#include <strsafe.h>
#include <winternl.h>
#include <shlwapi.h>
Include dependency graph for balance.cpp:

Go to the source code of this file.

Macros

#define NO_SHLWAPI_STRFCNS
 

Functions

static WCHAR hex_digit (uint8_t u)
 
static void serialize (void *data, ULONG len, WCHAR *s)
 
static INT_PTR CALLBACK stub_BalanceOptsDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
static INT_PTR CALLBACK stub_BalanceDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
static uint8_t from_hex_digit (WCHAR c)
 
static void unserialize (void *data, ULONG len, WCHAR *s)
 
void CALLBACK StartBalanceW (HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow)
 
void CALLBACK PauseBalanceW (HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow)
 
void CALLBACK StopBalanceW (HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow)
 

Variables

static uint64_t convtypes2 []
 

Macro Definition Documentation

◆ NO_SHLWAPI_STRFCNS

#define NO_SHLWAPI_STRFCNS

Definition at line 41 of file balance.cpp.

Function Documentation

◆ from_hex_digit()

static uint8_t from_hex_digit ( WCHAR  c)
static

Definition at line 1043 of file balance.cpp.

1043 {
1044 if (c >= 'a' && c <= 'f')
1045 return (uint8_t)(c - 'a' + 0xa);
1046 else if (c >= 'A' && c <= 'F')
1047 return (uint8_t)(c - 'A' + 0xa);
1048 else
1049 return (uint8_t)(c - '0');
1050}
const GLubyte * c
Definition: glext.h:8905
BYTE uint8_t
Definition: msvideo1.c:66

Referenced by unserialize().

◆ hex_digit()

static WCHAR hex_digit ( uint8_t  u)
static

Definition at line 49 of file balance.cpp.

49 {
50 if (u >= 0xa && u <= 0xf)
51 return (uint8_t)(u - 0xa + 'a');
52 else
53 return (uint8_t)(u + '0');
54}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240

◆ PauseBalanceW()

void CALLBACK PauseBalanceW ( HWND  hwnd,
HINSTANCE  hinst,
LPWSTR  lpszCmdLine,
int  nCmdShow 
)

Definition at line 1127 of file balance.cpp.

1127 {
1128 try {
1131 LUID luid;
1132
1134 throw last_error(GetLastError());
1135
1136 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
1137 throw last_error(GetLastError());
1138
1139 tp.PrivilegeCount = 1;
1140 tp.Privileges[0].Luid = luid;
1141 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1142
1143 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
1144 throw last_error(GetLastError());
1145
1148
1149 if (h != INVALID_HANDLE_VALUE) {
1153
1154 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_QUERY_BALANCE, nullptr, 0, &bqb2, sizeof(btrfs_query_balance));
1155 if (!NT_SUCCESS(Status))
1156 throw ntstatus_error(Status);
1157
1158 if (bqb2.status & BTRFS_BALANCE_PAUSED)
1159 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_RESUME_BALANCE, nullptr, 0, nullptr, 0);
1160 else if (bqb2.status & BTRFS_BALANCE_RUNNING)
1161 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_PAUSE_BALANCE, nullptr, 0, nullptr, 0);
1162 else
1163 return;
1164
1165 if (!NT_SUCCESS(Status))
1166 throw ntstatus_error(Status);
1167 } else
1168 throw last_error(GetLastError());
1169 } catch (const exception& e) {
1170 error_message(hwnd, e.what());
1171 }
1172}
LONG NTSTATUS
Definition: precomp.h:26
#define FSCTL_BTRFS_PAUSE_BALANCE
Definition: btrfsioctl.h:16
#define FSCTL_BTRFS_QUERY_BALANCE
Definition: btrfsioctl.h:15
#define FSCTL_BTRFS_RESUME_BALANCE
Definition: btrfsioctl.h:17
#define BTRFS_BALANCE_RUNNING
Definition: btrfsioctl.h:161
#define BTRFS_BALANCE_PAUSED
Definition: btrfsioctl.h:162
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
void error_message(HWND hwnd, const char *msg)
Definition: main.cpp:783
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:374
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2996
Status
Definition: gdiplustypes.h:25
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat token
Definition: glfuncs.h:210
#define e
Definition: ke_i.h:82
#define FILE_FLAG_OPEN_REPARSE_POINT
Definition: disk.h:39
#define FILE_FLAG_BACKUP_SEMANTICS
Definition: disk.h:41
static PIO_STATUS_BLOCK iosb
Definition: file.c:98
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define FILE_TRAVERSE
Definition: nt_native.h:643
NTSYSAPI NTSTATUS NTAPI NtFsControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
#define L(x)
Definition: ntvdm.h:50
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:930
#define TOKEN_QUERY
Definition: setypes.h:928
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63

◆ serialize()

static void serialize ( void data,
ULONG  len,
WCHAR s 
)
static

Definition at line 56 of file balance.cpp.

56 {
57 uint8_t* d;
58
59 d = (uint8_t*)data;
60
61 while (true) {
62 *s = hex_digit((uint8_t)(*d >> 4)); s++;
63 *s = hex_digit(*d & 0xf); s++;
64
65 d++;
66 len--;
67
68 if (len == 0) {
69 *s = 0;
70 return;
71 }
72 }
73}
#define hex_digit(c)
Definition: btrfs_drv.h:1748
GLdouble s
Definition: gl.h:2039
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum GLsizei len
Definition: glext.h:6722
#define d
Definition: ke_i.h:81

Referenced by BtrfsBalance::StartBalance().

◆ StartBalanceW()

void CALLBACK StartBalanceW ( HWND  hwnd,
HINSTANCE  hinst,
LPWSTR  lpszCmdLine,
int  nCmdShow 
)

Definition at line 1066 of file balance.cpp.

1066 {
1067 try {
1068 WCHAR *s, *vol, *block;
1072 LUID luid;
1073
1074 s = wcsstr(lpszCmdLine, L" ");
1075 if (!s)
1076 return;
1077
1078 s[0] = 0;
1079
1080 vol = lpszCmdLine;
1081 block = &s[1];
1082
1083 RtlZeroMemory(&bsb, sizeof(btrfs_start_balance));
1084 unserialize(&bsb, sizeof(btrfs_start_balance), block);
1085
1087 throw last_error(GetLastError());
1088
1089 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
1090 throw last_error(GetLastError());
1091
1092 tp.PrivilegeCount = 1;
1093 tp.Privileges[0].Luid = luid;
1094 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1095
1096 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
1097 throw last_error(GetLastError());
1098
1101
1102 if (h != INVALID_HANDLE_VALUE) {
1105
1106 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_START_BALANCE, &bsb, sizeof(btrfs_start_balance), nullptr, 0);
1107
1110 NTSTATUS Status2;
1111
1112 Status2 = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_QUERY_SCRUB, nullptr, 0, &bqs, sizeof(btrfs_query_scrub));
1113
1114 if ((NT_SUCCESS(Status2) || Status2 == STATUS_BUFFER_OVERFLOW) && bqs.status != BTRFS_SCRUB_STOPPED)
1116 }
1117
1118 if (!NT_SUCCESS(Status))
1119 throw ntstatus_error(Status);
1120 } else
1121 throw last_error(GetLastError());
1122 } catch (const exception& e) {
1123 error_message(hwnd, e.what());
1124 }
1125}
static void unserialize(void *data, ULONG len, WCHAR *s)
Definition: balance.cpp:1052
#define FSCTL_BTRFS_QUERY_SCRUB
Definition: btrfsioctl.h:24
#define FSCTL_BTRFS_START_BALANCE
Definition: btrfsioctl.h:14
#define BTRFS_SCRUB_STOPPED
Definition: btrfsioctl.h:195
#define IDS_BALANCE_SCRUB_RUNNING
Definition: resource.h:117
_CONST_RETURN wchar_t *__cdecl wcsstr(_In_z_ const wchar_t *_Str, _In_z_ const wchar_t *_SubStr)
#define STATUS_DEVICE_NOT_READY
Definition: shellext.h:70
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
static unsigned int block
Definition: xmlmemory.c:101
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ StopBalanceW()

void CALLBACK StopBalanceW ( HWND  hwnd,
HINSTANCE  hinst,
LPWSTR  lpszCmdLine,
int  nCmdShow 
)

Definition at line 1174 of file balance.cpp.

1174 {
1175 try {
1178 LUID luid;
1179
1181 throw last_error(GetLastError());
1182
1183 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
1184 throw last_error(GetLastError());
1185
1186 tp.PrivilegeCount = 1;
1187 tp.Privileges[0].Luid = luid;
1188 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1189
1190 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
1191 throw last_error(GetLastError());
1192
1195
1196 if (h != INVALID_HANDLE_VALUE) {
1200
1201 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_QUERY_BALANCE, nullptr, 0, &bqb2, sizeof(btrfs_query_balance));
1202 if (!NT_SUCCESS(Status))
1203 throw ntstatus_error(Status);
1204
1206 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_STOP_BALANCE, nullptr, 0, nullptr, 0);
1207 else
1208 return;
1209
1210 if (!NT_SUCCESS(Status))
1211 throw ntstatus_error(Status);
1212 } else
1213 throw last_error(GetLastError());
1214 } catch (const exception& e) {
1215 error_message(hwnd, e.what());
1216 }
1217}
#define FSCTL_BTRFS_STOP_BALANCE
Definition: btrfsioctl.h:18

◆ stub_BalanceDlgProc()

static INT_PTR CALLBACK stub_BalanceDlgProc ( HWND  hwndDlg,
UINT  uMsg,
WPARAM  wParam,
LPARAM  lParam 
)
static

Definition at line 965 of file balance.cpp.

965 {
966 BtrfsBalance* bb;
967
968 if (uMsg == WM_INITDIALOG) {
970 bb = (BtrfsBalance*)lParam;
971 } else {
973 }
974
975 if (bb)
976 return bb->BalanceDlgProc(hwndDlg, uMsg, wParam, lParam);
977 else
978 return false;
979}
INT_PTR CALLBACK BalanceDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: balance.cpp:861
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define GWLP_USERDATA
Definition: treelist.c:63
#define GetWindowLongPtrW
Definition: winuser.h:4832
#define WM_INITDIALOG
Definition: winuser.h:1742
#define SetWindowLongPtrW
Definition: winuser.h:5358

Referenced by BtrfsBalance::ShowBalance().

◆ stub_BalanceOptsDlgProc()

static INT_PTR CALLBACK stub_BalanceOptsDlgProc ( HWND  hwndDlg,
UINT  uMsg,
WPARAM  wParam,
LPARAM  lParam 
)
static

Definition at line 840 of file balance.cpp.

840 {
841 BtrfsBalance* bb;
842
843 if (uMsg == WM_INITDIALOG) {
845 bb = (BtrfsBalance*)lParam;
846 } else {
848 }
849
850 if (bb)
851 return bb->BalanceOptsDlgProc(hwndDlg, uMsg, wParam, lParam);
852 else
853 return false;
854}
INT_PTR CALLBACK BalanceOptsDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: balance.cpp:514

Referenced by BtrfsBalance::ShowBalanceOptions().

◆ unserialize()

static void unserialize ( void data,
ULONG  len,
WCHAR s 
)
static

Definition at line 1052 of file balance.cpp.

1052 {
1053 uint8_t* d;
1054
1055 d = (uint8_t*)data;
1056
1057 while (s[0] != 0 && s[1] != 0 && len > 0) {
1058 *d = (uint8_t)(from_hex_digit(s[0]) << 4) | from_hex_digit(s[1]);
1059
1060 s += 2;
1061 d++;
1062 len--;
1063 }
1064}
static uint8_t from_hex_digit(WCHAR c)
Definition: balance.cpp:1043
#define uint8_t
Definition: nsiface.idl:59

Referenced by StartBalanceW().

Variable Documentation

◆ convtypes2

uint64_t convtypes2[]
static
Initial value:
#define BLOCK_FLAG_SINGLE
Definition: btrfsioctl.h:141
#define BLOCK_FLAG_RAID1C4
Definition: btrfs.h:88
#define BLOCK_FLAG_RAID1C3
Definition: btrfs.h:87
#define BLOCK_FLAG_RAID5
Definition: shellext.h:82
#define BLOCK_FLAG_DUPLICATE
Definition: shellext.h:80
#define BLOCK_FLAG_RAID10
Definition: shellext.h:81
#define BLOCK_FLAG_RAID0
Definition: shellext.h:78
#define BLOCK_FLAG_RAID6
Definition: shellext.h:83
#define BLOCK_FLAG_RAID1
Definition: shellext.h:79

Definition at line 45 of file balance.cpp.

Referenced by BtrfsBalance::BalanceOptsDlgProc(), and BtrfsBalance::SaveBalanceOpts().