[go: up one dir, main page]

File: findfile.c

package info (click to toggle)
console-tools 1998.08.11-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 5,240 kB
  • ctags: 805
  • sloc: ansic: 8,241; sh: 2,947; yacc: 970; makefile: 356; lex: 287; pascal: 192; perl: 110
file content (740 lines) | stat: -rw-r--r-- 17,220 bytes parent folder | download
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
#include <config.h>

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <signal.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <lct/utils.h>

typedef enum
{
  compressed_not,
  compressed_gzip,
  compressed_compress,
  compressed_bzip2,
  compressed_lzop,
  compressor_MAX
} compressed_type;

char* decompressor_names[compressor_MAX] =
{
  "",
  "gunzip",
  "uncompress",
  "bunzip2",
  "lzop"
};

char* decompressor_options[compressor_MAX] =
{
  NULL,
  NULL,
  NULL,
  NULL,
  "-d" /* lzop has no "standalone" decompressor */
};

static const char *compressor_suffixes[] = {
  "",
  ".gz",
  ".lzo",
  ".Z",
  ".bz2",
  0 };

static pid_t pid_feeder = 0;
static pid_t pid_decompressor = 0;
static pid_t pid_identifier = 0;
static struct sigaction old_sa;
static compressed_type is_compressed;		  /* current file's compression */


static void print_status (int status, char* name, char* parent)
{
  if (WIFEXITED(status))
    fprintf (stderr, "%s: %s child exited anormally with code %d.\n",
	     parent, name, WEXITSTATUS(status));
  else if (WIFSIGNALED(status))
    fprintf (stderr, "%s: %s child was terminated by signal %d.\n",
	     parent, name, WTERMSIG(status));
  else if (WIFSTOPPED(status))
    fprintf (stderr, "%s: %s child was stopped by signal %d.\n",
	     parent, name, WSTOPSIG(status));
}

static void chld_handler(int sig)
{
  int status;
  pid_t pid;
  char* name;
  
  pid = wait(&status);
  
  if (pid == pid_feeder)
    {
      name = "Feeder";
      pid_feeder = 0;
    }
  else if (pid == pid_decompressor)
    {
      name = "Decompressor";
      pid_decompressor = 0;
    }
  else if (pid == pid_identifier)
    {
      name = "Identifier";
      pid_identifier = 0;
    }
  else
    {
      /* it's not one of our children */
      /* ...try to be nice to main program's handler - UNTESTED */
      if ((old_sa.sa_handler != SIG_IGN) && (old_sa.sa_handler != SIG_DFL))
	  old_sa.sa_handler (sig);
      return;
    }

  if (WIFEXITED(status) &&
      ((WEXITSTATUS(status) == 0) ||
       ((WEXITSTATUS(status) == 2) && (is_compressed == compressed_gzip))))
    {
      /* when no more of our children,
       * restore application's SIGCHLD handler 
       */
      if (! (pid_feeder || pid_decompressor || pid_identifier))
	{
	  sigaction (SIGCHLD, &old_sa, NULL);
	  memset (&old_sa, 0, sizeof(old_sa));
	}
    }
  else
    {
      print_status (status, name, "chld_handler()");
      exit (1);
    }

#if 0
  if (WIFEXITED(status))
      fprintf (stderr, "%s child exited OK.\n", name);
#endif
}


static int feed (FILE* inf, int outfd)
{
  char buf[512];		       /* FIXME: would it safe to make this larger ? */
  int count;
  
  do
    {
      if (-1 == (count = fread (buf, 1, sizeof(buf), inf)))
	  return -1;
      if ((count > 0) && (-1 == (count = write (outfd, buf, count))))
	  return -1;
    }
  while (count);
  
  return 0;
}


