summaryrefslogtreecommitdiffstats
path: root/private/utils/restore/src/new.c
blob: 4f57fa0ff31d03e3c1b405dfd9e63c2bd6243d95 (plain) (blame)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    new.c

Abstract:

    Functions dealing with new backup file format

Author:

    Ramon Juan San Andres (ramonsa) 20-Feb-1991


Revision History:


--*/


#include "restore.h"


//
//  A Disk in the "New" format can be either a DOS disk, an OS/2 1.1
//  disk, or an OS/2 1.2 disk.
//
typedef enum DISK_FORMAT {
    FORMAT_DOS,             //  DOS Backup format
    FORMAT_OS211,           //  OS/2 V1.1 Backup format
    FORMAT_OS212            //  OS/2 V1.2 Backup format
} DISK_FORMAT;


//
//  Global variables
//
NEW_DISKINFO        NewDiskInfo;    //  Disk header
BOOL                DiskNew;        //  TRUE if this is a new disk
HANDLE              ControlHandle;  //  Handle of control file
DISK_FORMAT         DiskFormat;     //  Format of the backup disk
WORD                NumEntries;     //  Number of entries in current dir

//
//  Buffers for containing directory and file records
//
BYTE    DirBuffer  [ sizeof( DIR_RECORD_OS212 )  ];
BYTE    FileBuffer [ sizeof( FILE_RECORD_OS212 ) ];

//
//  The following macros are used to access the fields of the directory
//  and file records of the different backup formats.
//
#define DirRecord       ( (PDIR_RECORD)DirBuffer )
#define DirRecordOs211  ( (PDIR_RECORD_OS211)DirBuffer )
#define DirRecordOs212  ( (PDIR_RECORD_OS212)DirBuffer )
#define FileRecord      ( (PFILE_RECORD)FileBuffer )
#define FileRecordOs211 ( (PFILE_RECORD_OS211)FileBuffer )
#define FileRecordOs212 ( (PFILE_RECORD_OS212)FileBuffer )

//
//  Macro for obtaining the offset of the next directory record in the
//  control file.
//
#define NextDirRecord                                                   \
    ( (DiskFormat == FORMAT_DOS)   ? DirRecord->NextDirRecord :         \
      (DiskFormat == FORMAT_OS211) ? DirRecordOs211->NextDirRecord :    \
      DirRecordOs212->NextDirRecord )


//
//  The names of the control and backup files. These are patched to
//  reflect drive and sequence number.
//
BOOLEAN UseSubdir = FALSE;
CHAR    ControlFile[] = "?:\\CONTROL.???";
CHAR    SubdirControlFile[] = "?:\\BACKUP\\CONTROL.???";
CHAR    BackupFile[]  = "?:\\BACKUP.XXX";
CHAR    SubdirBackupFile[] = "?:\\BACKUP\\BACKUP.XXX";



//
//  Local prototypes
//
static
BOOL
GetDirRecord (
    );

static
BOOL
GotOneFile (
    PFILE_INFO  FileInfo
    );







//  **********************************************************************

DWORD
New_VerifyDiskSequence (
    PWORD    Sequence
    )
