Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 *
 *
 *
 */


#include <kernel/func.h>
#include <kernel/model.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <semaphore.h>
#include <stdio.h>
#include <time.h>

#include "common.h"

#include <trace/trace.h>
#include <trace/queues.h>


int noscroll=0;
SEM console;

#define MPROC    ((50<(MAX_PROC-10))?50:MAX_PROC-10)

TASK viewdir(void *prof);
sem_t actmutex,actsync;
char *globpathname;
PID globpid;
int counter=0,actcounter=0;

void activate_task(int prof, char *pathname)
{
  char tname[32];
  NRT_TASK_MODEL m;
  PID pid;

REPEAT:
  sem_wait(&actmutex);

  if (actcounter>=MPROC) {
    struct timespec delay;
    delay.tv_sec = 0;
    delay.tv_nsec = 10000000;
    sem_signal(&actmutex);
    nanosleep(&delay, NULL);
    goto REPEAT;
  }
 
  globpathname=pathname;
  counter++;
  sprintf(tname,"tsk%i",counter);

  nrt_task_default_model(m);
  nrt_task_def_arg(m,(void*)counter);
 
  globpid=pid=task_create(tname,viewdir,&m,NULL);
  if (pid==-1) {
    sem_wait(&console);
    cprintf("can't create '%s'\n",tname);
    perror("can't create task");
    sem_signal(&console);
    sys_end();
    return;
  }
  task_activate(pid);
  sem_wait(&actsync);
  actcounter++;

  sem_signal(&actmutex);
}

/*
 *
 */


int filecounter=0;

TASK viewdir(void *pointer)
{
  struct dirent *den;
  struct stat st;
  char *str;
  DIR *d;
  int res;
  int x;
  char pathname[1024];
  PID mypid;
  int prof=(int)pointer;

  strcpy(pathname,globpathname);
  mypid=globpid;
  sem_signal(&actsync);  

  str=pathname+(x=strlen(pathname));
  d=opendir(pathname);
 
  if (d==NULL) {
    sem_wait(&console);
    cprintf("%03i ERR: can't open dir %s\n",prof,pathname);
    cprintf("errno: %i '%s'\n",errno,strerror(errno));
    sem_signal(&console);

    sys_end();
    l1_exit(0);
   
    goto END;
  }

  while ((den=readdir(d))!=NULL) {

    if (x==1&&*pathname=='/')
      strcat(pathname,den->d_name);
    else
      strcat(strcat(pathname,"/"),den->d_name);

    sem_wait(&console);
    if (noscroll) {
      place(0,10);
      cprintf("                                                           ");
      place(0,10);
    }
    cprintf("t%03i %s\n",prof,pathname);
    filecounter++;
    sem_signal(&console);

    if (!strcmp(den->d_name,".")) goto SKIP;
    if (!strcmp(den->d_name,"..")) goto SKIP;

    res=stat(pathname,&st);
    if (res!=0) {
      sem_wait(&console);
      cprintf("t%03i can't stat %s\n",prof,pathname);
      cprintf("errno: %i '%s'\n",errno,strerror(errno));
      sem_signal(&console);

      sys_end();
      l1_exit(0);
     
      closedir(d);
      goto END;
    } else {          
      if (S_ISDIR(st.st_mode)) {
        sem_wait(&console);
        sem_signal(&console);
        activate_task(prof,pathname);
      }
     
    }

  SKIP:
    *str='\0';
  }

  closedir(d);

END:
  sem_wait(&actmutex);
  actcounter--;
  sem_signal(&actmutex);

  return 0;
}

/*
 *
 */


int __register_sub_init_prologue(void)
{
  TRC_init_phase1(NULL);
  trc_register_circular_queue();
  trc_create_queue(TRC_CIRCULAR_QUEUE,NULL);
  return 0;
}

/*
 *
 */


int main(int argc,char *argv[])
{
  //  int res;

  showmessage("This test show all filenames of a directory of an hardisk\n"
              "recursively using a soft task for every directory.\n"
              "The tracer with a circular queue is activated.\n");

  sem_init(&console,0,1);
  sem_init(&actmutex,0,1);
  sem_init(&actsync,0,0);

  activate_task(-1,FROMDIR);

  {
    struct timespec delay;
    delay.tv_sec = 0;
    delay.tv_nsec = 500000000;
    for(;;) {
      sem_wait(&actmutex);
      if (actcounter==0) break;
      sem_signal(&actmutex);
      nanosleep(&delay, NULL);
    }
  }

  cprintf("\nfiles: %i\n",filecounter);
 
  waitend();
 
  return 0;
}