/****************************************
  mytest.c

  By Stephen Vermeulen 1989

  Placed in the public domain, do with as
  you please.

  This routine is used to recursively decend the file system tree from
  a specified directory location, reading the files into memory
  (if they will fit) as it goes.  I wrote this to try to isolate a
  problem I was having with a GVP030 board when used with SETCPU's
  FASTROM option and a SCSI hard disk interfaced with a 2090 card.

  Dave H:

  This program will walk the directory tree of a disk allocating
  and freeing memory buffers and reading in each file as it goes.
  By selecting the "mode" level you can specify which of these
  operations will be done.  As well you can specify several
  delays to be taken between the Open/Read/Close calls.  I tested
  this in two modes:

    1) SETCPU CACHE BURST
    2) SETCPU NOCACHE NOBURST FASTROM

  The first of these (caches on (both inst and data) and no fastrom)
  worked fine in all tests, the second (nocaches, but fastrom is on)
  only worked with modes 0, 1 and 2, but when mode 3 was used it would
  hang (cli/mouse movement lock up, but NEVER a guru or task held) at
  some point in the file tree.  By changing the first delay parameter
  (del) you can change where in the file tree the hang occurs (I
  tested values of 0, 10, 30, 40, 50 and 100) however the position of
  the hang is not predictable for different delays (the hang occurred
  at an earlier point with delay == 100 than with and other delay). By
  changing the second delay parameter it is possible to get the system
  to work (see below). When I included the "#ifdef TEST" lines it
  appeared that the Read() call is where the lockup takes place.

     mytest c2: 3 0  5   hung
     mytest c2: 3 0 25   hung
     mytest c2: 3 5 25   worked sometimes, hung other times
     mytest c2: 3 25 5   hung
     mytest c2: 3 25 0   hung
     mytest c2: 3 5  5   hung
     mytest c2: 3 0  0   hung

  Perhaps the system is returning from the Open() call too soon and
  the Read() then gets confused by an incorrectly/incompletely
  opened file?  It also appears that the system is returning from the
  Read() call before it completes since the scanning (files/sec)
  is faser for the "3 25 5" case than the "3 5 25" case.
*****************************************/

#include <stdio.h>
#include <libraries/dos.h>
#include <exec/memory.h>
#include <workbench/workbench.h>
#include <functions.h>
#include <stdio.h>

long del, del2;

ScanDir(lock, dirname, mode)
ULONG lock;
char *dirname;
int mode;
{
  ULONG tlock;
  char tdir[256], t;
  struct FileInfoBlock *fib;
  ULONG in;
  UBYTE *mem;
  long size;

  fib = (struct FileInfoBlock *) AllocMem((long) sizeof(struct FileInfoBlock), 0L);
  if (fib)
  {
    if (Examine(lock, fib))
    {
      while (ExNext(lock, fib))
      {
        strcpy(tdir, dirname);
        if (strlen(tdir))
        {
          t = tdir[strlen(tdir) - 1];
          if ((t != '/') && (t != ':')) strcat(tdir, "/");
        }
        strcat(tdir, fib->fib_FileName);
        if (fib->fib_DirEntryType > 0)
        {
          tlock = (ULONG) Lock(tdir, ACCESS_READ);
          ScanDir(tlock, tdir, mode);  /** it was a directory so recurse **/
          UnLock(tlock);
        }
        else
        {
          if (mode > 0)
          {
            size = fib->fib_Size;
            mem = (UBYTE *) AllocMem(size, MEMF_PUBLIC);
            if (mem)
            {
              if (mode > 1)
              {
                in = (ULONG) Open(tdir, MODE_OLDFILE);
                if (in)
                {
                  printf("%s %ld\n", tdir, size);
                  if (del2) Delay(del2);
                  if (mode > 2)
                  {
#ifdef TEST
                    putchar('-'); fflush(stdout); Delay(5L);
#endif
                    Read(in, mem, size);
#ifdef TEST
                    putchar('-'); fflush(stdout); Delay(5L);
#endif
                  }
                  if (del) Delay(del);
                  Close(in);
                }
              }
              else
                printf("mem only %s %ld\n", tdir, size);
              FreeMem(mem, size);
            }
          }
          else
            printf("%s\n", tdir);
        }
      }
    }
    FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  }
}

main(argc, argv)
int argc;
char *argv[];
{
  ULONG root;
  struct FileInfoBlock *fib;
  int mode;

  fib = (struct FileInfoBlock *) AllocMem((long) sizeof(struct FileInfoBlock), 0L);
  if (fib)
  {
    if ((argc != 3) && (argc != 4) && (argc != 5))
    {
      puts("Syntax:  mytest directory mode [delay1 [delay2]] ");
      puts("mode == 0 for directory scan only");
      puts("     == 1 for directory scan plus mem alloc/free");
      puts("     == 2 for dir scan plus alloc/open/close/free");
      puts("     == 3 for dir scan plus alloc/open/read/close/free");
      puts("if delay is specified (n/50 secs) the system will delay defore");
      puts("closing the file.  Delay2 is the delay after opening the file");
      puts("before a read operation will be attempted.");
    }
    else
    {
      mode = atoi(argv[2]);
      if (argc == 4)
      {
        del = atoi(argv[3]);
      }
      else
        del = 0;
      if (argc == 5)
      {
        del2 = atoi(argv[4]);
      }
      else
        del2 = 0;

      root = (ULONG) Lock(argv[1], ACCESS_READ);
      if (Examine(root, fib))
      {
        if (fib->fib_DirEntryType > 0)
          ScanDir(root, argv[1], mode);
      }
      UnLock(root);
    }
    FreeMem(fib, (long) sizeof(struct FileInfoBlock));
  }
}

