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 the Free Software Foundation
10 Written by: 1995 Miguel de Icaza.
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Library General Public License
16 as published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Library General Public License for more details.
24 You should have received a copy of the GNU Library General Public
25 License along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 * 1. We don't handle directories (thus undelfs_get_path is easy to write).
31 * 2. Files are on the local file system (we do not support vfs files
32 * because we would have to provide an io_manager for the ext2fs tools,
33 * and I don't think it would be too useful to undelete files
42 #ifdef HAVE_EXT2FS_EXT2_FS_H
43 #include <ext2fs/ext2_fs.h>
45 /* asm/types.h defines its own umode_t */
47 #include <linux/ext2_fs.h>
50 #include <ext2fs/ext2fs.h>
53 #include "../src/global.h"
54 #include "../src/tty.h" /* enable/disable interrupt key */
55 #include "../src/wtools.h" /* message() */
56 #include "../src/main.h" /* print_vfs_message */
79 /* We only allow one opened ext2fs */
80 static char *ext2_fname
;
81 static ext2_filsys fs
= NULL
;
82 static struct lsdel_struct lsd
;
83 static struct deleted_info
*delarray
;
84 static int num_delarray
, max_delarray
;
85 static char *block_buf
;
86 static char *undelfserr
= N_(" undelfs: error ");
87 static int readdir_ptr
;
88 static int undelfs_usage
;
89 static struct vfs_class vfs_undelfs_ops
;
91 /* To generate the . and .. entries use -2 */
92 #define READDIR_PTR_INIT 0
95 undelfs_shutdown (void)
109 undelfs_get_path (const char *dirname
, char **fsname
, char **file
)
113 /* To look like filesystem, we have virtual directories
114 /#undel:XXX, which have no subdirectories. XXX is replaced with
115 hda5, sdb8 etc, which is assumed to live under /dev.
120 if (strncmp (dirname
, "/#undel:", 8))
125 /* Since we don't allow subdirectories, it's easy to get a filename,
126 * just scan backwards for a slash */
130 p
= dirname
+ strlen (dirname
);
134 if (p
- dirname
> 2 && *(p
-1) == '/' && *(p
-2) == '.')
142 *file
= g_strdup (p
+1);
143 tmp
= g_strndup (dirname
, p
- dirname
);
144 *fsname
= g_strconcat ("/dev/", tmp
, (char *) NULL
);
150 *file
= g_strdup ("");
151 *fsname
= g_strconcat ("/dev/", dirname
, (char *) NULL
);
156 undelfs_lsdel_proc(ext2_filsys fs
, blk_t
*block_nr
, int blockcnt
, void *private)
158 struct lsdel_struct
*lsd
= (struct lsdel_struct
*) private;
162 if (*block_nr
< fs
->super
->s_first_data_block
||
163 *block_nr
>= fs
->super
->s_blocks_count
) {
168 if (!ext2fs_test_block_bitmap(fs
->block_map
,*block_nr
))
175 * Load information about deleted files.
176 * Don't abort if there is not enough memory - load as much as we can.
179 undelfs_loaddel (void)
183 struct ext2_inode inode
;
184 ext2_inode_scan scan
;
188 delarray
= g_try_malloc (sizeof (struct deleted_info
) * max_delarray
);
190 message (1, undelfserr
, _(" not enough memory "));
193 block_buf
= g_try_malloc (fs
->blocksize
* 3);
195 message (1, undelfserr
, _(" while allocating block buffer "));
198 if ((retval
= ext2fs_open_inode_scan (fs
, 0, &scan
))) {
199 message (1, undelfserr
, _(" open_inode_scan: %d "), retval
);
202 if ((retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
))) {
203 message (1, undelfserr
, _(" while starting inode scan %d "),
210 if ((count
++ % 1024) == 0)
212 ("undelfs: loading deleted files information %d inodes"),
214 if (inode
.i_dtime
== 0)
217 if (S_ISDIR (inode
.i_mode
))
226 ext2fs_block_iterate (fs
, ino
, 0, block_buf
,
227 undelfs_lsdel_proc
, &lsd
);
229 message (1, undelfserr
,
230 _(" while calling ext2_block_iterate %d "), retval
);
233 if (lsd
.free_blocks
&& !lsd
.bad_blocks
) {
234 if (num_delarray
>= max_delarray
) {
235 struct deleted_info
*delarray_new
=
236 g_try_realloc (delarray
,
237 sizeof (struct deleted_info
) *
238 (max_delarray
+ 50));
240 message (1, undelfserr
,
242 (" no more memory while reallocating array "));
245 delarray
= delarray_new
;
249 delarray
[num_delarray
].ino
= ino
;
250 delarray
[num_delarray
].mode
= inode
.i_mode
;
251 delarray
[num_delarray
].uid
= inode
.i_uid
;
252 delarray
[num_delarray
].gid
= inode
.i_gid
;
253 delarray
[num_delarray
].size
= inode
.i_size
;
254 delarray
[num_delarray
].dtime
= inode
.i_dtime
;
255 delarray
[num_delarray
].num_blocks
= lsd
.num_blocks
;
256 delarray
[num_delarray
].free_blocks
= lsd
.free_blocks
;
261 retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
);
263 message (1, undelfserr
, _(" while doing inode scan %d "),
268 readdir_ptr
= READDIR_PTR_INIT
;
269 ext2fs_close_inode_scan (scan
);
273 ext2fs_close_inode_scan (scan
);
285 * This function overrides com_err() from libcom_err library.
286 * It is used in libext2fs to report errors.
289 com_err (const char *whoami
, long err_code
, const char *fmt
, ...)
295 str
= g_strdup_vprintf (fmt
, ap
);
298 message (1, _(" Ext2lib error "), " %s (%s: %ld) ", str
, whoami
,
304 undelfs_opendir (struct vfs_class
*me
, const char *dirname
)
308 undelfs_get_path (dirname
, &file
, &f
);
312 /* We don't use the file name */
315 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
319 /* To avoid expensive re-scannings */
320 readdir_ptr
= READDIR_PTR_INIT
;
325 if (ext2fs_open (ext2_fname
, 0, 0, 0, unix_io_manager
, &fs
)){
326 message (1, undelfserr
, _(" Cannot open file %s "), ext2_fname
);
329 print_vfs_message (_("undelfs: reading inode bitmap..."));
330 if (ext2fs_read_inode_bitmap (fs
)){
331 message (1, undelfserr
,
332 _(" Cannot load inode bitmap from: \n %s \n"), ext2_fname
);
335 print_vfs_message (_("undelfs: reading block bitmap..."));
336 if (ext2fs_read_block_bitmap (fs
)){
337 message (1, undelfserr
,
338 _(" Cannot load block bitmap from: \n %s \n"), ext2_fname
);
341 /* Now load the deleted information */
342 if (!undelfs_loaddel ())
344 print_vfs_message (_("%s: done."), me
->name
);
347 print_vfs_message (_("%s: failure"), me
->name
);
355 undelfs_readdir(void *vfs_info
)
357 static union vfs_dirent undelfs_readdir_data
;
358 static char *const dirent_dest
= undelfs_readdir_data
.dent
.d_name
;
360 if (vfs_info
!= fs
) {
361 message (1, undelfserr
, _(" vfs_info is not fs! "));
364 if (readdir_ptr
== num_delarray
)
367 strcpy (dirent_dest
, readdir_ptr
== -2 ? "." : "..");
369 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%ld:%d",
370 (long) delarray
[readdir_ptr
].ino
,
371 delarray
[readdir_ptr
].num_blocks
);
374 compute_namelen(&undelfs_readdir_data
.dent
);
376 return &undelfs_readdir_data
;
380 undelfs_closedir (void *vfs_info
)
386 int f_index
; /* file index into delarray */
388 int error_code
; /* */
389 int pos
; /* file position */
390 int current
; /* used to determine current position in itereate */
396 /* Used by undelfs_read: */
397 char *dest_buffer
; /* destination buffer */
398 int count
; /* bytes to read */
401 /* We do not support lseek */
403 undelfs_open (struct vfs_class
*me
, const char *fname
, int flags
, int mode
)
407 undelfs_file
*p
= NULL
;
409 /* Only allow reads on this file system */
410 undelfs_get_path (fname
, &file
, &f
);
414 if (!ext2_fname
|| strcmp (ext2_fname
, file
)) {
415 message (1, undelfserr
,
416 _(" You have to chdir to extract files first "));
423 /* Search the file into delarray */
424 for (i
= 0; i
< num_delarray
; i
++) {
425 if (inode
!= delarray
[i
].ino
)
428 /* Found: setup all the structures needed by read */
429 p
= (undelfs_file
*) g_try_malloc (((gsize
) sizeof (undelfs_file
)));
435 p
->buf
= g_try_malloc (fs
->blocksize
);
447 p
->size
= delarray
[i
].size
;
456 undelfs_close (void *vfs_info
)
458 undelfs_file
*p
= vfs_info
;
466 undelfs_dump_read(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
469 undelfs_file
*p
= (undelfs_file
*) private;
475 p
->error_code
= io_channel_read_blk(fs
->io
, *blocknr
,
480 memset(p
->buf
, 0, fs
->blocksize
);
482 if (p
->pos
+ p
->count
< p
->current
){
486 if (p
->pos
> p
->current
+ fs
->blocksize
){
487 p
->current
+= fs
->blocksize
;
488 return 0; /* we have not arrived yet */
491 /* Now, we know we have to extract some data */
492 if (p
->pos
>= p
->current
){
494 /* First case: starting pointer inside this block */
495 if (p
->pos
+ p
->count
<= p
->current
+ fs
->blocksize
){
496 /* Fully contained */
497 copy_count
= p
->count
;
498 p
->finished
= p
->count
;
500 /* Still some more data */
501 copy_count
= fs
->blocksize
-(p
->pos
-p
->current
);
503 memcpy (p
->dest_buffer
, p
->buf
+ (p
->pos
-p
->current
), copy_count
);
505 /* Second case: we already have passed p->pos */
506 if (p
->pos
+p
->count
< p
->current
+fs
->blocksize
){
507 copy_count
= (p
->pos
+ p
->count
) - p
->current
;
508 p
->finished
= p
->count
;
510 copy_count
= fs
->blocksize
;
512 memcpy (p
->dest_buffer
, p
->buf
, copy_count
);
514 p
->dest_buffer
+= copy_count
;
515 p
->current
+= fs
->blocksize
;
523 undelfs_read (void *vfs_info
, char *buffer
, int count
)
525 undelfs_file
*p
= vfs_info
;
528 p
->dest_buffer
= buffer
;
533 if (p
->pos
+ p
->count
> p
->size
){
534 p
->count
= p
->size
- p
->pos
;
536 retval
= ext2fs_block_iterate(fs
, p
->inode
, 0, NULL
,
537 undelfs_dump_read
, p
);
539 message (1, undelfserr
, _(" while iterating over blocks "));
542 if (p
->error_code
&& !p
->finished
)
544 p
->pos
= p
->pos
+ (p
->dest_buffer
- buffer
);
545 return p
->dest_buffer
- buffer
;
549 undelfs_getindex (char *path
)
551 ext2_ino_t inode
= atol (path
);
554 for (i
= 0; i
< num_delarray
; i
++){
555 if (delarray
[i
].ino
== inode
)
562 undelfs_stat_int (int inode_index
, struct stat
*buf
)
565 buf
->st_ino
= delarray
[inode_index
].ino
;
566 buf
->st_mode
= delarray
[inode_index
].mode
;
568 buf
->st_uid
= delarray
[inode_index
].uid
;
569 buf
->st_gid
= delarray
[inode_index
].gid
;
570 buf
->st_size
= delarray
[inode_index
].size
;
571 buf
->st_atime
= delarray
[inode_index
].dtime
;
572 buf
->st_ctime
= delarray
[inode_index
].dtime
;
573 buf
->st_mtime
= delarray
[inode_index
].dtime
;
578 undelfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
583 undelfs_get_path (path
, &file
, &f
);
587 /* When called from save_cwd_stats we get an incorrect file and f here:
588 e.g. incorrect correct
589 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
590 file = "/dev" file="/dev/sda1"
591 f = "sda1" f ="401:1"
592 If the first char in f is no digit -> return error */
599 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
600 message (1, undelfserr
, _(" You have to chdir to extract files first "));
605 inode_index
= undelfs_getindex (f
);
609 if (inode_index
== -1)
612 return undelfs_stat_int (inode_index
, buf
);
615 #define undelfs_stat undelfs_lstat
618 undelfs_fstat (void *vfs_info
, struct stat
*buf
)
620 undelfs_file
*p
= vfs_info
;
622 return undelfs_stat_int (p
->f_index
, buf
);
626 undelfs_chdir(struct vfs_class
*me
, const char *path
)
631 undelfs_get_path (path
, &file
, &f
);
635 /* We may use access because ext2 file systems are local */
636 /* this could be fixed by making an ext2fs io manager to use */
637 /* our vfs, but that is left as an excercise for the reader */
638 if ((fd
= open (file
, O_RDONLY
)) == -1){
639 message (1, undelfserr
, _(" Cannot open file %s "), file
);
650 /* this has to stay here for now: vfs layer does not know how to emulate it */
652 undelfs_lseek(void *vfs_info
, off_t offset
, int whence
)
658 undelfs_getid (struct vfs_class
*me
, const char *path
)
660 char *fname
, *fsname
;
662 undelfs_get_path (path
, &fsname
, &fname
);
672 undelfs_nothingisopen(vfsid id
)
674 return !undelfs_usage
;
678 undelfs_free(vfsid id
)
685 undelfs_init(struct vfs_class
*me
) {
686 undelfserr
= _(undelfserr
);
690 #define undelfs_init NULL
696 vfs_undelfs_ops
.name
= "undelfs";
697 vfs_undelfs_ops
.prefix
= "undel:";
698 vfs_undelfs_ops
.init
= undelfs_init
;
699 vfs_undelfs_ops
.open
= undelfs_open
;
700 vfs_undelfs_ops
.close
= undelfs_close
;
701 vfs_undelfs_ops
.read
= undelfs_read
;
702 vfs_undelfs_ops
.opendir
= undelfs_opendir
;
703 vfs_undelfs_ops
.readdir
= undelfs_readdir
;
704 vfs_undelfs_ops
.closedir
= undelfs_closedir
;
705 vfs_undelfs_ops
.stat
= undelfs_stat
;
706 vfs_undelfs_ops
.lstat
= undelfs_lstat
;
707 vfs_undelfs_ops
.fstat
= undelfs_fstat
;
708 vfs_undelfs_ops
.chdir
= undelfs_chdir
;
709 vfs_undelfs_ops
.lseek
= undelfs_lseek
;
710 vfs_undelfs_ops
.getid
= undelfs_getid
;
711 vfs_undelfs_ops
.nothingisopen
= undelfs_nothingisopen
;
712 vfs_undelfs_ops
.free
= undelfs_free
;
713 vfs_register_class (&vfs_undelfs_ops
);