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