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.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Library General Public License
18 as published by the Free Software Foundation; either version 2 of
19 the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Library General Public License for more details.
26 You should have received a copy of the GNU Library General Public
27 License along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 * 1. We don't handle directories (thus undelfs_get_path is easy to write).
33 * 2. Files are on the local file system (we do not support vfs files
34 * because we would have to provide an io_manager for the ext2fs tools,
35 * and I don't think it would be too useful to undelete files
44 /* asm/types.h defines its own umode_t :-( */
46 #include <linux/ext2_fs.h>
47 #include <ext2fs/ext2fs.h>
72 /* We only allow one opened ext2fs */
73 static char *ext2_fname
;
74 static ext2_filsys fs
= NULL
;
75 static struct lsdel_struct lsd
;
76 static struct deleted_info
*delarray
;
77 static int num_delarray
, max_delarray
;
78 static char *block_buf
;
79 static char *undelfserr
= N_(" undelfs: error ");
80 static int readdir_ptr
;
81 static int undelfs_usage
;
83 /* To generate the . and .. entries use -2 */
84 #define READDIR_PTR_INIT 0
87 undelfs_shutdown (void)
104 undelfs_get_path (char *dirname
, char **ext2_fname
, char **file
)
108 /* To look like filesystem, we have virtual directories
109 /#undel:XXX, which have no subdirectories. XXX is replaced with
110 hda5, sdb8 etc, which is assumed to live under /dev.
115 if (strncmp (dirname
, "/#undel:", 8))
120 /* Since we don't allow subdirectories, it's easy to get a filename,
121 * just scan backwards for a slash */
125 p
= dirname
+ strlen (dirname
);
129 if (p
- dirname
> 2 && *(p
-1) == '/' && *(p
-2) == '.')
135 *file
= g_strdup (p
+1);
137 *ext2_fname
= g_strconcat ("/dev/", dirname
, NULL
);
143 *file
= g_strdup ("");
144 *ext2_fname
= g_strconcat ("/dev/", dirname
, NULL
);
149 lsdel_proc(ext2_filsys fs
, blk_t
*block_nr
, int blockcnt
, void *private)
151 struct lsdel_struct
*lsd
= (struct lsdel_struct
*) private;
155 if (*block_nr
< fs
->super
->s_first_data_block
||
156 *block_nr
>= fs
->super
->s_blocks_count
) {
161 if (!ext2fs_test_block_bitmap(fs
->block_map
,*block_nr
))
167 /* We don't use xmalloc, since we want to recover and not abort the program
168 * if we don't have enough memory
171 undelfs_loaddel (void)
175 struct ext2_inode inode
;
176 ext2_inode_scan scan
;
180 delarray
= g_new (struct deleted_info
, max_delarray
);
182 message_1s (1, undelfserr
, _(" not enough memory "));
185 block_buf
= g_malloc (fs
->blocksize
* 3);
187 message_1s (1, undelfserr
, _(" while allocating block buffer "));
190 if ((retval
= ext2fs_open_inode_scan(fs
, 0, &scan
))){
191 message_1s1d (1, undelfserr
, _(" open_inode_scan: %d "), retval
);
194 if ((retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
))){
195 message_1s1d (1, undelfserr
, _(" while starting inode scan %d "), retval
);
201 if ((count
++ % 1024) == 0)
202 print_vfs_message (_("undelfs: loading deleted files information %d inodes"), count
);
203 if (inode
.i_dtime
== 0)
206 if (S_ISDIR(inode
.i_mode
))
214 retval
= ext2fs_block_iterate(fs
, ino
, 0, block_buf
,
217 message_1s1d (1, undelfserr
, _(" while calling ext2_block_iterate %d "), retval
);
220 if (lsd
.free_blocks
&& !lsd
.bad_blocks
) {
221 if (num_delarray
>= max_delarray
) {
223 delarray
= g_renew (struct deleted_info
, delarray
, max_delarray
);
225 message_1s (1, undelfserr
, _(" no more memory while reallocating array "));
230 delarray
[num_delarray
].ino
= ino
;
231 delarray
[num_delarray
].mode
= inode
.i_mode
;
232 delarray
[num_delarray
].uid
= inode
.i_uid
;
233 delarray
[num_delarray
].gid
= inode
.i_gid
;
234 delarray
[num_delarray
].size
= inode
.i_size
;
235 delarray
[num_delarray
].dtime
= inode
.i_dtime
;
236 delarray
[num_delarray
].num_blocks
= lsd
.num_blocks
;
237 delarray
[num_delarray
].free_blocks
= lsd
.free_blocks
;
242 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
244 message_1s1d(1, undelfserr
, _(" while doing inode scan %d "), retval
);
248 readdir_ptr
= READDIR_PTR_INIT
;
249 ext2fs_close_inode_scan (scan
);
253 ext2fs_close_inode_scan (scan
);
263 void com_err (const char *str
, long err_code
, const char *s2
, ...)
267 cptr
= g_strdup_printf (" %s (%s: %ld) ", s2
, str
, err_code
);
268 message_1s (1, _(" Ext2lib error "), cptr
);
273 undelfs_opendir (vfs
*me
, char *dirname
)
277 undelfs_get_path (dirname
, &file
, &f
);
281 /* We don't use the file name */
284 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
288 /* To avoid expensive re-scannings */
289 readdir_ptr
= READDIR_PTR_INIT
;
294 if (ext2fs_open (ext2_fname
, 0, 0, 0, unix_io_manager
, &fs
)){
295 message_2s (1, undelfserr
, _(" Could not open file %s "), ext2_fname
);
298 print_vfs_message (_("undelfs: reading inode bitmap..."));
299 if (ext2fs_read_inode_bitmap (fs
)){
300 message_2s (1, undelfserr
,
301 _(" Could not load inode bitmap from: \n %s \n"), ext2_fname
);
304 print_vfs_message (_("undelfs: reading block bitmap..."));
305 if (ext2fs_read_block_bitmap (fs
)){
306 message_2s (1, undelfserr
,
307 _(" Could not load block bitmap from: \n %s \n"), ext2_fname
);
310 /* Now load the deleted information */
311 if (!undelfs_loaddel ())
313 print_vfs_message (_("%s: done."), me
->name
);
316 print_vfs_message (_("%s: failure"), me
->name
);
324 undelfs_readdir(void *vfs_info
)
326 static union vfs_dirent undelfs_readdir_data
;
329 if (vfs_info
!= fs
) {
330 message_1s(1, undelfserr
, _(" vfs_info is not fs! "));
333 if (readdir_ptr
== num_delarray
)
335 dirent_dest
= undelfs_readdir_data
.dent
.d_name
;
337 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%s",
338 readdir_ptr
== -2 ? "." : "..");
340 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%ld:%d",
341 (long) delarray
[readdir_ptr
].ino
,
342 delarray
[readdir_ptr
].num_blocks
);
345 compute_namelen(&undelfs_readdir_data
.dent
);
347 return &undelfs_readdir_data
;
351 undelfs_closedir (void *vfs_info
)
357 int f_index
; /* file index into delarray */
359 int error_code
; /* */
360 int pos
; /* file position */
361 int current
; /* used to determine current position in itereate */
367 /* Used by undelfs_read: */
368 char *dest_buffer
; /* destination buffer */
369 int count
; /* bytes to read */
372 /* We do not support lseek */
374 undelfs_open (vfs
*me
, char *fname
, int flags
, int mode
)
378 undelfs_file
*p
= NULL
;
380 /* Only allow reads on this file system */
381 undelfs_get_path (fname
, &file
, &f
);
385 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
386 message_1s (1, undelfserr
, _(" You have to chdir to extract files first "));
393 /* Search the file into delarray */
394 for (i
= 0; i
< num_delarray
; i
++){
395 if (inode
!= delarray
[i
].ino
)
398 /* Found: setup all the structures needed by read */
399 p
= g_new (undelfs_file
, 1);
405 p
->buf
= g_malloc (fs
->blocksize
);
417 p
->size
= delarray
[i
].size
;
426 undelfs_close (void *vfs_info
)
428 undelfs_file
*p
= vfs_info
;
436 dump_read(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
439 undelfs_file
*p
= (undelfs_file
*) private;
445 p
->error_code
= io_channel_read_blk(fs
->io
, *blocknr
,
450 memset(p
->buf
, 0, fs
->blocksize
);
452 if (p
->pos
+ p
->count
< p
->current
){
456 if (p
->pos
> p
->current
+ fs
->blocksize
){
457 p
->current
+= fs
->blocksize
;
458 return 0; /* we have not arrived yet */
461 /* Now, we know we have to extract some data */
462 if (p
->pos
>= p
->current
){
464 /* First case: starting pointer inside this block */
465 if (p
->pos
+ p
->count
<= p
->current
+ fs
->blocksize
){
466 /* Fully contained */
467 copy_count
= p
->count
;
468 p
->finished
= p
->count
;
470 /* Still some more data */
471 copy_count
= fs
->blocksize
-(p
->pos
-p
->current
);
473 memcpy (p
->dest_buffer
, p
->buf
+ (p
->pos
-p
->current
), copy_count
);
475 /* Second case: we already have passed p->pos */
476 if (p
->pos
+p
->count
< p
->current
+fs
->blocksize
){
477 copy_count
= (p
->pos
+ p
->count
) - p
->current
;
478 p
->finished
= p
->count
;
480 copy_count
= fs
->blocksize
;
482 memcpy (p
->dest_buffer
, p
->buf
, copy_count
);
484 p
->dest_buffer
+= copy_count
;
485 p
->current
+= fs
->blocksize
;
493 undelfs_read (void *vfs_info
, char *buffer
, int count
)
495 undelfs_file
*p
= vfs_info
;
498 p
->dest_buffer
= buffer
;
503 if (p
->pos
+ p
->count
> p
->size
){
504 p
->count
= p
->size
- p
->pos
;
506 retval
= ext2fs_block_iterate(fs
, p
->inode
, 0, NULL
,
509 message_1s (1, undelfserr
, _(" while iterating over blocks "));
512 if (p
->error_code
&& !p
->finished
)
514 p
->pos
= p
->pos
+ (p
->dest_buffer
- buffer
);
515 return p
->dest_buffer
- buffer
;
519 undelfs_getindex (char *path
)
521 ext2_ino_t inode
= atol (path
);
524 for (i
= 0; i
< num_delarray
; i
++){
525 if (delarray
[i
].ino
== inode
)
532 do_stat (int inode_index
, struct stat
*buf
)
535 buf
->st_ino
= delarray
[inode_index
].ino
;
536 buf
->st_mode
= delarray
[inode_index
].mode
;
538 buf
->st_uid
= delarray
[inode_index
].uid
;
539 buf
->st_gid
= delarray
[inode_index
].gid
;
540 buf
->st_size
= delarray
[inode_index
].size
;
541 buf
->st_atime
= delarray
[inode_index
].dtime
;
542 buf
->st_ctime
= delarray
[inode_index
].dtime
;
543 buf
->st_mtime
= delarray
[inode_index
].dtime
;
548 undelfs_lstat(vfs
*me
, char *path
, struct stat
*buf
)
553 undelfs_get_path (path
, &file
, &f
);
557 /* When called from save_cwd_stats we get an incorrect file and f here:
558 e.g. incorrect correct
559 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
560 file = "/dev" file="/dev/sda1"
561 f = "sda1" f ="401:1"
562 If the first char in f is no digit -> return error */
569 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
570 message_1s (1, undelfserr
, _(" You have to chdir to extract files first "));
575 inode_index
= undelfs_getindex (f
);
579 if (inode_index
== -1)
582 return do_stat (inode_index
, buf
);
586 undelfs_stat(vfs
*me
, char *path
, struct stat
*buf
)
588 return undelfs_lstat (me
, path
, buf
);
593 undelfs_fstat (void *vfs_info
, struct stat
*buf
)
595 undelfs_file
*p
= vfs_info
;
597 return do_stat (p
->f_index
, buf
);
601 undelfs_chdir(vfs
*me
, char *path
)
606 undelfs_get_path (path
, &file
, &f
);
610 /* We may use access because ext2 file systems are local */
611 /* this could be fixed by making an ext2fs io manager to use */
612 /* our vfs, but that is left as an excercise for the reader */
613 if ((fd
= open (file
, O_RDONLY
)) == -1){
614 message_2s (1, undelfserr
, _(" Could not open file: %s "), file
);
625 /* this has to stay here for now: vfs layer does not know how to emulate it */
627 undelfs_lseek(void *vfs_info
, off_t offset
, int whence
)
633 undelfs_getid(vfs
*me
, char *path
, struct vfs_stamping
**parent
)
635 char *ext2_fname
, *file
;
637 /* We run only on the local fs */
639 undelfs_get_path (path
, &ext2_fname
, &file
);
649 undelfs_nothingisopen(vfsid id
)
651 return !undelfs_usage
;
655 undelfs_free(vfsid id
)
662 undelfs_init(vfs
*me
) {
663 undelfserr
= _(undelfserr
);
667 #define undelfs_init NULL
670 vfs vfs_undelfs_ops
= {
671 NULL
, /* This is place of next pointer */
674 "undel:", /* prefix */
679 NULL
, /* fill_names */
713 undelfs_nothingisopen
,
716 NULL
, /* get_local_copy */