Menu

[r580]: / trunk / Src / UFolderBackup.pas  Maximize  Restore  History

Download this file

306 lines (270 with data), 10.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
{
* UFolderBackup.pas
*
* Class that can back up all the files in a folder into a single file. All
* files to be backed up must be text files.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UFolderBackup.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UFolderBackup;
interface
type
{
TFolderBackup:
Class that can back up and restore all the files in a folder to or from a
single file. All files to be backed up must be text files.
}
TFolderBackup = class(TObject)
strict private
var
fSrcFolder: string; // Folder to be backed up or restored
fBakFile: string; // Full path to backup file
fFileID: SmallInt; // Identifies the type of file
const
cWatermark = SmallInt($FFFF); // File watermark (v2 and later)
function SourceFileSpec(const FileName: string): string;
{Full path to a file in backed up folder.
@param FileName [in] Base name of file.
@return Full file spec of FileName in source folder.
}
public
constructor Create(const SrcFolder, BakFile: string;
const FileID: SmallInt);
{Class constructor. Creates backup object for specified folder and backup
file.
@param SrcFolder [in] Folder to be backed up / restored. Must contain
only text files. Sub folders are ignored.
@param BakFile [in] Name of backup file.
@param FileID [in] File type identifier.
}
procedure Backup;
{Backs up source folder into single backup file. Backup file is marked
with the file ID passed to constructor.
}
procedure Restore;
{Restores folder from backup file.
@except ECodeSnip raised if any checksum errors are found or, for v3 and
later format files, if file type id is incorrect.
}
end;
implementation
uses
// Delphi
SysUtils, Classes,
// Project
UCheckSum, UDataStreamIO, UDOSDateTime, UExceptions, UUtils;
{
Backup file format
------------------
File comprises ASCII text characters. Numbers are encoded in hex format. Only
text files can be backed up. There are three supported file formats. All three
formats can be read while only v3 files are written.
Version 1 Format
----------------
FileCount: SmallInt - number of files encoded in backup file
followed by FileCount file records of:
Name: SizedString; - name of file without path information
FileDate: LongInt; - file's modification date (DOS file stamp as
LongInt).
MD5: String[32]; - MD5 checksum of original file on server
(MD5 of Content should match this value)
Content: SizedString; - file contents
Version 2 Format
----------------
$FFFF - Indicator of post-v1 file type
$0002 - Indicator for v2 file type
FileCount: SmallInt - number of files encoded in backup file
followed by FileCount file records of:
Name: SizedString; - name of file without path information
FileDate: LongInt; - file's modification date (DOS file stamp as
LongInt).
MD5: String[32]; - MD5 checksum of original file on server
(MD5 of Content should match this value)
Content: SizedLongString; - file contents
Version 3 Format
----------------
$FFFF - Indicator of post-v1 file type
$0003 - Indicator for v3 file type
FileID: SmallInt - indicator for file type (user defined)
FileCount: SmallInt - number of files encoded in backup file
followed by FileCount file records of:
Name: SizedString; - name of file without path information
FileDate: LongInt; - file's modification date (DOS file stamp as
LongInt).
MD5: String[32]; - MD5 checksum of original file on server
(MD5 of Content should match this value)
Content: SizedLongString; - file contents
Data types
----------
SmallInt - 16 bit integer encoded as 4 hex digits
LongInt - 32 bit integer encoded as 8 hex digits
SizedString - SmallInt specifying string length followed by specified
number of characters
SizedLongString - LongInt specifying string length followed by specified
number of characters
String[32] - 32 character fixed length string
}
{ TFolderBackup }
procedure TFolderBackup.Backup;
{Backs up source folder into single backup file. Backup file is marked with
the file ID passed to constructor.
}
var
Writer: TDataStreamWriter; // object used to write data to stream
Files: TStringList; // list of files in database directory
FileName: string; // references each file in database
Content: string; // content of each database file
DOSDateTime: IDOSDateTime; // date stamp of each database file
begin
Files := nil;
// Create output stream to backup file
Writer := TDataStreamWriter.Create(
TFileStream.Create(fBakFile, fmCreate), True
);
try
// Get list of files in database
Files := TStringList.Create;
ListFiles(fSrcFolder, '*.*', Files, False);
// Write file header, including number of files
Writer.WriteSmallInt(cWatermark); // file marker
Writer.WriteSmallInt($0003); // file version
Writer.WriteSmallInt(fFileID); // file ID
Writer.WriteSmallInt(Files.Count); // number of files
// Write details of each file
for FileName in Files do
begin
Writer.WriteSizedString(FileName);
DOSDateTime := TDOSDateTimeFactory.CreateFromFile(
SourceFileSpec(FileName)
);
Writer.WriteLongInt(DOSDateTime.DateStamp);
Content := FileToString(SourceFileSpec(FileName));
Writer.WriteString(TCheckSum.Calculate(Content), 32);
Writer.WriteSizedLongString(Content);
end;
finally
FreeAndNil(Files);
FreeAndNil(Writer);
end;
end;
constructor TFolderBackup.Create(const SrcFolder, BakFile: string;
const FileID: SmallInt);
{Class constructor. Creates backup object for specified folder and backup
file.
@param SrcFolder [in] Folder to be backed up / restored. Must contain only
text files. Sub folders are ignored.
@param BakFile [in] Name of backup file.
@param FileID [in] File type identifier.
}
begin
inherited Create;
fSrcFolder := ExcludeTrailingPathDelimiter(SrcFolder);
fBakFile := BakFile;
fFileID := FileID;
end;
procedure TFolderBackup.Restore;
{Restores folder from backup file.
@except ECodeSnip raised if any checksum errors are found or, for v3 and
later format files, if file type id is incorrect.
}
var
Reader: TDataStreamReader; // object used to read data from file
FileCount: Integer; // number of files to restore
Idx: Integer; // loops through all files in backup
FileName: string; // name of file to restore
MD5: string; // checksum of file to restore
Content: string; // content of file to restore
DOSDateTime: IDOSDateTime; // date stamp of file to restore
HeaderWord: SmallInt; // first word value in file
Version: SmallInt; // file version
resourcestring
// Error message
sBadFileContent = 'Invalid content for file "%s"';
sBadFileID = 'Invalid file ID for file "%s"';
begin
// Make sure restore folder exists
EnsureFolders(fSrcFolder);
// Create reader to access data in backup file
Reader := TDataStreamReader.Create(
TFileStream.Create(fBakFile, fmOpenRead or fmShareDenyNone), True
);
try
// Get number of files stored in backup file and process each one
// Read 1st word of file. If it is $FFFF we have v2 or later file, otherwise
// it's v1
HeaderWord := Reader.ReadSmallInt;
if HeaderWord = cWatermark then
begin
// v2 or later: read version then file count
Version := Reader.ReadSmallInt;
if Version >= 3 then
begin
// version 3 of later: read file id
if Reader.ReadSmallInt <> fFileID then
raise ECodeSnip.CreateFmt(sBadFileID, [FileName]);
end;
FileCount := Reader.ReadSmallInt;
end
else
begin
// v1: file count was first word of file
Version := 1;
FileCount := HeaderWord;
end;
for Idx := 1 to FileCount do
begin
// Get file details: name, date stamp, checksum and content
FileName := SourceFileSpec(Reader.ReadSizedString);
DOSDateTime := TDOSDateTimeFactory.CreateFromDOSTimeStamp(
Reader.ReadLongInt
);
MD5 := Reader.ReadString(32);
if Version = 1 then
Content := Reader.ReadSizedString
else
Content := Reader.ReadSizedLongString;
if not TCheckSum.Compare(Content, MD5) then
raise ECodeSnip.CreateFmt(sBadFileContent, [FileName]);
// Write file and set date stamp
StringToFile(Content, FileName);
DOSDateTime.ApplyToFile(FileName);
end;
finally
FreeAndNil(Reader);
end;
end;
function TFolderBackup.SourceFileSpec(const FileName: string): string;
{Full path to a file in backed up folder.
@param FileName [in] Base name of file.
@return Full file spec of FileName in source folder.
}
begin
Result := IncludeTrailingPathDelimiter(fSrcFolder) + FileName;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.