nt-makefile-clean-targets-20010917
[openafs.git] / src / WINNT / afssvrcfg / salvage_dlg.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * INCLUDES ___________________________________________________________________
12  *
13  */
14 extern "C" {
15 #include <afs/param.h>
16 #include <afs/stds.h>
17 }
18
19 #include "afscfg.h"             // Main header for this application
20 #include "resource.h"
21 extern "C" {
22 #include "afs_bosAdmin.h"
23 }
24 #include "admin_info_dlg.h"
25
26
27 /*
28  * DEFINITIONS _________________________________________________________________
29  *
30  */
31 static const int DEFAULT_NUM_PROCESSES          = 4;
32 static const char *DEFAULT_LOG_FILE                     = "";
33 static const int NUM_PROCS_BUF_SIZE                     = 5;
34 static const int MIN_NUM_PROCESSES                      = 1;
35 static const int MAX_NUM_PROCESSES                      = 32;
36
37 static HWND hDlg = 0;                                           // HWND for this page's dialog
38 static BOOL bAdvanced;
39 static TCHAR szPartitionName[25];
40 static TCHAR szVolumeName[cchRESOURCE];
41 static TCHAR szNumProcesses[NUM_PROCS_BUF_SIZE];
42 static TCHAR szTempDir[_MAX_PATH];
43 static LPTSTR pszPartitionName;
44 static LPTSTR pszVolumeName;
45 static int nNumProcesses;
46
47
48 /*
49  * PROTOTYPES _________________________________________________________________
50  *
51  */
52 static void OnInitDialog(HWND hwndDlg);
53 static void CheckEnableButtons();
54 static void OnAdvanced();
55 static void UpdateControls();
56 static BOOL OnSalvage();
57 static DWORD WINAPI Salvage(LPVOID param);
58
59 BOOL CALLBACK SalvageDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
60
61
62 /*
63  * EXPORTED FUNCTIONS _________________________________________________________
64  *
65  */
66 BOOL ShowSalvageDlg(HWND hParent, LPCTSTR pszPartitionName)
67 {       
68         ASSERT(pszPartitionName);
69
70         lstrcpy(szPartitionName, pszPartitionName);
71
72         int nResult = ModalDialog(IDD_SALVAGE, hParent, (DLGPROC)SalvageDlgProc);
73
74     if (nResult != IDOK)
75         return FALSE;
76
77         // Create a thread to perform the salvage
78         DWORD dwThreadID;
79         g_CfgData.hSalvageThread = CreateThread(0, 0, Salvage, 0, 0, &dwThreadID);
80
81         return (g_CfgData.hSalvageThread != 0);
82 }
83
84
85 /*
86  * Dialog Proc _________________________________________________________________
87  *
88  */
89 BOOL CALLBACK SalvageDlgProc(HWND hwndDlg, UINT msg, WPARAM wp, LPARAM lp)
90 {
91         if (AfsAppLib_HandleHelp(IDD_SALVAGE, hwndDlg, msg, wp, lp))
92                 return TRUE;
93
94         switch (msg) {
95                 case WM_INITDIALOG:
96                         OnInitDialog(hwndDlg);
97                         break;
98
99                 case WM_COMMAND:
100                         switch (LOWORD(wp)) {
101                                 case IDC_VOLUME_NAME:
102                                 case IDC_NUM_PROCESSES:
103                                 case IDC_LOG_FILE:
104                                 case IDC_TEMP_DIR:
105                                         if (HIWORD(wp) == EN_CHANGE)
106                                                 UpdateControls();
107                                         break;
108
109                                 case IDC_SERVER:
110                                 case IDC_PARTITION:
111                                 case IDC_VOLUME:
112                                 case IDC_NUM_PROCESSES_CHECKBOX:
113                                         UpdateControls();
114                                         break;
115
116                                 case IDC_ADVANCED:
117                                         OnAdvanced();
118                                         break;
119
120                                 case IDCANCEL:
121                                         EndDialog(hDlg, IDCANCEL);
122                                         break;
123
124                                 case IDOK:
125                     if (OnSalvage())
126                                                 EndDialog(hDlg, IDOK);
127                                         break;
128                         }
129                 break;
130     }
131
132     return FALSE;
133 }
134
135
136 /*
137  * STATIC FUNCTIONS _________________________________________________________________
138  *
139  */
140
141 /*
142  * Event Handler Functions _________________________________________________________________
143  *
144  */
145 static void OnInitDialog(HWND hwndDlg)
146 {
147         hDlg = hwndDlg;
148
149         bAdvanced = TRUE;
150
151         TCHAR szNumProcesses[32];
152         _itot(DEFAULT_NUM_PROCESSES, szNumProcesses, 10);
153
154         SetWndText(hDlg, IDC_NUM_PROCESSES, szNumProcesses);
155         SetCheck(hDlg, IDC_NUM_PROCESSES_CHECKBOX);
156         SetWndText(hDlg, IDC_LOG_FILE, A2S(DEFAULT_LOG_FILE));
157
158         // If a partition name isn't selected, then only allow the salvage server option
159         if (szPartitionName[0] == 0) {
160                 SetEnable(hDlg, IDC_PARTITION, ES_DISABLE);
161                 SetEnable(hDlg, IDC_VOLUME, ES_DISABLE);
162                 SetCheck(hDlg, IDC_SERVER);
163         } else
164                 SetCheck(hDlg, IDC_PARTITION);
165         
166         // Close the Advanced portion of the dialog
167         OnAdvanced();
168 }
169
170 static void OnAdvanced()
171 {
172     static int nOffset = 0;
173
174         bAdvanced = !bAdvanced;
175
176         ShowAndEnable(hDlg, IDC_ADVANCED_FRAME, bAdvanced);     
177         ShowAndEnable(hDlg, IDC_LOG_FILE, bAdvanced);
178         ShowAndEnable(hDlg, IDC_LOG_FILE_LABEL, bAdvanced);
179         ShowAndEnable(hDlg, IDC_NUM_PROCESSES, bAdvanced);
180         ShowAndEnable(hDlg, IDC_TEMP_DIR, bAdvanced);
181         ShowAndEnable(hDlg, IDC_TEMP_DIR_LABEL, bAdvanced);
182         ShowAndEnable(hDlg, IDC_NUM_PROCESSES_CHECKBOX, bAdvanced);
183         ShowAndEnable(hDlg, IDC_DAMAGED_VOLUMES, bAdvanced);
184         ShowAndEnable(hDlg, IDC_SMALL_BLOCK_READS, bAdvanced);
185         ShowAndEnable(hDlg, IDC_FORCE_SALVAGE, bAdvanced);
186         ShowAndEnable(hDlg, IDC_FORCE_REBUILD, bAdvanced);
187         ShowAndEnable(hDlg, IDC_LIST_DAMAGED_INODES, bAdvanced);
188         ShowAndEnable(hDlg, IDC_LIST_OWNED_INDOES, bAdvanced);
189
190     // To show or hide the advanced section, we have to resize the dialog
191
192     // Get current position of the dialog
193     RECT rectDlg;
194     GetWindowRect(hDlg, &rectDlg);
195
196     // Figure out offset between full dialog and short dialog
197     if (nOffset == 0) {
198         // Find the frame containing the things we will hide or show
199         HWND hFrame = GetDlgItem(hDlg, IDC_ADVANCED_FRAME);
200     
201         // Get its dimensions
202         RECT rectFrame;
203         GetWindowRect(hFrame, &rectFrame);
204
205         // Find the distance between the bottom of the dialog and the top of the frame    
206         nOffset = rectDlg.bottom - rectFrame.top - 3;
207     }
208
209         int nCurOffset = nOffset;
210
211     if (!bAdvanced)
212                 nCurOffset *= -1;
213         
214     // Adjust dialog position
215         MoveWindow(hDlg, rectDlg.left, rectDlg.top, rectDlg.right - rectDlg.left, rectDlg.bottom - rectDlg.top + nCurOffset, TRUE);
216
217         SetWndText(hDlg, IDC_ADVANCED, bAdvanced ? IDS_ADVANCED_OPEN : IDS_ADVANCED_CLOSED);
218 }
219
220 static void UpdateControls()
221 {
222         // Update volume name controls
223         BOOL bVolume = IsButtonChecked(hDlg, IDC_VOLUME);
224         ENABLE_STATE es = bVolume ? ES_ENABLE : ES_DISABLE;
225         SetEnable(hDlg, IDC_VOLUME_NAME, es);
226         SetEnable(hDlg, IDC_VOLUME_NAME_LABEL, es);
227         GetWndText(hDlg, IDC_VOLUME_NAME, szVolumeName);
228
229         // Num processes edit control
230         BOOL bParallel = IsButtonChecked(hDlg, IDC_NUM_PROCESSES_CHECKBOX);
231         SetEnable(hDlg, IDC_NUM_PROCESSES, (ENABLE_STATE)bParallel);
232         GetWndText(hDlg, IDC_NUM_PROCESSES, szNumProcesses, NUM_PROCS_BUF_SIZE);
233
234         GetWndText(hDlg, IDC_LOG_FILE, g_CfgData.szSalvageLogFileName, _MAX_PATH);
235         GetWndText(hDlg, IDC_TEMP_DIR, szTempDir, _MAX_PATH);
236
237         // Should OK button be enabled or disabled?
238         BOOL bEnable = TRUE;
239
240         if (bVolume)
241                 bEnable = !!lstrlen(szVolumeName);
242
243         if (bEnable && bParallel)
244                 bEnable = !!lstrlen(szNumProcesses);
245
246         SetEnable(hDlg, IDOK, (ENABLE_STATE)bEnable);
247 }
248
249 /*
250  * Utility Functions _________________________________________________________________
251  *
252  */
253 static BOOL OnSalvage()
254 {
255         if (IsButtonChecked(hDlg, IDC_SERVER)) {
256                 pszPartitionName = 0;
257                 pszVolumeName = 0;
258         } else if (IsButtonChecked(hDlg, IDC_PARTITION)) {
259                 pszPartitionName = szPartitionName;
260                 pszVolumeName = 0;
261         } else if (IsButtonChecked(hDlg, IDC_VOLUME)) {
262                 pszPartitionName = szPartitionName;
263                 pszVolumeName = szVolumeName;
264         }
265
266         nNumProcesses = DEFAULT_NUM_PROCESSES;
267         if (IsButtonChecked(hDlg, IDC_NUM_PROCESSES_CHECKBOX)) {
268                 nNumProcesses = _ttoi(szNumProcesses);
269                 if ((nNumProcesses < MIN_NUM_PROCESSES) || (nNumProcesses > MAX_NUM_PROCESSES)) {
270                         ShowError(hDlg, 0, IDS_INVALID_NUM_SALVAGE_PROCESSSES);
271                         return FALSE;
272                 }
273         }
274
275         if (!g_CfgData.bReuseAdminInfo) {
276         if (!GetAdminInfo(hDlg, GAIO_LOGIN_ONLY))
277                 return FALSE;
278
279         if (!GetHandles(hDlg))
280             return FALSE;
281     }
282
283         return TRUE;
284 }
285
286 static DWORD WINAPI Salvage(LPVOID param)
287 {
288         afs_status_t nStatus;
289         void *hServer;
290         int nResult; 
291
292         nResult = bos_ServerOpen(g_hCell, GetHostnameA(), &hServer, &nStatus);
293         if (!nResult) {
294                 ShowError(hDlg, nStatus, IDS_BOS_OPEN_FAILED);
295                 return FALSE;
296         }
297
298         nResult = bos_Salvage(g_hCell, hServer, S2A(pszPartitionName), S2A(pszVolumeName), nNumProcesses, S2A(szTempDir), 0, VOS_NORMAL,
299                                                                 BOS_SALVAGE_DAMAGED_VOLUMES, BOS_SALVAGE_DONT_WRITE_INODES, BOS_SALVAGE_DONT_WRITE_ROOT_INODES,
300                                                                 BOS_SALVAGE_DONT_FORCE_DIRECTORIES, BOS_SALVAGE_DONT_FORCE_BLOCK_READS, &nStatus);
301         if (!nResult)
302         ShowError(hDlg, nStatus, IDS_SALVAGE_ERROR);
303
304     bos_ServerClose(hServer, &nStatus);
305     
306     g_CfgData.bReuseAdminInfo = nResult;
307
308         return nResult;
309 }
310