1 | #ifndef COPYFILE_H |
---|
2 | #define COPYFILE_H |
---|
3 | |
---|
4 | int countfiles(char* dirname, int* count, int first) |
---|
5 | { |
---|
6 | int ret = 0; |
---|
7 | DIR *d; |
---|
8 | char* tmpstr = NULL; |
---|
9 | |
---|
10 | //Open the directory specified by dirname |
---|
11 | d = opendir(dirname); |
---|
12 | |
---|
13 | //Check it was opened |
---|
14 | if(! d) |
---|
15 | { |
---|
16 | perr("Cannot open directory %s", dirname); |
---|
17 | return 1; |
---|
18 | } |
---|
19 | |
---|
20 | while(1) |
---|
21 | { |
---|
22 | struct dirent* entry; |
---|
23 | int path_length; |
---|
24 | char path[PATH_MAX]; |
---|
25 | |
---|
26 | snprintf(path, PATH_MAX, "%s", dirname); |
---|
27 | //Readdir gets subsequent entries from d |
---|
28 | entry = readdir(d); |
---|
29 | |
---|
30 | if(!entry) //There are no more entries in this directory, so break out of the while loop |
---|
31 | break; |
---|
32 | |
---|
33 | //for nfs mounts if file type is unknown use stat |
---|
34 | if(entry->d_type == DT_UNKNOWN) |
---|
35 | { |
---|
36 | tmpstr = ostrcat(dirname, "/", 0, 0); |
---|
37 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
38 | entry->d_type = getlfiletype(tmpstr); |
---|
39 | free(tmpstr); tmpstr = NULL; |
---|
40 | } |
---|
41 | |
---|
42 | //See if entry is a subdirectory of d |
---|
43 | if(entry->d_type == DT_DIR) |
---|
44 | { |
---|
45 | //Check that the directory is not d or d's parent |
---|
46 | if(entry->d_name != NULL && ostrcmp(entry->d_name, ".") != 0 && ostrcmp(entry->d_name, "..") != 0) |
---|
47 | { |
---|
48 | (*count)++; |
---|
49 | path_length = snprintf(path, PATH_MAX, "%s/%s", dirname, entry->d_name); |
---|
50 | if(path_length >= PATH_MAX) |
---|
51 | { |
---|
52 | err("path length has got too long"); |
---|
53 | ret = 1; |
---|
54 | break; |
---|
55 | } |
---|
56 | //Recursively call findfiles with the new path |
---|
57 | countfiles(path, count, 0); |
---|
58 | } |
---|
59 | } |
---|
60 | else //File |
---|
61 | { |
---|
62 | if(entry->d_type == DT_LNK) |
---|
63 | (*count)++; |
---|
64 | else if(entry->d_type == DT_BLK) |
---|
65 | (*count)++; |
---|
66 | else if(entry->d_type == DT_CHR) |
---|
67 | (*count)++; |
---|
68 | else if(entry->d_type == DT_FIFO) |
---|
69 | (*count)++; |
---|
70 | else if(entry->d_type == DT_REG) |
---|
71 | (*count)++; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | //After going through all the entries, close the directory |
---|
76 | if(d && closedir(d)) |
---|
77 | { |
---|
78 | perr("Could not close %s", dirname); |
---|
79 | ret = 1; |
---|
80 | } |
---|
81 | |
---|
82 | return ret; |
---|
83 | } |
---|
84 | |
---|
85 | //flag 0: copy file |
---|
86 | //flag 1: move file |
---|
87 | int copyfilereal(char* from, char* to, struct copyfile* node, int flag) |
---|
88 | { |
---|
89 | int fdfrom = -1, fdto = -1, ret = 0, readret = 0, writeret = 0; |
---|
90 | off64_t count = 0, len = 0; |
---|
91 | unsigned char* buf = NULL; |
---|
92 | |
---|
93 | fdfrom = open(from, O_RDONLY | O_LARGEFILE); |
---|
94 | if(fdfrom < 0) |
---|
95 | { |
---|
96 | perr("open from %s", from); |
---|
97 | ret = 1; |
---|
98 | goto end; |
---|
99 | } |
---|
100 | |
---|
101 | ret = unlink(to); |
---|
102 | if(ret != 0 && errno != ENOENT) |
---|
103 | { |
---|
104 | perr("remove file %s", to); |
---|
105 | ret = 1; |
---|
106 | goto end; |
---|
107 | } |
---|
108 | else |
---|
109 | ret = 0; |
---|
110 | |
---|
111 | fdto = open(to, O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE, 0777); |
---|
112 | if(fdto < 0) |
---|
113 | { |
---|
114 | perr("open to %s", to); |
---|
115 | ret = 1; |
---|
116 | goto end; |
---|
117 | } |
---|
118 | |
---|
119 | buf = malloc(MINMALLOC); |
---|
120 | if(buf == NULL) |
---|
121 | { |
---|
122 | err("no mem"); |
---|
123 | ret = 1; |
---|
124 | goto end; |
---|
125 | } |
---|
126 | |
---|
127 | len = lseek64(fdfrom, 0, SEEK_END); |
---|
128 | if(len < 0) |
---|
129 | { |
---|
130 | perr("can't get filelen %s", from); |
---|
131 | ret = 1; |
---|
132 | goto end; |
---|
133 | } |
---|
134 | |
---|
135 | if(lseek64(fdfrom, 0, SEEK_SET) < 0) |
---|
136 | { |
---|
137 | perr("can't seek to startpos %s", from); |
---|
138 | ret = 1; |
---|
139 | goto end; |
---|
140 | } |
---|
141 | |
---|
142 | if(node != NULL) node->maxkb += len; |
---|
143 | |
---|
144 | while(len != 0) |
---|
145 | { |
---|
146 | if(len - count > MINMALLOC) |
---|
147 | readret = dvbreadfd(fdfrom, buf, 0, MINMALLOC, -1, 1); |
---|
148 | else |
---|
149 | readret = dvbreadfd(fdfrom, buf, 0, len - count, -1, 1); |
---|
150 | if(readret <= 0) |
---|
151 | { |
---|
152 | err("read file %s", from); |
---|
153 | ret = 1; |
---|
154 | goto end; |
---|
155 | } |
---|
156 | |
---|
157 | if(node != NULL && node->stop == 1) |
---|
158 | { |
---|
159 | ret = 1; |
---|
160 | goto end; |
---|
161 | } |
---|
162 | |
---|
163 | writeret = dvbwrite(fdto, buf, readret, -1); |
---|
164 | if(writeret != readret) |
---|
165 | { |
---|
166 | err("write file %s", to); |
---|
167 | ret = 1; |
---|
168 | goto end; |
---|
169 | } |
---|
170 | |
---|
171 | if(node != NULL && node->stop == 1) |
---|
172 | { |
---|
173 | ret = 1; |
---|
174 | goto end; |
---|
175 | } |
---|
176 | |
---|
177 | count += readret; |
---|
178 | if(node != NULL) |
---|
179 | { |
---|
180 | node->aktkb += readret; |
---|
181 | if(node->maxfilecount == 1) |
---|
182 | node->proz = (int)ceil((((float)node->aktkb / node->maxkb) * 100)); |
---|
183 | } |
---|
184 | |
---|
185 | if(count == len) break; |
---|
186 | if(count > len) |
---|
187 | { |
---|
188 | err("write more then filelen %s", to); |
---|
189 | ret = 1; |
---|
190 | goto end; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | end: |
---|
195 | if(fdfrom >= 0) |
---|
196 | close(fdfrom); |
---|
197 | if(fdto >= 0) |
---|
198 | { |
---|
199 | close(fdto); |
---|
200 | if(ret == 1) unlink(to); |
---|
201 | } |
---|
202 | if(ret == 0 && flag == 1) |
---|
203 | { |
---|
204 | ret = unlink(from); |
---|
205 | if(ret != 0 && errno != ENOENT) |
---|
206 | { |
---|
207 | perr("remove file %s", from); |
---|
208 | unlink(to); |
---|
209 | ret = 1; |
---|
210 | } |
---|
211 | else |
---|
212 | ret = 0; |
---|
213 | } |
---|
214 | free(buf); |
---|
215 | |
---|
216 | if(node != NULL) |
---|
217 | { |
---|
218 | node->filecount++; |
---|
219 | if(node->maxfilecount > 1) |
---|
220 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
221 | } |
---|
222 | return ret; |
---|
223 | } |
---|
224 | |
---|
225 | //flag 0: copy file |
---|
226 | //flag 1: move file |
---|
227 | int copylink(char* from, char* to, struct copyfile* node, int flag) |
---|
228 | { |
---|
229 | int ret = 0; |
---|
230 | |
---|
231 | if(from == NULL || to == NULL) return 1; |
---|
232 | |
---|
233 | char* buf = calloc(1, MINMALLOC); |
---|
234 | if(buf == NULL) |
---|
235 | { |
---|
236 | err("no mem"); |
---|
237 | return 1; |
---|
238 | } |
---|
239 | |
---|
240 | ret = readlink(from, buf, MINMALLOC); |
---|
241 | if(ret < 0) |
---|
242 | { |
---|
243 | perr("read link %s", from); |
---|
244 | free(buf); buf = NULL; |
---|
245 | return 1; |
---|
246 | } |
---|
247 | |
---|
248 | ret = tpkcreatelink("", to, buf, 0); |
---|
249 | if(ret != 0) |
---|
250 | { |
---|
251 | free(buf); buf = NULL; |
---|
252 | err("create link"); |
---|
253 | return 1; |
---|
254 | } |
---|
255 | |
---|
256 | if(flag == 1) |
---|
257 | { |
---|
258 | ret = unlink(from); |
---|
259 | if(ret != 0 && errno != ENOENT) |
---|
260 | { |
---|
261 | perr("remove file %s", from); |
---|
262 | free(buf); buf = NULL; |
---|
263 | return 1; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | if(node != NULL) |
---|
268 | { |
---|
269 | node->filecount++; |
---|
270 | if(node->maxfilecount > 1) |
---|
271 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
272 | } |
---|
273 | free(buf); buf = NULL; |
---|
274 | return 0; |
---|
275 | } |
---|
276 | |
---|
277 | //flag 0: copy file |
---|
278 | //flag 1: move file |
---|
279 | int copyblk(char* from, char* to, struct copyfile* node, int flag) |
---|
280 | { |
---|
281 | int ret = 0; |
---|
282 | struct stat64 s; |
---|
283 | |
---|
284 | if(from == NULL || to == NULL) return 1; |
---|
285 | |
---|
286 | ret = stat64(from, &s); |
---|
287 | if(ret != 0) |
---|
288 | { |
---|
289 | perr("get file status %s", from); |
---|
290 | return 1; |
---|
291 | } |
---|
292 | |
---|
293 | ret = tpkcreateblk("", to, major(s.st_rdev), minor(s.st_rdev), 0); |
---|
294 | if(ret != 0) |
---|
295 | { |
---|
296 | err("create blk dev"); |
---|
297 | return 1; |
---|
298 | } |
---|
299 | |
---|
300 | if(flag == 1) |
---|
301 | { |
---|
302 | ret = unlink(from); |
---|
303 | if(ret != 0 && errno != ENOENT) |
---|
304 | { |
---|
305 | perr("remove file %s", from); |
---|
306 | return 1; |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | if(node != NULL) |
---|
311 | { |
---|
312 | node->filecount++; |
---|
313 | if(node->maxfilecount > 1) |
---|
314 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
315 | } |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | //flag 0: copy file |
---|
320 | //flag 1: move file |
---|
321 | int copychr(char* from, char* to, struct copyfile* node, int flag) |
---|
322 | { |
---|
323 | int ret = 0; |
---|
324 | struct stat64 s; |
---|
325 | |
---|
326 | if(from == NULL || to == NULL) return 1; |
---|
327 | |
---|
328 | ret = stat64(from, &s); |
---|
329 | if(ret != 0) |
---|
330 | { |
---|
331 | perr("get file status %s", from); |
---|
332 | return 1; |
---|
333 | } |
---|
334 | |
---|
335 | ret = tpkcreatechr("", to, major(s.st_rdev), minor(s.st_rdev), 0); |
---|
336 | if(ret != 0) |
---|
337 | { |
---|
338 | err("create chr dev"); |
---|
339 | return 1; |
---|
340 | } |
---|
341 | |
---|
342 | if(flag == 1) |
---|
343 | { |
---|
344 | ret = unlink(from); |
---|
345 | if(ret != 0 && errno != ENOENT) |
---|
346 | { |
---|
347 | perr("remove file %s", from); |
---|
348 | return 1; |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | if(node != NULL) |
---|
353 | { |
---|
354 | node->filecount++; |
---|
355 | if(node->maxfilecount > 1) |
---|
356 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
357 | } |
---|
358 | return 0; |
---|
359 | } |
---|
360 | |
---|
361 | //flag 0: copy file |
---|
362 | //flag 1: move file |
---|
363 | int copyfifo(char* from, char* to, struct copyfile* node, int flag) |
---|
364 | { |
---|
365 | int ret = 0; |
---|
366 | |
---|
367 | if(from == NULL || to == NULL) return 1; |
---|
368 | |
---|
369 | ret = tpkcreatefifo("", to, 0); |
---|
370 | if(ret != 0) |
---|
371 | { |
---|
372 | err("create fifo"); |
---|
373 | return 1; |
---|
374 | } |
---|
375 | |
---|
376 | if(flag == 1) |
---|
377 | { |
---|
378 | ret = unlink(from); |
---|
379 | if(ret != 0 && errno != ENOENT) |
---|
380 | { |
---|
381 | perr("remove file %s", from); |
---|
382 | return 1; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | if(node != NULL) |
---|
387 | { |
---|
388 | node->filecount++; |
---|
389 | if(node->maxfilecount > 1) |
---|
390 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
391 | } |
---|
392 | return 0; |
---|
393 | } |
---|
394 | |
---|
395 | //flag 0: copy file |
---|
396 | //flag 1: move file |
---|
397 | int copydir(char* dirfrom, char* dirto, struct copyfile* node, int first, int flag) |
---|
398 | { |
---|
399 | DIR *d; |
---|
400 | char* tmpstr = NULL, *tmpstr1 = NULL; |
---|
401 | int ret = 0; |
---|
402 | |
---|
403 | d = opendir(dirfrom); //Open the directory |
---|
404 | if(! d) //Check it was opened |
---|
405 | { |
---|
406 | perr("Cannot open directory %s", dirfrom); |
---|
407 | return 1; |
---|
408 | } |
---|
409 | |
---|
410 | while(1) |
---|
411 | { |
---|
412 | struct dirent* entry; |
---|
413 | int path_length = 0; |
---|
414 | char pathfrom[PATH_MAX]; |
---|
415 | char pathto[PATH_MAX]; |
---|
416 | |
---|
417 | snprintf(pathfrom, PATH_MAX, "%s", dirfrom); |
---|
418 | snprintf(pathto, PATH_MAX, "%s", dirto); |
---|
419 | entry = readdir(d); //Readdir gets subsequent entries from d |
---|
420 | |
---|
421 | if(!entry) //no more entries , so break |
---|
422 | break; |
---|
423 | |
---|
424 | //for nfs mounts if file type is unknown use stat |
---|
425 | if(entry->d_type == DT_UNKNOWN) |
---|
426 | { |
---|
427 | tmpstr = ostrcat(dirfrom, "/", 0, 0); |
---|
428 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
429 | entry->d_type = getlfiletype(tmpstr); |
---|
430 | free(tmpstr); tmpstr = NULL; |
---|
431 | } |
---|
432 | |
---|
433 | free(tmpstr); tmpstr = NULL; |
---|
434 | if(entry->d_type == DT_DIR) //dir |
---|
435 | { |
---|
436 | if(node != NULL && node->stop == 1) |
---|
437 | { |
---|
438 | ret = 1; |
---|
439 | break; |
---|
440 | } |
---|
441 | |
---|
442 | //Check that the directory is not d or d's parent |
---|
443 | if(entry->d_name != NULL && ostrcmp(entry->d_name, ".") != 0 && ostrcmp(entry->d_name, "..") != 0) |
---|
444 | { |
---|
445 | path_length = snprintf(pathfrom, PATH_MAX, "%s/%s", dirfrom, entry->d_name); |
---|
446 | if(path_length >= PATH_MAX) |
---|
447 | { |
---|
448 | err("path length has got too long"); |
---|
449 | ret = 1; |
---|
450 | break; |
---|
451 | } |
---|
452 | |
---|
453 | path_length = snprintf(pathto, PATH_MAX, "%s/%s", dirto, entry->d_name); |
---|
454 | if(path_length >= PATH_MAX) |
---|
455 | { |
---|
456 | err("path length has got too long"); |
---|
457 | ret = 1; |
---|
458 | break; |
---|
459 | } |
---|
460 | |
---|
461 | ret = tpkcreatedir("", pathto, 0); |
---|
462 | if(ret != 0) |
---|
463 | { |
---|
464 | err("create dir %s", pathto); |
---|
465 | ret = 1; |
---|
466 | break; |
---|
467 | } |
---|
468 | |
---|
469 | if(node != NULL) |
---|
470 | { |
---|
471 | node->filecount++; |
---|
472 | if(node->maxfilecount > 1) |
---|
473 | node->proz = (int)ceil((((float)node->filecount / node->maxfilecount) * 100)); |
---|
474 | } |
---|
475 | |
---|
476 | ret = copydir(pathfrom, pathto, node, 0, flag); //Recursively call with the new path |
---|
477 | if(ret != 0) |
---|
478 | { |
---|
479 | err("copydir %s", pathfrom); |
---|
480 | ret = 1; |
---|
481 | break; |
---|
482 | } |
---|
483 | |
---|
484 | if(flag == 1) |
---|
485 | { |
---|
486 | ret = rmdir(pathfrom); |
---|
487 | if(ret != 0 && errno != ENOENT) |
---|
488 | { |
---|
489 | perr("remove dir %s", pathfrom); |
---|
490 | ret = 1; |
---|
491 | break; |
---|
492 | } |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | else if(entry->d_type == DT_LNK) //link |
---|
497 | { |
---|
498 | if(node != NULL && node->stop == 1) |
---|
499 | { |
---|
500 | ret = 1; |
---|
501 | break; |
---|
502 | } |
---|
503 | |
---|
504 | tmpstr = ostrcat(tmpstr, pathfrom, 1, 0); |
---|
505 | tmpstr = ostrcat(tmpstr, "/", 1, 0); |
---|
506 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
507 | |
---|
508 | tmpstr1 = ostrcat(tmpstr1, pathto, 1, 0); |
---|
509 | tmpstr1 = ostrcat(tmpstr1, "/", 1, 0); |
---|
510 | tmpstr1 = ostrcat(tmpstr1, entry->d_name, 1, 0); |
---|
511 | |
---|
512 | if(copylink(tmpstr, tmpstr, node, flag) != 0) |
---|
513 | { |
---|
514 | free(tmpstr); tmpstr = NULL; |
---|
515 | free(tmpstr1); tmpstr1 = NULL; |
---|
516 | ret = 1; |
---|
517 | break; |
---|
518 | } |
---|
519 | |
---|
520 | free(tmpstr); tmpstr = NULL; |
---|
521 | free(tmpstr1); tmpstr1 = NULL; |
---|
522 | } |
---|
523 | else if(entry->d_type == DT_BLK) //block device |
---|
524 | { |
---|
525 | if(node != NULL && node->stop == 1) |
---|
526 | { |
---|
527 | ret = 1; |
---|
528 | break; |
---|
529 | } |
---|
530 | |
---|
531 | tmpstr = ostrcat(tmpstr, pathfrom, 1, 0); |
---|
532 | tmpstr = ostrcat(tmpstr, "/", 1, 0); |
---|
533 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
534 | |
---|
535 | tmpstr1 = ostrcat(tmpstr1, pathto, 1, 0); |
---|
536 | tmpstr1 = ostrcat(tmpstr1, "/", 1, 0); |
---|
537 | tmpstr1 = ostrcat(tmpstr1, entry->d_name, 1, 0); |
---|
538 | |
---|
539 | if(copyblk(tmpstr, tmpstr1, node, flag) != 0) |
---|
540 | { |
---|
541 | free(tmpstr); tmpstr = NULL; |
---|
542 | free(tmpstr1); tmpstr1 = NULL; |
---|
543 | ret = 1; |
---|
544 | break; |
---|
545 | } |
---|
546 | |
---|
547 | free(tmpstr); tmpstr = NULL; |
---|
548 | free(tmpstr1); tmpstr1 = NULL; |
---|
549 | } |
---|
550 | else if(entry->d_type == DT_CHR) //charcter device |
---|
551 | { |
---|
552 | if(node != NULL && node->stop == 1) |
---|
553 | { |
---|
554 | ret = 1; |
---|
555 | break; |
---|
556 | } |
---|
557 | |
---|
558 | tmpstr = ostrcat(tmpstr, pathfrom, 1, 0); |
---|
559 | tmpstr = ostrcat(tmpstr, "/", 1, 0); |
---|
560 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
561 | |
---|
562 | tmpstr1 = ostrcat(tmpstr1, pathto, 1, 0); |
---|
563 | tmpstr1 = ostrcat(tmpstr1, "/", 1, 0); |
---|
564 | tmpstr1 = ostrcat(tmpstr1, entry->d_name, 1, 0); |
---|
565 | |
---|
566 | if(copychr(tmpstr, tmpstr1, node, flag) != 0) |
---|
567 | { |
---|
568 | free(tmpstr); tmpstr = NULL; |
---|
569 | free(tmpstr1); tmpstr1 = NULL; |
---|
570 | ret = 1; |
---|
571 | break; |
---|
572 | } |
---|
573 | |
---|
574 | free(tmpstr); tmpstr = NULL; |
---|
575 | free(tmpstr1); tmpstr1 = NULL; |
---|
576 | } |
---|
577 | else if(entry->d_type == DT_FIFO) //fifo |
---|
578 | { |
---|
579 | if(node != NULL && node->stop == 1) |
---|
580 | { |
---|
581 | ret = 1; |
---|
582 | break; |
---|
583 | } |
---|
584 | |
---|
585 | tmpstr = ostrcat(tmpstr, pathfrom, 1, 0); |
---|
586 | tmpstr = ostrcat(tmpstr, "/", 1, 0); |
---|
587 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
588 | |
---|
589 | tmpstr1 = ostrcat(tmpstr1, pathto, 1, 0); |
---|
590 | tmpstr1 = ostrcat(tmpstr1, "/", 1, 0); |
---|
591 | tmpstr1 = ostrcat(tmpstr1, entry->d_name, 1, 0); |
---|
592 | |
---|
593 | if(copyfifo(tmpstr, tmpstr1, node, flag) != 0) |
---|
594 | { |
---|
595 | free(tmpstr); tmpstr = NULL; |
---|
596 | free(tmpstr1); tmpstr1 = NULL; |
---|
597 | ret = 1; |
---|
598 | break; |
---|
599 | } |
---|
600 | |
---|
601 | free(tmpstr); tmpstr = NULL; |
---|
602 | free(tmpstr1); tmpstr1 = NULL; |
---|
603 | } |
---|
604 | else if(entry->d_type == DT_REG) //file |
---|
605 | { |
---|
606 | if(node != NULL && node->stop == 1) |
---|
607 | { |
---|
608 | ret = 1; |
---|
609 | break; |
---|
610 | } |
---|
611 | |
---|
612 | tmpstr = ostrcat(tmpstr, pathfrom, 1, 0); |
---|
613 | tmpstr = ostrcat(tmpstr, "/", 1, 0); |
---|
614 | tmpstr = ostrcat(tmpstr, entry->d_name, 1, 0); |
---|
615 | |
---|
616 | tmpstr1 = ostrcat(tmpstr1, pathto, 1, 0); |
---|
617 | tmpstr1 = ostrcat(tmpstr1, "/", 1, 0); |
---|
618 | tmpstr1 = ostrcat(tmpstr1, entry->d_name, 1, 0); |
---|
619 | |
---|
620 | if(copyfilereal(tmpstr, tmpstr1, node, flag) != 0) |
---|
621 | { |
---|
622 | free(tmpstr); tmpstr = NULL; |
---|
623 | free(tmpstr1); tmpstr1 = NULL; |
---|
624 | ret = 1; |
---|
625 | break; |
---|
626 | } |
---|
627 | |
---|
628 | free(tmpstr); tmpstr = NULL; |
---|
629 | free(tmpstr1); tmpstr1 = NULL; |
---|
630 | } |
---|
631 | else //unknown |
---|
632 | { |
---|
633 | if(node != NULL && node->stop == 1) |
---|
634 | { |
---|
635 | ret = 1; |
---|
636 | break; |
---|
637 | } |
---|
638 | |
---|
639 | err("unknown filetype found"); |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | if(d && closedir(d)) |
---|
644 | { |
---|
645 | perr("Could not close %s", dirfrom); |
---|
646 | ret = 1; |
---|
647 | } |
---|
648 | |
---|
649 | return ret; |
---|
650 | } |
---|
651 | |
---|
652 | //flag 0: copy file |
---|
653 | //flag 1: move file |
---|
654 | int copyfile(char* from, char* to, struct copyfile* node, int flag) |
---|
655 | { |
---|
656 | char* tmpto = NULL, *tmpstr = NULL, *bname = NULL; |
---|
657 | int ret = 1, fromtype = 0, totype = 0; |
---|
658 | |
---|
659 | if(from == NULL || to == NULL || ostrcmp(from, to) == 0) |
---|
660 | { |
---|
661 | if(node != NULL) node->ret = 1; |
---|
662 | return 1; |
---|
663 | } |
---|
664 | |
---|
665 | fromtype = getlfiletype(from); |
---|
666 | totype = getlfiletype(to); |
---|
667 | |
---|
668 | tmpto = ostrcat(to, NULL, 0, 0); |
---|
669 | |
---|
670 | //check if link is a dir |
---|
671 | if(totype == DT_LNK) |
---|
672 | { |
---|
673 | char* rpath = realpath(tmpto, NULL); |
---|
674 | if(rpath != NULL) |
---|
675 | { |
---|
676 | if(getlfiletype(rpath) == DT_DIR) |
---|
677 | { |
---|
678 | free(tmpto); tmpto = NULL; |
---|
679 | tmpto = ostrcat(rpath, NULL, 0, 0); |
---|
680 | totype = DT_DIR; |
---|
681 | } |
---|
682 | } |
---|
683 | free(rpath); rpath = NULL; |
---|
684 | } |
---|
685 | |
---|
686 | if((fromtype == DT_LNK || fromtype == DT_BLK || fromtype == DT_CHR || fromtype == DT_FIFO || fromtype == DT_REG) && totype == DT_DIR) |
---|
687 | { |
---|
688 | tmpstr = ostrcat(from, NULL, 0, 0); |
---|
689 | if(tmpstr != NULL) |
---|
690 | bname = basename(tmpstr); |
---|
691 | tmpto = ostrcat(tmpto, "/", 1, 0); |
---|
692 | tmpto = ostrcat(tmpto, bname, 1, 0); |
---|
693 | free(tmpstr); tmpstr = NULL; |
---|
694 | |
---|
695 | totype = fromtype; |
---|
696 | } |
---|
697 | |
---|
698 | if(fromtype == DT_DIR && totype == DT_DIR) |
---|
699 | ret = copydir(from, tmpto, node, 1, flag); |
---|
700 | else if(fromtype == DT_LNK && totype != DT_DIR) |
---|
701 | ret = copylink(from, tmpto, node, flag); |
---|
702 | else if(fromtype == DT_BLK && totype != DT_DIR) |
---|
703 | ret = copyblk(from, tmpto, node, flag); |
---|
704 | else if(fromtype == DT_CHR && totype != DT_DIR) |
---|
705 | ret = copychr(from, tmpto, node, flag); |
---|
706 | else if(fromtype == DT_FIFO && totype != DT_DIR) |
---|
707 | ret = copyfifo(from, tmpto, node, flag); |
---|
708 | else if(fromtype == DT_REG && totype != DT_DIR) |
---|
709 | ret = copyfilereal(from, tmpto, node, flag); |
---|
710 | else |
---|
711 | err("copy not allowed"); |
---|
712 | |
---|
713 | free(tmpto); tmpto = NULL; |
---|
714 | if(node != NULL) node->ret = ret; |
---|
715 | return ret; |
---|
716 | } |
---|
717 | |
---|
718 | void copyfilestruct(struct stimerthread* timernode, struct copyfile* node, int flag) |
---|
719 | { |
---|
720 | if(node != NULL) |
---|
721 | copyfile(node->from, node->to, node, 0); |
---|
722 | } |
---|
723 | |
---|
724 | void movefilestruct(struct stimerthread* timernode, struct copyfile* node, int flag) |
---|
725 | { |
---|
726 | if(node != NULL) |
---|
727 | copyfile(node->from, node->to, node, 1); |
---|
728 | } |
---|
729 | |
---|
730 | //flag 0: copy |
---|
731 | //flag 1: move |
---|
732 | int screencopy(char* title, char* from, char* to, int flag) |
---|
733 | { |
---|
734 | int rcret = -1, count = 0, ret = 0, fromthread = 0, sleeptime = 2; |
---|
735 | struct skin* copyfile = getscreen("copyfile"); |
---|
736 | struct skin* progress = getscreennode(copyfile, "progress"); |
---|
737 | struct skin* filefrom = getscreennode(copyfile, "filefrom"); |
---|
738 | struct skin* fileto = getscreennode(copyfile, "fileto"); |
---|
739 | struct skin* maxkb = getscreennode(copyfile, "maxkb"); |
---|
740 | struct skin* aktkb = getscreennode(copyfile, "aktkb"); |
---|
741 | struct skin* filecount = getscreennode(copyfile, "filecount"); |
---|
742 | struct skin* framebuffer = getscreen("framebuffer"); |
---|
743 | char* bg = NULL, *tmpstr = NULL; |
---|
744 | struct copyfile* cnode = NULL; |
---|
745 | |
---|
746 | if(pthread_self() != status.mainthread) |
---|
747 | fromthread = 1; |
---|
748 | |
---|
749 | if(from == NULL || to == NULL) |
---|
750 | { |
---|
751 | err("NULL detect"); |
---|
752 | return 1; |
---|
753 | } |
---|
754 | |
---|
755 | changetitle(copyfile, title); |
---|
756 | changetext(filefrom, from); |
---|
757 | changetext(fileto, to); |
---|
758 | progress->progresssize = 0; |
---|
759 | changetext(maxkb, NULL); |
---|
760 | changetext(aktkb, NULL); |
---|
761 | changetext(filecount, NULL); |
---|
762 | |
---|
763 | if(fromthread == 1) |
---|
764 | { |
---|
765 | delrc(getrcconfigint("rcvolup", NULL), NULL, NULL); |
---|
766 | delrc(getrcconfigint("rcvoldown", NULL), NULL, NULL); |
---|
767 | delrc(getrcconfigint("rcmute", NULL), NULL, NULL); |
---|
768 | m_lock(&status.drawingmutex, 0); |
---|
769 | m_lock(&status.rcmutex, 10); |
---|
770 | setnodeattr(copyfile, framebuffer, 2); |
---|
771 | status.rcowner = copyfile; |
---|
772 | bg = savescreen(copyfile); |
---|
773 | drawscreen(copyfile, 0, 2); |
---|
774 | } |
---|
775 | else |
---|
776 | drawscreen(copyfile, 0, 0); |
---|
777 | |
---|
778 | cnode = calloc(1, sizeof(struct copyfile)); |
---|
779 | if(cnode == NULL) |
---|
780 | { |
---|
781 | err("no mem"); |
---|
782 | return 1; |
---|
783 | } |
---|
784 | |
---|
785 | cnode->from = from; |
---|
786 | cnode->to = to; |
---|
787 | cnode->ret = -1; |
---|
788 | cnode->stop = 0; |
---|
789 | cnode->filecount = 0; |
---|
790 | cnode->maxfilecount = 1; |
---|
791 | |
---|
792 | ret = countfiles(from, &count, 1); |
---|
793 | if(ret == 0) |
---|
794 | cnode->maxfilecount = count; |
---|
795 | |
---|
796 | if(flag == 1) |
---|
797 | addtimer(&movefilestruct, START, 1000, 1, (void*)cnode, NULL, NULL); |
---|
798 | else |
---|
799 | addtimer(©filestruct, START, 1000, 1, (void*)cnode, NULL, NULL); |
---|
800 | |
---|
801 | while(1) |
---|
802 | { |
---|
803 | rcret = waitrc(copyfile, 1000, 0); |
---|
804 | |
---|
805 | if(rcret == RCTIMEOUT) |
---|
806 | { |
---|
807 | progress->progresssize = cnode->proz; |
---|
808 | |
---|
809 | if(cnode->maxkb < 1024) |
---|
810 | tmpstr = ostrcat(_("Filesize (B): "), oitoa(cnode->maxkb), 0, 1); |
---|
811 | else if(cnode->maxkb < 1048576) |
---|
812 | tmpstr = ostrcat(_("Filesize (KB): "), oitoa(cnode->maxkb / 1024), 0, 1); |
---|
813 | else |
---|
814 | tmpstr = ostrcat(_("Filesize (MB): "), oitoa(cnode->maxkb / 1024 / 1024), 0, 1); |
---|
815 | changetext(maxkb, tmpstr); |
---|
816 | free(tmpstr); tmpstr = NULL; |
---|
817 | |
---|
818 | if(flag == 1) |
---|
819 | { |
---|
820 | if(cnode->aktkb < 1024) |
---|
821 | tmpstr = ostrcat(_("Move (B): "), oitoa(cnode->aktkb), 0, 1); |
---|
822 | else if(cnode->aktkb < 1048576) |
---|
823 | tmpstr = ostrcat(_("Move (KB): "), oitoa(cnode->aktkb / 1024), 0, 1); |
---|
824 | else |
---|
825 | tmpstr = ostrcat(_("Move (MB): "), oitoa(cnode->aktkb / 1024 / 1024), 0, 1); |
---|
826 | } |
---|
827 | else |
---|
828 | { |
---|
829 | if(cnode->aktkb < 1024) |
---|
830 | tmpstr = ostrcat(_("Copy (B): "), oitoa(cnode->aktkb), 0, 1); |
---|
831 | else if(cnode->aktkb < 1048576) |
---|
832 | tmpstr = ostrcat(_("Copy (KB): "), oitoa(cnode->aktkb / 1024), 0, 1); |
---|
833 | else |
---|
834 | tmpstr = ostrcat(_("Copy (MB): "), oitoa(cnode->aktkb / 1024 / 1024), 0, 1); |
---|
835 | } |
---|
836 | changetext(aktkb, tmpstr); |
---|
837 | free(tmpstr); tmpstr = NULL; |
---|
838 | |
---|
839 | tmpstr = ostrcat(_("Amount of files: "), oitoa(cnode->filecount), 0, 1); |
---|
840 | tmpstr = ostrcat(tmpstr, " / ", 1, 0); |
---|
841 | tmpstr = ostrcat(tmpstr, oitoa(cnode->maxfilecount), 1, 1); |
---|
842 | changetext(filecount, tmpstr); |
---|
843 | free(tmpstr); tmpstr = NULL; |
---|
844 | } |
---|
845 | drawscreen(copyfile, 0, 0); |
---|
846 | if(rcret == getrcconfigint("rcexit", NULL)) break; |
---|
847 | if(cnode->ret == 0) |
---|
848 | { |
---|
849 | progress->progresssize = cnode->proz; |
---|
850 | |
---|
851 | if(flag == 1) |
---|
852 | { |
---|
853 | if(cnode->aktkb < 1024) |
---|
854 | tmpstr = ostrcat(_("Move (B): "), oitoa(cnode->aktkb), 0, 1); |
---|
855 | else if(cnode->aktkb < 1048576) |
---|
856 | tmpstr = ostrcat(_("Move (KB): "), oitoa(cnode->aktkb / 1024), 0, 1); |
---|
857 | else |
---|
858 | tmpstr = ostrcat(_("Move (MB): "), oitoa(cnode->aktkb / 1024 / 1024), 0, 1); |
---|
859 | } |
---|
860 | else |
---|
861 | { |
---|
862 | if(cnode->aktkb < 1024) |
---|
863 | tmpstr = ostrcat(_("Copy (B): "), oitoa(cnode->aktkb), 0, 1); |
---|
864 | else if(cnode->aktkb < 1048576) |
---|
865 | tmpstr = ostrcat(_("Copy (KB): "), oitoa(cnode->aktkb / 1024), 0, 1); |
---|
866 | else |
---|
867 | tmpstr = ostrcat(_("Copy (MB): "), oitoa(cnode->aktkb / 1024 / 1024), 0, 1); |
---|
868 | } |
---|
869 | changetext(aktkb, tmpstr); |
---|
870 | free(tmpstr); tmpstr = NULL; |
---|
871 | |
---|
872 | break; |
---|
873 | } |
---|
874 | if(cnode->ret > 0) break; |
---|
875 | } |
---|
876 | |
---|
877 | if(cnode->ret > 0 && rcret != getrcconfigint("rcexit", NULL)) |
---|
878 | { |
---|
879 | if(flag == 1) |
---|
880 | { |
---|
881 | changetext(filefrom, _("file move error")); |
---|
882 | changetext(fileto, _("file move error")); |
---|
883 | } |
---|
884 | else |
---|
885 | { |
---|
886 | changetext(filefrom, _("file copy error")); |
---|
887 | changetext(fileto, _("file copy error")); |
---|
888 | } |
---|
889 | sleeptime = 5; |
---|
890 | } |
---|
891 | else |
---|
892 | { |
---|
893 | changetext(filefrom, _("wait for stopping")); |
---|
894 | changetext(fileto, _("wait for stopping")); |
---|
895 | } |
---|
896 | drawscreen(copyfile, 0, 0); |
---|
897 | cnode->stop = 1; |
---|
898 | sleep(sleeptime); |
---|
899 | count = 0; |
---|
900 | while(cnode->ret < 0 && count < 10) |
---|
901 | { |
---|
902 | count++; |
---|
903 | sleep(1); |
---|
904 | } |
---|
905 | ret = cnode->ret; |
---|
906 | |
---|
907 | if(count < 10) |
---|
908 | { |
---|
909 | free(cnode); |
---|
910 | cnode = NULL; |
---|
911 | } |
---|
912 | else |
---|
913 | addoldentry((void*)cnode, 2, time(NULL) + 7200, NULL); |
---|
914 | |
---|
915 | if(fromthread == 1) |
---|
916 | { |
---|
917 | clearscreennolock(copyfile); |
---|
918 | restorescreen(bg, copyfile); |
---|
919 | blitfb(0); |
---|
920 | sleep(1); |
---|
921 | status.rcowner = NULL; |
---|
922 | m_unlock(&status.rcmutex, 10); |
---|
923 | m_unlock(&status.drawingmutex, 0); |
---|
924 | addrc(getrcconfigint("rcvolup", NULL), screenvolumeup, NULL, NULL); |
---|
925 | addrc(getrcconfigint("rcvoldown", NULL), screenvolumedown, NULL, NULL); |
---|
926 | addrc(getrcconfigint("rcmute", NULL), screenmute, NULL, NULL); |
---|
927 | } |
---|
928 | else |
---|
929 | { |
---|
930 | clearscreen(copyfile); |
---|
931 | drawscreen(skin, 0, 0); |
---|
932 | } |
---|
933 | return ret; |
---|
934 | } |
---|
935 | |
---|
936 | #endif |
---|