/* convenience macro - return FD */
static FILE* do_pipe(const char* filename, FILE* minus_meaning,
				const struct magicset* magics, int* magic_return)
{
  FILE *inf;
  int file[2];		/* pipe transmitting original file (to gzip or identifier) */
  int uncompressed[2];	/* pipe transmitting uncompressed file (raw or gunzipped) */
  int identified[2];	/* pipe transmitting uncompressed magically-identified file */
  int is_seekable;
  unsigned char zmagicbuf[9];	/* buffer used to get compressed magic number */
  unsigned i;
  FILE *in;
  struct magic* m;		       /* loop index */
  
  /* the input file */
  if (NULL == (inf = xfopen (filename, "r", minus_meaning)))
    return NULL;

  /* read for magic */
  if (fread (zmagicbuf, sizeof(zmagicbuf), 1, inf) != 1)
    {
      perror ("fread magic");
      exit (1);
    }

  /* identify compressed format by magic */
  if ((zmagicbuf[0] == 037) && (zmagicbuf[1] == 0213))/* gzip */
    {
      is_compressed = compressed_gzip;
      is_seekable = 0;
    }
  else if ((zmagicbuf[0] == 037) && (zmagicbuf[1] == 0235))	/* compress */
    {
      is_compressed = compressed_compress;
      is_seekable = 0;
    }
  else if ((zmagicbuf[0] == 'B') && (zmagicbuf[1] == 'Z')
	   && (zmagicbuf[2] == 'h'))		  /* bzip2 */
    {
      is_compressed = compressed_bzip2;
      is_seekable = 0;
    }
  else if (!strncmp (zmagicbuf, "\211LZO\0\r\n\32\n", 9))
    {
      is_compressed = compressed_lzop;
      is_seekable = 0;
    }
  else
    {
      is_compressed = 0;
      
      is_seekable = (-1 != fseek (inf, 0, SEEK_SET));
      
      /* uncompressed seekable file and no need for
       * magic identification, just use raw file */
      if (is_seekable && !magic_return)
	  return inf;
    }

  /*
   * case when file is not seekable
   * ie, either compressed, or a "-" on command-line
   */
  if (!is_seekable)
    {
      /* open pipes */
      if (-1 == pipe (uncompressed))
	  return NULL;

      /*
       * case when file is compressed
       */
      if (is_compressed)
	{
	  if (-1 == pipe (file))
	      return NULL;
	  
	  /* fork a decompressor */
	  switch (pid_decompressor = fork())
	    {
	    case -1:
	      perror ("decompressor fork");
	      exit (1);
	    case 0:	/* gzip child */
	      /* unused FD's: */
	      if (-1 == close (file[1])) 
		  {
		    perror ("decompressor: close file[1]");
		    exit(1);
		  }
	      if (-1 == close (uncompressed[0])) 
		  {
		    perror ("decompressor: close uncompressed[0]");
		    exit(1);
		  }
	      
	      /* I/O pipes */
	      /*   "file" feeding stdin */
	      fclose (stdin);
	      if (-1 == dup2 (file[0], STDIN_FILENO)) 
		  {
		    perror ("decompressor: dup2 file[0]");
		    exit(1);
		  }
	      /*   "uncompressed" fed from stdout */
	      fclose (stdout);
	      if (-1 == dup2 (uncompressed[1], STDOUT_FILENO)) 
		  {
		    perror ("decompressor: dup2 uncompressed[1]");
		    exit(1);
		  }
	      
	      execlp (decompressor_names[is_compressed],
		      decompressor_names[is_compressed],
		      decompressor_options[is_compressed],
		      NULL);
	      if (errno==ENOENT)
		fprintf (stderr, "Decompressor program not found: %s\n",
			 decompressor_names[is_compressed]);
	      else
		perror ("decompressor: execlp");
	      
	      exit (1);
	      
	    default:	/* parent */
	      i = close (file[0]);
	      assert (-1 != i);
	      i = close (uncompressed[1]);
	      assert (-1 != i);
	    }
	}
      
      /* fork a feeder-process (for gzip/compress, or for reader directly) */
      switch (pid_feeder = fork())
	{
	case -1:
	  perror ("feeder fork");
	  exit (1);
	case 0:	/* feeder child: read from `inf' - write to `file' */
	  /* unused FD's: */
	  if (!is_compressed)
	      if (-1 == close (uncompressed[0])) 
		  {
		    perror ("feeder: close uncompressed[0]");
		    exit(1);
		  }
	  
	  /* feed zmagicbuf, whether magic or not */
	  if (write (is_compressed ? file[1] : uncompressed[1], zmagicbuf, sizeof(zmagicbuf))
	      != sizeof(zmagicbuf))
	    {
	      perror ("feeder: write zmagicbuf");
	      exit(1);
	    }
	  
	  if (feed (inf, is_compressed ? file[1] : uncompressed[1]))
	    {
	      perror ("feeder: feed");
	      exit(1);
	    }
	  
	  /* EOF */
	  exit (0);
	default:	/* parent */
	  i = close (is_compressed ? file[1] : uncompressed[1]);
	  assert (-1 != i);
	  i = fclose (inf);
	  assert (-1 != i);
	}
    } /* if unseekable */

  /*
   * test uncompressed file by magic if asked to
   */
  if (magic_return)
    {
      char* magicIDbuf;
      
       /* setup FD's */
      if (is_seekable)
	  in = inf;
      else 
	  in = fdopen (uncompressed[0], "r");
      
      /* try to read enough to identify the magic */
      magicIDbuf = (char*)malloc(magics->max_length_hint);
      if (fread (magicIDbuf, magics->max_length_hint, 1, in) != 1)
	{
#ifndef NDEBUG
	  perror ("fread");
#endif
	  errno = EINVAL;
	  return NULL;
	}

      /* check each magic until one matches */
      for (m = (struct magic*)magics->m; m->id != FF_END; m++)
	{
	  if (!m->magic)
	      *magic_return = m->id;
	  else
	    {
	      int ok = 1;
	      
	      for (i = 0; ok && i < m->length; i++)
		  ok = ok && ( ( magicIDbuf[i] & m->mask[i] ) == m->magic[i] );
	      
	      if (ok)
		{
		  *magic_return = m->id;
		  break; /* for magics */
		}
	    }
	}
      
      /* rewind if possible */
      if (is_seekable)
	  rewind (inf);
      else /* feed to user using a child process */
	{
	  if (-1 == pipe (identified))
	      return NULL;

	  switch (pid_identifier = fork())
	    {
	    case -1:
	      perror ("identifier fork");
	      exit (1);
	    case 0: /* identifier child */
	      if (-1 == close (identified[0])) 
		{
		  perror ("identifier: close identified[0]");
		  exit (1);
		}

	      /* feed magicIDbuf */
	      if (write (identified[1], magicIDbuf, magics->max_length_hint) != (int)magics->max_length_hint)
		{
		  perror ("identifier: write magicIDbuf");
		  exit (1);
		}
	      
	      if (feed (in, identified[1]))
		{
		  perror ("identifier: feed");
		  exit (1);
		}
	      
	      /* EOF */
	      exit (0);
	    default: /* parent */
	      if (is_seekable)
		{
		  i = close (uncompressed[0]);
		  assert (-1 != i);
		}
	      else
		{
		  i = fclose (inf);
		  assert (-1 != i);
		}
	      assert (-1 != i);
	      i = close (identified[1]);
	    }
	}
    }

  return is_seekable ? inf 
      : fdopen (magic_return ? identified[0] 
		: uncompressed[0], "r");
}


