~ubuntu-branches/ubuntu/precise/libisoburn/precise

« back to all changes in this revision

Viewing changes to xorriso/findjob.c

  • Committer: Bazaar Package Importer
  • Author(s): George Danchev
  • Date: 2011-05-26 16:21:32 UTC
  • mfrom: (9.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110526162132-9lzoagfaggnera53
Tags: 1.0.8.pl00-4
* [MA] Improve package descriptions and README.Debian.
* [MA] Migrate to format "3.0 (quilt)" and compatibility 8.
  + debian/control: Drop build-dep on 'cdbs'. Require debhelper (>= 8).
  + debian/rules: Reformulate using 'dh'.
  + debian/libisoburn{1,-dev,-doc}.docs: New files.
  + debian/xorriso.docs: Addition of upstream documents.
* [GD] Make sure doxygen documentaton (doc package) is not built
       when dpkg-buildpackage -B is called (i.e. autobuilders).
* [GD] Move doxygen, graphviz to Build-Depends-Indep.
* [GD] Add missing copyrights for debian packaging.
* [GD] Standards-Version: 3.9.2 (no changes needed).
* [GD] More package description and README.Debian improvements;
       thanks to Tony Mancill <tmancill@debian.org>.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* xorriso - creates, loads, manipulates and burns ISO 9660 filesystem images.
 
3
 
 
4
   Copyright 2007-2010 Thomas Schmitt, <scdbackup@gmx.net>
 
5
 
 
6
   Provided under GPL version 2 or later.
 
7
 
 
8
   This file contains the implementation of classes FindjoB, ExprnodE,
 
9
   ExprtesT which perform tree searches in libisofs or in POSIX filesystem
 
10
*/
 
11
 
 
12
#ifdef HAVE_CONFIG_H
 
13
#include "../config.h"
 
14
#endif
 
15
 
 
16
#include <ctype.h>
 
17
#include <sys/types.h>
 
18
#include <unistd.h>
 
19
#include <stdlib.h>
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <sys/stat.h>
 
23
#include <sys/time.h>
 
24
#include <time.h>
 
25
#include <dirent.h>
 
26
#include <errno.h>
 
27
 
 
28
 
 
29
#include "xorriso.h"
 
30
#include "xorriso_private.h"
 
31
#include "xorrisoburn.h"
 
32
 
 
33
 
 
34
/* ----------------------- Exprtest ----------------------- */
 
35
 
 
36
 
 
37
int Exprtest_new( struct ExprtesT **ftest, struct FindjoB *boss, int flag)
 
38
{
 
39
 struct ExprtesT *f;
 
40
 
 
41
 *ftest= f= TSOB_FELD(struct ExprtesT,1);
 
42
 if(f==NULL)
 
43
   return(-1);
 
44
 f->boss= boss;
 
45
 f->invert= 0;
 
46
 f->test_type= -1;
 
47
 f->arg1= NULL;
 
48
 f->arg2= NULL;
 
49
 return(1);
 
50
}
 
51
 
 
52
 
 
53
int Exprtest_destroy(struct ExprtesT **ftest, int flag)
 
54
{
 
55
 struct ExprtesT *f;
 
56
 
 
57
 f= *ftest;
 
58
 if(f==NULL)
 
59
   return(0);
 
60
 
 
61
 if(f->test_type == 1) {
 
62
   if(f->arg1 != NULL)
 
63
     free(f->arg1);
 
64
   if(f->arg2 != NULL) {
 
65
     regfree(f->arg2);
 
66
     free(f->arg2);
 
67
   }
 
68
 } else if(f->test_type == 9) {
 
69
   /* arg1 is not an allocated value */;
 
70
 } else {
 
71
   if(f->arg1 != NULL)
 
72
     free(f->arg1);
 
73
   if(f->arg2 != NULL)
 
74
     free(f->arg2);
 
75
 }
 
76
 free((char *) f);
 
77
 *ftest= NULL;
 
78
 return(1);
 
79
}
 
80
 
 
81
 
 
82
/* ----------------------- Nttpfnode ----------------------- */
 
83
 
 
84
 
 
85
int Exprnode_new(struct ExprnodE **fnode, struct FindjoB *job,
 
86
                 struct ExprnodE *up, char *origin, int flag)
 
87
/*
 
88
 bit0= set invert-property
 
89
 bit1= set use_shortcuts
 
90
*/
 
91
{
 
92
 struct ExprnodE *n;
 
93
 int ret,i;
 
94
 
 
95
 *fnode= n= TSOB_FELD(struct ExprnodE,1);
 
96
 if(n == NULL)
 
97
   return(-1);
 
98
 for(i= 0; i < sizeof(n->origin); i++)
 
99
   n->origin[i]= 0;
 
100
 strncpy(n->origin, origin, sizeof(n->origin) - 1);
 
101
 n->up= up;
 
102
 n->invert= (flag & 1);
 
103
 n->assoc= 0;
 
104
 n->use_shortcuts= !!(flag & 2);
 
105
 n->left= NULL;
 
106
 n->left_op= -1;
 
107
 n->right= NULL;
 
108
 n->right_op= -1;
 
109
 n->sub= NULL;
 
110
 n->is_if_then_else= 0;
 
111
 n->true_branch= NULL;
 
112
 n->false_branch= NULL;
 
113
 n->test= NULL;
 
114
 n->own_value= -1;
 
115
 n->composed_value= -1;
 
116
 
 
117
 ret= Exprtest_new(&(n->test), job, 0);
 
118
 if(ret<=0){
 
119
   Exprnode_destroy(fnode, 0);
 
120
   return(-1);
 
121
 }
 
122
 return(1);
 
123
}
 
124
 
 
125
 
 
126
int Exprnode_destroy(struct ExprnodE **fnode, int flag)
 
127
{
 
128
 if(*fnode == NULL)
 
129
   return(0);
 
130
 Exprnode_destroy(&((*fnode)->right),0);
 
131
 Exprnode_destroy(&((*fnode)->sub),0);
 
132
 Exprnode_destroy(&((*fnode)->true_branch),0);
 
133
 Exprnode_destroy(&((*fnode)->false_branch),0);
 
134
 Exprtest_destroy(&((*fnode)->test),0);
 
135
 free((char *) *fnode);
 
136
 *fnode= NULL;
 
137
 return(1);
 
138
}
 
139
 
 
140
 
 
141
int Exprnode_set_is_if(struct ExprnodE *fnode, int value, int flag)
 
142
{
 
143
 fnode->is_if_then_else= value;
 
144
 return(1);
 
145
}
 
146
 
 
147
 
 
148
int Exprnode_is_if(struct ExprnodE *fnode, int flag)
 
149
{
 
150
  return(fnode->is_if_then_else);
 
151
}
 
152
 
 
153
 
 
154
int Exprnode_set_branch(struct ExprnodE *fnode, struct ExprnodE *target,
 
155
                        int flag)
 
156
/*
 
157
 bit0= false_branch (else true_branch)
 
158
*/
 
159
{
 
160
 struct ExprnodE **branch;
 
161
 
 
162
 if(flag&1) 
 
163
   branch= &(fnode->false_branch);
 
164
 else
 
165
   branch= &(fnode->true_branch);
 
166
 Exprnode_destroy(branch,0);
 
167
 (*branch)= target;
 
168
 return(1);
 
169
}
 
170
 
 
171
 
 
172
int Exprnode_get_branch(struct ExprnodE *fnode, struct ExprnodE **branch,
 
173
                        int flag)
 
174
/*
 
175
 bit0= false_branch (else true_branch)
 
176
*/
 
177
{
 
178
 if(flag&1) 
 
179
   (*branch)= fnode->false_branch;
 
180
 else
 
181
   (*branch)= fnode->true_branch;
 
182
 return(1);
 
183
}
 
184
 
 
185
 
 
186
int Exprnode_is_defined(struct ExprnodE *fnode, int flag)
 
187
{
 
188
 struct ExprtesT *ftest;
 
189
 
 
190
 if(fnode==NULL)
 
191
   return(0);
 
192
 if(fnode->sub!=NULL)
 
193
   return(1);
 
194
 ftest= fnode->test;
 
195
 if(ftest==NULL)
 
196
   return(0);
 
197
 if(ftest->test_type>=0)
 
198
   return(1);
 
199
 return(0);
 
200
}
 
201
 
 
202
 
 
203
int Exprnode_own_value(struct XorrisO *xorriso, struct ExprnodE *fnode, 
 
204
                       void *node, char *name, char *path,
 
205
                       struct stat *boss_stbuf, struct stat *stbuf, int flag)
 
206
/*
 
207
flag:
 
208
return: (also from Exprtest_match() and Exprnode_tree_value() )
 
209
 <0 = error
 
210
  0 = does not match
 
211
  1 = does match
 
212
  2 = immediate decision : does not match
 
213
  3 = immediate decision : does match
 
214
*/
 
215
{
 
216
 int ret;
 
217
 
 
218
 if(fnode==NULL)
 
219
   return(1);
 
220
 if(fnode->sub!=NULL) {
 
221
   ret= Exprnode_tree_value(xorriso, fnode->sub, -1,
 
222
                            node, name, path, boss_stbuf, stbuf, 0);
 
223
 } else {
 
224
   ret= Exprtest_match(xorriso, fnode->test, node, name, path,
 
225
                       boss_stbuf, stbuf, 0);
 
226
 }
 
227
 if(ret<0)
 
228
   return(ret);
 
229
 if(ret>1)
 
230
   return(ret);
 
231
 if(fnode->invert)
 
232
   ret= !ret;
 
233
 return(ret);
 
234
}
 
235
 
 
236
 
 
237
int Exprnode_op(int value1, int value2, int op, int flag)
 
238
{
 
239
 int ret;
 
240
 
 
241
 if(op==0)
 
242
   ret= value1 || value2 ;
 
243
 else
 
244
   ret= value1 && value2 ;
 
245
 return(ret);
 
246
}
 
247
 
 
248
 
 
249
int Exprnode_tree_value(struct XorrisO *xorriso, struct ExprnodE *fnode,
 
250
                        int left_value, void *node, char *name, char *path,
 
251
                        struct stat *boss_stbuf, struct stat *stbuf, int flag)
 
252
/*
 
253
 bit0-7= testmode: 0=head , 1=filename
 
254
return: (also from Nntpftest_match() and Nntpfnode_own_value() )
 
255
 <0 = error
 
256
  0 = does not match
 
257
  1 = does match
 
258
  2 = immediate decision : does not match
 
259
  3 = immediate decision : does match
 
260
*/
 
261
{
 
262
 int value= 1,ret;
 
263
 
 
264
 if(fnode==NULL)
 
265
   return(1);
 
266
 if(!Exprnode_is_defined(fnode,0))
 
267
   return(1);
 
268
 
 
269
 if(fnode->use_shortcuts && fnode->left!=NULL){
 
270
   fnode->composed_value= left_value;
 
271
   if(fnode->left_op==0) {/* OR */
 
272
     if(left_value!=0) 
 
273
       goto ex;
 
274
   } else {                /* AND */
 
275
     if(left_value==0)
 
276
       goto ex;
 
277
   }
 
278
 }
 
279
 fnode->composed_value= fnode->own_value= 
 
280
    Exprnode_own_value(xorriso, fnode, node, name, path, boss_stbuf, stbuf, 0);
 
281
 if(fnode->own_value < 0 || fnode->own_value > 1)
 
282
   return(fnode->own_value);
 
283
 
 
284
 if(fnode->assoc == 0){ /* left associative */
 
285
   if(fnode->left != NULL && left_value >= 0)
 
286
     fnode->composed_value= 
 
287
       Exprnode_op(left_value, fnode->own_value, fnode->left_op, 0);
 
288
   /* compute right value */
 
289
   /* is the right value relevant ? */
 
290
   if(fnode->right!=NULL){
 
291
     if(fnode->use_shortcuts){
 
292
       if(fnode->right_op==0) {/* OR */
 
293
         if(fnode->composed_value!=0)
 
294
           goto ex;
 
295
       } else {                /* AND */
 
296
         if(fnode->composed_value==0)
 
297
           goto ex;
 
298
       }  
 
299
     }
 
300
     value= Exprnode_tree_value(xorriso, fnode->right,fnode->composed_value,
 
301
                               node, name, path, boss_stbuf, stbuf, 0);
 
302
     if(value<0 || value>1)
 
303
       return(value);
 
304
     fnode->composed_value= value;
 
305
   }
 
306
 }else{ /* right associative */
 
307
   if(fnode->right!=NULL){
 
308
     /* is the right value relevant ? */
 
309
     if(fnode->use_shortcuts){
 
310
       if(fnode->right_op==0) {/* OR */
 
311
         if(fnode->composed_value!=0)
 
312
           goto ex;
 
313
       } else {                /* AND */
 
314
         if(fnode->composed_value==0)
 
315
           goto ex;
 
316
       }
 
317
     }
 
318
     value= Exprnode_tree_value(xorriso, fnode->right,fnode->own_value,
 
319
                               node, name, path, boss_stbuf, stbuf, 0);
 
320
     if(value<0||value>1)
 
321
       return(value);
 
322
   } else
 
323
     value= fnode->own_value;
 
324
   fnode->composed_value= value;
 
325
   if(fnode->left!=NULL && left_value>=0)
 
326
     fnode->composed_value=
 
327
       Exprnode_op(left_value,fnode->composed_value,fnode->left_op,0);
 
328
 }
 
329
ex:
 
330
 ret= fnode->composed_value;
 
331
 if(fnode->is_if_then_else) {
 
332
   /* The if-condition is evaluated. Now follow the chosen branch */
 
333
   struct ExprnodE *branch;
 
334
   if(ret>0) 
 
335
     branch= fnode->true_branch;
 
336
   else 
 
337
     branch= fnode->false_branch;
 
338
   if(branch!=NULL) {
 
339
     ret= Exprnode_tree_value(xorriso, branch, -1,
 
340
                              node, name, path, boss_stbuf, stbuf, 0);
 
341
     if(ret<0)
 
342
       return(ret);
 
343
     if(ret>1)
 
344
       return(ret);
 
345
   }
 
346
   fnode->composed_value= ret;
 
347
 }
 
348
 return(fnode->composed_value);
 
349
}
 
350
 
 
351
 
 
352
/* --------------------- Findjob -------------------- */
 
353
 
 
354
 
 
355
int Findjob_new(struct FindjoB **o, char *start_path, int flag)
 
356
{
 
357
 struct FindjoB *m;
 
358
 int ret;
 
359
 
 
360
 m= *o= TSOB_FELD(struct FindjoB,1);
 
361
 if(m==NULL)
 
362
   return(-1);
 
363
 m->start_path= NULL;
 
364
 m->test_tree= NULL;
 
365
 m->cursor= NULL;
 
366
 m->invert= 0;
 
367
 m->use_shortcuts= 1;
 
368
 m->action= 0; /* print */
 
369
 m->prune= 0;
 
370
 m->target= NULL; /* a mere pointer, not managed memory */
 
371
 m->text_2= NULL; /* a mere pointer, not managed memory */
 
372
 m->user= 0;
 
373
 m->group= 0;
 
374
 m->type= 0;
 
375
 m->date= 0;
 
376
 m->start_path= strdup(start_path);
 
377
 if(m->start_path==NULL)
 
378
   goto failed;
 
379
 m->found_path= NULL;
 
380
 m->estim_upper_size= 0;
 
381
 m->estim_lower_size= 0;
 
382
 m->subjob= NULL;
 
383
 m->errmsg[0]= 0;
 
384
 m->errn= 0;
 
385
 
 
386
 ret= Exprnode_new(&(m->test_tree), m, NULL, "-find", (m->use_shortcuts)<<1);
 
387
 if(ret<=0)
 
388
   goto failed;
 
389
 m->cursor= m->test_tree;
 
390
 return(1);
 
391
 
 
392
failed:;
 
393
 Findjob_destroy(o, 0);
 
394
 return(-1);
 
395
}
 
396
 
 
397
 
 
398
int Findjob_destroy(struct FindjoB **o, int flag)
 
399
{
 
400
 struct FindjoB *m;
 
401
 
 
402
 m= *o;
 
403
 if(m==NULL)
 
404
   return(0);
 
405
 if(m->test_tree != NULL)
 
406
   Exprnode_destroy(&(m->test_tree), 0); 
 
407
 if(m->start_path != NULL)
 
408
   free(m->start_path);
 
409
 free((char *) *o);
 
410
 *o= NULL;
 
411
 return(1);
 
412
}
 
413
 
 
414
 
 
415
int Findjob_set_start_path(struct FindjoB *o, char *start_path, int flag)
 
416
{
 
417
 if(o->start_path!=NULL)
 
418
   free(o->start_path);
 
419
 if(start_path!=NULL) {
 
420
   o->start_path= strdup(start_path);
 
421
   if(o->start_path==NULL)
 
422
     return(-1);
 
423
 } else
 
424
   o->start_path= NULL;
 
425
 return(1);
 
426
}
 
427
 
 
428
 
 
429
int Findjob_get_start_path(struct FindjoB *o, char **start_path, int flag)
 
430
{
 
431
 *start_path= o->start_path;
 
432
 return(1);
 
433
}
 
434
 
 
435
 
 
436
int Findjob_cursor_complete( struct FindjoB *job, int flag)
 
437
{
 
438
 int ret;
 
439
 
 
440
 if(job==NULL)
 
441
   return(0);
 
442
 ret= Exprnode_is_defined(job->cursor,0);
 
443
 return(ret);
 
444
}
 
445
 
 
446
 
 
447
int Findjob_is_restrictive(struct FindjoB *job, int flag)
 
448
{
 
449
 if(job == NULL)
 
450
   return(0);
 
451
 if(job->test_tree == NULL)
 
452
   return(0);
 
453
 if(!Exprnode_is_defined(job->test_tree, 0))
 
454
   return(0);
 
455
 return(1);
 
456
}
 
457
 
 
458
 
 
459
int Findjob_new_node(struct FindjoB *job, struct ExprnodE **fnode,
 
460
                     char *origin, int flag)
 
461
/*
 
462
 bit0= open new branch 
 
463
 bit1= with bit1 : do not register as sub-node of job->cursor
 
464
*/
 
465
{
 
466
 int ret;
 
467
 struct ExprnodE *f;
 
468
 
 
469
 ret= Exprnode_new(fnode,job,NULL,origin,
 
470
                   job->invert|((job->use_shortcuts)<<1));
 
471
 if(ret<=0)
 
472
   return(ret);
 
473
 f= *fnode;
 
474
 if(flag&1) {
 
475
   f->up= job->cursor;
 
476
   if(job->cursor!=NULL && !(flag&2)) {
 
477
     if(job->cursor->sub!=NULL) {
 
478
       /* This would become a memory leak */
 
479
       job->errn= -2;
 
480
       sprintf(job->errmsg,
 
481
               "Program error while parsing -job : sub branch overwrite");
 
482
       return(0);
 
483
     } else
 
484
       job->cursor->sub= f;
 
485
   }
 
486
 } else {
 
487
   f->up= job->cursor->up;
 
488
   f->left= job->cursor;
 
489
   if(job->cursor!=NULL)
 
490
     job->cursor->right= f;
 
491
 }
 
492
 job->invert= 0;
 
493
 return(1);
 
494
}
 
495
 
 
496
 
 
497
/* If an operator is expected : use -and
 
498
   @param flag bit0= prepare for a pseudo-test:
 
499
                     if an operator is expected, do nothing and return 2
 
500
*/
 
501
int Findjob_default_and(struct FindjoB *o, int flag)
 
502
{
 
503
 int ret;
 
504
 
 
505
 if(Findjob_cursor_complete(o, 0)) {
 
506
   if(flag & 1) 
 
507
     return(2);
 
508
   ret= Findjob_and(o, 0);
 
509
   if(ret <= 0)
 
510
     return(ret);
 
511
 }
 
512
 return(1);
 
513
}
 
514
 
 
515
 
 
516
int Findjob_open_bracket(struct FindjoB *job, int flag)
 
517
{
 
518
 int ret;
 
519
 struct ExprnodE *fnode;
 
520
 
 
521
 ret= Findjob_default_and(job, 0);
 
522
 if(ret <= 0)
 
523
   return(ret);
 
524
 ret= Findjob_new_node(job, &fnode, "-sub", 1);
 
525
 if(ret <= 0)
 
526
   return(ret);
 
527
 job->cursor= fnode;
 
528
 return(1);
 
529
}
 
530
 
 
531
 
 
532
int Findjob_close_bracket(struct FindjoB *job, int flag)
 
533
{
 
534
 if(!Findjob_cursor_complete(job, 0)) {
 
535
   job->errn= -3;
 
536
   sprintf(job->errmsg,
 
537
     "Unary operator or expression expected, closing-bracket found");
 
538
   return(0);
 
539
 }
 
540
 
 
541
 if(job->cursor->up==NULL){
 
542
   job->errn= -1;
 
543
   sprintf(job->errmsg,
 
544
           "No bracket open when encountering closing bracket.");
 
545
   return(0);
 
546
 }
 
547
 job->cursor= job->cursor->up;
 
548
 return(1);
 
549
}
 
550
 
 
551
 
 
552
int Findjob_not(struct FindjoB *job, int flag)
 
553
{
 
554
 int ret;
 
555
 
 
556
 ret= Findjob_default_and(job, 0);
 
557
 if(ret <= 0)
 
558
   return(ret);
 
559
 job->cursor->invert= !job->cursor->invert;
 
560
 return(1);
 
561
}
 
562
 
 
563
 
 
564
int Findjob_and(struct FindjoB *job, int flag)
 
565
{
 
566
 int ret;
 
567
 struct ExprnodE *fnode;
 
568
 
 
569
 if(!Findjob_cursor_complete(job, 0)) {
 
570
   job->errn= -3;
 
571
   sprintf(job->errmsg,
 
572
           "Unary operator or expression expected, binary operator found");
 
573
   return(0);
 
574
 }
 
575
 
 
576
 ret= Findjob_new_node(job, &fnode, "-and", 0);
 
577
 if(ret<=0)
 
578
   return(ret);
 
579
 job->cursor->right_op= 1;
 
580
 job->cursor->assoc= 1;        /* compute right side first */
 
581
 fnode->left_op= 1;
 
582
 fnode->assoc= 0;              /* compute left side first */
 
583
 job->cursor= fnode;
 
584
 return(1);
 
585
}
 
586
 
 
587
 
 
588
int Findjob_or(struct FindjoB *job, int flag)
 
589
{
 
590
 int ret;
 
591
 struct ExprnodE *fnode;
 
592
 
 
593
 if(!Findjob_cursor_complete(job, 0)) {
 
594
   job->errn= -3;
 
595
   sprintf(job->errmsg,
 
596
           "Unary operator or expression expected, binary operator found");
 
597
   return(0);
 
598
 }
 
599
 
 
600
 ret= Findjob_new_node(job, &fnode, "-or", 0);
 
601
 if(ret<=0)
 
602
   return(ret);
 
603
 job->cursor->right= fnode;
 
604
 job->cursor->right_op= 0;
 
605
                                    /* if existing : compute left side first */
 
606
 job->cursor->assoc= (job->cursor->left == NULL);
 
607
 fnode->left= job->cursor;
 
608
 fnode->left_op= 0;
 
609
 fnode->assoc= 0;            /* no right side yet : compute left side first */
 
610
 job->cursor= fnode;
 
611
 return(1);
 
612
}
 
613
 
 
614
 
 
615
int Findjob_if(struct FindjoB *job, int flag)
 
616
{
 
617
 int ret;
 
618
 struct ExprnodE *fnode;
 
619
 
 
620
 ret= Findjob_default_and(job, 0);
 
621
 if(ret <= 0)
 
622
   return(ret);
 
623
 ret= Findjob_new_node(job, &fnode, "-if", 1);
 
624
 if(ret<=0)
 
625
   return(ret);
 
626
 Exprnode_set_is_if(fnode,1,0);
 
627
 job->cursor= fnode;
 
628
 return(1);
 
629
}
 
630
 
 
631
 
 
632
int Findjob_then(struct FindjoB *job, int flag)
 
633
{
 
634
 int ret;
 
635
 struct ExprnodE *fnode,*branch= NULL;
 
636
 
 
637
 if(! Findjob_cursor_complete(job,0)) {
 
638
   job->errn= -3;
 
639
   sprintf(job->errmsg,
 
640
           "Unary operator or expression expected, -then-operator found");
 
641
   return(0);
 
642
 } 
 
643
 /* Finding the -if that matches this -then
 
644
    Do not go up one node but look for the leftmost one.
 
645
    If everything is right we are at level of the -if node */
 
646
 while(job->cursor->left!=NULL)
 
647
   job->cursor= job->cursor->left;
 
648
 Exprnode_get_branch(job->cursor, &branch, 0);
 
649
 if(!Exprnode_is_if(job->cursor, 0) || branch != NULL) {
 
650
   job->errn= -5;
 
651
   sprintf(job->errmsg, "-then-operator found outside its proper range.");
 
652
   return(0);
 
653
 }
 
654
 ret= Findjob_new_node(job, &fnode, "-then", 1|2);
 
655
 if(ret <= 0)
 
656
   return(ret);
 
657
 Exprnode_set_branch(job->cursor, fnode, 0);
 
658
 job->cursor= fnode;
 
659
 return(1);
 
660
}
 
661
 
 
662
 
 
663
int Findjob_else(struct FindjoB *job, int flag)
 
664
{
 
665
 int ret;
 
666
 struct ExprnodE *fnode, *true_branch, *false_branch;
 
667
 
 
668
 if(! Findjob_cursor_complete(job, 0)) {
 
669
   job->errn= -3;
 
670
   sprintf(job->errmsg,
 
671
           "Unary operator or expression expected, -else-operator found");
 
672
   return(0);
 
673
 } 
 
674
 if(job->cursor->up == NULL)
 
675
   goto improper_range;
 
676
 job->cursor= job->cursor->up;
 
677
 Exprnode_get_branch(job->cursor, &true_branch, 0);
 
678
 Exprnode_get_branch(job->cursor, &false_branch, 1);
 
679
 if(!Exprnode_is_if(job->cursor, 0) || 
 
680
    true_branch == NULL || false_branch != NULL) {
 
681
improper_range:;
 
682
   job->errn= -5;
 
683
   sprintf(job->errmsg, "-else-operator found outside its proper range.");
 
684
   return(0);
 
685
 }
 
686
 ret= Findjob_new_node(job, &fnode, "-else", 1 | 2);
 
687
 if(ret <= 0)
 
688
   return(ret);
 
689
 Exprnode_set_branch(job->cursor, fnode, 1);
 
690
 job->cursor= fnode;
 
691
 return(1);
 
692
}
 
693
 
 
694
 
 
695
int Findjob_elseif(struct FindjoB *job, int flag)
 
696
{
 
697
 int ret;
 
698
 struct ExprnodE *true_branch, *false_branch;
 
699
 
 
700
 if(!Findjob_cursor_complete(job, 0)) {
 
701
   job->errn= -3;
 
702
   sprintf(job->errmsg,
 
703
           "Unary operator or expression expected, -elseif-operator found");
 
704
   return(0);
 
705
 } 
 
706
 if(job->cursor->up == NULL)
 
707
   goto improper_range;
 
708
 job->cursor= job->cursor->up;
 
709
 Exprnode_get_branch(job->cursor, &true_branch, 0);
 
710
 Exprnode_get_branch(job->cursor, &false_branch, 1);
 
711
 if(!Exprnode_is_if(job->cursor, 0) || 
 
712
    true_branch==NULL || false_branch!=NULL) {
 
713
improper_range:;
 
714
   job->errn= -5;
 
715
   sprintf(job->errmsg,
 
716
           "-elseif-operator found outside its proper range.");
 
717
   return(0);
 
718
 }
 
719
 job->cursor= job->cursor->up;
 
720
 /* -elseif is equivalent to the three-step sequence :  -endif -or -if
 
721
    ( -endif has already been performed by following job->cursor->up ) */
 
722
 ret= Findjob_or(job, 0);
 
723
 if(ret <= 0)
 
724
   return(0);
 
725
 ret= Findjob_if(job, 0);
 
726
 if(ret <= 0)
 
727
   return(0);
 
728
 return(1);
 
729
}
 
730
 
 
731
 
 
732
int Findjob_endif(struct FindjoB *job, int flag)
 
733
{
 
734
 struct ExprnodE *true_branch;
 
735
 
 
736
 if(!Findjob_cursor_complete(job,0)) {
 
737
   job->errn= -3;
 
738
   sprintf(job->errmsg,
 
739
           "Unary operator or expression expected, -endif found");
 
740
   return(0);
 
741
 }
 
742
 if(job->cursor->up==NULL)
 
743
   goto improper_range;
 
744
 /* test wether parent node is -if */
 
745
 job->cursor= job->cursor->up;
 
746
 Exprnode_get_branch(job->cursor, &true_branch, 0);
 
747
 if(!Exprnode_is_if(job->cursor,0) || true_branch == NULL) {
 
748
improper_range:;
 
749
   job->errn= -5;
 
750
   sprintf(job->errmsg, "-endif-mark found outside its proper range.");
 
751
   return(0);
 
752
 }
 
753
 /* go to grand parent node */
 
754
 job->cursor= job->cursor->up;
 
755
 return(1);
 
756
}
 
757
 
 
758
 
 
759
/* @param flag bit0-1=  0= -name , 1= -wholename , 2= -disk_name
 
760
*/
 
761
int Findjob_set_name_expr(struct FindjoB *o, char *name_expr, int flag)
 
762
{
 
763
 char regexpr[2*SfileadrL+2];
 
764
 regex_t *name_re;
 
765
 struct ExprtesT *t;
 
766
 int ret;
 
767
   
 
768
 if(strlen(name_expr)>=SfileadrL)
 
769
   return(0);
 
770
 
 
771
 ret= Findjob_default_and(o, 0);
 
772
 if(ret <= 0)
 
773
   return(ret);
 
774
 t= o->cursor->test;
 
775
 t->test_type= 1;
 
776
 if ((flag & 3) == 1)
 
777
   t->test_type= 13;
 
778
 else if((flag & 3) == 2)
 
779
   t->test_type= 16;
 
780
 name_re= (regex_t *) calloc(1, sizeof(regex_t));
 
781
 if(name_re == NULL)
 
782
   return(-1);
 
783
 t->arg1= strdup(name_expr);
 
784
 if(t->arg1 == NULL) {
 
785
   free((char *) name_re);
 
786
   return(-1);
 
787
 }
 
788
 Xorriso__bourne_to_reg(name_expr, regexpr, 0);
 
789
 if(regcomp(name_re, regexpr, 0) != 0)
 
790
   return(0);
 
791
 t->arg2= name_re;
 
792
 return(1);
 
793
}
 
794
 
 
795
 
 
796
int Findjob_set_file_type(struct FindjoB *o, char file_type, int flag)
 
797
{
 
798
 static char known[]= {"bcdpf-lsmeX"};
 
799
 struct ExprtesT *t;
 
800
 int ret;
 
801
 
 
802
 ret= Findjob_default_and(o, 0);
 
803
 if(ret <= 0)
 
804
   return(ret);
 
805
 
 
806
 if(file_type != 0)
 
807
   if(strchr(known, file_type) == NULL)
 
808
     return(0);
 
809
 t= o->cursor->test;
 
810
 t->test_type= 2;
 
811
 t->arg1= calloc(1, 1);
 
812
 if(t->arg1 == NULL)
 
813
   return(-1);
 
814
 *((char *) t->arg1)= file_type;
 
815
 return(1);
 
816
}
 
817
 
 
818
 
 
819
/* @param value -1= only without property, 1= only with property
 
820
   @param flag bit0= pseudo-test:
 
821
                     if no operator is open, do nothing and return 2
 
822
*/
 
823
int Findjob_set_prop_filter(struct FindjoB *o, int test_type, int value,
 
824
                            int flag)
 
825
{
 
826
 struct ExprtesT *t;
 
827
 int ret;
 
828
 
 
829
 ret= Findjob_default_and(o, flag & 1);
 
830
 if(ret <= 0 || ret == 2)
 
831
   return(ret);
 
832
 
 
833
 t= o->cursor->test;
 
834
 t->test_type= test_type;
 
835
 if(value < 0)
 
836
   t->invert= !t->invert;
 
837
 return(1);
 
838
}
 
839
 
 
840
 
 
841
/* @param value -1= only undamaged files, 1= only damaged files
 
842
*/
 
843
int Findjob_set_damage_filter(struct FindjoB *o, int value, int flag)
 
844
{
 
845
 int ret;
 
846
 
 
847
 ret= Findjob_set_prop_filter(o, 3, value, 0);
 
848
 return(ret);
 
849
}
 
850
 
 
851
 
 
852
int Findjob_set_lba_range(struct FindjoB *o, int start_lba, int count,
 
853
                          int flag)
 
854
{
 
855
 struct ExprtesT *t;
 
856
 int ret;
 
857
 
 
858
 ret= Findjob_default_and(o, 0);
 
859
 if(ret <= 0)
 
860
   return(ret);
 
861
 
 
862
 t= o->cursor->test;
 
863
 t->test_type= 4;
 
864
 t->arg1= calloc(sizeof(int), 1);
 
865
 t->arg2= calloc(sizeof(int), 1);
 
866
 if(t->arg1 == NULL || t->arg2 == NULL)
 
867
   return(-1);
 
868
 *((int *) t->arg1)= start_lba;
 
869
 if(start_lba > 0)
 
870
   *((int *) t->arg2)= start_lba + count - 1;
 
871
 else
 
872
   *((int *) t->arg2)= start_lba - count + 1;
 
873
 return(1);
 
874
}
 
875
 
 
876
 
 
877
int Findjob_set_test_hidden(struct FindjoB *o, int mode, int flag)
 
878
{
 
879
 struct ExprtesT *t;
 
880
 int ret;
 
881
 
 
882
 ret= Findjob_default_and(o, 0);
 
883
 if(ret <= 0)
 
884
   return(ret);
 
885
 
 
886
 t= o->cursor->test;
 
887
 t->test_type= 17;
 
888
 t->arg1= calloc(sizeof(int), 1);
 
889
 if(t->arg1 == NULL)
 
890
   return(-1);
 
891
 *((int *) t->arg1)= mode;
 
892
 return(1);
 
893
}
 
894
 
 
895
 
 
896
/* @param value -1= files without ACL, 1= only files with ACL
 
897
*/
 
898
int Findjob_set_acl_filter(struct FindjoB *o, int value, int flag)
 
899
{
 
900
 int ret;
 
901
 
 
902
 ret= Findjob_set_prop_filter(o, 5, value, 0);
 
903
 return(ret);
 
904
}
 
905
 
 
906
 
 
907
/* @param value -1= files without xattr, 1= only files with xattr
 
908
   @param flag bit0=-has_any_xattr rather than -has_xattr
 
909
*/
 
910
int Findjob_set_xattr_filter(struct FindjoB *o, int value, int flag)
 
911
{
 
912
 int ret;
 
913
 
 
914
 ret= Findjob_set_prop_filter(o, (flag & 1 ? 14 : 6), value, 0);
 
915
 return(ret);
 
916
}
 
917
 
 
918
 
 
919
/* @param value -1= files without aaip, 1= only files with aaip
 
920
*/
 
921
int Findjob_set_aaip_filter(struct FindjoB *o, int value, int flag)
 
922
{
 
923
 int ret;
 
924
 
 
925
 ret= Findjob_set_prop_filter(o, 7, value, 0);
 
926
 return(ret);
 
927
}
 
928
 
 
929
 
 
930
/* @param value -1= files without filter, 1= files with filter
 
931
*/
 
932
int Findjob_set_filter_filter(struct FindjoB *o, int value, int flag)
 
933
{
 
934
 int ret;
 
935
 
 
936
 ret= Findjob_set_prop_filter(o, 8, value, 0);
 
937
 return(ret);
 
938
}
 
939
 
 
940
 
 
941
int Findjob_set_wanted_node(struct FindjoB *o, void *wanted_node, int flag)
 
942
{
 
943
 struct ExprtesT *t;
 
944
 int ret;
 
945
 
 
946
 ret= Findjob_default_and(o, 0);
 
947
 if(ret <= 0)
 
948
   return(ret);
 
949
 
 
950
 t= o->cursor->test;
 
951
 t->test_type= 9;
 
952
 t->arg1= wanted_node;
 
953
 return(1);
 
954
}
 
955
 
 
956
 
 
957
int Findjob_set_commit_filter_2(struct FindjoB *o, int flag)
 
958
{
 
959
 int ret;
 
960
 
 
961
 ret= Findjob_default_and(o, 0);
 
962
 if(ret <= 0)
 
963
   return(ret);
 
964
 
 
965
 o->cursor->test->test_type= 10;
 
966
 return(1);
 
967
}
 
968
 
 
969
 
 
970
int Findjob_set_decision(struct FindjoB *o, char *decision, int flag)
 
971
{
 
972
 struct ExprtesT *t;
 
973
 int ret;
 
974
 
 
975
 ret= Findjob_default_and(o, 0);
 
976
 if(ret <= 0)
 
977
   return(ret);
 
978
 
 
979
 t= o->cursor->test;
 
980
 t->test_type= 11;
 
981
 t->arg1= strdup(decision);
 
982
 if(t->arg1 == NULL)
 
983
   return(-1);
 
984
 return(1);
 
985
}
 
986
 
 
987
 
 
988
/* @param value -1= true, 1= false
 
989
   @param flag bit0= pseudo-test:
 
990
                     if no operator is open, do nothing and return 2
 
991
*/
 
992
int Findjob_set_false(struct FindjoB *o, int value, int flag)
 
993
{
 
994
 int ret;
 
995
 
 
996
 ret= Findjob_set_prop_filter(o, 0, value, flag & 1);
 
997
 return(ret);
 
998
}
 
999
 
 
1000
 
 
1001
int Findjob_set_prune(struct FindjoB *o, int flag)
 
1002
{
 
1003
 int ret;
 
1004
 
 
1005
 ret= Findjob_set_prop_filter(o, 12, 0, 0);
 
1006
 return(ret);
 
1007
}
 
1008
 
 
1009
 
 
1010
int Findjob_set_found_path(struct FindjoB *o, char *path, int flag)
 
1011
{
 
1012
 if(o->found_path != NULL)
 
1013
   free(o->found_path);
 
1014
 if(path != NULL) {
 
1015
   o->found_path= strdup(path);
 
1016
   if(o->found_path == NULL)
 
1017
     return(-1);
 
1018
 } else
 
1019
   o->found_path= NULL;
 
1020
 return(1);
 
1021
}
 
1022
 
 
1023
 
 
1024
int Findjob_get_found_path(struct FindjoB *o, char **path, int flag)
 
1025
{
 
1026
 *path= o->found_path;
 
1027
 return(1);
 
1028
}
 
1029
 
 
1030
 
 
1031
int Findjob_get_action(struct FindjoB *o, int flag)
 
1032
{
 
1033
 return(o->action);
 
1034
}  
 
1035
 
 
1036
 
 
1037
/* @return <0 error, >=0 see above struct FindjoB.action
 
1038
*/
 
1039
int Findjob_get_action_parms(struct FindjoB *o, char **target, char **text_2,
 
1040
                             uid_t *user, gid_t *group,
 
1041
                             mode_t *mode_and, mode_t *mode_or,
 
1042
                             int *type, time_t *date, struct FindjoB **subjob,
 
1043
                             int flag)
 
1044
{
 
1045
 *target= o->target;
 
1046
 *text_2= o->text_2;
 
1047
 *user= o->user;
 
1048
 *group= o->group;
 
1049
 *mode_and= o->mode_and;
 
1050
 *mode_or= o->mode_or;
 
1051
 *type= o->type;
 
1052
 *date= o->date;
 
1053
 *subjob= o->subjob;
 
1054
 return(o->action);
 
1055
}
 
1056
 
 
1057
 
 
1058
int Findjob_test_2(struct XorrisO *xorriso, struct FindjoB *o,
 
1059
                   void *node, char *name, char *path,
 
1060
                   struct stat *boss_stbuf, struct stat *stbuf, int flag)
 
1061
{
 
1062
 int ret;
 
1063
 
 
1064
 ret= Exprnode_tree_value(xorriso, o->test_tree, -1,
 
1065
                          node, name, path, boss_stbuf, stbuf, 0);
 
1066
 if(ret == 3)
 
1067
   ret= 1;
 
1068
 else if(ret == 2)
 
1069
   ret= 0;
 
1070
 return(ret);
 
1071
}
 
1072
 
 
1073
 
 
1074
int Findjob_set_action_target(struct FindjoB *o, int action, char *target,
 
1075
                              int flag)
 
1076
{
 
1077
 o->action= action;
 
1078
 o->target= target;
 
1079
 return(1);
 
1080
}
 
1081
 
 
1082
 
 
1083
int Findjob_set_action_type(struct FindjoB *o, int action, int type,
 
1084
                              int flag)
 
1085
{
 
1086
 o->action= action;
 
1087
 o->type= type;
 
1088
 return(1);
 
1089
}
 
1090
 
 
1091
 
 
1092
int Findjob_set_action_text_2(struct FindjoB *o, int action, char *target,
 
1093
                              char* text_2, int flag)
 
1094
{
 
1095
 o->action= action;
 
1096
 o->target= target;
 
1097
 o->text_2= text_2;
 
1098
 return(1);
 
1099
}
 
1100
 
 
1101
 
 
1102
/* @param flag bit0= recursive
 
1103
*/
 
1104
int Findjob_set_action_chown(struct FindjoB *o, uid_t user,int flag)
 
1105
{
 
1106
 int ret;
 
1107
 
 
1108
 if(flag&1) {
 
1109
   o->action= 0;
 
1110
   Findjob_destroy(&(o->subjob), 0);
 
1111
   ret= Findjob_new(&(o->subjob), "", 0);
 
1112
   if(ret<=0)
 
1113
     return(-1);
 
1114
   Findjob_set_action_chown(o->subjob, user, 0);
 
1115
   o->action= 9;
 
1116
 } else {
 
1117
   o->action= 4;
 
1118
   o->user= user;
 
1119
 }
 
1120
 return(1);
 
1121
}
 
1122
 
 
1123
 
 
1124
/* @param flag bit0= recursive
 
1125
*/
 
1126
int Findjob_set_action_chgrp(struct FindjoB *o, gid_t group, int flag)
 
1127
{
 
1128
 int ret;
 
1129
 
 
1130
 if(flag&1) {
 
1131
   o->action= 0;
 
1132
   Findjob_destroy(&(o->subjob), 0);
 
1133
   ret= Findjob_new(&(o->subjob), "", 0);
 
1134
   if(ret<=0)
 
1135
     return(-1);
 
1136
   Findjob_set_action_chgrp(o->subjob, group, 0);
 
1137
   o->action= 10;
 
1138
 } else {
 
1139
   o->action= 5;
 
1140
   o->group= group;
 
1141
 }
 
1142
 return(1);
 
1143
}
 
1144
 
 
1145
 
 
1146
/* @param flag bit0= recursive
 
1147
*/
 
1148
int Findjob_set_action_chmod(struct FindjoB *o,
 
1149
                             mode_t mode_and, mode_t mode_or, int flag)
 
1150
{
 
1151
 int ret;
 
1152
 
 
1153
 if(flag&1) {
 
1154
   o->action= 0;
 
1155
   Findjob_destroy(&(o->subjob), 0);
 
1156
   ret= Findjob_new(&(o->subjob), "", 0);
 
1157
   if(ret<=0)
 
1158
     return(-1);
 
1159
   Findjob_set_action_chmod(o->subjob, mode_and, mode_or, 0);
 
1160
   o->action= 11;
 
1161
 } else {
 
1162
   o->action= 6;
 
1163
   o->mode_and= mode_and;
 
1164
   o->mode_or= mode_or;
 
1165
 }
 
1166
 return(1);
 
1167
}
 
1168
 
 
1169
 
 
1170
/* @param flag bit0= recursive
 
1171
*/
 
1172
int Findjob_set_action_ad(struct FindjoB *o, int type, time_t date, int flag)
 
1173
{
 
1174
 int ret;
 
1175
 
 
1176
 if(flag&1) {
 
1177
   o->action= 0;
 
1178
   Findjob_destroy(&(o->subjob), 0);
 
1179
   ret= Findjob_new(&(o->subjob), "", 0);
 
1180
   if(ret<=0)
 
1181
     return(-1);
 
1182
   Findjob_set_action_ad(o->subjob, type, date, 0);
 
1183
   o->action= 12;
 
1184
 } else {
 
1185
   o->action= 7;
 
1186
   o->type= type;
 
1187
   o->date= date;
 
1188
 }
 
1189
 return(1);
 
1190
}
 
1191
 
 
1192
 
 
1193
int Findjob_set_action_subjob(struct FindjoB *o, int action,
 
1194
                              struct FindjoB *subjob, int flag)
 
1195
{
 
1196
 o->action= action;
 
1197
 Findjob_destroy(&(o->subjob), 0);
 
1198
 o->subjob= subjob;
 
1199
 return(1);
 
1200
}
 
1201
 
 
1202
 
 
1203
int Findjob_set_action_found_path(struct FindjoB *o, int flag)
 
1204
{
 
1205
 o->action= 23;
 
1206
 Findjob_set_found_path(o, NULL, 0);
 
1207
 return(1);
 
1208
}
 
1209