Subversion Repositories shark

Rev

Go to most recent revision | Details | 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
#define sem_signal sem_post
29
 
30
int noscroll=0;
31
SEM console;
32
 
33
#define MPROC    ((50<(MAX_PROC-10))?50:MAX_PROC-10)
34
 
35
TASK viewdir(void *prof);
36
sem_t actmutex,actsync;
37
char *globpathname;
38
PID globpid;
39
int counter=0,actcounter=0;
40
 
41
void activate_task(int prof, char *pathname)
42
{
43
  char tname[32];
44
  NRT_TASK_MODEL m;
45
  PID pid;
46
 
47
REPEAT:
48
  sem_wait(&actmutex);
49
 
50
  if (actcounter>=MPROC) {
51
    struct timespec delay;
52
    delay.tv_sec = 0;
53
    delay.tv_nsec = 10000000;
54
    sem_signal(&actmutex);
55
    nanosleep(&delay, NULL);
56
    goto REPEAT;
57
  }
58
 
59
  globpathname=pathname;
60
  counter++;
61
  sprintf(tname,"tsk%i",counter);
62
 
63
  nrt_task_default_model(m);
64
  nrt_task_def_arg(m,(void*)counter);
65
 
66
  globpid=pid=task_create(tname,viewdir,&m,NULL);
67
  if (pid==-1) {
68
    sem_wait(&console);
69
    cprintf("can't create '%s'\n",tname);
70
    perror("can't create task");
71
    sem_signal(&console);
72
    sys_end();
73
    return;
74
  }
75
  task_activate(pid);
76
  sem_wait(&actsync);
77
  actcounter++;
78
 
79
  sem_signal(&actmutex);
80
}
81
 
82
/*
83
 *
84
 */
85
 
86
int filecounter=0;
87
 
88
TASK viewdir(void *pointer)
89
{
90
  struct dirent *den;
91
  struct stat st;
92
  char *str;
93
  DIR *d;
94
  int res;
95
  int x;
96
  char pathname[1024];
97
  PID mypid;
98
  int prof=(int)pointer;
99
 
100
  strcpy(pathname,globpathname);
101
  mypid=globpid;
102
  sem_signal(&actsync);  
103
 
104
  str=pathname+(x=strlen(pathname));
105
  d=opendir(pathname);
106
 
107
  if (d==NULL) {
108
    sem_wait(&console);
109
    cprintf("%03i ERR: can't open dir %s\n",prof,pathname);
110
    cprintf("errno: %i '%s'\n",errno,strerror(errno));
111
    sem_signal(&console);
112
 
113
    sys_end();
114
    l1_exit(0);
115
 
116
    goto END;
117
  }
118
 
119
  while ((den=readdir(d))!=NULL) {
120
 
121
    if (x==1&&*pathname=='/')
122
      strcat(pathname,den->d_name);
123
    else
124
      strcat(strcat(pathname,"/"),den->d_name);
125
 
126
    sem_wait(&console);
127
    if (noscroll) {
128
      place(0,10);
129
      cprintf("                                                           ");
130
      place(0,10);
131
    }
132
    cprintf("t%03i %s\n",prof,pathname);
133
    filecounter++;
134
    sem_signal(&console);
135
 
136
    if (!strcmp(den->d_name,".")) goto SKIP;
137
    if (!strcmp(den->d_name,"..")) goto SKIP;
138
 
139
    res=stat(pathname,&st);
140
    if (res!=0) {
141
      sem_wait(&console);
142
      cprintf("t%03i can't stat %s\n",prof,pathname);
143
      cprintf("errno: %i '%s'\n",errno,strerror(errno));
144
      sem_signal(&console);
145
 
146
      sys_end();
147
      l1_exit(0);
148
 
149
      closedir(d);
150
      goto END;
151
    } else {          
152
      if (S_ISDIR(st.st_mode)) {
153
        sem_wait(&console);
154
        sem_signal(&console);
155
        activate_task(prof,pathname);
156
      }
157
 
158
    }
159
 
160
  SKIP:
161
    *str='\0';
162
  }
163
 
164
  closedir(d);
165
 
166
END:
167
  sem_wait(&actmutex);
168
  actcounter--;
169
  sem_signal(&actmutex);
170
 
171
  return 0;
172
}
173
 
174
/*
175
 *
176
 */
177
 
178
int __register_sub_init_prologue(void)
179
{
180
  TRC_CIRCULAR_PARMS args;
181
  int q;
182
 
183
  TRC_init_phase1(NULL);
184
  trc_register_circular_queue();
185
 
186
  trc_circular_default_parms(args);
187
  trc_circular_def_onlinetask(args);
188
  q=trc_create_queue(TRC_CIRCULAR_QUEUE,&args);
189
 
190
  trc_assign_class_to_queue(TRC_CLASS_SYSTEM,q);
191
  trc_trace_class(TRC_CLASS_SYSTEM);
192
 
193
  return 0;
194
}
195
 
196
/*
197
 *
198
 */
199
 
200
int main(int argc,char *argv[])
201
{
202
  //  int res;
203
 
204
  showmessage("This test show all filenames of a directory of an hardisk\n"
205
              "recursively using a soft task for every directory.\n"
206
              "The tracer with a circular queue is activated that write\n"
207
              "on hardisk online (while the system is running)\n");
208
 
209
  sem_init(&console,0,1);
210
  sem_init(&actmutex,0,1);
211
  sem_init(&actsync,0,0);
212
 
213
  activate_task(-1,FROMDIR);
214
  //activate_task(-1,"/");
215
 
216
  {
217
    struct timespec delay;
218
    delay.tv_sec = 0;
219
    delay.tv_nsec = 500000000;
220
    for(;;) {
221
      sem_wait(&actmutex);
222
      if (actcounter==0) break;
223
      sem_signal(&actmutex);
224
      nanosleep(&delay, NULL);
225
    }
226
  }
227
 
228
  cprintf("\nfiles: %i\n",filecounter);
229
 
230
  waitend();
231
 
232
  return 0;
233
}