1 /* UnDel File System: Midnight Commander file system.
3 This file system is intended to be used together with the
4 ext2fs library to recover files from ext2fs file systems.
6 Parts of this program were taken from the lsdel.c and dump.c files
7 written by Ted Ts'o (tytso@mit.edu) for the ext2fs package.
9 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
10 2005, 2007 Free Software Foundation, Inc.
11 Written by: 1995 Miguel de Icaza.
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Library General Public License
17 as published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 * 1. We don't handle directories (thus undelfs_get_path is easy to write).
32 * 2. Files are on the local file system (we do not support vfs files
33 * because we would have to provide an io_manager for the ext2fs tools,
34 * and I don't think it would be too useful to undelete files
43 #ifdef HAVE_EXT2FS_EXT2_FS_H
44 #include <ext2fs/ext2_fs.h>
46 /* asm/types.h defines its own umode_t */
48 #include <linux/ext2_fs.h>
51 #include <ext2fs/ext2fs.h>
54 #include "../src/global.h"
55 #include "../src/tty.h" /* enable/disable interrupt key */
56 #include "../src/wtools.h" /* message() */
57 #include "../src/main.h" /* print_vfs_message */
80 /* We only allow one opened ext2fs */
81 static char *ext2_fname
;
82 static ext2_filsys fs
= NULL
;
83 static struct lsdel_struct lsd
;
84 static struct deleted_info
*delarray
;
85 static int num_delarray
, max_delarray
;
86 static char *block_buf
;
87 static char *undelfserr
= N_(" undelfs: error ");
88 static int readdir_ptr
;
89 static int undelfs_usage
;
90 static struct vfs_class vfs_undelfs_ops
;
92 /* To generate the . and .. entries use -2 */
93 #define READDIR_PTR_INIT 0
96 undelfs_shutdown (void)
110 undelfs_get_path (const char *dirname
, char **fsname
, char **file
)
114 /* To look like filesystem, we have virtual directories
115 /#undel:XXX, which have no subdirectories. XXX is replaced with
116 hda5, sdb8 etc, which is assumed to live under /dev.
121 if (strncmp (dirname
, "/#undel:", 8))
126 /* Since we don't allow subdirectories, it's easy to get a filename,
127 * just scan backwards for a slash */
131 p
= dirname
+ strlen (dirname
);
135 if (p
- dirname
> 2 && *(p
-1) == '/' && *(p
-2) == '.')
143 *file
= g_strdup (p
+1);
144 tmp
= g_strndup (dirname
, p
- dirname
);
145 *fsname
= g_strconcat ("/dev/", tmp
, (char *) NULL
);
151 *file
= g_strdup ("");
152 *fsname
= g_strconcat ("/dev/", dirname
, (char *) NULL
);
157 undelfs_lsdel_proc(ext2_filsys fs
, blk_t
*block_nr
, int blockcnt
, void *private)
159 struct lsdel_struct
*lsd
= (struct lsdel_struct
*) private;
163 if (*block_nr
< fs
->super
->s_first_data_block
||
164 *block_nr
>= fs
->super
->s_blocks_count
) {
169 if (!ext2fs_test_block_bitmap(fs
->block_map
,*block_nr
))
176 * Load information about deleted files.
177 * Don't abort if there is not enough memory - load as much as we can.
180 undelfs_loaddel (void)
184 struct ext2_inode inode
;
185 ext2_inode_scan scan
;
189 delarray
= g_try_malloc (sizeof (struct deleted_info
) * max_delarray
);
191 message (1, undelfserr
, _(" not enough memory "));
194 block_buf
= g_try_malloc (fs
->blocksize
* 3);
196 message (1, undelfserr
, _(" while allocating block buffer "));
199 if ((retval
= ext2fs_open_inode_scan (fs
, 0, &scan
))) {
200 message (1, undelfserr
, _(" open_inode_scan: %d "), retval
);
203 if ((retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
))) {
204 message (1, undelfserr
, _(" while starting inode scan %d "),
211 if ((count
++ % 1024) == 0)
213 ("undelfs: loading deleted files information %d inodes"),
215 if (inode
.i_dtime
== 0)
218 if (S_ISDIR (inode
.i_mode
))
227 ext2fs_block_iterate (fs
, ino
, 0, block_buf
,
228 undelfs_lsdel_proc
, &lsd
);
230 message (1, undelfserr
,
231 _(" while calling ext2_block_iterate %d "), retval
);
234 if (lsd
.free_blocks
&& !lsd
.bad_blocks
) {
235 if (num_delarray
>= max_delarray
) {
236 struct deleted_info
*delarray_new
=
237 g_try_realloc (delarray
,
238 sizeof (struct deleted_info
) *
239 (max_delarray
+ 50));
241 message (1, undelfserr
,
243 (" no more memory while reallocating array "));
246 delarray
= delarray_new
;
250 delarray
[num_delarray
].ino
= ino
;
251 delarray
[num_delarray
].mode
= inode
.i_mode
;
252 delarray
[num_delarray
].uid
= inode
.i_uid
;
253 delarray
[num_delarray
].gid
= inode
.i_gid
;
254 delarray
[num_delarray
].size
= inode
.i_size
;
255 delarray
[num_delarray
].dtime
= inode
.i_dtime
;
256 delarray
[num_delarray
].num_blocks
= lsd
.num_blocks
;
257 delarray
[num_delarray
].free_blocks
= lsd
.free_blocks
;
262 retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
);
264 message (1, undelfserr
, _(" while doing inode scan %d "),
269 readdir_ptr
= READDIR_PTR_INIT
;
270 ext2fs_close_inode_scan (scan
);
274 ext2fs_close_inode_scan (scan
);
286 * This function overrides com_err() from libcom_err library.
287 * It is used in libext2fs to report errors.
290 com_err (const char *whoami
, long err_code
, const char *fmt
, ...)
296 str
= g_strdup_vprintf (fmt
, ap
);
299 message (1, _(" Ext2lib error "), " %s (%s: %ld) ", str
, whoami
,
305 undelfs_opendir (struct vfs_class
*me
, const char *dirname
)
309 undelfs_get_path (dirname
, &file
, &f
);
313 /* We don't use the file name */
316 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
320 /* To avoid expensive re-scannings */
321 readdir_ptr
= READDIR_PTR_INIT
;
326 if (ext2fs_open (ext2_fname
, 0, 0, 0, unix_io_manager
, &fs
)){
327 message (1, undelfserr
, _(" Cannot open file %s "), ext2_fname
);
330 print_vfs_message (_("undelfs: reading inode bitmap..."));
331 if (ext2fs_read_inode_bitmap (fs
)){
332 message (1, undelfserr
,
333 _(" Cannot load inode bitmap from: \n %s \n"), ext2_fname
);
336 print_vfs_message (_("undelfs: reading block bitmap..."));
337 if (ext2fs_read_block_bitmap (fs
)){
338 message (1, undelfserr
,
339 _(" Cannot load block bitmap from: \n %s \n"), ext2_fname
);
342 /* Now load the deleted information */
343 if (!undelfs_loaddel ())
345 print_vfs_message (_("%s: done."), me
->name
);
348 print_vfs_message (_("%s: failure"), me
->name
);
356 undelfs_readdir(void *vfs_info
)
358 static union vfs_dirent undelfs_readdir_data
;
359 static char *const dirent_dest
= undelfs_readdir_data
.dent
.d_name
;
361 if (vfs_info
!= fs
) {
362 message (1, undelfserr
, _(" vfs_info is not fs! "));
365 if (readdir_ptr
== num_delarray
)
368 strcpy (dirent_dest
, readdir_ptr
== -2 ? "." : "..");
370 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%ld:%d",
371 (long) delarray
[readdir_ptr
].ino
,
372 delarray
[readdir_ptr
].num_blocks
);
375 compute_namelen(&undelfs_readdir_data
.dent
);
377 return &undelfs_readdir_data
;
381 undelfs_closedir (void *vfs_info
)
387 int f_index
; /* file index into delarray */
389 int error_code
; /* */
390 int pos
; /* file position */
391 int current
; /* used to determine current position in itereate */
397 /* Used by undelfs_read: */
398 char *dest_buffer
; /* destination buffer */
399 int count
; /* bytes to read */
402 /* We do not support lseek */
404 undelfs_open (struct vfs_class
*me
, const char *fname
, int flags
, int mode
)
408 undelfs_file
*p
= NULL
;
410 /* Only allow reads on this file system */
411 undelfs_get_path (fname
, &file
, &f
);
415 if (!ext2_fname
|| strcmp (ext2_fname
, file
)) {
416 message (1, undelfserr
,
417 _(" You have to chdir to extract files first "));
424 /* Search the file into delarray */
425 for (i
= 0; i
< num_delarray
; i
++) {
426 if (inode
!= delarray
[i
].ino
)
429 /* Found: setup all the structures needed by read */
430 p
= (undelfs_file
*) g_try_malloc (((gsize
) sizeof (undelfs_file
)));
436 p
->buf
= g_try_malloc (fs
->blocksize
);
448 p
->size
= delarray
[i
].size
;
457 undelfs_close (void *vfs_info
)
459 undelfs_file
*p
= vfs_info
;
467 undelfs_dump_read(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
470 undelfs_file
*p
= (undelfs_file
*) private;
476 p
->error_code
= io_channel_read_blk(fs
->io
, *blocknr
,
481 memset(p
->buf
, 0, fs
->blocksize
);
483 if (p
->pos
+ p
->count
< p
->current
){
487 if (p
->pos
> p
->current
+ fs
->blocksize
){
488 p
->current
+= fs
->blocksize
;
489 return 0; /* we have not arrived yet */
492 /* Now, we know we have to extract some data */
493 if (p
->pos
>= p
->current
){
495 /* First case: starting pointer inside this block */
496 if (p
->pos
+ p
->count
<= p
->current
+ fs
->blocksize
){
497 /* Fully contained */
498 copy_count
= p
->count
;
499 p
->finished
= p
->count
;
501 /* Still some more data */
502 copy_count
= fs
->blocksize
-(p
->pos
-p
->current
);
504 memcpy (p
->dest_buffer
, p
->buf
+ (p
->pos
-p
->current
), copy_count
);
506 /* Second case: we already have passed p->pos */
507 if (p
->pos
+p
->count
< p
->current
+fs
->blocksize
){
508 copy_count
= (p
->pos
+ p
->count
) - p
->current
;
509 p
->finished
= p
->count
;
511 copy_count
= fs
->blocksize
;
513 memcpy (p
->dest_buffer
, p
->buf
, copy_count
);
515 p
->dest_buffer
+= copy_count
;
516 p
->current
+= fs
->blocksize
;
524 undelfs_read (void *vfs_info
, char *buffer
, int count
)
526 undelfs_file
*p
= vfs_info
;
529 p
->dest_buffer
= buffer
;
534 if (p
->pos
+ p
->count
> p
->size
){
535 p
->count
= p
->size
- p
->pos
;
537 retval
= ext2fs_block_iterate(fs
, p
->inode
, 0, NULL
,
538 undelfs_dump_read
, p
);
540 message (1, undelfserr
, _(" while iterating over blocks "));
543 if (p
->error_code
&& !p
->finished
)
545 p
->pos
= p
->pos
+ (p
->dest_buffer
- buffer
);
546 return p
->dest_buffer
- buffer
;
550 undelfs_getindex (char *path
)
552 ext2_ino_t inode
= atol (path
);
555 for (i
= 0; i
< num_delarray
; i
++){
556 if (delarray
[i
].ino
== inode
)
563 undelfs_stat_int (int inode_index
, struct stat
*buf
)
566 buf
->st_ino
= delarray
[inode_index
].ino
;
567 buf
->st_mode
= delarray
[inode_index
].mode
;
569 buf
->st_uid
= delarray
[inode_index
].uid
;
570 buf
->st_gid
= delarray
[inode_index
].gid
;
571 buf
->st_size
= delarray
[inode_index
].size
;
572 buf
->st_atime
= delarray
[inode_index
].dtime
;
573 buf
->st_ctime
= delarray
[inode_index
].dtime
;
574 buf
->st_mtime
= delarray
[inode_index
].dtime
;
579 undelfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
584 undelfs_get_path (path
, &file
, &f
);
588 /* When called from save_cwd_stats we get an incorrect file and f here:
589 e.g. incorrect correct
590 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
591 file = "/dev" file="/dev/sda1"
592 f = "sda1" f ="401:1"
593 If the first char in f is no digit -> return error */
600 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
601 message (1, undelfserr
, _(" You have to chdir to extract files first "));
606 inode_index
= undelfs_getindex (f
);
610 if (inode_index
== -1)
613 return undelfs_stat_int (inode_index
, buf
);
616 #define undelfs_stat undelfs_lstat
619 undelfs_fstat (void *vfs_info
, struct stat
*buf
)
621 undelfs_file
*p
= vfs_info
;
623 return undelfs_stat_int (p
->f_index
, buf
);
627 undelfs_chdir(struct vfs_class
*me
, const char *path
)
632 undelfs_get_path (path
, &file
, &f
);
636 /* We may use access because ext2 file systems are local */
637 /* this could be fixed by making an ext2fs io manager to use */
638 /* our vfs, but that is left as an excercise for the reader */
639 if ((fd
= open (file
, O_RDONLY
)) == -1){
640 message (1, undelfserr
, _(" Cannot open file %s "), file
);
651 /* this has to stay here for now: vfs layer does not know how to emulate it */
653 undelfs_lseek(void *vfs_info
, off_t offset
, int whence
)
659 undelfs_getid (struct vfs_class
*me
, const char *path
)
661 char *fname
, *fsname
;
663 undelfs_get_path (path
, &fsname
, &fname
);
673 undelfs_nothingisopen(vfsid id
)
675 return !undelfs_usage
;
679 undelfs_free(vfsid id
)
686 undelfs_init(struct vfs_class
*me
) {
687 undelfserr
= _(undelfserr
);
691 #define undelfs_init NULL
697 vfs_undelfs_ops
.name
= "undelfs";
698 vfs_undelfs_ops
.prefix
= "undel:";
699 vfs_undelfs_ops
.init
= undelfs_init
;
700 vfs_undelfs_ops
.open
= undelfs_open
;
701 vfs_undelfs_ops
.close
= undelfs_close
;
702 vfs_undelfs_ops
.read
= undelfs_read
;
703 vfs_undelfs_ops
.opendir
= undelfs_opendir
;
704 vfs_undelfs_ops
.readdir
= undelfs_readdir
;
705 vfs_undelfs_ops
.closedir
= undelfs_closedir
;
706 vfs_undelfs_ops
.stat
= undelfs_stat
;
707 vfs_undelfs_ops
.lstat
= undelfs_lstat
;
708 vfs_undelfs_ops
.fstat
= undelfs_fstat
;
709 vfs_undelfs_ops
.chdir
= undelfs_chdir
;
710 vfs_undelfs_ops
.lseek
= undelfs_lseek
;
711 vfs_undelfs_ops
.getid
= undelfs_getid
;
712 vfs_undelfs_ops
.nothingisopen
= undelfs_nothingisopen
;
713 vfs_undelfs_ops
.free
= undelfs_free
;
714 vfs_register_class (&vfs_undelfs_ops
);