/*++

Routine Description:

    Verify that a certain backup disk is mounted.

Arguments:

    OUT Sequence    -   Supplies pointer to the desired sequence number.

Return Value:

    DISK_OK             if format and sequence match,
    DISK_OUTOFSEQUENCE  if new format but wrong sequence
    DISK_UNKNOWN        if not in new format

--*/
{

    DWORD           NumRead;        //  Number of bytes read
    FILETIME        UtcLastTime;    //  Time last written, UTC
    FILETIME        LastTime;       //  Time last written, local time
    HANDLE          FindHandle;     //  Handle for FindFirstFile
    WIN32_FIND_DATA FindData;       //  Buffer for FindFirstFile

    //
    //  Patch the name of the control file.
    //
    ControlFile[0]  = BackupFile[0] = *SourceSpec;
    SubdirControlFile[0] = SubdirBackupFile[0] = *SourceSpec;

    ControlFile[11] = ControlFile[12] = ControlFile[13] = '?';
    SubdirControlFile[18] = SubdirControlFile[19] = SubdirControlFile[20] = '?';

    //
    //  Try to find the control file
    //
    UseSubdir = FALSE;
    FindHandle = FindFirstFile(ControlFile, &FindData);

    if (FindHandle == INVALID_HANDLE_VALUE) {

        //  Did not find the control file in the root.  If this
        //  is not a removable drive, look in the subdirectory
        //  BACKUP.
        //
        if( SourceDriveType != DRIVE_REMOVABLE &&
            (FindHandle = FindFirstFile(SubdirControlFile, &FindData)) != INVALID_HANDLE_VALUE ) {

            UseSubdir = TRUE;

        } else {

            //
            //  There is no control file, this is not a backup disk in the
            //  new format.
            //
            return DISK_UNKNOWN;
        }
    }

    //
    //  Patch the control and backup file names.
    //
    ControlFile[13] = BackupFile[12] = FindData.cFileName[10];
    ControlFile[12] = BackupFile[11] = FindData.cFileName[9];
    ControlFile[11] = BackupFile[10] = FindData.cFileName[8];

    SubdirControlFile[20] = SubdirBackupFile[19] = FindData.cFileName[10];
    SubdirControlFile[19] = SubdirBackupFile[18] = FindData.cFileName[9];
    SubdirControlFile[18] = SubdirBackupFile[17] = FindData.cFileName[8];

    FindClose(FindHandle);

    //
    //  Open the control file
    //
    ControlHandle = CreateFile( UseSubdir ? SubdirControlFile : ControlFile,
                                GENERIC_READ,
                                FILE_SHARE_READ,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL );

    if (ControlHandle == INVALID_HANDLE_VALUE) {
        //
        //  We should never get here, because we know the file is there!
        //
        return DISK_UNKNOWN;
    }


    //
    //  Read the header of the control file and verify it.
    //
    ReadFile( ControlHandle, &NewDiskInfo, sizeof(NEW_DISKINFO), &NumRead, NULL);

    if (NumRead != sizeof(NEW_DISKINFO)) {
        //
        //  The Name might say otherwise, but this is not a valid control
        //  file.
        //
        CloseHandle(ControlHandle);
        return DISK_UNKNOWN;
    }


    NewDiskInfo.Id[6] = '\0';
    if (strcmp(NewDiskInfo.Id, BACKUP_ID)) {
        //
        //  The control file does not have the correct signature.
        //
        CloseHandle(ControlHandle);
        return DISK_UNKNOWN;
    }

    if (NewDiskInfo.SequenceNumber != (BYTE)*Sequence) {
        //
        //  The disk is in the correct format, but has an incorrect
        //  sequence number.
        //
        CloseHandle(ControlHandle);
        *Sequence = NewDiskInfo.SequenceNumber;
        return (DISK_OUTOFSEQUENCE | DISK_NEW_FORMAT);
    }

    //
    //  The disk is O.K. obtain the backup date. (We disregard the time)
    //
    GetFileTime( ControlHandle,
                 NULL,
                 NULL,
                 &UtcLastTime );

    // Convert the file time into local time:
    //
    FileTimeToLocalFileTime( &UtcLastTime, &LastTime );

    FileTimeToDosDateTime( &LastTime,
                           (LPWORD)&BackupDate,
                           (LPWORD)&BackupTime );


    DiskNew = TRUE;

    //
    //  Determine if the disk is a DOS, OS211 or OS212 backup disk
    //
    SetFilePointer(ControlHandle, sizeof(NEW_DISKINFO), 0, FILE_BEGIN);

    ReadFile( ControlHandle, DirRecord, sizeof(DIR_RECORD), &NumRead, NULL);


    if ( NumRead != sizeof(DIR_RECORD)) {

        if ( NumRead >= sizeof( DIR_RECORD_OS212 ) - MAX_PATH ) {
            if ( DirRecordOs212->SystemVersion == SYSTEM_VERSION_OS2_12 ) {
                DiskFormat = FORMAT_OS212;
            } else {
                return DiskFormat = DISK_UNKNOWN;
            }
        } else {
            return DiskFormat = DISK_UNKNOWN;
        }

    } else {

        if ( DirRecordOs212->SystemVersion == SYSTEM_VERSION_OS2_12 ) {
            DiskFormat = FORMAT_OS212;
        } else if ( DirRecord->Length > sizeof(DIR_RECORD) ) {
            DiskFormat = FORMAT_OS211;
        } else if ( DirRecord->Length == sizeof(DIR_RECORD) ) {
            DiskFormat = FORMAT_DOS;
        } else {
            return DiskFormat = DISK_UNKNOWN;
        }
    }

    return ( DISK_OK | DISK_NEW_FORMAT );

}




