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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
41 /* asm/types.h defines its own umode_t :-( */
43 #include <linux/ext2_fs.h>
44 #include <ext2fs/ext2fs.h>
69 /* We only allow one opened ext2fs */
70 static char *ext2_fname
;
71 static ext2_filsys fs
= NULL
;
72 static struct lsdel_struct lsd
;
73 static struct deleted_info
*delarray
;
74 static int num_delarray
, max_delarray
;
75 static char *block_buf
;
76 static char *undelfserr
= N_(" undelfs: error ");
77 static int readdir_ptr
;
78 static int undelfs_usage
;
80 /* To generate the . and .. entries use -2 */
81 #define READDIR_PTR_INIT 0
84 undelfs_shutdown (void)
101 undelfs_get_path (char *dirname
, char **ext2_fname
, char **file
)
105 /* To look like filesystem, we have virtual directories
106 /#undel:XXX, which have no subdirectories. XXX is replaced with
107 hda5, sdb8 etc, which is assumed to live under /dev.
112 if (strncmp (dirname
, "/#undel:", 8))
117 /* Since we don't allow subdirectories, it's easy to get a filename,
118 * just scan backwards for a slash */
122 p
= dirname
+ strlen (dirname
);
126 if (p
- dirname
> 2 && *(p
-1) == '/' && *(p
-2) == '.')
132 *file
= g_strdup (p
+1);
134 *ext2_fname
= g_strconcat ("/dev/", dirname
, NULL
);
140 *file
= g_strdup ("");
141 *ext2_fname
= g_strconcat ("/dev/", dirname
, NULL
);
146 lsdel_proc(ext2_filsys fs
, blk_t
*block_nr
, int blockcnt
, void *private)
148 struct lsdel_struct
*lsd
= (struct lsdel_struct
*) private;
152 if (*block_nr
< fs
->super
->s_first_data_block
||
153 *block_nr
>= fs
->super
->s_blocks_count
) {
158 if (!ext2fs_test_block_bitmap(fs
->block_map
,*block_nr
))
164 /* We don't use xmalloc, since we want to recover and not abort the program
165 * if we don't have enough memory
168 undelfs_loaddel (void)
172 struct ext2_inode inode
;
173 ext2_inode_scan scan
;
177 delarray
= g_new (struct deleted_info
, max_delarray
);
179 message_1s (1, undelfserr
, _(" not enough memory "));
182 block_buf
= g_malloc (fs
->blocksize
* 3);
184 message_1s (1, undelfserr
, _(" while allocating block buffer "));
187 if ((retval
= ext2fs_open_inode_scan(fs
, 0, &scan
))){
188 message_1s1d (1, undelfserr
, _(" open_inode_scan: %d "), retval
);
191 if ((retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
))){
192 message_1s1d (1, undelfserr
, _(" while starting inode scan %d "), retval
);
198 if ((count
++ % 1024) == 0)
199 print_vfs_message (_("undelfs: loading deleted files information %d inodes"), count
);
200 if (inode
.i_dtime
== 0)
203 if (S_ISDIR(inode
.i_mode
))
211 retval
= ext2fs_block_iterate(fs
, ino
, 0, block_buf
,
214 message_1s1d (1, undelfserr
, _(" while calling ext2_block_iterate %d "), retval
);
217 if (lsd
.free_blocks
&& !lsd
.bad_blocks
) {
218 if (num_delarray
>= max_delarray
) {
220 delarray
= g_renew (struct deleted_info
, delarray
, max_delarray
);
222 message_1s (1, undelfserr
, _(" no more memory while reallocating array "));
227 delarray
[num_delarray
].ino
= ino
;
228 delarray
[num_delarray
].mode
= inode
.i_mode
;
229 delarray
[num_delarray
].uid
= inode
.i_uid
;
230 delarray
[num_delarray
].gid
= inode
.i_gid
;
231 delarray
[num_delarray
].size
= inode
.i_size
;
232 delarray
[num_delarray
].dtime
= inode
.i_dtime
;
233 delarray
[num_delarray
].num_blocks
= lsd
.num_blocks
;
234 delarray
[num_delarray
].free_blocks
= lsd
.free_blocks
;
239 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
241 message_1s1d(1, undelfserr
, _(" while doing inode scan %d "), retval
);
245 readdir_ptr
= READDIR_PTR_INIT
;
246 ext2fs_close_inode_scan (scan
);
250 ext2fs_close_inode_scan (scan
);
260 void com_err (const char *str
, long err_code
, const char *s2
, ...)
264 cptr
= g_strdup_printf (" %s (%s: %ld) ", s2
, str
, err_code
);
265 message_1s (1, _(" Ext2lib error "), cptr
);
270 undelfs_opendir (vfs
*me
, char *dirname
)
274 undelfs_get_path (dirname
, &file
, &f
);
278 /* We don't use the file name */
281 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
285 /* To avoid expensive re-scannings */
286 readdir_ptr
= READDIR_PTR_INIT
;
291 if (ext2fs_open (ext2_fname
, 0, 0, 0, unix_io_manager
, &fs
)){
292 message_2s (1, undelfserr
, _(" Could not open file %s "), ext2_fname
);
295 print_vfs_message (_("undelfs: reading inode bitmap..."));
296 if (ext2fs_read_inode_bitmap (fs
)){
297 message_2s (1, undelfserr
,
298 _(" Could not load inode bitmap from: \n %s \n"), ext2_fname
);
301 print_vfs_message (_("undelfs: reading block bitmap..."));
302 if (ext2fs_read_block_bitmap (fs
)){
303 message_2s (1, undelfserr
,
304 _(" Could not load block bitmap from: \n %s \n"), ext2_fname
);
307 /* Now load the deleted information */
308 if (!undelfs_loaddel ())
310 print_vfs_message (_("%s: done."), me
->name
);
313 print_vfs_message (_("%s: failure"), me
->name
);
321 undelfs_readdir(void *vfs_info
)
323 static union vfs_dirent undelfs_readdir_data
;
326 if (vfs_info
!= fs
) {
327 message_1s(1, undelfserr
, _(" vfs_info is not fs! "));
330 if (readdir_ptr
== num_delarray
)
332 dirent_dest
= undelfs_readdir_data
.dent
.d_name
;
334 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%s",
335 readdir_ptr
== -2 ? "." : "..");
337 g_snprintf(dirent_dest
, MC_MAXPATHLEN
, "%ld:%d",
338 (long) delarray
[readdir_ptr
].ino
,
339 delarray
[readdir_ptr
].num_blocks
);
342 compute_namelen(&undelfs_readdir_data
.dent
);
344 return &undelfs_readdir_data
;
348 undelfs_closedir (void *vfs_info
)
354 int f_index
; /* file index into delarray */
356 int error_code
; /* */
357 int pos
; /* file position */
358 int current
; /* used to determine current position in itereate */
364 /* Used by undelfs_read: */
365 char *dest_buffer
; /* destination buffer */
366 int count
; /* bytes to read */
369 /* We do not support lseek */
371 undelfs_open (vfs
*me
, char *fname
, int flags
, int mode
)
375 undelfs_file
*p
= NULL
;
377 /* Only allow reads on this file system */
378 undelfs_get_path (fname
, &file
, &f
);
382 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
383 message_1s (1, undelfserr
, _(" You have to chdir to extract files first "));
390 /* Search the file into delarray */
391 for (i
= 0; i
< num_delarray
; i
++){
392 if (inode
!= delarray
[i
].ino
)
395 /* Found: setup all the structures needed by read */
396 p
= g_new (undelfs_file
, 1);
402 p
->buf
= g_malloc (fs
->blocksize
);
414 p
->size
= delarray
[i
].size
;
423 undelfs_close (void *vfs_info
)
425 undelfs_file
*p
= vfs_info
;
433 dump_read(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
436 undelfs_file
*p
= (undelfs_file
*) private;
442 p
->error_code
= io_channel_read_blk(fs
->io
, *blocknr
,
447 memset(p
->buf
, 0, fs
->blocksize
);
449 if (p
->pos
+ p
->count
< p
->current
){
453 if (p
->pos
> p
->current
+ fs
->blocksize
){
454 p
->current
+= fs
->blocksize
;
455 return 0; /* we have not arrived yet */
458 /* Now, we know we have to extract some data */
459 if (p
->pos
>= p
->current
){
461 /* First case: starting pointer inside this block */
462 if (p
->pos
+ p
->count
<= p
->current
+ fs
->blocksize
){
463 /* Fully contained */
464 copy_count
= p
->count
;
465 p
->finished
= p
->count
;
467 /* Still some more data */
468 copy_count
= fs
->blocksize
-(p
->pos
-p
->current
);
470 memcpy (p
->dest_buffer
, p
->buf
+ (p
->pos
-p
->current
), copy_count
);
472 /* Second case: we already have passed p->pos */
473 if (p
->pos
+p
->count
< p
->current
+fs
->blocksize
){
474 copy_count
= (p
->pos
+ p
->count
) - p
->current
;
475 p
->finished
= p
->count
;
477 copy_count
= fs
->blocksize
;
479 memcpy (p
->dest_buffer
, p
->buf
, copy_count
);
481 p
->dest_buffer
+= copy_count
;
482 p
->current
+= fs
->blocksize
;
490 undelfs_read (void *vfs_info
, char *buffer
, int count
)
492 undelfs_file
*p
= vfs_info
;
495 p
->dest_buffer
= buffer
;
500 if (p
->pos
+ p
->count
> p
->size
){
501 p
->count
= p
->size
- p
->pos
;
503 retval
= ext2fs_block_iterate(fs
, p
->inode
, 0, NULL
,
506 message_1s (1, undelfserr
, _(" while iterating over blocks "));
509 if (p
->error_code
&& !p
->finished
)
511 p
->pos
= p
->pos
+ (p
->dest_buffer
- buffer
);
512 return p
->dest_buffer
- buffer
;
516 undelfs_getindex (char *path
)
518 ext2_ino_t inode
= atol (path
);
521 for (i
= 0; i
< num_delarray
; i
++){
522 if (delarray
[i
].ino
== inode
)
529 do_stat (int inode_index
, struct stat
*buf
)
532 buf
->st_ino
= delarray
[inode_index
].ino
;
533 buf
->st_mode
= delarray
[inode_index
].mode
;
535 buf
->st_uid
= delarray
[inode_index
].uid
;
536 buf
->st_gid
= delarray
[inode_index
].gid
;
537 buf
->st_size
= delarray
[inode_index
].size
;
538 buf
->st_atime
= delarray
[inode_index
].dtime
;
539 buf
->st_ctime
= delarray
[inode_index
].dtime
;
540 buf
->st_mtime
= delarray
[inode_index
].dtime
;
545 undelfs_lstat(vfs
*me
, char *path
, struct stat
*buf
)
550 undelfs_get_path (path
, &file
, &f
);
554 /* When called from save_cwd_stats we get an incorrect file and f here:
555 e.g. incorrect correct
556 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
557 file = "/dev" file="/dev/sda1"
558 f = "sda1" f ="401:1"
559 If the first char in f is no digit -> return error */
566 if (!ext2_fname
|| strcmp (ext2_fname
, file
)){
567 message_1s (1, undelfserr
, _(" You have to chdir to extract files first "));
572 inode_index
= undelfs_getindex (f
);
576 if (inode_index
== -1)
579 return do_stat (inode_index
, buf
);
583 undelfs_stat(vfs
*me
, char *path
, struct stat
*buf
)
585 return undelfs_lstat (me
, path
, buf
);
590 undelfs_fstat (void *vfs_info
, struct stat
*buf
)
592 undelfs_file
*p
= vfs_info
;
594 return do_stat (p
->f_index
, buf
);
598 undelfs_chdir(vfs
*me
, char *path
)
603 undelfs_get_path (path
, &file
, &f
);
607 /* We may use access because ext2 file systems are local */
608 /* this could be fixed by making an ext2fs io manager to use */
609 /* our vfs, but that is left as an excercise for the reader */
610 if ((fd
= open (file
, O_RDONLY
)) == -1){
611 message_2s (1, undelfserr
, _(" Could not open file %s "), file
);
622 /* this has to stay here for now: vfs layer does not know how to emulate it */
624 undelfs_lseek(void *vfs_info
, off_t offset
, int whence
)
630 undelfs_getid(vfs
*me
, char *path
, struct vfs_stamping
**parent
)
632 char *ext2_fname
, *file
;
634 /* We run only on the local fs */
636 undelfs_get_path (path
, &ext2_fname
, &file
);
646 undelfs_nothingisopen(vfsid id
)
648 return !undelfs_usage
;
652 undelfs_free(vfsid id
)
659 undelfs_init(vfs
*me
) {
660 undelfserr
= _(undelfserr
);
664 #define undelfs_init NULL
667 vfs vfs_undelfs_ops
= {
668 NULL
, /* This is place of next pointer */
671 "undel:", /* prefix */
676 NULL
, /* fill_names */
710 undelfs_nothingisopen
,
713 NULL
, /* get_local_copy */