1*c2c66affSColin Finck #include "Defragment.h"
2*c2c66affSColin Finck 
3*c2c66affSColin Finck 
4*c2c66affSColin Finck // Ahh yes I ripped this from my old Findupes project :)
5*c2c66affSColin Finck // Fits a path name, composed of a path (i.e. "c:\blah\blah\cha\cha") and a filename ("stuff.txt")
6*c2c66affSColin Finck // and fits it to a given length. If it has to truncate it will first truncate from the path,
7*c2c66affSColin Finck // substituting in periods. So you might end up with something like:
8*c2c66affSColin Finck // C:\Program Files\Micro...\Register.exe
FitName(wchar_t * destination,const wchar_t * path,const wchar_t * filename,uint32 totalWidth)9*c2c66affSColin Finck int FitName (wchar_t *destination, const wchar_t *path, const wchar_t *filename, uint32 totalWidth)
10*c2c66affSColin Finck {
11*c2c66affSColin Finck 	uint32 pathLen=0;
12*c2c66affSColin Finck 	uint32 fnLen=0;
13*c2c66affSColin Finck 	uint32 halfTotLen=0;
14*c2c66affSColin Finck 	uint32 len4fn=0;     /* number of chars remaining for filename after path is applied */
15*c2c66affSColin Finck 	uint32 len4path=0;   /* number of chars for path before filename is applied          */
16*c2c66affSColin Finck 	wchar_t fmtStrPath[20]=L"";
17*c2c66affSColin Finck 	wchar_t fmtStrFile[20]=L"";
18*c2c66affSColin Finck 	wchar_t fmtString[40]=L"";
19*c2c66affSColin Finck 
20*c2c66affSColin Finck     /*
21*c2c66affSColin Finck 	assert (destination != NULL);
22*c2c66affSColin Finck 	assert (path != NULL);
23*c2c66affSColin Finck 	assert (filename != NULL);
24*c2c66affSColin Finck 	assert (totalWidth != 0);
25*c2c66affSColin Finck     */
26*c2c66affSColin Finck 
27*c2c66affSColin Finck 	pathLen = wcslen(path);
28*c2c66affSColin Finck 	fnLen = wcslen(filename);
29*c2c66affSColin Finck 	if (!(totalWidth % 2))
30*c2c66affSColin Finck 		halfTotLen=totalWidth / 2;
31*c2c66affSColin Finck 	else
32*c2c66affSColin Finck 		halfTotLen=(totalWidth-1) / 2;  /* -1 because otherwise (halfTotLen*2) ==
33*c2c66affSColin Finck (totalWidth+1) which wouldn't be good */
34*c2c66affSColin Finck 
35*c2c66affSColin Finck 	/* determine how much width the path and filename each get */
36*c2c66affSColin Finck 	if ( (pathLen >= halfTotLen) && (fnLen < halfTotLen) )
37*c2c66affSColin Finck 	{
38*c2c66affSColin Finck 		len4fn = fnLen;
39*c2c66affSColin Finck 		len4path = (totalWidth - len4fn);
40*c2c66affSColin Finck 	}
41*c2c66affSColin Finck 
42*c2c66affSColin Finck 	if ( (pathLen < halfTotLen) && (fnLen < halfTotLen) )
43*c2c66affSColin Finck 	{
44*c2c66affSColin Finck 		len4fn = fnLen;
45*c2c66affSColin Finck 		len4path = pathLen;
46*c2c66affSColin Finck 	}
47*c2c66affSColin Finck 
48*c2c66affSColin Finck 	if ( (pathLen >= halfTotLen) && (fnLen >= halfTotLen) )
49*c2c66affSColin Finck 	{
50*c2c66affSColin Finck 		len4fn = halfTotLen;
51*c2c66affSColin Finck 		len4path = halfTotLen;
52*c2c66affSColin Finck 	}
53*c2c66affSColin Finck 
54*c2c66affSColin Finck 	if ( (pathLen < halfTotLen) && (fnLen >= halfTotLen) )
55*c2c66affSColin Finck 	{
56*c2c66affSColin Finck 		len4path = pathLen;
57*c2c66affSColin Finck 		len4fn = (totalWidth - len4path);
58*c2c66affSColin Finck 	}
59*c2c66affSColin Finck 	/*
60*c2c66affSColin Finck 		if halfTotLen was adjusted above to avoid a rounding error, give the
61*c2c66affSColin Finck 		extra wchar_t to the filename
62*c2c66affSColin Finck 	*/
63*c2c66affSColin Finck 	if (halfTotLen < (totalWidth/2)) len4path++;
64*c2c66affSColin Finck 
65*c2c66affSColin Finck 	if (pathLen > len4path)	swprintf (fmtStrPath, L"%%.%ds...\\", len4path-4);
66*c2c66affSColin Finck 	else
67*c2c66affSColin Finck 		swprintf (fmtStrPath, L"%%s");
68*c2c66affSColin Finck 
69*c2c66affSColin Finck 	if (fnLen > len4fn)	swprintf (fmtStrFile, L"%%.%ds...", len4fn-3);
70*c2c66affSColin Finck 	else
71*c2c66affSColin Finck 		swprintf (fmtStrFile, L"%%s");
72*c2c66affSColin Finck 
73*c2c66affSColin Finck 	wcscpy (fmtString, fmtStrPath);
74*c2c66affSColin Finck 	wcscat (fmtString, fmtStrFile);
75*c2c66affSColin Finck 	/*swprintf (fmtString, L"%s%s", fmtStrPath, fmtStrFile);*/
76*c2c66affSColin Finck 	swprintf (destination, fmtString, path,filename);
77*c2c66affSColin Finck 
78*c2c66affSColin Finck 	return (1);
79*c2c66affSColin Finck }
80*c2c66affSColin Finck 
Defragment(wstring Name,DefragType DefragMethod)81*c2c66affSColin Finck Defragment::Defragment (wstring Name, DefragType DefragMethod)
82*c2c66affSColin Finck {
83*c2c66affSColin Finck     Method = DefragMethod;
84*c2c66affSColin Finck     DoLimitLength = true;
85*c2c66affSColin Finck     Error = false;
86*c2c66affSColin Finck     Done = false;
87*c2c66affSColin Finck     PleaseStop = false;
88*c2c66affSColin Finck     PleasePause = false;
89*c2c66affSColin Finck     DriveName = Name;
90*c2c66affSColin Finck     StatusPercent = 0.0f;
91*c2c66affSColin Finck     LastBMPUpdate = GetTickCount ();
92*c2c66affSColin Finck 
93*c2c66affSColin Finck     SetStatusString (L"Opening volume " + Name);
94*c2c66affSColin Finck     if (!Volume.Open (Name))
95*c2c66affSColin Finck     {
96*c2c66affSColin Finck         SetStatusString (L"Error opening volume " + Name);
97*c2c66affSColin Finck         Error = true;
98*c2c66affSColin Finck         Done = true;
99*c2c66affSColin Finck         StatusPercent = 100.0f;
100*c2c66affSColin Finck     }
101*c2c66affSColin Finck 
102*c2c66affSColin Finck     return;
103*c2c66affSColin Finck }
104*c2c66affSColin Finck 
105*c2c66affSColin Finck 
~Defragment()106*c2c66affSColin Finck Defragment::~Defragment ()
107*c2c66affSColin Finck {
108*c2c66affSColin Finck     if (!IsDoneYet ())
109*c2c66affSColin Finck     {
110*c2c66affSColin Finck         Stop ();
111*c2c66affSColin Finck         while (!IsDoneYet()  &&  !HasError())
112*c2c66affSColin Finck         {
113*c2c66affSColin Finck             SetStatusString (L"Waiting for thread to stop ...");
114*c2c66affSColin Finck             Sleep (150);
115*c2c66affSColin Finck         }
116*c2c66affSColin Finck     }
117*c2c66affSColin Finck 
118*c2c66affSColin Finck     Volume.Close ();
119*c2c66affSColin Finck     return;
120*c2c66affSColin Finck }
121*c2c66affSColin Finck 
122*c2c66affSColin Finck 
SetStatusString(wstring NewStatus)123*c2c66affSColin Finck void Defragment::SetStatusString (wstring NewStatus)
124*c2c66affSColin Finck {
125*c2c66affSColin Finck     Lock ();
126*c2c66affSColin Finck     StatusString = NewStatus;
127*c2c66affSColin Finck     Unlock ();
128*c2c66affSColin Finck 
129*c2c66affSColin Finck     return;
130*c2c66affSColin Finck }
131*c2c66affSColin Finck 
132*c2c66affSColin Finck 
GetStatusString(void)133*c2c66affSColin Finck wstring Defragment::GetStatusString (void)
134*c2c66affSColin Finck {
135*c2c66affSColin Finck     wstring ReturnVal;
136*c2c66affSColin Finck 
137*c2c66affSColin Finck     Lock ();
138*c2c66affSColin Finck     ReturnVal = StatusString;
139*c2c66affSColin Finck     Unlock ();
140*c2c66affSColin Finck 
141*c2c66affSColin Finck     return (ReturnVal);
142*c2c66affSColin Finck }
143*c2c66affSColin Finck 
144*c2c66affSColin Finck 
GetStatusPercent(void)145*c2c66affSColin Finck double Defragment::GetStatusPercent (void)
146*c2c66affSColin Finck {
147*c2c66affSColin Finck     return (StatusPercent);
148*c2c66affSColin Finck }
149*c2c66affSColin Finck 
150*c2c66affSColin Finck 
IsDoneYet(void)151*c2c66affSColin Finck bool Defragment::IsDoneYet (void)
152*c2c66affSColin Finck {
153*c2c66affSColin Finck     return (Done);
154*c2c66affSColin Finck }
155*c2c66affSColin Finck 
156*c2c66affSColin Finck 
Start(void)157*c2c66affSColin Finck void Defragment::Start (void)
158*c2c66affSColin Finck {
159*c2c66affSColin Finck     uint32 i;
160*c2c66affSColin Finck     uint64 FirstFreeLCN;
161*c2c66affSColin Finck     uint64 TotalClusters;
162*c2c66affSColin Finck     uint64 ClustersProgress;
163*c2c66affSColin Finck     wchar_t PrintName[80];
164*c2c66affSColin Finck     int Width = 70;
165*c2c66affSColin Finck 
166*c2c66affSColin Finck     if (Error)
167*c2c66affSColin Finck         goto DoneDefrag;
168*c2c66affSColin Finck 
169*c2c66affSColin Finck     // First thing: build a file list.
170*c2c66affSColin Finck     SetStatusString (L"Getting volume bitmap");
171*c2c66affSColin Finck     if (!Volume.GetBitmap())
172*c2c66affSColin Finck     {
173*c2c66affSColin Finck         SetStatusString (L"Could not get volume " + DriveName + L" bitmap");
174*c2c66affSColin Finck         Error = true;
175*c2c66affSColin Finck         goto DoneDefrag;
176*c2c66affSColin Finck     }
177*c2c66affSColin Finck 
178*c2c66affSColin Finck     LastBMPUpdate = GetTickCount ();
179*c2c66affSColin Finck 
180*c2c66affSColin Finck     if (PleaseStop)
181*c2c66affSColin Finck         goto DoneDefrag;
182*c2c66affSColin Finck 
183*c2c66affSColin Finck     SetStatusString (L"Obtaining volume geometry");
184*c2c66affSColin Finck     if (!Volume.ObtainInfo ())
185*c2c66affSColin Finck     {
186*c2c66affSColin Finck         SetStatusString (L"Could not obtain volume " + DriveName + L" geometry");
187*c2c66affSColin Finck         Error = true;
188*c2c66affSColin Finck         goto DoneDefrag;
189*c2c66affSColin Finck     }
190*c2c66affSColin Finck 
191*c2c66affSColin Finck     if (PleaseStop)
192*c2c66affSColin Finck         goto DoneDefrag;
193*c2c66affSColin Finck 
194*c2c66affSColin Finck     SetStatusString (L"Building file database for volume " + DriveName);
195*c2c66affSColin Finck     if (!Volume.BuildFileList (PleaseStop, StatusPercent))
196*c2c66affSColin Finck     {
197*c2c66affSColin Finck         SetStatusString (L"Could not build file database for volume " + DriveName);
198*c2c66affSColin Finck         Error = true;
199*c2c66affSColin Finck         goto DoneDefrag;
200*c2c66affSColin Finck     }
201*c2c66affSColin Finck 
202*c2c66affSColin Finck     if (PleaseStop)
203*c2c66affSColin Finck         goto DoneDefrag;
204*c2c66affSColin Finck 
205*c2c66affSColin Finck     SetStatusString (L"Analyzing database for " + DriveName);
206*c2c66affSColin Finck     TotalClusters = 0;
207*c2c66affSColin Finck     for (i = 0; i < Volume.GetDBFileCount(); i++)
208*c2c66affSColin Finck     {
209*c2c66affSColin Finck         TotalClusters += Volume.GetDBFile(i).Clusters;
210*c2c66affSColin Finck     }
211*c2c66affSColin Finck 
212*c2c66affSColin Finck     // Defragment!
213*c2c66affSColin Finck     ClustersProgress = 0;
214*c2c66affSColin Finck 
215*c2c66affSColin Finck     // Find first free LCN for speedier searches ...
216*c2c66affSColin Finck     Volume.FindFreeRange (0, 1, FirstFreeLCN);
217*c2c66affSColin Finck 
218*c2c66affSColin Finck     if (PleaseStop)
219*c2c66affSColin Finck         goto DoneDefrag;
220*c2c66affSColin Finck 
221*c2c66affSColin Finck     // Analyze?
222*c2c66affSColin Finck     if (Method == DefragAnalyze)
223*c2c66affSColin Finck     {
224*c2c66affSColin Finck         uint32 j;
225*c2c66affSColin Finck 
226*c2c66affSColin Finck         Report.RootPath = Volume.GetRootPath ();
227*c2c66affSColin Finck 
228*c2c66affSColin Finck         Report.FraggedFiles.clear ();
229*c2c66affSColin Finck         Report.UnfraggedFiles.clear ();
230*c2c66affSColin Finck         Report.UnmovableFiles.clear ();
231*c2c66affSColin Finck 
232*c2c66affSColin Finck         Report.FilesCount = Volume.GetDBFileCount () - Volume.GetDBDirCount ();
233*c2c66affSColin Finck         Report.DirsCount = Volume.GetDBDirCount ();
234*c2c66affSColin Finck         Report.DiskSizeBytes = Volume.GetVolumeInfo().TotalBytes;
235*c2c66affSColin Finck 
236*c2c66affSColin Finck         Report.FilesSizeClusters = 0;
237*c2c66affSColin Finck         Report.FilesSlackBytes = 0;
238*c2c66affSColin Finck         Report.FilesSizeBytes = 0;
239*c2c66affSColin Finck         Report.FilesFragments = 0;
240*c2c66affSColin Finck 
241*c2c66affSColin Finck         for (j = 0; j < Volume.GetDBFileCount(); j++)
242*c2c66affSColin Finck         {
243*c2c66affSColin Finck             FileInfo Info;
244*c2c66affSColin Finck 
245*c2c66affSColin Finck             Info = Volume.GetDBFile (j);
246*c2c66affSColin Finck 
247*c2c66affSColin Finck             Report.FilesFragments += max ((size_t)1, Info.Fragments.size()); // add 1 fragment even for 0 bytes/0 cluster files
248*c2c66affSColin Finck 
249*c2c66affSColin Finck             if (Info.Attributes.Process == 0)
250*c2c66affSColin Finck                 continue;
251*c2c66affSColin Finck 
252*c2c66affSColin Finck             SetStatusString (Volume.GetDBDir (Info.DirIndice) + Info.Name);
253*c2c66affSColin Finck 
254*c2c66affSColin Finck             Report.FilesSizeClusters += Info.Clusters;
255*c2c66affSColin Finck             Report.FilesSizeBytes += Info.Size;
256*c2c66affSColin Finck 
257*c2c66affSColin Finck             if (Info.Attributes.Unmovable == 1)
258*c2c66affSColin Finck                 Report.UnmovableFiles.push_back (j);
259*c2c66affSColin Finck 
260*c2c66affSColin Finck             if (Info.Fragments.size() > 1)
261*c2c66affSColin Finck                 Report.FraggedFiles.push_back (j);
262*c2c66affSColin Finck             else
263*c2c66affSColin Finck                 Report.UnfraggedFiles.push_back (j);
264*c2c66affSColin Finck 
265*c2c66affSColin Finck             StatusPercent = ((double)j / (double)Report.FilesCount) * 100.0f;
266*c2c66affSColin Finck         }
267*c2c66affSColin Finck 
268*c2c66affSColin Finck         Report.FilesSizeOnDisk = Report.FilesSizeClusters * (uint64)Volume.GetVolumeInfo().ClusterSize;
269*c2c66affSColin Finck         Report.FilesSlackBytes = Report.FilesSizeOnDisk - Report.FilesSizeBytes;
270*c2c66affSColin Finck         Report.AverageFragments = (double)Report.FilesFragments / (double)Report.FilesCount;
271*c2c66affSColin Finck         Report.PercentFragged = 100.0f * ((double)(signed)Report.FraggedFiles.size() / (double)(signed)Report.FilesCount);
272*c2c66affSColin Finck 
273*c2c66affSColin Finck         uint64 Percent;
274*c2c66affSColin Finck         Percent = (10000 * Report.FilesSlackBytes) / Report.FilesSizeOnDisk;
275*c2c66affSColin Finck         Report.PercentSlack = (double)(signed)Percent / 100.0f;
276*c2c66affSColin Finck     }
277*c2c66affSColin Finck     else
278*c2c66affSColin Finck     // Go through all the files and ... defragment them!
279*c2c66affSColin Finck     for (i = 0; i < Volume.GetDBFileCount(); i++)
280*c2c66affSColin Finck     {
281*c2c66affSColin Finck         FileInfo Info;
282*c2c66affSColin Finck         bool Result;
283*c2c66affSColin Finck         uint64 TargetLCN;
284*c2c66affSColin Finck         uint64 PreviousClusters;
285*c2c66affSColin Finck 
286*c2c66affSColin Finck         // What? They want us to pause? Oh ok.
287*c2c66affSColin Finck         if (PleasePause)
288*c2c66affSColin Finck         {
289*c2c66affSColin Finck             SetStatusString (L"Paused");
290*c2c66affSColin Finck             PleasePause = false;
291*c2c66affSColin Finck 
292*c2c66affSColin Finck             while (PleasePause == false)
293*c2c66affSColin Finck             {
294*c2c66affSColin Finck                 Sleep (50);
295*c2c66affSColin Finck             }
296*c2c66affSColin Finck 
297*c2c66affSColin Finck             PleasePause = false;
298*c2c66affSColin Finck         }
299*c2c66affSColin Finck 
300*c2c66affSColin Finck         if (PleaseStop)
301*c2c66affSColin Finck         {
302*c2c66affSColin Finck             SetStatusString (L"Stopping");
303*c2c66affSColin Finck             break;
304*c2c66affSColin Finck         }
305*c2c66affSColin Finck 
306*c2c66affSColin Finck         //
307*c2c66affSColin Finck         Info = Volume.GetDBFile (i);
308*c2c66affSColin Finck 
309*c2c66affSColin Finck         PreviousClusters = ClustersProgress;
310*c2c66affSColin Finck         ClustersProgress += Info.Clusters;
311*c2c66affSColin Finck 
312*c2c66affSColin Finck         if (Info.Attributes.Process == 0)
313*c2c66affSColin Finck             continue;
314*c2c66affSColin Finck 
315*c2c66affSColin Finck         if (!DoLimitLength)
316*c2c66affSColin Finck             SetStatusString (Volume.GetDBDir (Info.DirIndice) + Info.Name);
317*c2c66affSColin Finck         else
318*c2c66affSColin Finck         {
319*c2c66affSColin Finck             FitName (PrintName, Volume.GetDBDir (Info.DirIndice).c_str(), Info.Name.c_str(), Width);
320*c2c66affSColin Finck             SetStatusString (PrintName);
321*c2c66affSColin Finck         }
322*c2c66affSColin Finck 
323*c2c66affSColin Finck         // Calculate percentage complete
324*c2c66affSColin Finck         StatusPercent = 100.0f * double((double)PreviousClusters / (double)TotalClusters);
325*c2c66affSColin Finck 
326*c2c66affSColin Finck         // Can't defrag directories yet
327*c2c66affSColin Finck         if (Info.Attributes.Directory == 1)
328*c2c66affSColin Finck             continue;
329*c2c66affSColin Finck 
330*c2c66affSColin Finck         // Can't defrag 0 byte files :)
331*c2c66affSColin Finck         if (Info.Fragments.empty())
332*c2c66affSColin Finck             continue;
333*c2c66affSColin Finck 
334*c2c66affSColin Finck         // If doing fast defrag, skip non-fragmented files
335*c2c66affSColin Finck         // Note: This assumes that the extents stored in Info.Fragments
336*c2c66affSColin Finck         //       are consolidated. I.e. we assume it is NOT the case that
337*c2c66affSColin Finck         //       two extents account for a sequential range of (non-
338*c2c66affSColin Finck         //       fragmented) clusters.
339*c2c66affSColin Finck         if (Info.Fragments.size() == 1  &&  Method == DefragFast)
340*c2c66affSColin Finck             continue;
341*c2c66affSColin Finck 
342*c2c66affSColin Finck         // Otherwise, defrag0rize it!
343*c2c66affSColin Finck         int Retry = 3;  // retry a few times
344*c2c66affSColin Finck         while (Retry > 0)
345*c2c66affSColin Finck         {
346*c2c66affSColin Finck             // Find a place that can fit the file
347*c2c66affSColin Finck             Result = Volume.FindFreeRange (FirstFreeLCN, Info.Clusters, TargetLCN);
348*c2c66affSColin Finck 
349*c2c66affSColin Finck             // If yes, try moving it
350*c2c66affSColin Finck             if (Result)
351*c2c66affSColin Finck             {
352*c2c66affSColin Finck                 // If we're doing an extensive defrag and the file is already defragmented
353*c2c66affSColin Finck                 // and if its new location would be after its current location, don't
354*c2c66affSColin Finck                 // move it.
355*c2c66affSColin Finck                 if (Method == DefragExtensive  &&  Info.Fragments.size() == 1  &&
356*c2c66affSColin Finck                     TargetLCN > Info.Fragments[0].StartLCN)
357*c2c66affSColin Finck                 {
358*c2c66affSColin Finck                     Retry = 1;
359*c2c66affSColin Finck                 }
360*c2c66affSColin Finck                 else
361*c2c66affSColin Finck                 {
362*c2c66affSColin Finck                     if (Volume.MoveFileDumb (i, TargetLCN))
363*c2c66affSColin Finck                     {
364*c2c66affSColin Finck                         Retry = 1; // yay, all done with this file.
365*c2c66affSColin Finck                         Volume.FindFreeRange (0, 1, FirstFreeLCN);
366*c2c66affSColin Finck                     }
367*c2c66affSColin Finck                 }
368*c2c66affSColin Finck             }
369*c2c66affSColin Finck 
370*c2c66affSColin Finck             // New: Only update bitmap if it's older than 15 seconds
371*c2c66affSColin Finck             if ((GetTickCount() - LastBMPUpdate) < 15000)
372*c2c66affSColin Finck                 Retry = 1;
373*c2c66affSColin Finck             else
374*c2c66affSColin Finck             if (!Result  ||  Retry != 1)
375*c2c66affSColin Finck             {   // hmm. Wait for a moment, then update the drive bitmap
376*c2c66affSColin Finck                 //SetStatusString (L"(Reobtaining volume " + DriveName + L" bitmap)");
377*c2c66affSColin Finck 
378*c2c66affSColin Finck                 if (!DoLimitLength)
379*c2c66affSColin Finck                 {
380*c2c66affSColin Finck                     SetStatusString (GetStatusString() + wstring (L" ."));
381*c2c66affSColin Finck                 }
382*c2c66affSColin Finck 
383*c2c66affSColin Finck                 if (Volume.GetBitmap ())
384*c2c66affSColin Finck                 {
385*c2c66affSColin Finck                     LastBMPUpdate = GetTickCount ();
386*c2c66affSColin Finck 
387*c2c66affSColin Finck                     if (!DoLimitLength)
388*c2c66affSColin Finck                         SetStatusString (Volume.GetDBDir (Info.DirIndice) + Info.Name);
389*c2c66affSColin Finck                     else
390*c2c66affSColin Finck                         SetStatusString (PrintName);
391*c2c66affSColin Finck 
392*c2c66affSColin Finck                     Volume.FindFreeRange (0, 1, FirstFreeLCN);
393*c2c66affSColin Finck                 }
394*c2c66affSColin Finck                 else
395*c2c66affSColin Finck                 {
396*c2c66affSColin Finck                     SetStatusString (L"Could not re-obtain volume " + DriveName + L" bitmap");
397*c2c66affSColin Finck                     Error = true;
398*c2c66affSColin Finck                 }
399*c2c66affSColin Finck             }
400*c2c66affSColin Finck 
401*c2c66affSColin Finck             Retry--;
402*c2c66affSColin Finck         }
403*c2c66affSColin Finck 
404*c2c66affSColin Finck         if (Error == true)
405*c2c66affSColin Finck             break;
406*c2c66affSColin Finck     }
407*c2c66affSColin Finck 
408*c2c66affSColin Finck DoneDefrag:
409*c2c66affSColin Finck     wstring OldStatus;
410*c2c66affSColin Finck 
411*c2c66affSColin Finck     OldStatus = GetStatusString ();
412*c2c66affSColin Finck     StatusPercent = 99.999999f;
413*c2c66affSColin Finck     SetStatusString (L"Closing volume " + DriveName);
414*c2c66affSColin Finck     Volume.Close ();
415*c2c66affSColin Finck     StatusPercent = 100.0f;
416*c2c66affSColin Finck 
417*c2c66affSColin Finck     // If there was an error then the wstring has already been set
418*c2c66affSColin Finck     if (Error)
419*c2c66affSColin Finck         SetStatusString (OldStatus);
420*c2c66affSColin Finck     else
421*c2c66affSColin Finck     if (PleaseStop)
422*c2c66affSColin Finck         SetStatusString (L"Volume " + DriveName + L" defragmentation was stopped");
423*c2c66affSColin Finck     else
424*c2c66affSColin Finck         SetStatusString (L"Finished defragmenting " + DriveName);
425*c2c66affSColin Finck 
426*c2c66affSColin Finck     Done = true;
427*c2c66affSColin Finck 
428*c2c66affSColin Finck     return;
429*c2c66affSColin Finck }
430*c2c66affSColin Finck 
431*c2c66affSColin Finck 
TogglePause(void)432*c2c66affSColin Finck void Defragment::TogglePause (void)
433*c2c66affSColin Finck {
434*c2c66affSColin Finck     Lock ();
435*c2c66affSColin Finck     SetStatusString (L"Pausing ...");
436*c2c66affSColin Finck     PleasePause = true;
437*c2c66affSColin Finck     Unlock ();
438*c2c66affSColin Finck 
439*c2c66affSColin Finck     return;
440*c2c66affSColin Finck }
441*c2c66affSColin Finck 
442*c2c66affSColin Finck 
Stop(void)443*c2c66affSColin Finck void Defragment::Stop (void)
444*c2c66affSColin Finck {
445*c2c66affSColin Finck     Lock ();
446*c2c66affSColin Finck     SetStatusString (L"Stopping ...");
447*c2c66affSColin Finck     PleaseStop = true;
448*c2c66affSColin Finck     Unlock ();
449*c2c66affSColin Finck 
450*c2c66affSColin Finck     return;
451*c2c66affSColin Finck }
452*c2c66affSColin Finck 
453*c2c66affSColin Finck 
HasError(void)454*c2c66affSColin Finck bool Defragment::HasError (void)
455*c2c66affSColin Finck {
456*c2c66affSColin Finck     return (Error);
457*c2c66affSColin Finck }
458*c2c66affSColin Finck 
459