Subversion Repositories shark

Rev

Rev 1655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 1
/*
2
 *
3
 *
4
 *
5
 */
6
 
7
#include <kernel/func.h>
8
#include <kernel/model.h>
9
 
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <sys/mount.h>
13
#include <dirent.h>
14
#include <fcntl.h>
15
#include <unistd.h>
16
#include <errno.h>
17
#include <string.h>
18
#include <stdlib.h>
19
#include <semaphore.h>
20
#include <stdio.h>
21
#include <time.h>
22
 
23
#include "common.h"
24
 
25
#include <trace/trace.h>
26
#include <trace/queues.h>
27
 
28
 
29
int noscroll=0;
30
SEM console;
31
 
32
#define MPROC    ((50<(MAX_PROC-10))?50:MAX_PROC-10)
33
 
34
TASK viewdir(void *prof);
35
sem_t actmutex,actsync;
36
char *globpathname;
37
PID globpid;
38
int counter=0,actcounter=0;
39
 
40
void activate_task(int prof, char *pathname)
41
{
42
  char tname[32];
43
  NRT_TASK_MODEL m;
44
  PID pid;
45
 
46
REPEAT:
47
  sem_wait(&actmutex);
48
 
49
  if (actcounter>=MPROC) {
50
    struct timespec delay;
51
    delay.tv_sec = 0;
52
    delay.tv_nsec = 10000000;
53
    sem_signal(&actmutex);
54
    nanosleep(&delay, NULL);
55
    goto REPEAT;
56
  }
57
 
58
  globpathname=pathname;
59
  counter++;
60
  sprintf(tname,"tsk%i",counter);
61
 
62
  nrt_task_default_model(m);
63
  nrt_task_def_arg(m,(void*)counter);
64
 
65
  globpid=pid=task_create(tname,viewdir,&m,NULL);
66
  if (pid==-1) {
67
    sem_wait(&console);
68
    cprintf("can't create '%s'\n",tname);
69
    perror("can't create task");
70
    sem_signal(&console);
71
    sys_end();
72
    return;
73
  }
74
  task_activate(pid);
75
  sem_wait(&actsync);
76
  actcounter++;
77
 
78
  sem_signal(&actmutex);
79
}
80
 
81
/*
82
 *
83
 */
84
 
85
int filecounter=0;
86
 
87
TASK viewdir(void *pointer)
88
{
89
  struct dirent *den;
90
  struct stat st;
91
  char *str;
92
  DIR *d;
93
  int res;
94
  int x;
95
  char pathname[1024];
96
  PID mypid;
97
  int prof=(int)pointer;
98
 
99
  strcpy(pathname,globpathname);
100
  mypid=globpid;
101
  sem_signal(&actsync);  
102
 
103
  str=pathname+(x=strlen(pathname));
104
  d=opendir(pathname);
105
 
106
  if (d==NULL) {
107
    sem_wait(&console);
108
    cprintf("%03i ERR: can't open dir %s\n",prof,pathname);
109
    cprintf("errno: %i '%s'\n",errno,strerror(errno));
110
    sem_signal(&console);
111
 
112
    sys_end();
113
    l1_exit(0);
114
 
115
    goto END;
116
  }
117
 
118
  while ((den=readdir(d))!=NULL) {
119
 
120
    if (x==1&&*pathname=='/')
121
      strcat(pathname,den->d_name);
122
    else
123
      strcat(strcat(pathname,"/"),den->d_name);
124
 
125
    sem_wait(&console);
126
    if (noscroll) {
127
      place(0,10);
128
      cprintf("                                                           ");
129
      place(0,10);
130
    }
131
    cprintf("t%03i %s\n",prof,pathname);
132
    filecounter++;
133
    sem_signal(&console);
134
 
135
    if (!strcmp(den->d_name,".")) goto SKIP;
136
    if (!strcmp(den->d_name,"..")) goto SKIP;
137
 
138
    res=stat(pathname,&st);
139
    if (res!=0) {
140
      sem_wait(&console);
141
      cprintf("t%03i can't stat %s\n",prof,pathname);
142
      cprintf("errno: %i '%s'\n",errno,strerror(errno));
143
      sem_signal(&console);
144
 
145
      sys_end();
146
      l1_exit(0);
147
 
148
      closedir(d);
149
      goto END;
150
    } else {          
151
      if (S_ISDIR(st.st_mode)) {
152
        sem_wait(&console);
153
        sem_signal(&console);
154
        activate_task(prof,pathname);
155
      }
156
 
157
    }
158
 
159
  SKIP:
160
    *str='\0';
161
  }
162
 
163
  closedir(d);
164
 
165
END:
166
  sem_wait(&actmutex);
167
  actcounter--;
168
  sem_signal(&actmutex);
169
 
170
  return 0;
171
}
172
 
173
/*
174
 *
175
 */
176
 
177
int __register_sub_init_prologue(void)
178
{
179
  TRC_init_phase1(NULL);
180
  trc_register_circular_queue();
181
  trc_create_queue(TRC_CIRCULAR_QUEUE,NULL);
182
  return 0;
183
}
184
 
185
/*
186
 *
187
 */
188
 
189
int main(int argc,char *argv[])
190
{
191
  //  int res;
192
 
193
  showmessage("This test show all filenames of a directory of an hardisk\n"
194
              "recursively using a soft task for every directory.\n"
195
              "The tracer with a circular queue is activated.\n");
196
 
197
  sem_init(&console,0,1);
198
  sem_init(&actmutex,0,1);
199
  sem_init(&actsync,0,0);
200
 
201
  activate_task(-1,FROMDIR);
202
 
203
  {
204
    struct timespec delay;
205
    delay.tv_sec = 0;
206
    delay.tv_nsec = 500000000;
207
    for(;;) {
208
      sem_wait(&actmutex);
209
      if (actcounter==0) break;
210
      sem_signal(&actmutex);
211
      nanosleep(&delay, NULL);
212
    }
213
  }
214
 
215
  cprintf("\nfiles: %i\n",filecounter);
216
 
217
  waitend();
218
 
219
  return 0;
220
}