static void maybekillzombie (pid_t *pid, char* name)
{
  int status;
  
  /* if pid is 0, process is already dead */
  if (!*pid) return;

  /* return if no child to wait for */
  if (0 == waitpid (*pid, &status, WNOHANG)) return;
      
  if (WIFEXITED(status) &&
      ((WEXITSTATUS(status) == 0) ||
       ((WEXITSTATUS(status) == 2) && (is_compressed == compressed_gzip))))
    {
#ifndef NDEBUG
      fprintf (stderr, "Zombie %d caught.\n", *pid);
#endif
      *pid = 0;
    }
  else if (WIFEXITED(status) || WIFSIGNALED(status) || WIFSTOPPED(status))
    {
      print_status (status, name, "maybekillzombie()");
      exit (1);
    }
#ifndef NDEBUG
  else
    fprintf (stderr, "One for nothing.\n");
#endif
}


static int findfile_recursive(const char *fnam, const char *path, const char **suffixes,
			      char *fullname, size_t maxfullength,
			      int min_depth, int max_depth)
{
  const char **sp, **cs;
  char* target;					  /* what to search right now */
  char* first_slash;				  /* NULL means look for a file */
  char* newpath = 0;				  /* full path to target */
  char* ptr_to_target = 0;			  /* save allocated ptr */
  struct stat st;
  int found = 0;				  /* entry found */
  int retcode = 0;

  assert (fnam);
  assert (fnam[0]);
  assert ((!path) || path[0]);			  /* if not NULL, should not be "" */

  /* check for leading "/" and tune args */
  if ((path == NULL) && (fnam[0] == '/'))
    {
      fnam++;
      path = "";
    }


ffr_next_component:
  /* extract the dir entry to look for into `target' */
  ptr_to_target = target = xstrdup(fnam);
  first_slash = strchr(fnam, '/');
  if (first_slash)
    target[first_slash-fnam] = 0;

  /* readability macro */
#define WANT_A_DIR (first_slash)
  
  /* interpret a sequence of *'s to raise max_depth */
  if (target[0] == '*')
    {
      while (target[0] == '*')
	{
	  if (max_depth>=0) max_depth++;	  /* do not increment if -1 */
	  target++;
	}
      /* something else after *'s is an error */
      if (target[0])
	{
	  errno = EINVAL;
	  retcode = -1;
	  goto ffr_exit;
	}
      /* next path component */
      fnam = first_slash + 1;
      free (ptr_to_target);
      goto ffr_next_component;
    }
  

  /* maybe look for target in this dir */
  if (min_depth == 0)
    {
      /* set full path to target in newpath */
      if (path)
	{
	  newpath = xmalloc (strlen(path) + strlen (target) + 2);
	  sprintf (newpath, "%s/%s", path, target);
	}
      else
	{
	  newpath = xmalloc (strlen (target) + 1);
	  strcpy (newpath, target);
	}
    
    
      if (WANT_A_DIR)
	/* look for it */
	if (stat (newpath, &st) == -1)
	  {
	    if (first_slash)
	      {
		errno = ENOENT;
		retcode = -1;
		goto ffr_exit;
	      }
	  }
	else
	  /* if we look for a dir */
	  if (S_ISDIR(st.st_mode))
	    {
	      retcode = findfile_recursive(first_slash+1, newpath, suffixes,
					   fullname, maxfullength,
					   min_depth, max_depth);
	      found = 1;
	    }
	  else
	    {
	      /* not a dir */
	      errno = ENOTDIR;
	      retcode = -1;
	      goto ffr_exit;
	    }
      else
	/* we want a file */
	for (sp = suffixes; *sp && !found; sp++) 
	  for (cs = compressor_suffixes; *cs && !found; cs++) 
	    {
	      if (strlen(newpath) + strlen(*sp) + strlen(*cs) + 1 > maxfullength)
		{
		  fprintf (stderr, "Warning: ignoring a possible path (too long).");
		  continue;
		}
	      sprintf(fullname, "%s%s%s", newpath, *sp, *cs);

	      /* check for file */
	      if (-1 != stat(fullname, &st))
		{
		  found = 1;
		  retcode = 0;
		}
	    }
    }

  
  /* maybe search in subdirs */
  if ((!found) && (max_depth > 0))
    {
      DIR* d;
      struct dirent *de;
      
      if (NULL == (d = opendir(path)))
	return -1;
      
      while ((!found) && (de = readdir(d)) != NULL)
	if (de->d_name[0] != '.')
	  {
	    free (newpath);
	    newpath = xmalloc (strlen(path) + strlen (de->d_name) + 2);
	    sprintf (newpath, "%s/%s", path, de->d_name);
	  
	    if ((-1 != stat (newpath, &st)) && S_ISDIR(st.st_mode))
	      {
		retcode = findfile_recursive (fnam, newpath, suffixes, fullname, maxfullength,
					      min_depth ? (min_depth - 1) : 0,
					      max_depth ? (max_depth - 1) : 0);
		if (retcode == 0)
		  found = 1;
		else if (errno != ENOENT)
		  {
		    closedir(d);		  /* should not change errno */
		    goto ffr_exit;
		  }
	      }
	  }
      closedir(d);
    }

  /* if nothing was found */
  if (!found)
    {
      errno = ENOENT;
      retcode = -1;
    }
  
ffr_exit:
  {
    int saved_errno = errno;
    if (newpath) free (newpath);
    if (ptr_to_target) free (ptr_to_target);
    errno = saved_errno;
  }
  return retcode;
}

