This source file includes following definitions.
- undelfs_shutdown
- undelfs_get_path
- undelfs_lsdel_proc
- undelfs_loaddel
- undelfs_opendir
- undelfs_readdir
- undelfs_closedir
- undelfs_open
- undelfs_close
- undelfs_dump_read
- undelfs_read
- undelfs_getindex
- undelfs_stat_int
- undelfs_lstat
- undelfs_fstat
- undelfs_chdir
- undelfs_lseek
- undelfs_getid
- undelfs_nothingisopen
- undelfs_free
- undelfs_init
- com_err
- vfs_init_undelfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #include <config.h>
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <ext2fs/ext2_fs.h>
52 #include <ext2fs/ext2fs.h>
53 #include <ctype.h>
54
55 #include "lib/global.h"
56
57 #include "lib/util.h"
58 #include "lib/widget.h"
59 #include "lib/vfs/xdirentry.h"
60 #include "lib/vfs/utilvfs.h"
61 #include "lib/vfs/vfs.h"
62
63 #include "undelfs.h"
64
65
66
67
68
69
70 #define READDIR_PTR_INIT 0
71
72 #define undelfs_stat undelfs_lstat
73
74
75
76 struct deleted_info
77 {
78 ext2_ino_t ino;
79 unsigned short mode;
80 unsigned short uid;
81 unsigned short gid;
82 unsigned long size;
83 time_t dtime;
84 int num_blocks;
85 int free_blocks;
86 };
87
88 struct lsdel_struct
89 {
90 ext2_ino_t inode;
91 int num_blocks;
92 int free_blocks;
93 int bad_blocks;
94 };
95
96 typedef struct
97 {
98 int f_index;
99 char *buf;
100 int error_code;
101 off_t pos;
102 off_t current;
103 gboolean finished;
104 ext2_ino_t inode;
105 int bytes_read;
106 off_t size;
107
108
109 char *dest_buffer;
110 size_t count;
111 } undelfs_file;
112
113
114
115
116
117
118 static char *ext2_fname;
119 static ext2_filsys fs = NULL;
120 static struct lsdel_struct lsd;
121 static struct deleted_info *delarray;
122 static int num_delarray, max_delarray;
123 static char *block_buf;
124 static const char *undelfserr = N_("undelfs: error");
125 static int readdir_ptr;
126 static int undelfs_usage;
127
128 static struct vfs_s_subclass undelfs_subclass;
129 static struct vfs_class *vfs_undelfs_ops = VFS_CLASS (&undelfs_subclass);
130
131
132
133
134
135 static void
136 undelfs_shutdown (void)
137 {
138 if (fs)
139 ext2fs_close (fs);
140 fs = NULL;
141 MC_PTR_FREE (ext2_fname);
142 MC_PTR_FREE (delarray);
143 MC_PTR_FREE (block_buf);
144 }
145
146
147
148 static void
149 undelfs_get_path (const vfs_path_t *vpath, char **fsname, char **file)
150 {
151 const char *p, *dirname;
152
153 dirname = vfs_path_get_last_path_str (vpath);
154
155
156
157
158
159
160 *fsname = NULL;
161
162 if (strncmp (dirname, "undel://", 8) != 0)
163 return;
164
165 dirname += 8;
166
167
168
169 if (*dirname == '\0')
170 return;
171
172 p = dirname + strlen (dirname);
173 #if 0
174
175
176 if (p - dirname > 2 && IS_PATH_SEP (p[-1]) && p[-2] == '.')
177 *(p = p - 2) = 0;
178 #endif
179
180 while (p > dirname)
181 {
182 if (IS_PATH_SEP (*p))
183 {
184 char *tmp;
185
186 *file = g_strdup (p + 1);
187 tmp = g_strndup (dirname, p - dirname);
188 *fsname = g_strconcat ("/dev/", tmp, (char *) NULL);
189 g_free (tmp);
190 return;
191 }
192 p--;
193 }
194 *file = g_strdup ("");
195 *fsname = g_strconcat ("/dev/", dirname, (char *) NULL);
196 }
197
198
199
200 static int
201 undelfs_lsdel_proc (ext2_filsys _fs, blk_t *block_nr, int blockcnt, void *private)
202 {
203 struct lsdel_struct *_lsd = (struct lsdel_struct *) private;
204 (void) blockcnt;
205 _lsd->num_blocks++;
206
207 if (*block_nr < _fs->super->s_first_data_block || *block_nr >= _fs->super->s_blocks_count)
208 {
209 _lsd->bad_blocks++;
210 return BLOCK_ABORT;
211 }
212
213 if (!ext2fs_test_block_bitmap (_fs->block_map, *block_nr))
214 _lsd->free_blocks++;
215
216 return 0;
217 }
218
219
220
221
222
223
224
225 static int
226 undelfs_loaddel (void)
227 {
228 int retval, count;
229 ext2_ino_t ino;
230 struct ext2_inode inode;
231 ext2_inode_scan scan;
232
233 max_delarray = 100;
234 num_delarray = 0;
235 delarray = g_try_malloc (sizeof (struct deleted_info) * max_delarray);
236 if (!delarray)
237 {
238 message (D_ERROR, undelfserr, "%s", _("not enough memory"));
239 return 0;
240 }
241 block_buf = g_try_malloc (fs->blocksize * 3);
242 if (!block_buf)
243 {
244 message (D_ERROR, undelfserr, "%s", _("while allocating block buffer"));
245 goto free_delarray;
246 }
247 retval = ext2fs_open_inode_scan (fs, 0, &scan);
248 if (retval != 0)
249 {
250 message (D_ERROR, undelfserr, _("open_inode_scan: %d"), retval);
251 goto free_block_buf;
252 }
253 retval = ext2fs_get_next_inode (scan, &ino, &inode);
254 if (retval != 0)
255 {
256 message (D_ERROR, undelfserr, _("while starting inode scan %d"), retval);
257 goto error_out;
258 }
259 count = 0;
260 while (ino)
261 {
262 if ((count++ % 1024) == 0)
263 vfs_print_message (_("undelfs: loading deleted files information %d inodes"), count);
264 if (inode.i_dtime == 0)
265 goto next;
266
267 if (S_ISDIR (inode.i_mode))
268 goto next;
269
270 lsd.inode = ino;
271 lsd.num_blocks = 0;
272 lsd.free_blocks = 0;
273 lsd.bad_blocks = 0;
274
275 retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
276 if (retval)
277 {
278 message (D_ERROR, undelfserr, _("while calling ext2_block_iterate %d"), retval);
279 goto next;
280 }
281 if (lsd.free_blocks && !lsd.bad_blocks)
282 {
283 if (num_delarray >= max_delarray)
284 {
285 struct deleted_info *delarray_new = g_try_realloc (delarray,
286 sizeof (struct deleted_info) *
287 (max_delarray + 50));
288 if (!delarray_new)
289 {
290 message (D_ERROR, undelfserr, "%s",
291 _("no more memory while reallocating array"));
292 goto error_out;
293 }
294 delarray = delarray_new;
295 max_delarray += 50;
296 }
297
298 delarray[num_delarray].ino = ino;
299 delarray[num_delarray].mode = inode.i_mode;
300 delarray[num_delarray].uid = inode.i_uid;
301 delarray[num_delarray].gid = inode.i_gid;
302 delarray[num_delarray].size = inode.i_size;
303 delarray[num_delarray].dtime = inode.i_dtime;
304 delarray[num_delarray].num_blocks = lsd.num_blocks;
305 delarray[num_delarray].free_blocks = lsd.free_blocks;
306 num_delarray++;
307 }
308
309 next:
310 retval = ext2fs_get_next_inode (scan, &ino, &inode);
311 if (retval)
312 {
313 message (D_ERROR, undelfserr, _("while doing inode scan %d"), retval);
314 goto error_out;
315 }
316 }
317 readdir_ptr = READDIR_PTR_INIT;
318 ext2fs_close_inode_scan (scan);
319 return 1;
320
321 error_out:
322 ext2fs_close_inode_scan (scan);
323 free_block_buf:
324 MC_PTR_FREE (block_buf);
325 free_delarray:
326 MC_PTR_FREE (delarray);
327 return 0;
328 }
329
330
331
332 static void *
333 undelfs_opendir (const vfs_path_t *vpath)
334 {
335 char *file, *f = NULL;
336 const char *class_name;
337
338 class_name = vfs_path_get_last_path_vfs (vpath)->name;
339 undelfs_get_path (vpath, &file, &f);
340 if (file == NULL)
341 {
342 g_free (f);
343 return 0;
344 }
345
346
347 g_free (f);
348
349 if (!ext2_fname || strcmp (ext2_fname, file))
350 {
351 undelfs_shutdown ();
352 ext2_fname = file;
353 }
354 else
355 {
356
357 readdir_ptr = READDIR_PTR_INIT;
358 g_free (file);
359 return fs;
360 }
361
362 if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
363 {
364 message (D_ERROR, undelfserr, _("Cannot open file %s"), ext2_fname);
365 return 0;
366 }
367 vfs_print_message ("%s", _("undelfs: reading inode bitmap..."));
368 if (ext2fs_read_inode_bitmap (fs))
369 {
370 message (D_ERROR, undelfserr, _("Cannot load inode bitmap from:\n%s"), ext2_fname);
371 goto quit_opendir;
372 }
373 vfs_print_message ("%s", _("undelfs: reading block bitmap..."));
374 if (ext2fs_read_block_bitmap (fs))
375 {
376 message (D_ERROR, undelfserr, _("Cannot load block bitmap from:\n%s"), ext2_fname);
377 goto quit_opendir;
378 }
379
380 if (!undelfs_loaddel ())
381 goto quit_opendir;
382 vfs_print_message (_("%s: done."), class_name);
383 return fs;
384 quit_opendir:
385 vfs_print_message (_("%s: failure"), class_name);
386 ext2fs_close (fs);
387 fs = NULL;
388 return 0;
389 }
390
391
392
393 static struct vfs_dirent *
394 undelfs_readdir (void *vfs_info)
395 {
396 struct vfs_dirent *dirent;
397
398 if (vfs_info != fs)
399 {
400 message (D_ERROR, undelfserr, "%s", _("vfs_info is not fs!"));
401 return NULL;
402 }
403 if (readdir_ptr == num_delarray)
404 return NULL;
405 if (readdir_ptr < 0)
406 dirent = vfs_dirent_init (NULL, readdir_ptr == -2 ? "." : "..", 0);
407 else
408 {
409 char dirent_dest[MC_MAXPATHLEN];
410
411 g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
412 (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
413 dirent = vfs_dirent_init (NULL, dirent_dest, 0);
414 }
415 readdir_ptr++;
416
417 return dirent;
418 }
419
420
421
422 static int
423 undelfs_closedir (void *vfs_info)
424 {
425 (void) vfs_info;
426 return 0;
427 }
428
429
430
431
432 static void *
433 undelfs_open (const vfs_path_t *vpath, int flags, mode_t mode)
434 {
435 char *file, *f = NULL;
436 ext2_ino_t inode, i;
437 undelfs_file *p = NULL;
438 (void) flags;
439 (void) mode;
440
441
442 undelfs_get_path (vpath, &file, &f);
443 if (file == NULL)
444 {
445 g_free (f);
446 return 0;
447 }
448
449 if (!ext2_fname || strcmp (ext2_fname, file))
450 {
451 message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
452 g_free (file);
453 g_free (f);
454 return 0;
455 }
456 inode = atol (f);
457
458
459 for (i = 0; i < (ext2_ino_t) num_delarray; i++)
460 {
461 if (inode != delarray[i].ino)
462 continue;
463
464
465 p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
466 if (!p)
467 {
468 g_free (file);
469 g_free (f);
470 return 0;
471 }
472 p->buf = g_try_malloc (fs->blocksize);
473 if (!p->buf)
474 {
475 g_free (p);
476 g_free (file);
477 g_free (f);
478 return 0;
479 }
480 p->inode = inode;
481 p->finished = FALSE;
482 p->f_index = i;
483 p->error_code = 0;
484 p->pos = 0;
485 p->size = delarray[i].size;
486 }
487 g_free (file);
488 g_free (f);
489 undelfs_usage++;
490 return p;
491 }
492
493
494
495 static int
496 undelfs_close (void *vfs_info)
497 {
498 undelfs_file *p = vfs_info;
499 g_free (p->buf);
500 g_free (p);
501 undelfs_usage--;
502 return 0;
503 }
504
505
506
507 static int
508 undelfs_dump_read (ext2_filsys param_fs, blk_t *blocknr, int blockcnt, void *private)
509 {
510 int copy_count;
511 undelfs_file *p = (undelfs_file *) private;
512
513 if (blockcnt < 0)
514 return 0;
515
516 if (*blocknr)
517 {
518 p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
519 if (p->error_code)
520 return BLOCK_ABORT;
521 }
522 else
523 memset (p->buf, 0, param_fs->blocksize);
524
525 if (p->pos + (off_t) p->count < p->current)
526 {
527 p->finished = TRUE;
528 return BLOCK_ABORT;
529 }
530 if (p->pos > p->current + param_fs->blocksize)
531 {
532 p->current += param_fs->blocksize;
533 return 0;
534 }
535
536
537 if (p->pos >= p->current)
538 {
539
540
541 if (p->pos + (off_t) p->count <= p->current + param_fs->blocksize)
542 {
543
544 copy_count = p->count;
545 p->finished = (p->count != 0);
546 }
547 else
548 {
549
550 copy_count = param_fs->blocksize - (p->pos - p->current);
551 }
552 memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
553 }
554 else
555 {
556
557 if (p->pos + (off_t) p->count < p->current + param_fs->blocksize)
558 {
559 copy_count = (p->pos + p->count) - p->current;
560 p->finished = (p->count != 0);
561 }
562 else
563 {
564 copy_count = param_fs->blocksize;
565 }
566 memcpy (p->dest_buffer, p->buf, copy_count);
567 }
568 p->dest_buffer += copy_count;
569 p->current += param_fs->blocksize;
570 if (p->finished)
571 {
572 return BLOCK_ABORT;
573 }
574 return 0;
575 }
576
577
578
579 static ssize_t
580 undelfs_read (void *vfs_info, char *buffer, size_t count)
581 {
582 undelfs_file *p = vfs_info;
583 int retval;
584
585 p->dest_buffer = buffer;
586 p->current = 0;
587 p->finished = FALSE;
588 p->count = count;
589
590 if (p->pos + (off_t) p->count > p->size)
591 {
592 p->count = p->size - p->pos;
593 }
594 retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
595 if (retval)
596 {
597 message (D_ERROR, undelfserr, "%s", _("while iterating over blocks"));
598 return -1;
599 }
600 if (p->error_code && !p->finished)
601 return 0;
602 p->pos = p->pos + (p->dest_buffer - buffer);
603 return p->dest_buffer - buffer;
604 }
605
606
607
608 static long
609 undelfs_getindex (char *path)
610 {
611 ext2_ino_t inode = atol (path);
612 int i;
613
614 for (i = 0; i < num_delarray; i++)
615 {
616 if (delarray[i].ino == inode)
617 return i;
618 }
619 return -1;
620 }
621
622
623
624 static int
625 undelfs_stat_int (int inode_index, struct stat *buf)
626 {
627 buf->st_dev = 0;
628 buf->st_ino = delarray[inode_index].ino;
629 buf->st_mode = delarray[inode_index].mode;
630 buf->st_nlink = 1;
631 buf->st_uid = delarray[inode_index].uid;
632 buf->st_gid = delarray[inode_index].gid;
633 buf->st_size = delarray[inode_index].size;
634
635 vfs_zero_stat_times (buf);
636 buf->st_atime = delarray[inode_index].dtime;
637 buf->st_ctime = delarray[inode_index].dtime;
638 buf->st_mtime = delarray[inode_index].dtime;
639
640 return 0;
641 }
642
643
644
645 static int
646 undelfs_lstat (const vfs_path_t *vpath, struct stat *buf)
647 {
648 int inode_index;
649 char *file, *f = NULL;
650
651 undelfs_get_path (vpath, &file, &f);
652 if (file == NULL)
653 {
654 g_free (f);
655 return 0;
656 }
657
658
659
660
661
662
663
664 if (!isdigit (*f))
665 {
666 g_free (file);
667 g_free (f);
668 return -1;
669 }
670
671 if (!ext2_fname || strcmp (ext2_fname, file))
672 {
673 g_free (file);
674 g_free (f);
675 message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
676 return 0;
677 }
678 inode_index = undelfs_getindex (f);
679 g_free (file);
680 g_free (f);
681
682 if (inode_index == -1)
683 return -1;
684
685 return undelfs_stat_int (inode_index, buf);
686 }
687
688
689
690 static int
691 undelfs_fstat (void *vfs_info, struct stat *buf)
692 {
693 undelfs_file *p = vfs_info;
694
695 return undelfs_stat_int (p->f_index, buf);
696 }
697
698
699
700 static int
701 undelfs_chdir (const vfs_path_t *vpath)
702 {
703 char *file, *f = NULL;
704 int fd;
705
706 undelfs_get_path (vpath, &file, &f);
707 if (file == NULL)
708 {
709 g_free (f);
710 return (-1);
711 }
712
713
714
715
716 fd = open (file, O_RDONLY);
717 if (fd == -1)
718 {
719 message (D_ERROR, undelfserr, _("Cannot open file \"%s\""), file);
720 g_free (f);
721 g_free (file);
722 return -1;
723 }
724 close (fd);
725 g_free (f);
726 g_free (file);
727 return 0;
728 }
729
730
731
732
733 static off_t
734 undelfs_lseek (void *vfs_info, off_t offset, int whence)
735 {
736 (void) vfs_info;
737 (void) offset;
738 (void) whence;
739
740 return -1;
741 }
742
743
744
745 static vfsid
746 undelfs_getid (const vfs_path_t *vpath)
747 {
748 char *fname = NULL, *fsname;
749 gboolean ok;
750
751 undelfs_get_path (vpath, &fsname, &fname);
752 ok = fsname != NULL;
753
754 g_free (fname);
755 g_free (fsname);
756
757 return ok ? (vfsid) fs : NULL;
758 }
759
760
761
762 static gboolean
763 undelfs_nothingisopen (vfsid id)
764 {
765 (void) id;
766
767 return (undelfs_usage == 0);
768 }
769
770
771
772 static void
773 undelfs_free (vfsid id)
774 {
775 (void) id;
776
777 undelfs_shutdown ();
778 }
779
780
781
782 #ifdef ENABLE_NLS
783 static int
784 undelfs_init (struct vfs_class *me)
785 {
786 (void) me;
787
788 undelfserr = _(undelfserr);
789 return 1;
790 }
791 #else
792 #define undelfs_init NULL
793 #endif
794
795
796
797
798
799
800
801
802
803 void
804 com_err (const char *whoami, long err_code, const char *fmt, ...)
805 {
806 va_list ap;
807 char *str;
808
809 va_start (ap, fmt);
810 str = g_strdup_vprintf (fmt, ap);
811 va_end (ap);
812
813 message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code);
814 g_free (str);
815 }
816
817
818
819 void
820 vfs_init_undelfs (void)
821 {
822
823 memset (&undelfs_subclass, 0, sizeof (undelfs_subclass));
824
825 vfs_init_class (vfs_undelfs_ops, "undelfs", VFSF_UNKNOWN, "undel");
826 vfs_undelfs_ops->init = undelfs_init;
827 vfs_undelfs_ops->open = undelfs_open;
828 vfs_undelfs_ops->close = undelfs_close;
829 vfs_undelfs_ops->read = undelfs_read;
830 vfs_undelfs_ops->opendir = undelfs_opendir;
831 vfs_undelfs_ops->readdir = undelfs_readdir;
832 vfs_undelfs_ops->closedir = undelfs_closedir;
833 vfs_undelfs_ops->stat = undelfs_stat;
834 vfs_undelfs_ops->lstat = undelfs_lstat;
835 vfs_undelfs_ops->fstat = undelfs_fstat;
836 vfs_undelfs_ops->chdir = undelfs_chdir;
837 vfs_undelfs_ops->lseek = undelfs_lseek;
838 vfs_undelfs_ops->getid = undelfs_getid;
839 vfs_undelfs_ops->nothingisopen = undelfs_nothingisopen;
840 vfs_undelfs_ops->free = undelfs_free;
841 vfs_register_class (vfs_undelfs_ops);
842 }
843
844