summaryrefslogtreecommitdiffstats
path: root/private/utils/ntbackup/src/addfse.c
blob: 5535438ab1f13dcbb14241420c128ca48b787098 (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
/**
Copyright(c) Maynard Electronics, Inc. 1984-89


     Name:         addfse.c

     Description:  This file contains code to add and remove FSEs from
          a BSD

        $Log:   N:\LOGFILES\ADDFSE.C_V  $

   Rev 1.22   24 Mar 1994 13:21:24   MIKEP
change 1 to sizeof(CHAR) 

   Rev 1.21   10 Mar 1994 17:37:08   MARINA
FSE_UpdateMark/unicode: change 1 to sizeof(CHAR)

   Rev 1.20   17 Jan 1994 17:17:44   BARRY
Got rid of TEXT macros in msasserts

   Rev 1.19   16 Dec 1993 10:20:10   BARRY
Change INT8_PTRs to VOID_PTRs

   Rev 1.18   04 Aug 1993 15:26:16   JOHNES
803EPR0592 - When in FSE_CompareForSub, if one FSE has complex data and the
other doesn't, if the complex data is empty, pretend it doesn't exist.

This will fix the particular way this problem showed up. There are still
ways it could happen. A better fix would require more complexity in
FSE_CompareForSub and how it deals with include/exclude complex info. This
is more than I can do right now.



   Rev 1.17   27 Jul 1993 14:42:16   MARILYN
changed || to && in FSE_CheckForSub

   Rev 1.16   09 Jun 1993 19:21:28   MIKEP
enable c++ build

   Rev 1.15   09 Oct 1992 11:44:02   DAVEV
Unicode (CHAR_PTR) pointer cast validation

   Rev 1.14   10 Jun 1992 15:49:18   TIMN
changed header name

   Rev 1.13   26 May 1992 11:19:24   TIMN
Cleaned up memory fx calls

   Rev 1.12   21 May 1992 17:03:26   TIMN
Changed maxByteLen to memoryCMP

   Rev 1.11   19 May 1992 15:18:56   TIMN
Changed asterisks to define's

   Rev 1.10   18 May 1992 16:03:00   TIMN
Changed str functions to mem

   Rev 1.9   14 May 1992 12:13:06   TIMN
Changed CHARs to INT8, strcpy to memcpy

   Rev 1.8   13 May 1992 11:41:12   TIMN
Added TEXT() macro to literals, but not msassert literals

   Rev 1.7   28 Feb 1992 09:22:40   STEVEN
partial excludes being full excludes

   Rev 1.6   14 Jan 1992 10:23:48   STEVEN
fix warnings for WIN32

   Rev 1.5   02 Oct 1991 15:32:06   STEVEN
selection type ENTIRE DIR when should be PARTIAL

   Rev 1.4   30 Sep 1991 09:36:38   STEVEN
Needed to update status when removing an FSE

   Rev 1.3   23 Aug 1991 16:58:56   STEVEN
now checks to see if in queue before it removes it

   Rev 1.2   12 Jun 1991 16:02:14   STEVEN
BSDU code review

   Rev 1.1   29 May 1991 17:21:34   STEVEN
Re-Design of BSDU for New Fast File Restore

   Rev 1.0   09 May 1991 13:41:12   HUNTER
Initial revision.

**/
#include <malloc.h>
#include <stdlib.h>
#include <string.h>

#include "stdtypes.h"
#include "msassert.h"

#include "std_err.h"
#include "queues.h"
#include "stdwcs.h"

#include "bsdu.h"

#define DateAfter( date1, date2 )   (-DateBefore( (date1), (date2) ))

static VOID FSE_UpdateMark( FSE_PTR fse ) ;
static BOOLEAN FSE_CompareForSub( FSE_PTR fse1, FSE_PTR fse2 );
static BOOLEAN DateBefore( DATE_TIME_PTR date1, DATE_TIME_PTR date2 ) ;
static BOOLEAN DateEqual( DATE_TIME_PTR date1, DATE_TIME_PTR date2 ) ;
static BOOLEAN FSE_ComplexDataEmpty( FSE_COMPLEX_PTR cplx );
/**/
/**

     Name:         BSD_RemoveFSE()

     Description:  This function removes an FSE from the specified
          BSD.

     Modified:     5/17/1991   14:43:52

     Returns:      None

     Notes:

     See also:     $/SEE( BSD_AddFSE() )$

     Declaration:

**/
/* begin declaration */
VOID BSD_RemoveFSE(
FSE_PTR fse )      /* I - specifies which FSE to remove */
{
     BSD_PTR bsd ;
     INT16   ret_val = SUCCESS ;

     msassert( fse != NULL );

     bsd = (BSD_PTR)GetQueueElemPtr( &(fse->q) ) ;

     if ( bsd != NULL ) {

          ret_val = RemoveQueueElem( &(bsd->fse_q_hdr), &(fse->q) ) ;
     }

     if ( ret_val == SUCCESS ) {

          if( fse->tgt != NULL ) {
               bsd->tgt_fse_exist-- ;
               free( fse->tgt ) ;
          }

          if( fse->cplx != NULL ) {
               free( fse->cplx->pre_m_date ) ;
               free( fse->cplx->post_m_date ) ;
               free( fse->cplx->access_date ) ;
               free( fse->cplx->backup_date ) ;
               free( fse->cplx ) ;
          }

          free( fse ) ;

     } else {
          msassert( ("FSE NOT found in BSD", 0) );
     }

     if ( bsd != NULL ) {
          fse = BSD_GetFirstFSE( bsd ) ;
          if ( fse == NULL ) {
               bsd->select_status = NONE_SELECTED ;
          }
     }
}
/**/
/**

     Name:         BSD_CreateFSE()

     Description:  This function allocates memory for an FSE.  It then
          initializes the allocated memory with the data provided.  This
          function only initializes the simple selection data.  To set
          the complex selection data( dates & attributes) other FSE
          function calls must be made.

     Modified:     8/9/1989

     Returns:      Error codes:
          OUT_OF_MEMORY
          SUCCESS

     Notes:

     See also:     $/SEE( BSD_AddFSE(), FSE_SetComplexInfo() )$

     Declaration:

**/
/* begin declaration */
INT16 BSD_CreatFSE(
FSE_PTR  *fse,   /* O - The FSE which is craeted                        */
INT16    oper,   /* I - The operation type .i.e INCLUDE/EXCLUDE         */
VOID_PTR dname,  /* I - The directory name (NULL impregnated)           */
INT16    dsize,  /* I - The size of the directory name                  */
VOID_PTR fname,  /* I - The file name                                   */
INT16    fnsize, /* I - size of file name                               */
BOOLEAN  wilds,  /* I - TRUE if the '?' and '*' are wildcards           */
BOOLEAN  subs )  /* I - TRUE if selection should include subdirectories */
{
     INT16    ret_val ;

     msassert( fse != NULL ) ;
     msassert( dsize > 0 ) ;
     msassert( fnsize > 0 ) ;

     *fse = (FSE_PTR)calloc( 1, sizeof( **fse ) + dsize + fnsize ) ;

     if ( *fse != NULL ) {

          (*fse)->flgs.inc_exc = oper ;

          FSE_SetIncSubFlag( *fse, subs ) ;
          FSE_SetWildFlag( *fse, wilds ) ;

          (*fse)->dir            = (*fse + 1);
          (*fse)->dir_leng       = dsize ;
          (*fse)->fname          = (BYTE_PTR)(*fse)->dir + dsize ;
          (*fse)->fname_leng     = fnsize ;
          (*fse)->flgs.del_files = NON_DELETED_FILES_ONLY ;
          memcpy( (*fse)->dir, dname, dsize ) ;
          memcpy( (*fse)->fname, fname, fnsize ) ;

          ret_val = SUCCESS ;

     } else {

          ret_val = OUT_OF_MEMORY ;
     }

     return ret_val ;
}
/**/
/**

     Name:         BSD_AddFSE()

     Description:  This function adds an fse to the queue of FSE. Before
     the element is added to the QUEUE, a check is made to see if
     this fse is a subset or superset of an element already in the
     FSL.  If it is, then the queue is optimized by removing the
     the appropriate element.


     Modified:     8/9/1989

     Returns:      None

     Notes:

     See also:     $/SEE( BSD_RemoveFSE(), BSD_CreatFSE() )$

     Declaration:

**/
/* begin declaration */
VOID BSD_AddFSE(
BSD_PTR  bsd,   /* I - What BSD to add the FSE to */
FSE_PTR  fse )  /* I - The FSE to add             */
{
     FSE_PTR node ;
     FSE_PTR node2;
     BOOLEAN include_found = FALSE ;

     msassert( fse != NULL );
     msassert( bsd != NULL );

     /* make sure the FSE is not already enqueued */
     msassert( GetQueueElemPtr( &(fse->q) ) == NULL ) ;

     node = (FSE_PTR)QueueHead( &(bsd->fse_q_hdr) ) ;

     while ( node != NULL ) {

          if ( FSE_CompareForSub( fse, node ) ) {
               /* the new FSE supersedes the older FSE */

               node2   = (FSE_PTR)QueueNext( &(node->q) ) ;

               BSD_RemoveFSE( node ) ;
               node    = node2 ;

          } else {

               if ( FSE_GetOperType( node ) == INCLUDE ) {
                    include_found = TRUE ;
               }
               node = (FSE_PTR)QueueNext( &(node->q) ) ;

          }
     }

     if ( !include_found ) {
          BSD_ClearAllFSE( bsd ) ;
     }

     if ( FSE_HasTargetInfo( fse ) ) {
          bsd->tgt_fse_exist ++ ;
     }

     SetQueueElemPtr( &(fse->q), bsd ) ;

     EnQueueElem( &(bsd->fse_q_hdr),  &(fse->q), FALSE ) ;

     FSE_UpdateMark( fse ) ;

}
/**/
/**

     Name:         FSE_UpdateMark()

     Description:  This function updates the mark status of the fse and the
          bsd which contains the fse.


     Modified:     8/10/1989

     Returns:      none

     Notes:        This function is only called by BSD_AddFSE()

     See also:     $/SEE( BSD_AddFSE() )$

     Declaration:

**/
/* begin declaration */
static VOID FSE_UpdateMark(
FSE_PTR fse )       /*I - Fse to update the mark status in */
{
     BSD_PTR bsd ;
     FSE_COMPLEX_PTR cplx ;
     FSE_PTR temp_fse ;

     bsd = (BSD_PTR)GetQueueElemPtr( &(fse->q) ) ;
     cplx = fse->cplx ;

     if ( FSE_GetOperType( fse ) == INCLUDE ) {

          fse->flgs.select_type = PARTIAL_SELECTION ;
          bsd->select_status    = SOME_SELECTED ;

          if( FSE_GetIncSubFlag( fse )  && ( fse->dir_leng == sizeof( CHAR ) ) ) {
               bsd->select_status = ALL_SELECTED ;
          }

          if ( ( !memorycmp( fse->fname, fse->fname_leng, ALL_FILES, ALL_FILES_LENG ) ||
                 !memorycmp( fse->fname, fse->fname_leng, ALL_FILES2, ALL_FILES2_LENG ) ) &&
               FSE_GetWildFlag( fse ) &&
               ( cplx == NULL ) &&
               FSE_GetIncSubFlag( fse ) &&
               (FSE_GetDeletedVersionFlg( fse ) != DELETED_FILES_ONLY) ) {

               fse->flgs.select_type = ENTIRE_DIR_SELECTION ;

          } else {
               fse->flgs.select_type = PARTIAL_SELECTION ;
               bsd->select_status    = SOME_SELECTED ;
          }

          if ( !FSE_GetIncSubFlag( fse ) ) {

               if ( !FSE_GetWildFlag( fse ) ||
                    (!strchr(fse->fname, TEXT('?')) && !strchr(fse->fname, TEXT('*')) ) ) {

                    fse->flgs.select_type = SINGLE_FILE_SELECTION ;
               }
          }

     } else {      /* fse is an exclude */

          fse->flgs.select_type = PARTIAL_SELECTION ;

          if ( !FSE_GetIncSubFlag( fse ) ) {

               if ( !FSE_GetWildFlag( fse ) ||
                    (!strchr(fse->fname, TEXT('?')) && !strchr(fse->fname, TEXT('*')) ) ) {

                    fse->flgs.select_type = SINGLE_FILE_SELECTION ;
               }
          }

          if ( FSE_GetIncSubFlag( fse ) && ( fse->dir_leng == sizeof(CHAR) ) &&
            ( !memorycmp( fse->fname, fse->fname_leng, ALL_FILES, ALL_FILES_LENG ) ||
            !memorycmp( fse->fname, fse->fname_leng, ALL_FILES2, ALL_FILES2_LENG ) ) &&
            FSE_GetWildFlag( fse ) && (fse->cplx == NULL) ) {

               bsd->select_status = NONE_SELECTED ;

          } else if ( bsd->select_status == ALL_SELECTED ) {

               bsd->select_status = SOME_SELECTED ;

          }

          temp_fse = BSD_GetFirstFSE( bsd ) ;

          while ( temp_fse != NULL ) {

               if ( temp_fse->flgs.select_type == ENTIRE_DIR_SELECTION ) {
                    if ( (fse->dir_leng >= temp_fse->dir_leng) &&
                         !memicmp( fse->dir, temp_fse->dir, temp_fse->dir_leng ) ) {

                         temp_fse->flgs.select_type = PARTIAL_SELECTION ;

                    } else if ( (fse->dir_leng < temp_fse->dir_leng) &&
                         FSE_GetIncSubFlag( fse ) &&
                         !memicmp( fse->dir, temp_fse->dir, fse->dir_leng ) ) {

                         temp_fse->flgs.select_type = PARTIAL_SELECTION ;
                    }
               }

               temp_fse = BSD_GetNextFSE( fse ) ;
          }
     }
}
/**/
/**

     Name:         FSE_CompareForSub()

     Description:  This function compares two FSEs to see if one is a
          subset of the other.  The possibilities are fse1 < fse2,
          fse1 > fse2, fse1 = fse2, no comparison.


     Modified:     8/10/1989

     Returns:      TRUE if FSE2 <= FSE1

     Notes:        This function is only called by BSD_AddFSE() for
          optimization purposes.

     See also:     $/SEE( BSD_AddFSE() )$

     Declaration:

**/
/* begin declaration */
static BOOLEAN FSE_CompareForSub(
FSE_PTR fse1,    /* I - File Selection Element to compare */
FSE_PTR fse2 )   /* I - File Selection Element to compare */
{
     INT16 ret_val = FALSE ;
     FSE_COMPLEX_PTR cplx1;
     FSE_COMPLEX_PTR cplx2;
     FSE_TGT_INFO_PTR tgt1 ;
     FSE_TGT_INFO_PTR tgt2 ;

     cplx1 = fse1->cplx ;
     cplx2 = fse2->cplx ;
     tgt1  = fse1->tgt ;
     tgt2  = fse2->tgt ;

     if( fse1->dir_leng <= fse2->dir_leng ) {
          /* fse2 could be a subset of fse1 */
          ret_val = TRUE ;

          if( memicmp( fse1->dir, fse2->dir, fse1->dir_leng ) ) {
               ret_val = FALSE ;

          } else if( !FSE_GetIncSubFlag( fse1 ) && ( fse1->dir_leng != fse2->dir_leng ) ) {
               ret_val = FALSE ;

          } else if( FSE_GetIncSubFlag( fse2 ) && !FSE_GetIncSubFlag( fse1 ) ) {
               ret_val = FALSE ;

          /* If we made it this far then The directory matches */

          } else if( memorycmp( fse1->fname, fse1->fname_leng, fse2->fname, fse2->fname_leng ) &&
            ( memorycmp( fse1->fname, fse1->fname_leng, ALL_FILES, ALL_FILES_LENG ) &&
              memorycmp( fse1->fname, fse1->fname_leng, ALL_FILES2, ALL_FILES2_LENG ) ) ||
            !FSE_GetWildFlag(fse1) ) {
               ret_val = FALSE ;

          } else if ( (cplx2 != NULL) || (cplx1 != NULL) ) {
               if ( cplx2 == NULL ) {
                         /* if the complex data struture isn't empty then  */
                         /* these two FSE's can't be equal.                */
                     if ( FSE_ComplexDataEmpty( cplx1 ) == FALSE ) {
                         ret_val = FALSE ;

                     }
               } else if ( cplx1 == NULL ) {
                         /* if the complex data struture isn't empty then  */
                         /* these two FSE's can't be equal.                */
                     if ( FSE_ComplexDataEmpty( cplx2 ) == FALSE ) {
                         ret_val = FALSE ;

                     }
               } else {

                    if( DateAfter( cplx2->pre_m_date, cplx1->pre_m_date ) ) {
                         ret_val = FALSE ;

                    } else if( DateBefore( cplx2->post_m_date, cplx1->post_m_date ) ) {
                         ret_val = FALSE ;

                    } else if( DateBefore( cplx2->access_date, cplx1->access_date ) ) {
                         ret_val = FALSE ;

                    } else if( !DateEqual( cplx2->backup_date, cplx1->backup_date ) ) {
                         ret_val = FALSE ;

                         /* more bits means less files */
                    } else if( ( (cplx1->attr_on_mask | cplx2->attr_on_mask) != cplx2->attr_on_mask ) ||
                    ( (cplx1->attr_off_mask & cplx2->attr_off_mask) != cplx2->attr_off_mask ) ) {
                         ret_val = FALSE ;
                    }
               }
          }
     }
     return( ret_val ) ;
}


/**/
/**

     Name:         FSE_ComplexDataEmpty()

     Description:  This function checks to see if all parts of an FSE's
                   complex data structure are EMPTY. By EMPTY, I mean not
                   effecting the files selections.

                   This comes in handy when comparing a simple FSE and
                   a complex FSE. If there is no data in the complex field
                   of the one, then you might as well ignore it.


     Modified:     8/4/1993 (JES)

     Returns:      TRUE  - the structure is empty.
                   FALSE - the structure isn't empty.


     Notes:        This function is only called by BSD_AddFSE() for
                   optimization purposes.

     See also:

     Declaration:

**/
static BOOLEAN FSE_ComplexDataEmpty( FSE_COMPLEX_PTR cplx )
{

     BOOLEAN   ret_val = TRUE ;

     msassert( cplx != NULL ) ;

     if ( cplx->pre_m_date != NULL ) {
          ret_val = FALSE ;
     }

     if ( cplx->post_m_date != NULL ) {
          ret_val = FALSE ;
     }

     if ( cplx->access_date != NULL ) {
          ret_val = FALSE ;
     }

     if ( cplx->backup_date != NULL ) {
          ret_val = FALSE ;
     }

     if ( cplx->attr_on_mask != 0 ) {
          ret_val = FALSE ;
     }

     if ( cplx->attr_off_mask != 0 ) {
          ret_val = FALSE ;
     }

     return ret_val ;

} /* FSE_ComplexDataEmpty */



static BOOLEAN DateBefore(
DATE_TIME_PTR date1,
DATE_TIME_PTR date2 )
{
     INT16 ret_val ;

     if ( ( date1 == NULL ) || ( !date1->date_valid ) ) {
          ret_val = FALSE ;

     } else if ( ( date2 == NULL ) || ( !date2->date_valid ) ) {
          ret_val = TRUE ;

     } else {
          ret_val = (INT16)( CompDate( date1, date2 ) < 0 ) ;
     }

     return ( ret_val ) ;
}

static BOOLEAN DateEqual(
DATE_TIME_PTR date1,
DATE_TIME_PTR date2 )
{
     INT16 ret_val ;

     if ( ( date1 == NULL ) || ( !date1->date_valid ) ) {

          if ( ( date2 == NULL ) || ( !date2->date_valid ) ) {
               ret_val = TRUE ;
          } else {
               ret_val = FALSE ;
          }

     } else if ( ( date2 == NULL ) || ( !date2->date_valid ) ) {
          ret_val = FALSE ;

     } else {
          ret_val = (INT16)( !memcmp( date1, date2, sizeof( DATE_TIME ) ) ) ;
     }

     return ( ret_val ) ;

}