Following prototypes of functions was changed in VFS-module API:
[midnight-commander.git] / src / vfs / undelfs / undelfs.c
blob04a79df4e9612bd4cb40f6cfbdbec005aa507316
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.
12 1997 Norbert Warmuth.
13 2000 Pavel Machek
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. */
29 /**
30 * \file
31 * \brief Source: UnDel File System
33 * Assumptions:
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
41 #include <config.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <fcntl.h>
48 #ifdef HAVE_EXT2FS_EXT2_FS_H
49 #include <ext2fs/ext2_fs.h>
50 #else
51 /* asm/types.h defines its own umode_t */
52 #undef umode_t
53 #include <linux/ext2_fs.h>
54 #endif
56 #include <ext2fs/ext2fs.h>
57 #include <ctype.h>
59 #include "lib/global.h"
61 #include "lib/widget.h" /* message() */
62 #include "lib/vfs/utilvfs.h"
63 #include "lib/vfs/vfs.h"
65 #include "undelfs.h"
67 /*** global variables ****************************************************************************/
69 /*** file scope macro definitions ****************************************************************/
71 /* To generate the . and .. entries use -2 */
72 #define READDIR_PTR_INIT 0
74 #define undelfs_stat undelfs_lstat
76 /*** file scope type declarations ****************************************************************/
78 struct deleted_info
80 ext2_ino_t ino;
81 unsigned short mode;
82 unsigned short uid;
83 unsigned short gid;
84 unsigned long size;
85 time_t dtime;
86 int num_blocks;
87 int free_blocks;
90 struct lsdel_struct
92 ext2_ino_t inode;
93 int num_blocks;
94 int free_blocks;
95 int bad_blocks;
98 typedef struct
100 int f_index; /* file index into delarray */
101 char *buf;
102 int error_code; /* */
103 off_t pos; /* file position */
104 off_t current; /* used to determine current position in itereate */
105 gboolean finished;
106 ext2_ino_t inode;
107 int bytes_read;
108 off_t size;
110 /* Used by undelfs_read: */
111 char *dest_buffer; /* destination buffer */
112 size_t count; /* bytes to read */
113 } undelfs_file;
115 /*** file scope variables ************************************************************************/
117 /* We only allow one opened ext2fs */
118 static char *ext2_fname;
119 static ext2_filsys fs = NULL;
120 static struct lsdel_struct lsd;
121 static struct deleted_info *delarray;
122 static int num_delarray, max_delarray;
123 static char *block_buf;
124 static const char *undelfserr = N_("undelfs: error");
125 static int readdir_ptr;
126 static int undelfs_usage;
127 static struct vfs_class vfs_undelfs_ops;
129 /*** file scope functions ************************************************************************/
130 /* --------------------------------------------------------------------------------------------- */
132 static void
133 undelfs_shutdown (void)
135 if (fs)
136 ext2fs_close (fs);
137 fs = NULL;
138 g_free (ext2_fname);
139 ext2_fname = NULL;
140 g_free (delarray);
141 delarray = NULL;
142 g_free (block_buf);
143 block_buf = NULL;
146 /* --------------------------------------------------------------------------------------------- */
148 static void
149 undelfs_get_path (const char *dirname, char **fsname, char **file)
151 const char *p;
153 /* To look like filesystem, we have virtual directories
154 /#undel:XXX, which have no subdirectories. XXX is replaced with
155 hda5, sdb8 etc, which is assumed to live under /dev.
156 -- pavel@ucw.cz */
158 *fsname = NULL;
160 if (strncmp (dirname, "/#undel:", 8))
161 return;
163 dirname += 8;
165 /* Since we don't allow subdirectories, it's easy to get a filename,
166 * just scan backwards for a slash */
167 if (*dirname == 0)
168 return;
170 p = dirname + strlen (dirname);
171 #if 0
172 /* Strip trailing ./
174 if (p - dirname > 2 && *(p - 1) == '/' && *(p - 2) == '.')
175 *(p = p - 2) = 0;
176 #endif
178 while (p > dirname)
180 if (*p == '/')
182 char *tmp;
184 *file = g_strdup (p + 1);
185 tmp = g_strndup (dirname, p - dirname);
186 *fsname = g_strconcat ("/dev/", tmp, (char *) NULL);
187 g_free (tmp);
188 return;
190 p--;
192 *file = g_strdup ("");
193 *fsname = g_strconcat ("/dev/", dirname, (char *) NULL);
194 return;
197 /* --------------------------------------------------------------------------------------------- */
199 static int
200 undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *private)
202 struct lsdel_struct *_lsd = (struct lsdel_struct *) private;
203 (void) blockcnt;
204 _lsd->num_blocks++;
206 if (*block_nr < _fs->super->s_first_data_block || *block_nr >= _fs->super->s_blocks_count)
208 _lsd->bad_blocks++;
209 return BLOCK_ABORT;
212 if (!ext2fs_test_block_bitmap (_fs->block_map, *block_nr))
213 _lsd->free_blocks++;
215 return 0;
218 /* --------------------------------------------------------------------------------------------- */
220 * Load information about deleted files.
221 * Don't abort if there is not enough memory - load as much as we can.
224 static int
225 undelfs_loaddel (void)
227 int retval, count;
228 ext2_ino_t ino;
229 struct ext2_inode inode;
230 ext2_inode_scan scan;
232 max_delarray = 100;
233 num_delarray = 0;
234 delarray = g_try_malloc (sizeof (struct deleted_info) * max_delarray);
235 if (!delarray)
237 message (D_ERROR, undelfserr, _("not enough memory"));
238 return 0;
240 block_buf = g_try_malloc (fs->blocksize * 3);
241 if (!block_buf)
243 message (D_ERROR, undelfserr, _("while allocating block buffer"));
244 goto free_delarray;
246 retval = ext2fs_open_inode_scan (fs, 0, &scan);
247 if (retval != 0)
249 message (D_ERROR, undelfserr, _("open_inode_scan: %d"), retval);
250 goto free_block_buf;
252 retval = ext2fs_get_next_inode (scan, &ino, &inode);
253 if (retval != 0)
255 message (D_ERROR, undelfserr, _("while starting inode scan %d"), retval);
256 goto error_out;
258 count = 0;
259 while (ino)
261 if ((count++ % 1024) == 0)
262 vfs_print_message (_("undelfs: loading deleted files information %d inodes"), count);
263 if (inode.i_dtime == 0)
264 goto next;
266 if (S_ISDIR (inode.i_mode))
267 goto next;
269 lsd.inode = ino;
270 lsd.num_blocks = 0;
271 lsd.free_blocks = 0;
272 lsd.bad_blocks = 0;
274 retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
275 if (retval)
277 message (D_ERROR, undelfserr, _("while calling ext2_block_iterate %d"), retval);
278 goto next;
280 if (lsd.free_blocks && !lsd.bad_blocks)
282 if (num_delarray >= max_delarray)
284 struct deleted_info *delarray_new = g_try_realloc (delarray,
285 sizeof (struct deleted_info) *
286 (max_delarray + 50));
287 if (!delarray_new)
289 message (D_ERROR, undelfserr, _("no more memory while reallocating array"));
290 goto error_out;
292 delarray = delarray_new;
293 max_delarray += 50;
296 delarray[num_delarray].ino = ino;
297 delarray[num_delarray].mode = inode.i_mode;
298 delarray[num_delarray].uid = inode.i_uid;
299 delarray[num_delarray].gid = inode.i_gid;
300 delarray[num_delarray].size = inode.i_size;
301 delarray[num_delarray].dtime = inode.i_dtime;
302 delarray[num_delarray].num_blocks = lsd.num_blocks;
303 delarray[num_delarray].free_blocks = lsd.free_blocks;
304 num_delarray++;
307 next:
308 retval = ext2fs_get_next_inode (scan, &ino, &inode);
309 if (retval)
311 message (D_ERROR, undelfserr, _("while doing inode scan %d"), retval);
312 goto error_out;
315 readdir_ptr = READDIR_PTR_INIT;
316 ext2fs_close_inode_scan (scan);
317 return 1;
319 error_out:
320 ext2fs_close_inode_scan (scan);
321 free_block_buf:
322 g_free (block_buf);
323 block_buf = NULL;
324 free_delarray:
325 g_free (delarray);
326 delarray = NULL;
327 return 0;
330 /* --------------------------------------------------------------------------------------------- */
332 static void *
333 undelfs_opendir (const vfs_path_t * vpath)
335 char *file, *f;
336 vfs_path_element_t *path_element;
338 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
339 undelfs_get_path (vpath->unparsed, &file, &f);
340 if (!file)
341 return 0;
343 /* We don't use the file name */
344 g_free (f);
346 if (!ext2_fname || strcmp (ext2_fname, file))
348 undelfs_shutdown ();
349 ext2_fname = file;
351 else
353 /* To avoid expensive re-scannings */
354 readdir_ptr = READDIR_PTR_INIT;
355 g_free (file);
356 return fs;
359 if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
361 message (D_ERROR, undelfserr, _("Cannot open file %s"), ext2_fname);
362 return 0;
364 vfs_print_message (_("undelfs: reading inode bitmap..."));
365 if (ext2fs_read_inode_bitmap (fs))
367 message (D_ERROR, undelfserr, _("Cannot load inode bitmap from:\n%s"), ext2_fname);
368 goto quit_opendir;
370 vfs_print_message (_("undelfs: reading block bitmap..."));
371 if (ext2fs_read_block_bitmap (fs))
373 message (D_ERROR, undelfserr, _("Cannot load block bitmap from:\n%s"), ext2_fname);
374 goto quit_opendir;
376 /* Now load the deleted information */
377 if (!undelfs_loaddel ())
378 goto quit_opendir;
379 vfs_print_message (_("%s: done."), path_element->class->name);
380 return fs;
381 quit_opendir:
382 vfs_print_message (_("%s: failure"), path_element->class->name);
383 ext2fs_close (fs);
384 fs = NULL;
385 return 0;
388 /* --------------------------------------------------------------------------------------------- */
390 static void *
391 undelfs_readdir (void *vfs_info)
393 static union vfs_dirent undelfs_readdir_data;
394 static char *const dirent_dest = undelfs_readdir_data.dent.d_name;
396 if (vfs_info != fs)
398 message (D_ERROR, undelfserr, _("vfs_info is not fs!"));
399 return NULL;
401 if (readdir_ptr == num_delarray)
402 return NULL;
403 if (readdir_ptr < 0)
404 strcpy (dirent_dest, readdir_ptr == -2 ? "." : "..");
405 else
406 g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
407 (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
408 readdir_ptr++;
410 compute_namelen (&undelfs_readdir_data.dent);
412 return &undelfs_readdir_data;
415 /* --------------------------------------------------------------------------------------------- */
417 static int
418 undelfs_closedir (void *vfs_info)
420 (void) vfs_info;
421 return 0;
424 /* --------------------------------------------------------------------------------------------- */
425 /* We do not support lseek */
427 static void *
428 undelfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
430 char *file, *f;
431 ext2_ino_t inode, i;
432 undelfs_file *p = NULL;
433 (void) flags;
434 (void) mode;
436 /* Only allow reads on this file system */
437 undelfs_get_path (vpath->unparsed, &file, &f);
438 if (!file)
439 return 0;
441 if (!ext2_fname || strcmp (ext2_fname, file))
443 message (D_ERROR, undelfserr, _("You have to chdir to extract files first"));
444 g_free (file);
445 g_free (f);
446 return 0;
448 inode = atol (f);
450 /* Search the file into delarray */
451 for (i = 0; i < (ext2_ino_t) num_delarray; i++)
453 if (inode != delarray[i].ino)
454 continue;
456 /* Found: setup all the structures needed by read */
457 p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
458 if (!p)
460 g_free (file);
461 g_free (f);
462 return 0;
464 p->buf = g_try_malloc (fs->blocksize);
465 if (!p->buf)
467 g_free (p);
468 g_free (file);
469 g_free (f);
470 return 0;
472 p->inode = inode;
473 p->finished = FALSE;
474 p->f_index = i;
475 p->error_code = 0;
476 p->pos = 0;
477 p->size = delarray[i].size;
479 g_free (file);
480 g_free (f);
481 undelfs_usage++;
482 return p;
485 /* --------------------------------------------------------------------------------------------- */
487 static int
488 undelfs_close (void *vfs_info)
490 undelfs_file *p = vfs_info;
491 g_free (p->buf);
492 g_free (p);
493 undelfs_usage--;
494 return 0;
497 /* --------------------------------------------------------------------------------------------- */
499 static int
500 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
502 int copy_count;
503 undelfs_file *p = (undelfs_file *) private;
505 if (blockcnt < 0)
506 return 0;
508 if (*blocknr)
510 p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
511 if (p->error_code)
512 return BLOCK_ABORT;
514 else
515 memset (p->buf, 0, param_fs->blocksize);
517 if (p->pos + (off_t) p->count < p->current)
519 p->finished = TRUE;
520 return BLOCK_ABORT;
522 if (p->pos > p->current + param_fs->blocksize)
524 p->current += param_fs->blocksize;
525 return 0; /* we have not arrived yet */
528 /* Now, we know we have to extract some data */
529 if (p->pos >= p->current)
532 /* First case: starting pointer inside this block */
533 if (p->pos + (off_t) p->count <= p->current + param_fs->blocksize)
535 /* Fully contained */
536 copy_count = p->count;
537 p->finished = (p->count != 0);
539 else
541 /* Still some more data */
542 copy_count = param_fs->blocksize - (p->pos - p->current);
544 memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
546 else
548 /* Second case: we already have passed p->pos */
549 if (p->pos + (off_t) p->count < p->current + param_fs->blocksize)
551 copy_count = (p->pos + p->count) - p->current;
552 p->finished = (p->count != 0);
554 else
556 copy_count = param_fs->blocksize;
558 memcpy (p->dest_buffer, p->buf, copy_count);
560 p->dest_buffer += copy_count;
561 p->current += param_fs->blocksize;
562 if (p->finished)
564 return BLOCK_ABORT;
566 return 0;
569 /* --------------------------------------------------------------------------------------------- */
571 static ssize_t
572 undelfs_read (void *vfs_info, char *buffer, size_t count)
574 undelfs_file *p = vfs_info;
575 int retval;
577 p->dest_buffer = buffer;
578 p->current = 0;
579 p->finished = FALSE;
580 p->count = count;
582 if (p->pos + (off_t) p->count > p->size)
584 p->count = p->size - p->pos;
586 retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
587 if (retval)
589 message (D_ERROR, undelfserr, _("while iterating over blocks"));
590 return -1;
592 if (p->error_code && !p->finished)
593 return 0;
594 p->pos = p->pos + (p->dest_buffer - buffer);
595 return p->dest_buffer - buffer;
598 /* --------------------------------------------------------------------------------------------- */
600 static long
601 undelfs_getindex (char *path)
603 ext2_ino_t inode = atol (path);
604 int i;
606 for (i = 0; i < num_delarray; i++)
608 if (delarray[i].ino == inode)
609 return i;
611 return -1;
614 /* --------------------------------------------------------------------------------------------- */
616 static int
617 undelfs_stat_int (int inode_index, struct stat *buf)
619 buf->st_dev = 0;
620 buf->st_ino = delarray[inode_index].ino;
621 buf->st_mode = delarray[inode_index].mode;
622 buf->st_nlink = 1;
623 buf->st_uid = delarray[inode_index].uid;
624 buf->st_gid = delarray[inode_index].gid;
625 buf->st_size = delarray[inode_index].size;
626 buf->st_atime = delarray[inode_index].dtime;
627 buf->st_ctime = delarray[inode_index].dtime;
628 buf->st_mtime = delarray[inode_index].dtime;
629 return 0;
632 /* --------------------------------------------------------------------------------------------- */
634 static int
635 undelfs_lstat (const vfs_path_t * vpath, struct stat *buf)
637 int inode_index;
638 char *file, *f;
640 undelfs_get_path (vpath->unparsed, &file, &f);
641 if (!file)
642 return 0;
644 /* When called from save_cwd_stats we get an incorrect file and f here:
645 e.g. incorrect correct
646 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
647 file = "/dev" file="/dev/sda1"
648 f = "sda1" f ="401:1"
649 If the first char in f is no digit -> return error */
650 if (!isdigit (*f))
652 g_free (file);
653 g_free (f);
654 return -1;
657 if (!ext2_fname || strcmp (ext2_fname, file))
659 message (D_ERROR, undelfserr, _("You have to chdir to extract files first"));
660 g_free (file);
661 g_free (f);
662 return 0;
664 inode_index = undelfs_getindex (f);
665 g_free (file);
666 g_free (f);
668 if (inode_index == -1)
669 return -1;
671 return undelfs_stat_int (inode_index, buf);
674 /* --------------------------------------------------------------------------------------------- */
676 static int
677 undelfs_fstat (void *vfs_info, struct stat *buf)
679 undelfs_file *p = vfs_info;
681 return undelfs_stat_int (p->f_index, buf);
684 /* --------------------------------------------------------------------------------------------- */
686 static int
687 undelfs_chdir (const vfs_path_t * vpath)
689 char *file, *f;
690 int fd;
692 undelfs_get_path (vpath->unparsed, &file, &f);
693 if (!file)
694 return -1;
696 /* We may use access because ext2 file systems are local */
697 /* this could be fixed by making an ext2fs io manager to use */
698 /* our vfs, but that is left as an excercise for the reader */
699 fd = open (file, O_RDONLY);
700 if (fd == -1)
702 message (D_ERROR, undelfserr, _("Cannot open file \"%s\""), file);
703 g_free (f);
704 g_free (file);
705 return -1;
707 close (fd);
708 g_free (f);
709 g_free (file);
710 return 0;
713 /* --------------------------------------------------------------------------------------------- */
715 /* this has to stay here for now: vfs layer does not know how to emulate it */
716 static off_t
717 undelfs_lseek (void *vfs_info, off_t offset, int whence)
719 (void) vfs_info;
720 (void) offset;
721 (void) whence;
723 return -1;
726 /* --------------------------------------------------------------------------------------------- */
728 static vfsid
729 undelfs_getid (const vfs_path_t * vpath)
731 char *fname, *fsname;
733 undelfs_get_path (vpath->unparsed, &fsname, &fname);
735 if (!fsname)
736 return NULL;
737 g_free (fname);
738 g_free (fsname);
739 return (vfsid) fs;
742 /* --------------------------------------------------------------------------------------------- */
744 static int
745 undelfs_nothingisopen (vfsid id)
747 (void) id;
749 return !undelfs_usage;
752 /* --------------------------------------------------------------------------------------------- */
754 static void
755 undelfs_free (vfsid id)
757 (void) id;
759 undelfs_shutdown ();
762 /* --------------------------------------------------------------------------------------------- */
764 #ifdef ENABLE_NLS
765 static int
766 undelfs_init (struct vfs_class *me)
768 (void) me;
770 undelfserr = _(undelfserr);
771 return 1;
773 #else
774 #define undelfs_init NULL
775 #endif
777 /* --------------------------------------------------------------------------------------------- */
778 /*** public functions ****************************************************************************/
779 /* --------------------------------------------------------------------------------------------- */
781 * This function overrides com_err() from libcom_err library.
782 * It is used in libext2fs to report errors.
785 void
786 com_err (const char *whoami, long err_code, const char *fmt, ...)
788 va_list ap;
789 char *str;
791 va_start (ap, fmt);
792 str = g_strdup_vprintf (fmt, ap);
793 va_end (ap);
795 message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code);
796 g_free (str);
799 /* --------------------------------------------------------------------------------------------- */
801 void
802 init_undelfs (void)
804 vfs_undelfs_ops.name = "undelfs";
805 vfs_undelfs_ops.prefix = "undel:";
806 vfs_undelfs_ops.init = undelfs_init;
807 vfs_undelfs_ops.open = undelfs_open;
808 vfs_undelfs_ops.close = undelfs_close;
809 vfs_undelfs_ops.read = undelfs_read;
810 vfs_undelfs_ops.opendir = undelfs_opendir;
811 vfs_undelfs_ops.readdir = undelfs_readdir;
812 vfs_undelfs_ops.closedir = undelfs_closedir;
813 vfs_undelfs_ops.stat = undelfs_stat;
814 vfs_undelfs_ops.lstat = undelfs_lstat;
815 vfs_undelfs_ops.fstat = undelfs_fstat;
816 vfs_undelfs_ops.chdir = undelfs_chdir;
817 vfs_undelfs_ops.lseek = undelfs_lseek;
818 vfs_undelfs_ops.getid = undelfs_getid;
819 vfs_undelfs_ops.nothingisopen = undelfs_nothingisopen;
820 vfs_undelfs_ops.free = undelfs_free;
821 vfs_register_class (&vfs_undelfs_ops);
824 /* --------------------------------------------------------------------------------------------- */