//  **********************************************************************


BOOL
New_IsLastBackupDisk (
    void
    )
/*++

Routine Description:

    Determines if the backup disk is the last one.

Arguments:

    None

Return Value:

    TRUE if disk is the last one, FALSE otherwise

--*/
{
    return (NewDiskInfo.Flag == LAST_BACKUP_DISK);
}





//  **********************************************************************

PFILE_INFO
New_GetNextFile (
    void
    )
/*++

Routine Description:

    Gets next file which meets the restore criteria.

Arguments:

    None

Return Value:

    Pointer to File info

--*/
{

    PFILE_INFO  FileInfo;   //  Pointer to FileInfo structure


    if (DiskNew) {
        //
        //  New disk. Read first Dir record
        //
        SetFilePointer(ControlHandle, sizeof(NEW_DISKINFO), 0, FILE_BEGIN);

        if ( !GetDirRecord()) {
            return NULL;
        }

        DiskNew = FALSE;
    }

    //
    //  Assume that we will find a suitable file. Allocate memory
    //  for its information structure. This memory must be freed after
    //  the file has been restored.
    //
    FileInfo = Malloc(sizeof(FILE_INFO));

    if (GotOneFile(FileInfo)) {
        //
        //  Found a file, return its information
        //
        return FileInfo;
    } else {
        //
        //  No luck.
        //
        Free(FileInfo);
        return NULL;
    }
}



//  **********************************************************************

BOOL
GetDirRecord (
    )
/*++

Routine Description:

    Gets directory record

Arguments:

    None

Return Value:

    TRUE if directory record read, FALSE otherwise

--*/
{
    DWORD       NumRead;    //  Number of bytes read

    switch ( DiskFormat ) {

    case FORMAT_DOS:
        ReadFile(ControlHandle, DirRecord, sizeof(DIR_RECORD), &NumRead, NULL);
        if (NumRead != sizeof(DIR_RECORD)) {
            return FALSE;
        }
        NumEntries = DirRecord->NumEntries;
        break;

    case FORMAT_OS211:
        ReadFile(ControlHandle, DirRecordOs211, sizeof(DIR_RECORD_OS211), &NumRead, NULL);
        if (NumRead != sizeof(DIR_RECORD_OS211)) {
            return FALSE;
        }
        NumEntries = DirRecordOs211->NumEntries;
        break;

    case FORMAT_OS212:
        ReadFile( ControlHandle,
                  DirRecordOs212,
                  sizeof(DIR_RECORD_OS212) - MAX_PATH,
                  &NumRead,
                  NULL );
        if ( NumRead != sizeof(DIR_RECORD_OS212) - MAX_PATH ) {
            return FALSE;
        }
        ReadFile( ControlHandle,
                  (PBYTE)DirRecordOs212 + sizeof(DIR_RECORD_OS212) - MAX_PATH,
                  DirRecordOs212->Length - sizeof(DIR_RECORD_OS212) + MAX_PATH,
                  &NumRead,
                  NULL );
        if ( NumRead != DirRecordOs212->Length - sizeof(DIR_RECORD_OS212) + MAX_PATH ) {
            return FALSE;
        }
        NumEntries = DirRecordOs212->NumEntries;
        break;

    default:
        return FALSE;

    }
    return TRUE;
}



//  **********************************************************************

static
BOOL
GotOneFile (
    PFILE_INFO  FileInfo
    )