FILE* findfile_simple(const char *fnam, const char **dirpath, const char **suffixes)
{
  return findfile (fnam, dirpath, suffixes,
		   NULL, 0, NULL, NULL, NULL);
}

/* find input file; leave name in fullname[] */
FILE* findfile(const char *fnam, const char **dirpath, const char **suffixes,
	       char *fullname, size_t maxfullength, FILE* minus_meaning,
	       const struct magicset* magics, int* magic_return)
{
  const char **dp;
  FILE *fp = NULL;
  struct sigaction sa;
  int found;
  sigset_t sigset, old_sigset;
#ifndef NDEBUG
  int retries = 300;
#else
  int retries = 30;
#endif

  if (maxfullength == 0)
    {
      maxfullength = 1024;
      fullname = (char*)malloc (maxfullength * (sizeof(char)));
      if (fullname == NULL) return NULL;
    }
  else
    assert (fullname);
  
  /* if a dead child of do_pipe was not caught in/after a
   * former call, fail
   */
  while (retries && (pid_feeder || pid_decompressor || pid_identifier))
    {
#ifndef NDEBUG
      fprintf (stderr, "f = %d, d = %d, i = %d\n", pid_feeder, pid_decompressor, pid_identifier);
#endif
      usleep (100);
#if 1
      maybekillzombie(&pid_feeder, "feeder");
      maybekillzombie(&pid_decompressor, "decompressor");
      maybekillzombie(&pid_identifier, "identifier");
#endif
      retries--;
    }
  if (!retries)
    {
      fprintf (stderr, "findfile(): timeout waiting for undead child(ren) ?\n");
      exit (EX_SOFTWARE);
    }

  /* install custom signal handler for dead children */
  /* only if our previous handler is no more here */
  sigaction (SIGCHLD, NULL, &sa);
  if (sa.sa_handler != chld_handler)
    {
      memset (&sa, 0, sizeof (sa));
      sa.sa_handler = chld_handler;
      sigaction (SIGCHLD, &sa, &old_sa);
    }

  /* if we are asked to test a magic, and we don't have a magic list... */
  if (magic_return && !magics)
    {
      /* ...then say we don't know */
      *magic_return = FF_UNKNOWN;
      /* and do as if we weren't asked to */
      magic_return = NULL;
    }
  
  /* try to find an existing file according to PATH dirpath, and to default suffixes */
  found = 0;
  for (dp = dirpath; *dp && !found; dp++) 
    {
      if (*fnam == '/' && **dp)	       /* if fnam is an absolute filename spec, */ 
	  continue;		       /* then skip until we have an empty entry in path */

      {
	char fname_buf[1024];

	if (strlen(*dp) + strlen(fnam) +1 > sizeof (fname_buf))
	  {
	    fprintf (stderr, "Buffer overflow - aborting\n");
	    exit (1);
	  }
	
	sprintf(fname_buf, "%s%s", *dp, fnam);

	if (-1 != findfile_recursive (fname_buf, NULL, suffixes, fullname, maxfullength, 0, 0))
	  {
	    /* block SIGCHLD */
	    sigemptyset (&sigset);
	    sigaddset (&sigset, SIGCHLD);
	    sigprocmask (SIG_BLOCK, &sigset, &old_sigset);

	    fp = do_pipe(fullname, minus_meaning, magics, magic_return);

	    /* restore sig mask */
	    sigprocmask (SIG_SETMASK, &old_sigset, NULL);

	    /* fp may be NULL if do_pipe() fails */
	    found = 1;
	  }
	else if (errno != ENOENT)
	  {
	    /* return, pass errno */
	    fp = NULL;
	    found = 1;
	  }
      }
    }

  return found ? fp : NULL;
}