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 * \brief Source: UnDel File System
35 * 1. We don't handle directories (thus undelfs_get_path is easy to write).
36 * 2. Files are on the local file system (we do not support vfs files
37 * because we would have to provide an io_manager for the ext2fs tools,
38 * and I don't think it would be too useful to undelete files
48 #ifdef HAVE_EXT2FS_EXT2_FS_H
49 #include <ext2fs/ext2_fs.h>
51 /* asm/types.h defines its own umode_t */
53 #include <linux/ext2_fs.h>
56 #include <ext2fs/ext2fs.h>
59 #include "../src/global.h"
60 #include "../src/wtools.h" /* message() */
61 #include "../src/main.h" /* print_vfs_message */
84 /* We only allow one opened ext2fs */
85 static char *ext2_fname
;
86 static ext2_filsys fs
= NULL
;
87 static struct lsdel_struct lsd
;
88 static struct deleted_info
*delarray
;
89 static int num_delarray
, max_delarray
;
90 static char *block_buf
;
91 static const char *undelfserr
= N_(" undelfs: error ");
92 static int readdir_ptr
;
93 static int undelfs_usage
;
94 static struct vfs_class vfs_undelfs_ops
;
96 /* To generate the . and .. entries use -2 */
97 #define READDIR_PTR_INIT 0
100 undelfs_shutdown (void)
114 undelfs_get_path (const char *dirname
, char **fsname
, char **file
)
118 /* To look like filesystem, we have virtual directories
119 /#undel:XXX, which have no subdirectories. XXX is replaced with
120 hda5, sdb8 etc, which is assumed to live under /dev.
125 if (strncmp (dirname
, "/#undel:", 8))
130 /* Since we don't allow subdirectories, it's easy to get a filename,
131 * just scan backwards for a slash */
135 p
= dirname
+ strlen (dirname
);
139 if (p
- dirname
> 2 && *(p
-1) == '/' && *(p
-2) == '.')
147 *file
= g_strdup (p
+1);
148 tmp
= g_strndup (dirname
, p
- dirname
);
149 *fsname
= g_strconcat ("/dev/", tmp
, (char *) NULL
);
155 *file
= g_strdup ("");
156 *fsname
= g_strconcat ("/dev/", dirname
, (char *) NULL
);
161 undelfs_lsdel_proc(ext2_filsys _fs
, blk_t
*block_nr
, int blockcnt
, void *private)
163 struct lsdel_struct
*_lsd
= (struct lsdel_struct
*) private;
167 if (*block_nr
< _fs
->super
->s_first_data_block
||
168 *block_nr
>= _fs
->super
->s_blocks_count
) {
173 if (!ext2fs_test_block_bitmap(_fs
->block_map
,*block_nr
))
180 * Load information about deleted files.
181 * Don't abort if there is not enough memory - load as much as we can.
184 undelfs_loaddel (void)
188 struct ext2_inode inode
;
189 ext2_inode_scan scan
;
193 delarray
= g_try_malloc (sizeof (struct deleted_info
) * max_delarray
);
195 message (D_ERROR
, undelfserr
, _(" not enough memory "));
198 block_buf
= g_try_malloc (fs
->blocksize
* 3);
200 message (D_ERROR
, undelfserr
, _(" while allocating block buffer "));
203 if ((retval
= ext2fs_open_inode_scan (fs
, 0, &scan
))) {
204 message (D_ERROR
, undelfserr
, _(" open_inode_scan: %d "), retval
);
207 if ((retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
))) {
208 message (D_ERROR
, undelfserr
, _(" while starting inode scan %d "),
215 if ((count
++ % 1024) == 0)
217 ("undelfs: loading deleted files information %d inodes"),
219 if (inode
.i_dtime
== 0)
222 if (S_ISDIR (inode
.i_mode
))
231 ext2fs_block_iterate (fs
, ino
, 0, block_buf
,
232 undelfs_lsdel_proc
, &lsd
);
234 message (D_ERROR
, undelfserr
,
235 _(" while calling ext2_block_iterate %d "), retval
);
238 if (lsd
.free_blocks
&& !lsd
.bad_blocks
) {
239 if (num_delarray
>= max_delarray
) {
240 struct deleted_info
*delarray_new
=
241 g_try_realloc (delarray
,
242 sizeof (struct deleted_info
) *
243 (max_delarray
+ 50));
245 message (D_ERROR
, undelfserr
,
247 (" no more memory while reallocating array "));
250 delarray
= delarray_new
;
254 delarray
[num_delarray
].ino
= ino
;
255 delarray
[num_delarray
].mode
= inode
.i_mode
;
256 delarray
[num_delarray
].uid
= inode
.i_uid
;
257 delarray
[num_delarray
].gid
= inode
.i_gid
;
258 delarray
[num_delarray
].size
= inode
.i_size
;
259 delarray
[num_delarray
].dtime
= inode
.i_dtime
;
260 delarray
[num_delarray
].num_blocks
= lsd
.num_blocks
;
261 delarray
[num_delarray
].free_blocks
= lsd
.free_blocks
;
266 retval
= ext2fs_get_next_inode (scan
, &ino
, &inode
);
268 message (D_ERROR
, undelfserr
, _(" while doing inode scan %d "),
273 readdir_ptr
= READDIR_PTR_INIT
;
274 ext2fs_close_inode_scan (scan
);
278 ext2fs_close_inode_scan (scan
);
290 * This function overrides com_err() from libcom_err library.
291 * It is used in libext2fs to report errors.
294 com_err (const char *whoami
, long err_code
, const char *fmt
, ...)
300 str
= g_strdup_vprintf (fmt
, ap
);
303 message (D_ERROR
, _(" Ext2lib error "), " %s (%s: %ld) ", str
, whoami
,
309 undelfs_opendir (struct vfs_class
*me
, const char *dirname
)
313 undelfs_get_path (dirname
, &file
, &f
);
317 /* We don't use the file name */
320 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
324 /* To avoid expensive re-scannings */
325 readdir_ptr
= READDIR_PTR_INIT
;
330 if (ext2fs_open (ext2_fname
, 0, 0, 0, unix_io_manager
, &fs
)){
331 message (D_ERROR
, undelfserr
, _(" Cannot open file %s "), ext2_fname
);
334 print_vfs_message (_("undelfs: reading inode bitmap..."));
335 if (ext2fs_read_inode_bitmap (fs
)){
336 message (D_ERROR
, undelfserr
,
337 _(" Cannot load inode bitmap from: \n %s \n"), ext2_fname
);
340 print_vfs_message (_("undelfs: reading block bitmap..."));
341 if (ext2fs_read_block_bitmap (fs
)){
342 message (D_ERROR
, undelfserr
,
343 _(" Cannot load block bitmap from: \n %s \n"), ext2_fname
);
346 /* Now load the deleted information */
347 if (!undelfs_loaddel ())
349 print_vfs_message (_("%s: done."), me
->name
);
352 print_vfs_message (_("%s: failure"), me
->name
);
360 undelfs_readdir(void *vfs_info
)
362 static union vfs_dirent undelfs_readdir_data
;
363 static char *const dirent_dest
= undelfs_readdir_data
.dent
.d_name
;
365 if (vfs_info
!= fs
) {
366 message (D_ERROR
, undelfserr
, _(" vfs_info is not fs! "));
369 if (readdir_ptr
== num_delarray
)
372 strcpy (dirent_dest
, readdir_ptr
== -2 ? "." : "..");
374 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%ld:%d",
375 (long) delarray
[readdir_ptr
].ino
,
376 delarray
[readdir_ptr
].num_blocks
);
379 compute_namelen(&undelfs_readdir_data
.dent
);
381 return &undelfs_readdir_data
;
385 undelfs_closedir (void *vfs_info
)
391 int f_index
; /* file index into delarray */
393 int error_code
; /* */
394 int pos
; /* file position */
395 int current
; /* used to determine current position in itereate */
401 /* Used by undelfs_read: */
402 char *dest_buffer
; /* destination buffer */
403 int count
; /* bytes to read */
406 /* We do not support lseek */
408 undelfs_open (struct vfs_class
*me
, const char *fname
, int flags
, int mode
)
412 undelfs_file
*p
= NULL
;
414 /* Only allow reads on this file system */
415 undelfs_get_path (fname
, &file
, &f
);
419 if (!ext2_fname
|| strcmp (ext2_fname
, file
)) {
420 message (D_ERROR
, undelfserr
,
421 _(" You have to chdir to extract files first "));
428 /* Search the file into delarray */
429 for (i
= 0; i
< num_delarray
; i
++) {
430 if (inode
!= delarray
[i
].ino
)
433 /* Found: setup all the structures needed by read */
434 p
= (undelfs_file
*) g_try_malloc (((gsize
) sizeof (undelfs_file
)));
440 p
->buf
= g_try_malloc (fs
->blocksize
);
452 p
->size
= delarray
[i
].size
;
461 undelfs_close (void *vfs_info
)
463 undelfs_file
*p
= vfs_info
;
471 undelfs_dump_read(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
474 undelfs_file
*p
= (undelfs_file
*) private;
480 p
->error_code
= io_channel_read_blk(fs
->io
, *blocknr
,
485 memset(p
->buf
, 0, fs
->blocksize
);
487 if (p
->pos
+ p
->count
< p
->current
){
491 if (p
->pos
> p
->current
+ fs
->blocksize
){
492 p
->current
+= fs
->blocksize
;
493 return 0; /* we have not arrived yet */
496 /* Now, we know we have to extract some data */
497 if (p
->pos
>= p
->current
){
499 /* First case: starting pointer inside this block */
500 if (p
->pos
+ p
->count
<= p
->current
+ fs
->blocksize
){
501 /* Fully contained */
502 copy_count
= p
->count
;
503 p
->finished
= p
->count
;
505 /* Still some more data */
506 copy_count
= fs
->blocksize
-(p
->pos
-p
->current
);
508 memcpy (p
->dest_buffer
, p
->buf
+ (p
->pos
-p
->current
), copy_count
);
510 /* Second case: we already have passed p->pos */
511 if (p
->pos
+p
->count
< p
->current
+fs
->blocksize
){
512 copy_count
= (p
->pos
+ p
->count
) - p
->current
;
513 p
->finished
= p
->count
;
515 copy_count
= fs
->blocksize
;
517 memcpy (p
->dest_buffer
, p
->buf
, copy_count
);
519 p
->dest_buffer
+= copy_count
;
520 p
->current
+= fs
->blocksize
;
528 undelfs_read (void *vfs_info
, char *buffer
, int count
)
530 undelfs_file
*p
= vfs_info
;
533 p
->dest_buffer
= buffer
;
538 if (p
->pos
+ p
->count
> p
->size
){
539 p
->count
= p
->size
- p
->pos
;
541 retval
= ext2fs_block_iterate(fs
, p
->inode
, 0, NULL
,
542 undelfs_dump_read
, p
);
544 message (D_ERROR
, undelfserr
, _(" while iterating over blocks "));
547 if (p
->error_code
&& !p
->finished
)
549 p
->pos
= p
->pos
+ (p
->dest_buffer
- buffer
);
550 return p
->dest_buffer
- buffer
;
554 undelfs_getindex (char *path
)
556 ext2_ino_t inode
= atol (path
);
559 for (i
= 0; i
< num_delarray
; i
++){
560 if (delarray
[i
].ino
== inode
)
567 undelfs_stat_int (int inode_index
, struct stat
*buf
)
570 buf
->st_ino
= delarray
[inode_index
].ino
;
571 buf
->st_mode
= delarray
[inode_index
].mode
;
573 buf
->st_uid
= delarray
[inode_index
].uid
;
574 buf
->st_gid
= delarray
[inode_index
].gid
;
575 buf
->st_size
= delarray
[inode_index
].size
;
576 buf
->st_atime
= delarray
[inode_index
].dtime
;
577 buf
->st_ctime
= delarray
[inode_index
].dtime
;
578 buf
->st_mtime
= delarray
[inode_index
].dtime
;
583 undelfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
588 undelfs_get_path (path
, &file
, &f
);
592 /* When called from save_cwd_stats we get an incorrect file and f here:
593 e.g. incorrect correct
594 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
595 file = "/dev" file="/dev/sda1"
596 f = "sda1" f ="401:1"
597 If the first char in f is no digit -> return error */
604 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
605 message (D_ERROR
, undelfserr
, _(" You have to chdir to extract files first "));
610 inode_index
= undelfs_getindex (f
);
614 if (inode_index
== -1)
617 return undelfs_stat_int (inode_index
, buf
);
620 #define undelfs_stat undelfs_lstat
623 undelfs_fstat (void *vfs_info
, struct stat
*buf
)
625 undelfs_file
*p
= vfs_info
;
627 return undelfs_stat_int (p
->f_index
, buf
);
631 undelfs_chdir(struct vfs_class
*me
, const char *path
)
636 undelfs_get_path (path
, &file
, &f
);
640 /* We may use access because ext2 file systems are local */
641 /* this could be fixed by making an ext2fs io manager to use */
642 /* our vfs, but that is left as an excercise for the reader */
643 if ((fd
= open (file
, O_RDONLY
)) == -1){
644 message (D_ERROR
, undelfserr
, _(" Cannot open file %s "), file
);
655 /* this has to stay here for now: vfs layer does not know how to emulate it */
657 undelfs_lseek(void *vfs_info
, off_t offset
, int whence
)
663 undelfs_getid (struct vfs_class
*me
, const char *path
)
665 char *fname
, *fsname
;
667 undelfs_get_path (path
, &fsname
, &fname
);
677 undelfs_nothingisopen(vfsid id
)
679 return !undelfs_usage
;
683 undelfs_free(vfsid id
)
690 undelfs_init(struct vfs_class
*me
) {
691 undelfserr
= _(undelfserr
);
695 #define undelfs_init NULL
701 vfs_undelfs_ops
.name
= "undelfs";
702 vfs_undelfs_ops
.prefix
= "undel:";
703 vfs_undelfs_ops
.init
= undelfs_init
;
704 vfs_undelfs_ops
.open
= undelfs_open
;
705 vfs_undelfs_ops
.close
= undelfs_close
;
706 vfs_undelfs_ops
.read
= undelfs_read
;
707 vfs_undelfs_ops
.opendir
= undelfs_opendir
;
708 vfs_undelfs_ops
.readdir
= undelfs_readdir
;
709 vfs_undelfs_ops
.closedir
= undelfs_closedir
;
710 vfs_undelfs_ops
.stat
= undelfs_stat
;
711 vfs_undelfs_ops
.lstat
= undelfs_lstat
;
712 vfs_undelfs_ops
.fstat
= undelfs_fstat
;
713 vfs_undelfs_ops
.chdir
= undelfs_chdir
;
714 vfs_undelfs_ops
.lseek
= undelfs_lseek
;
715 vfs_undelfs_ops
.getid
= undelfs_getid
;
716 vfs_undelfs_ops
.nothingisopen
= undelfs_nothingisopen
;
717 vfs_undelfs_ops
.free
= undelfs_free
;
718 vfs_register_class (&vfs_undelfs_ops
);