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