/*++

Routine Description:

    Gets next file which meets the restore criteria.

Arguments:

    OUT FileInfo -  Supplies pointer to file information structure to
                    be filled-in.

Return Value:

    TRUE if a suitable file was found
    FALSE otherwise

--*/
{

    DWORD   NumRead;    //  Number of bytes read


    //
    //  Make sure that we hava a FileInfo structure
    //
    if (!FileInfo) {
        return FALSE;
    }


    while ( TRUE ) {

        if (NumEntries--) {
            //
            //  There are more files under this directory
            //
            //  Read file record
            //

            switch ( DiskFormat ) {

            case FORMAT_DOS:
            case FORMAT_OS211:
                ReadFile(ControlHandle, FileRecord, sizeof(FILE_RECORD), &NumRead, NULL);
                if (NumRead != sizeof(FILE_RECORD)) {
                    return FALSE;
                }
                //
                //  Initialize FileInfo structure
                //
                FileInfo->Path[0] = '\\';
                if ( DiskFormat == FORMAT_DOS ) {
                    strcpy(&(FileInfo->Path[1]), DirRecord->Path);
                    if (strlen(DirRecord->Path) != 0) {
                        strcat(FileInfo->Path, "\\");
                    }
                } else {
                    strcpy(&(FileInfo->Path[1]), DirRecordOs211->Path);
                    if (strlen(DirRecordOs211->Path) != 0) {
                        strcat(FileInfo->Path, "\\");
                    }
                }
                memcpy(FileInfo->FileName, FileRecord->FileName, 12);
                FileInfo->FileName[12]  = '\0';
                FileInfo->Sequence          = FileRecord->SequenceNumber;
                FileInfo->Offset            = FileRecord->FileOffset;
                FileInfo->PartSize          = FileRecord->PartSize;
                FileInfo->Attr              = FileRecord->Attr;
                DATE_WORD(FileInfo->Date)   = FileRecord->Date;
                TIME_WORD(FileInfo->Time)   = FileRecord->Time;
                FileInfo->Flag    = FILEINFO_OK;
                if (FileRecord->Flag & LAST_FILECHUNK) {
                    FileInfo->Flag |= FILEINFO_LAST;
                }
                break;


            case FORMAT_OS212:
                ReadFile(ControlHandle,
                         FileRecordOs212,
                         sizeof(FILE_RECORD_OS212) - MAX_PATH,
                         &NumRead,
                         NULL);
                if (NumRead != sizeof(FILE_RECORD_OS212) - MAX_PATH) {
                    return FALSE;
                }
                ReadFile(ControlHandle,
                         (PBYTE)FileRecordOs212 + sizeof(FILE_RECORD_OS212) - MAX_PATH,
                         FileRecordOs212->NameLength+1,
                         &NumRead,
                         NULL);
                if (NumRead != (DWORD)(FileRecordOs212->NameLength+1)) {
                    return FALSE;
                }
                //
                //  Initialize FileInfo structure
                //
                FileInfo->Path[0] = '\\';
                memcpy( &(FileInfo->Path[1]), DirRecordOs212->Path, DirRecordOs212->PathLength );
                FileInfo->Path[ DirRecordOs212->PathLength+1] = '\0';
                if (DirRecordOs212->PathLength != 0) {
                    strcat(FileInfo->Path, "\\");
                }
                memcpy(FileInfo->FileName, FileRecordOs212->FileName, FileRecordOs212->NameLength);
                FileInfo->FileName[FileRecordOs212->NameLength]  = '\0';
                FileInfo->Sequence          = FileRecordOs212->SequenceNumber;
                FileInfo->Offset            = FileRecordOs212->FileOffset;
                FileInfo->PartSize          = FileRecordOs212->PartSize;
                FileInfo->Attr              = FileRecordOs212->Attr;
                DATE_WORD(FileInfo->Date)   = FileRecordOs212->Date;
                TIME_WORD(FileInfo->Time)   = FileRecordOs212->Time;
                FileInfo->Flag    = FILEINFO_OK;
                if (FileRecordOs212->Flag & LAST_FILECHUNK) {
                    FileInfo->Flag |= FILEINFO_LAST;
                }
                break;

            default:
                return FALSE;

            }

            MakeFullPath(FileInfo->TargetPath, DestinationDrive, FileInfo->Path, FileInfo->FileName);

            //
            //  If this FileInfo matches restore criteria, we got our guy.
            //
            if (FileMatch(FileInfo)) {
                return TRUE;
            }

        } else {
            //
            //  No more files under current directory
            //
            if (NextDirRecord == LAST_DIR_RECORD) {
                //
                //  No more entries in this disk
                //
                CloseHandle(ControlHandle);
                return FALSE;
            } else {
                //
                //  Get next dir record
                //
                SetFilePointer(ControlHandle, NextDirRecord, 0, FILE_BEGIN);

                if ( !GetDirRecord() ) {
                    return FALSE;
                }
            }
        }
    }

    return FALSE;
}





