Merge branch '2121_dir_symlink'
[kaloumi3.git] / lib / vfs / mc-vfs / undelfs.c
blobfba0f83cbe6129fb47dc75e5689703a374dfd249
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"
60 #include "src/wtools.h" /* message() */
61 #include "src/main.h" /* print_vfs_message */
62 #include "utilvfs.h"
63 #include "vfs.h"
64 #include "vfs-impl.h"
66 struct deleted_info
68 ext2_ino_t ino;
69 unsigned short mode;
70 unsigned short uid;
71 unsigned short gid;
72 unsigned long size;
73 time_t dtime;
74 int num_blocks;
75 int free_blocks;
78 struct lsdel_struct
80 ext2_ino_t inode;
81 int num_blocks;
82 int free_blocks;
83 int bad_blocks;
86 /* We only allow one opened ext2fs */
87 static char *ext2_fname;
88 static ext2_filsys fs = NULL;
89 static struct lsdel_struct lsd;
90 static struct deleted_info *delarray;
91 static int num_delarray, max_delarray;
92 static char *block_buf;
93 static const char *undelfserr = N_(" undelfs: error ");
94 static int readdir_ptr;
95 static int undelfs_usage;
96 static struct vfs_class vfs_undelfs_ops;
98 /* To generate the . and .. entries use -2 */
99 #define READDIR_PTR_INIT 0
101 static void
102 undelfs_shutdown (void)
104 if (fs)
105 ext2fs_close (fs);
106 fs = NULL;
107 g_free (ext2_fname);
108 ext2_fname = NULL;
109 g_free (delarray);
110 delarray = NULL;
111 g_free (block_buf);
112 block_buf = NULL;
115 static void
116 undelfs_get_path (const char *dirname, char **fsname, char **file)
118 const char *p;
120 /* To look like filesystem, we have virtual directories
121 /#undel:XXX, which have no subdirectories. XXX is replaced with
122 hda5, sdb8 etc, which is assumed to live under /dev.
123 -- pavel@ucw.cz */
125 *fsname = NULL;
127 if (strncmp (dirname, "/#undel:", 8))
128 return;
130 dirname += 8;
132 /* Since we don't allow subdirectories, it's easy to get a filename,
133 * just scan backwards for a slash */
134 if (*dirname == 0)
135 return;
137 p = dirname + strlen (dirname);
138 #if 0
139 /* Strip trailing ./
141 if (p - dirname > 2 && *(p - 1) == '/' && *(p - 2) == '.')
142 *(p = p - 2) = 0;
143 #endif
145 while (p > dirname)
147 if (*p == '/')
149 char *tmp;
151 *file = g_strdup (p + 1);
152 tmp = g_strndup (dirname, p - dirname);
153 *fsname = g_strconcat ("/dev/", tmp, (char *) NULL);
154 g_free (tmp);
155 return;
157 p--;
159 *file = g_strdup ("");
160 *fsname = g_strconcat ("/dev/", dirname, (char *) NULL);
161 return;
164 static int
165 undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *private)
167 struct lsdel_struct *_lsd = (struct lsdel_struct *) private;
168 (void) blockcnt;
169 _lsd->num_blocks++;
171 if (*block_nr < _fs->super->s_first_data_block || *block_nr >= _fs->super->s_blocks_count)
173 _lsd->bad_blocks++;
174 return BLOCK_ABORT;
177 if (!ext2fs_test_block_bitmap (_fs->block_map, *block_nr))
178 _lsd->free_blocks++;
180 return 0;
184 * Load information about deleted files.
185 * Don't abort if there is not enough memory - load as much as we can.
187 static int
188 undelfs_loaddel (void)
190 int retval, count;
191 ext2_ino_t ino;
192 struct ext2_inode inode;
193 ext2_inode_scan scan;
195 max_delarray = 100;
196 num_delarray = 0;
197 delarray = g_try_malloc (sizeof (struct deleted_info) * max_delarray);
198 if (!delarray)
200 message (D_ERROR, undelfserr, _(" not enough memory "));
201 return 0;
203 block_buf = g_try_malloc (fs->blocksize * 3);
204 if (!block_buf)
206 message (D_ERROR, undelfserr, _(" while allocating block buffer "));
207 goto free_delarray;
209 if ((retval = ext2fs_open_inode_scan (fs, 0, &scan)))
211 message (D_ERROR, undelfserr, _(" open_inode_scan: %d "), retval);
212 goto free_block_buf;
214 if ((retval = ext2fs_get_next_inode (scan, &ino, &inode)))
216 message (D_ERROR, undelfserr, _(" while starting inode scan %d "), retval);
217 goto error_out;
220 count = 0;
221 while (ino)
223 if ((count++ % 1024) == 0)
224 print_vfs_message (_("undelfs: loading deleted files information %d inodes"), count);
225 if (inode.i_dtime == 0)
226 goto next;
228 if (S_ISDIR (inode.i_mode))
229 goto next;
231 lsd.inode = ino;
232 lsd.num_blocks = 0;
233 lsd.free_blocks = 0;
234 lsd.bad_blocks = 0;
236 retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
237 if (retval)
239 message (D_ERROR, undelfserr, _(" while calling ext2_block_iterate %d "), retval);
240 goto next;
242 if (lsd.free_blocks && !lsd.bad_blocks)
244 if (num_delarray >= max_delarray)
246 struct deleted_info *delarray_new = g_try_realloc (delarray,
247 sizeof (struct deleted_info) *
248 (max_delarray + 50));
249 if (!delarray_new)
251 message (D_ERROR, undelfserr, _(" no more memory while reallocating array "));
252 goto error_out;
254 delarray = delarray_new;
255 max_delarray += 50;
258 delarray[num_delarray].ino = ino;
259 delarray[num_delarray].mode = inode.i_mode;
260 delarray[num_delarray].uid = inode.i_uid;
261 delarray[num_delarray].gid = inode.i_gid;
262 delarray[num_delarray].size = inode.i_size;
263 delarray[num_delarray].dtime = inode.i_dtime;
264 delarray[num_delarray].num_blocks = lsd.num_blocks;
265 delarray[num_delarray].free_blocks = lsd.free_blocks;
266 num_delarray++;
269 next:
270 retval = ext2fs_get_next_inode (scan, &ino, &inode);
271 if (retval)
273 message (D_ERROR, undelfserr, _(" while doing inode scan %d "), retval);
274 goto error_out;
277 readdir_ptr = READDIR_PTR_INIT;
278 ext2fs_close_inode_scan (scan);
279 return 1;
281 error_out:
282 ext2fs_close_inode_scan (scan);
283 free_block_buf:
284 g_free (block_buf);
285 block_buf = NULL;
286 free_delarray:
287 g_free (delarray);
288 delarray = NULL;
289 return 0;
294 * This function overrides com_err() from libcom_err library.
295 * It is used in libext2fs to report errors.
297 void
298 com_err (const char *whoami, long err_code, const char *fmt, ...)
300 va_list ap;
301 char *str;
303 va_start (ap, fmt);
304 str = g_strdup_vprintf (fmt, ap);
305 va_end (ap);
307 message (D_ERROR, _(" Ext2lib error "), " %s (%s: %ld) ", str, whoami, err_code);
308 g_free (str);
311 static void *
312 undelfs_opendir (struct vfs_class *me, const char *dirname)
314 char *file, *f;
316 undelfs_get_path (dirname, &file, &f);
317 if (!file)
318 return 0;
320 /* We don't use the file name */
321 g_free (f);
323 if (!ext2_fname || strcmp (ext2_fname, file))
325 undelfs_shutdown ();
326 ext2_fname = file;
328 else
330 /* To avoid expensive re-scannings */
331 readdir_ptr = READDIR_PTR_INIT;
332 g_free (file);
333 return fs;
336 if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
338 message (D_ERROR, undelfserr, _(" Cannot open file %s "), ext2_fname);
339 return 0;
341 print_vfs_message (_("undelfs: reading inode bitmap..."));
342 if (ext2fs_read_inode_bitmap (fs))
344 message (D_ERROR, undelfserr, _(" Cannot load inode bitmap from: \n %s \n"), ext2_fname);
345 goto quit_opendir;
347 print_vfs_message (_("undelfs: reading block bitmap..."));
348 if (ext2fs_read_block_bitmap (fs))
350 message (D_ERROR, undelfserr, _(" Cannot load block bitmap from: \n %s \n"), ext2_fname);
351 goto quit_opendir;
353 /* Now load the deleted information */
354 if (!undelfs_loaddel ())
355 goto quit_opendir;
356 print_vfs_message (_("%s: done."), me->name);
357 return fs;
358 quit_opendir:
359 print_vfs_message (_("%s: failure"), me->name);
360 ext2fs_close (fs);
361 fs = NULL;
362 return 0;
366 static void *
367 undelfs_readdir (void *vfs_info)
369 static union vfs_dirent undelfs_readdir_data;
370 static char *const dirent_dest = undelfs_readdir_data.dent.d_name;
372 if (vfs_info != fs)
374 message (D_ERROR, undelfserr, _(" vfs_info is not fs! "));
375 return NULL;
377 if (readdir_ptr == num_delarray)
378 return NULL;
379 if (readdir_ptr < 0)
380 strcpy (dirent_dest, readdir_ptr == -2 ? "." : "..");
381 else
382 g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
383 (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
384 readdir_ptr++;
386 compute_namelen (&undelfs_readdir_data.dent);
388 return &undelfs_readdir_data;
391 static int
392 undelfs_closedir (void *vfs_info)
394 (void) vfs_info;
395 return 0;
398 typedef struct
400 int f_index; /* file index into delarray */
401 char *buf;
402 int error_code; /* */
403 int pos; /* file position */
404 int current; /* used to determine current position in itereate */
405 int finished;
406 ext2_ino_t inode;
407 int bytes_read;
408 long size;
410 /* Used by undelfs_read: */
411 char *dest_buffer; /* destination buffer */
412 int count; /* bytes to read */
413 } undelfs_file;
415 /* We do not support lseek */
416 static void *
417 undelfs_open (struct vfs_class *me, const char *fname, int flags, int mode)
419 char *file, *f;
420 ext2_ino_t inode, i;
421 undelfs_file *p = NULL;
422 (void) me;
423 (void) flags;
424 (void) mode;
426 /* Only allow reads on this file system */
427 undelfs_get_path (fname, &file, &f);
428 if (!file)
429 return 0;
431 if (!ext2_fname || strcmp (ext2_fname, file))
433 message (D_ERROR, undelfserr, _(" You have to chdir to extract files first "));
434 g_free (file);
435 g_free (f);
436 return 0;
438 inode = atol (f);
440 /* Search the file into delarray */
441 for (i = 0; i < (ext2_ino_t) num_delarray; i++)
443 if (inode != delarray[i].ino)
444 continue;
446 /* Found: setup all the structures needed by read */
447 p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
448 if (!p)
450 g_free (file);
451 g_free (f);
452 return 0;
454 p->buf = g_try_malloc (fs->blocksize);
455 if (!p->buf)
457 g_free (p);
458 g_free (file);
459 g_free (f);
460 return 0;
462 p->inode = inode;
463 p->finished = 0;
464 p->f_index = i;
465 p->error_code = 0;
466 p->pos = 0;
467 p->size = delarray[i].size;
469 g_free (file);
470 g_free (f);
471 undelfs_usage++;
472 return p;
475 static int
476 undelfs_close (void *vfs_info)
478 undelfs_file *p = vfs_info;
479 g_free (p->buf);
480 g_free (p);
481 undelfs_usage--;
482 return 0;
485 static int
486 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
488 int copy_count;
489 undelfs_file *p = (undelfs_file *) private;
491 if (blockcnt < 0)
492 return 0;
494 if (*blocknr)
496 p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
497 if (p->error_code)
498 return BLOCK_ABORT;
500 else
501 memset (p->buf, 0, param_fs->blocksize);
503 if (p->pos + p->count < p->current)
505 p->finished = 1;
506 return BLOCK_ABORT;
508 if ((size_t) p->pos > p->current + param_fs->blocksize)
510 p->current += param_fs->blocksize;
511 return 0; /* we have not arrived yet */
514 /* Now, we know we have to extract some data */
515 if (p->pos >= p->current)
518 /* First case: starting pointer inside this block */
519 if ((size_t) (p->pos + p->count) <= p->current + param_fs->blocksize)
521 /* Fully contained */
522 copy_count = p->count;
523 p->finished = p->count;
525 else
527 /* Still some more data */
528 copy_count = param_fs->blocksize - (p->pos - p->current);
530 memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
532 else
534 /* Second case: we already have passed p->pos */
535 if ((size_t) (p->pos + p->count) < p->current + param_fs->blocksize)
537 copy_count = (p->pos + p->count) - p->current;
538 p->finished = p->count;
540 else
542 copy_count = param_fs->blocksize;
544 memcpy (p->dest_buffer, p->buf, copy_count);
546 p->dest_buffer += copy_count;
547 p->current += param_fs->blocksize;
548 if (p->finished)
550 return BLOCK_ABORT;
552 return 0;
555 static ssize_t
556 undelfs_read (void *vfs_info, char *buffer, int count)
558 undelfs_file *p = vfs_info;
559 int retval;
561 p->dest_buffer = buffer;
562 p->current = 0;
563 p->finished = 0;
564 p->count = count;
566 if (p->pos + p->count > p->size)
568 p->count = p->size - p->pos;
570 retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
571 if (retval)
573 message (D_ERROR, undelfserr, _(" while iterating over blocks "));
574 return -1;
576 if (p->error_code && !p->finished)
577 return 0;
578 p->pos = p->pos + (p->dest_buffer - buffer);
579 return p->dest_buffer - buffer;
582 static long
583 undelfs_getindex (char *path)
585 ext2_ino_t inode = atol (path);
586 int i;
588 for (i = 0; i < num_delarray; i++)
590 if (delarray[i].ino == inode)
591 return i;
593 return -1;
596 static int
597 undelfs_stat_int (int inode_index, struct stat *buf)
599 buf->st_dev = 0;
600 buf->st_ino = delarray[inode_index].ino;
601 buf->st_mode = delarray[inode_index].mode;
602 buf->st_nlink = 1;
603 buf->st_uid = delarray[inode_index].uid;
604 buf->st_gid = delarray[inode_index].gid;
605 buf->st_size = delarray[inode_index].size;
606 buf->st_atime = delarray[inode_index].dtime;
607 buf->st_ctime = delarray[inode_index].dtime;
608 buf->st_mtime = delarray[inode_index].dtime;
609 return 0;
612 static int
613 undelfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
615 int inode_index;
616 char *file, *f;
617 (void) me;
619 undelfs_get_path (path, &file, &f);
620 if (!file)
621 return 0;
623 /* When called from save_cwd_stats we get an incorrect file and f here:
624 e.g. incorrect correct
625 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
626 file = "/dev" file="/dev/sda1"
627 f = "sda1" f ="401:1"
628 If the first char in f is no digit -> return error */
629 if (!isdigit (*f))
631 g_free (file);
632 g_free (f);
633 return -1;
636 if (!ext2_fname || strcmp (ext2_fname, file))
638 message (D_ERROR, undelfserr, _(" You have to chdir to extract files first "));
639 g_free (file);
640 g_free (f);
641 return 0;
643 inode_index = undelfs_getindex (f);
644 g_free (file);
645 g_free (f);
647 if (inode_index == -1)
648 return -1;
650 return undelfs_stat_int (inode_index, buf);
653 #define undelfs_stat undelfs_lstat
655 static int
656 undelfs_fstat (void *vfs_info, struct stat *buf)
658 undelfs_file *p = vfs_info;
660 return undelfs_stat_int (p->f_index, buf);
663 static int
664 undelfs_chdir (struct vfs_class *me, const char *path)
666 char *file, *f;
667 int fd;
668 (void) me;
670 undelfs_get_path (path, &file, &f);
671 if (!file)
672 return -1;
674 /* We may use access because ext2 file systems are local */
675 /* this could be fixed by making an ext2fs io manager to use */
676 /* our vfs, but that is left as an excercise for the reader */
677 if ((fd = open (file, O_RDONLY)) == -1)
679 message (D_ERROR, undelfserr, _(" Cannot open file %s "), file);
680 g_free (f);
681 g_free (file);
682 return -1;
684 close (fd);
685 g_free (f);
686 g_free (file);
687 return 0;
690 /* this has to stay here for now: vfs layer does not know how to emulate it */
691 static off_t
692 undelfs_lseek (void *vfs_info, off_t offset, int whence)
694 (void) vfs_info;
695 (void) offset;
696 (void) whence;
698 return -1;
701 static vfsid
702 undelfs_getid (struct vfs_class *me, const char *path)
704 char *fname, *fsname;
705 (void) me;
707 undelfs_get_path (path, &fsname, &fname);
709 if (!fsname)
710 return NULL;
711 g_free (fname);
712 g_free (fsname);
713 return (vfsid) fs;
716 static int
717 undelfs_nothingisopen (vfsid id)
719 (void) id;
721 return !undelfs_usage;
724 static void
725 undelfs_free (vfsid id)
727 (void) id;
729 undelfs_shutdown ();
732 #ifdef ENABLE_NLS
733 static int
734 undelfs_init (struct vfs_class *me)
736 (void) me;
738 undelfserr = _(undelfserr);
739 return 1;
741 #else
742 #define undelfs_init NULL
743 #endif
745 void
746 init_undelfs (void)
748 vfs_undelfs_ops.name = "undelfs";
749 vfs_undelfs_ops.prefix = "undel:";
750 vfs_undelfs_ops.init = undelfs_init;
751 vfs_undelfs_ops.open = undelfs_open;
752 vfs_undelfs_ops.close = undelfs_close;
753 vfs_undelfs_ops.read = undelfs_read;
754 vfs_undelfs_ops.opendir = undelfs_opendir;
755 vfs_undelfs_ops.readdir = undelfs_readdir;
756 vfs_undelfs_ops.closedir = undelfs_closedir;
757 vfs_undelfs_ops.stat = undelfs_stat;
758 vfs_undelfs_ops.lstat = undelfs_lstat;
759 vfs_undelfs_ops.fstat = undelfs_fstat;
760 vfs_undelfs_ops.chdir = undelfs_chdir;
761 vfs_undelfs_ops.lseek = undelfs_lseek;
762 vfs_undelfs_ops.getid = undelfs_getid;
763 vfs_undelfs_ops.nothingisopen = undelfs_nothingisopen;
764 vfs_undelfs_ops.free = undelfs_free;
765 vfs_register_class (&vfs_undelfs_ops);