//  **********************************************************************

BOOL
New_RestoreFile (
    PFILE_INFO  FileInfo
    )
/*++

Routine Description:

    Restores one file.

Arguments:

    IN  FileInfo        -   Supplies pointer to information structure

Return Value:

    TRUE if file restored, FALSE otherwise

--*/
{
    DWORD       CreationDisposition;    //  How to open the target file
    HANDLE      HandleSrc;              //  Handle to BACKUP.XXX
    HANDLE      HandleDst;              //  Handle to target file
    FILETIME    UtcFileTime;            //  Time to set (UTC)
    FILETIME    LocalFileTime;          //  Time to set (Local)
    BOOL        RestoreStatus = TRUE;      //  Status of restore;


    //
    //  Open (or create) the target file
    //
    if (FileInfo->Sequence == 1 ) {
        //
        //  First chunk of the file. Create a new target file (overwritting
        //  the existing one.
        //
        CreationDisposition = CREATE_ALWAYS;
    } else {
        //
        //  Not the first chunk of the file. Apend to the existing file
        //
        CreationDisposition = OPEN_EXISTING;
    }

    HandleDst = CreateFile( FileInfo->TargetPath,
                            GENERIC_WRITE,
                            0,
                            NULL,
                            CreationDisposition,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL );

    SetFilePointer( HandleDst,
                    0,
                    0,
                    FILE_END );



    //
    //  Open the BACKUP.XXX file
    //
    HandleSrc = CreateFile( UseSubdir ? SubdirBackupFile : BackupFile,
                            GENERIC_READ,
                            FILE_SHARE_READ,
                            NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL );

    SetFilePointer( HandleSrc,
                    FileInfo->Offset,
                    0,
                    FILE_BEGIN );


    //
    //  Everything set. Now copy the file
    //
    if (!(RestoreStatus = CopyData(HandleSrc, HandleDst, FileInfo->PartSize))) {
        CloseHandle(HandleSrc);
        CloseHandle(HandleDst);
        return RestoreStatus;
    }

    //
    //  If this is the last chunk of the file, the we set the
    //  attributes, date, etc.  Note that the date and time stored
    //  in the FILE_INFO structure is local time, so it has to
    //  be converted to UTC before we can feed it to SetFileTime.
    //
    if (FileInfo->Flag & FILEINFO_LAST) {

        BOOL    StatusOk;

        SetFileAttributes(FileInfo->TargetPath, FileInfo->Attr);

        StatusOk = DosDateTimeToFileTime( DATE_WORD(FileInfo->Date),
                                          TIME_WORD(FileInfo->Time),
                                          &LocalFileTime );

        LocalFileTimeToFileTime( &LocalFileTime, &UtcFileTime );

        StatusOk |= SetFileTime( HandleDst,
                                 &UtcFileTime,
                                 &UtcFileTime,
                                 &UtcFileTime );

        if (!StatusOk) {
            return FALSE;
        } else {
            //        FileInfo->TargetPath,
	        //	      (DWORD)FileInfo->Date.Date.Day,	(DWORD)FileInfo->Date.Date.Month,   (DWORD)FileInfo->Date.Date.Year,
	        //	      (DWORD)FileInfo->Time.Time.Hours, (DWORD)FileInfo->Time.Time.Minutes, (DWORD)FileInfo->Time.Time.DoubleSeconds*2));
        }
    }

    CloseHandle(HandleSrc);
    CloseHandle(HandleDst);

    return RestoreStatus;
}