Ticket #2097: clean up before 4.7.2 release.
[midnight-commander.git] / lib / vfs / mc-vfs / undelfs.c
blob532f5ac2d5074b35e5250f199ae5d528258510b5
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 retval = ext2fs_open_inode_scan (fs, 0, &scan);
210 if (retval != 0)
212 message (D_ERROR, undelfserr, _(" open_inode_scan: %d "), retval);
213 goto free_block_buf;
215 retval = ext2fs_get_next_inode (scan, &ino, &inode);
216 if (retval != 0)
218 message (D_ERROR, undelfserr, _(" while starting inode scan %d "), retval);
219 goto error_out;
221 count = 0;
222 while (ino)
224 if ((count++ % 1024) == 0)
225 print_vfs_message (_("undelfs: loading deleted files information %d inodes"), count);
226 if (inode.i_dtime == 0)
227 goto next;
229 if (S_ISDIR (inode.i_mode))
230 goto next;
232 lsd.inode = ino;
233 lsd.num_blocks = 0;
234 lsd.free_blocks = 0;
235 lsd.bad_blocks = 0;
237 retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
238 if (retval)
240 message (D_ERROR, undelfserr, _(" while calling ext2_block_iterate %d "), retval);
241 goto next;
243 if (lsd.free_blocks && !lsd.bad_blocks)
245 if (num_delarray >= max_delarray)
247 struct deleted_info *delarray_new = g_try_realloc (delarray,
248 sizeof (struct deleted_info) *
249 (max_delarray + 50));
250 if (!delarray_new)
252 message (D_ERROR, undelfserr, _(" no more memory while reallocating array "));
253 goto error_out;
255 delarray = delarray_new;
256 max_delarray += 50;
259 delarray[num_delarray].ino = ino;
260 delarray[num_delarray].mode = inode.i_mode;
261 delarray[num_delarray].uid = inode.i_uid;
262 delarray[num_delarray].gid = inode.i_gid;
263 delarray[num_delarray].size = inode.i_size;
264 delarray[num_delarray].dtime = inode.i_dtime;
265 delarray[num_delarray].num_blocks = lsd.num_blocks;
266 delarray[num_delarray].free_blocks = lsd.free_blocks;
267 num_delarray++;
270 next:
271 retval = ext2fs_get_next_inode (scan, &ino, &inode);
272 if (retval)
274 message (D_ERROR, undelfserr, _(" while doing inode scan %d "), retval);
275 goto error_out;
278 readdir_ptr = READDIR_PTR_INIT;
279 ext2fs_close_inode_scan (scan);
280 return 1;
282 error_out:
283 ext2fs_close_inode_scan (scan);
284 free_block_buf:
285 g_free (block_buf);
286 block_buf = NULL;
287 free_delarray:
288 g_free (delarray);
289 delarray = NULL;
290 return 0;
295 * This function overrides com_err() from libcom_err library.
296 * It is used in libext2fs to report errors.
298 void
299 com_err (const char *whoami, long err_code, const char *fmt, ...)
301 va_list ap;
302 char *str;
304 va_start (ap, fmt);
305 str = g_strdup_vprintf (fmt, ap);
306 va_end (ap);
308 message (D_ERROR, _(" Ext2lib error "), " %s (%s: %ld) ", str, whoami, err_code);
309 g_free (str);
312 static void *
313 undelfs_opendir (struct vfs_class *me, const char *dirname)
315 char *file, *f;
317 undelfs_get_path (dirname, &file, &f);
318 if (!file)
319 return 0;
321 /* We don't use the file name */
322 g_free (f);
324 if (!ext2_fname || strcmp (ext2_fname, file))
326 undelfs_shutdown ();
327 ext2_fname = file;
329 else
331 /* To avoid expensive re-scannings */
332 readdir_ptr = READDIR_PTR_INIT;
333 g_free (file);
334 return fs;
337 if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
339 message (D_ERROR, undelfserr, _(" Cannot open file %s "), ext2_fname);
340 return 0;
342 print_vfs_message (_("undelfs: reading inode bitmap..."));
343 if (ext2fs_read_inode_bitmap (fs))
345 message (D_ERROR, undelfserr, _(" Cannot load inode bitmap from: \n %s \n"), ext2_fname);
346 goto quit_opendir;
348 print_vfs_message (_("undelfs: reading block bitmap..."));
349 if (ext2fs_read_block_bitmap (fs))
351 message (D_ERROR, undelfserr, _(" Cannot load block bitmap from: \n %s \n"), ext2_fname);
352 goto quit_opendir;
354 /* Now load the deleted information */
355 if (!undelfs_loaddel ())
356 goto quit_opendir;
357 print_vfs_message (_("%s: done."), me->name);
358 return fs;
359 quit_opendir:
360 print_vfs_message (_("%s: failure"), me->name);
361 ext2fs_close (fs);
362 fs = NULL;
363 return 0;
367 static void *
368 undelfs_readdir (void *vfs_info)
370 static union vfs_dirent undelfs_readdir_data;
371 static char *const dirent_dest = undelfs_readdir_data.dent.d_name;
373 if (vfs_info != fs)
375 message (D_ERROR, undelfserr, _(" vfs_info is not fs! "));
376 return NULL;
378 if (readdir_ptr == num_delarray)
379 return NULL;
380 if (readdir_ptr < 0)
381 strcpy (dirent_dest, readdir_ptr == -2 ? "." : "..");
382 else
383 g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
384 (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
385 readdir_ptr++;
387 compute_namelen (&undelfs_readdir_data.dent);
389 return &undelfs_readdir_data;
392 static int
393 undelfs_closedir (void *vfs_info)
395 (void) vfs_info;
396 return 0;
399 typedef struct
401 int f_index; /* file index into delarray */
402 char *buf;
403 int error_code; /* */
404 int pos; /* file position */
405 int current; /* used to determine current position in itereate */
406 int finished;
407 ext2_ino_t inode;
408 int bytes_read;
409 long size;
411 /* Used by undelfs_read: */
412 char *dest_buffer; /* destination buffer */
413 int count; /* bytes to read */
414 } undelfs_file;
416 /* We do not support lseek */
417 static void *
418 undelfs_open (struct vfs_class *me, const char *fname, int flags, int mode)
420 char *file, *f;
421 ext2_ino_t inode, i;
422 undelfs_file *p = NULL;
423 (void) me;
424 (void) flags;
425 (void) mode;
427 /* Only allow reads on this file system */
428 undelfs_get_path (fname, &file, &f);
429 if (!file)
430 return 0;
432 if (!ext2_fname || strcmp (ext2_fname, file))
434 message (D_ERROR, undelfserr, _(" You have to chdir to extract files first "));
435 g_free (file);
436 g_free (f);
437 return 0;
439 inode = atol (f);
441 /* Search the file into delarray */
442 for (i = 0; i < (ext2_ino_t) num_delarray; i++)
444 if (inode != delarray[i].ino)
445 continue;
447 /* Found: setup all the structures needed by read */
448 p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
449 if (!p)
451 g_free (file);
452 g_free (f);
453 return 0;
455 p->buf = g_try_malloc (fs->blocksize);
456 if (!p->buf)
458 g_free (p);
459 g_free (file);
460 g_free (f);
461 return 0;
463 p->inode = inode;
464 p->finished = 0;
465 p->f_index = i;
466 p->error_code = 0;
467 p->pos = 0;
468 p->size = delarray[i].size;
470 g_free (file);
471 g_free (f);
472 undelfs_usage++;
473 return p;
476 static int
477 undelfs_close (void *vfs_info)
479 undelfs_file *p = vfs_info;
480 g_free (p->buf);
481 g_free (p);
482 undelfs_usage--;
483 return 0;
486 static int
487 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
489 int copy_count;
490 undelfs_file *p = (undelfs_file *) private;
492 if (blockcnt < 0)
493 return 0;
495 if (*blocknr)
497 p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
498 if (p->error_code)
499 return BLOCK_ABORT;
501 else
502 memset (p->buf, 0, param_fs->blocksize);
504 if (p->pos + p->count < p->current)
506 p->finished = 1;
507 return BLOCK_ABORT;
509 if ((size_t) p->pos > p->current + param_fs->blocksize)
511 p->current += param_fs->blocksize;
512 return 0; /* we have not arrived yet */
515 /* Now, we know we have to extract some data */
516 if (p->pos >= p->current)
519 /* First case: starting pointer inside this block */
520 if ((size_t) (p->pos + p->count) <= p->current + param_fs->blocksize)
522 /* Fully contained */
523 copy_count = p->count;
524 p->finished = p->count;
526 else
528 /* Still some more data */
529 copy_count = param_fs->blocksize - (p->pos - p->current);
531 memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
533 else
535 /* Second case: we already have passed p->pos */
536 if ((size_t) (p->pos + p->count) < p->current + param_fs->blocksize)
538 copy_count = (p->pos + p->count) - p->current;
539 p->finished = p->count;
541 else
543 copy_count = param_fs->blocksize;
545 memcpy (p->dest_buffer, p->buf, copy_count);
547 p->dest_buffer += copy_count;
548 p->current += param_fs->blocksize;
549 if (p->finished)
551 return BLOCK_ABORT;
553 return 0;
556 static ssize_t
557 undelfs_read (void *vfs_info, char *buffer, int count)
559 undelfs_file *p = vfs_info;
560 int retval;
562 p->dest_buffer = buffer;
563 p->current = 0;
564 p->finished = 0;
565 p->count = count;
567 if (p->pos + p->count > p->size)
569 p->count = p->size - p->pos;
571 retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
572 if (retval)
574 message (D_ERROR, undelfserr, _(" while iterating over blocks "));
575 return -1;
577 if (p->error_code && !p->finished)
578 return 0;
579 p->pos = p->pos + (p->dest_buffer - buffer);
580 return p->dest_buffer - buffer;
583 static long
584 undelfs_getindex (char *path)
586 ext2_ino_t inode = atol (path);
587 int i;
589 for (i = 0; i < num_delarray; i++)
591 if (delarray[i].ino == inode)
592 return i;
594 return -1;
597 static int
598 undelfs_stat_int (int inode_index, struct stat *buf)
600 buf->st_dev = 0;
601 buf->st_ino = delarray[inode_index].ino;
602 buf->st_mode = delarray[inode_index].mode;
603 buf->st_nlink = 1;
604 buf->st_uid = delarray[inode_index].uid;
605 buf->st_gid = delarray[inode_index].gid;
606 buf->st_size = delarray[inode_index].size;
607 buf->st_atime = delarray[inode_index].dtime;
608 buf->st_ctime = delarray[inode_index].dtime;
609 buf->st_mtime = delarray[inode_index].dtime;
610 return 0;
613 static int
614 undelfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
616 int inode_index;
617 char *file, *f;
618 (void) me;
620 undelfs_get_path (path, &file, &f);
621 if (!file)
622 return 0;
624 /* When called from save_cwd_stats we get an incorrect file and f here:
625 e.g. incorrect correct
626 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
627 file = "/dev" file="/dev/sda1"
628 f = "sda1" f ="401:1"
629 If the first char in f is no digit -> return error */
630 if (!isdigit (*f))
632 g_free (file);
633 g_free (f);
634 return -1;
637 if (!ext2_fname || strcmp (ext2_fname, file))
639 message (D_ERROR, undelfserr, _(" You have to chdir to extract files first "));
640 g_free (file);
641 g_free (f);
642 return 0;
644 inode_index = undelfs_getindex (f);
645 g_free (file);
646 g_free (f);
648 if (inode_index == -1)
649 return -1;
651 return undelfs_stat_int (inode_index, buf);
654 #define undelfs_stat undelfs_lstat
656 static int
657 undelfs_fstat (void *vfs_info, struct stat *buf)
659 undelfs_file *p = vfs_info;
661 return undelfs_stat_int (p->f_index, buf);
664 static int
665 undelfs_chdir (struct vfs_class *me, const char *path)
667 char *file, *f;
668 int fd;
669 (void) me;
671 undelfs_get_path (path, &file, &f);
672 if (!file)
673 return -1;
675 /* We may use access because ext2 file systems are local */
676 /* this could be fixed by making an ext2fs io manager to use */
677 /* our vfs, but that is left as an excercise for the reader */
678 fd = open (file, O_RDONLY);
679 if (fd == -1)
681 message (D_ERROR, undelfserr, _(" Cannot open file %s "), file);
682 g_free (f);
683 g_free (file);
684 return -1;
686 close (fd);
687 g_free (f);
688 g_free (file);
689 return 0;
692 /* this has to stay here for now: vfs layer does not know how to emulate it */
693 static off_t
694 undelfs_lseek (void *vfs_info, off_t offset, int whence)
696 (void) vfs_info;
697 (void) offset;
698 (void) whence;
700 return -1;
703 static vfsid
704 undelfs_getid (struct vfs_class *me, const char *path)
706 char *fname, *fsname;
707 (void) me;
709 undelfs_get_path (path, &fsname, &fname);
711 if (!fsname)
712 return NULL;
713 g_free (fname);
714 g_free (fsname);
715 return (vfsid) fs;
718 static int
719 undelfs_nothingisopen (vfsid id)
721 (void) id;
723 return !undelfs_usage;
726 static void
727 undelfs_free (vfsid id)
729 (void) id;
731 undelfs_shutdown ();
734 #ifdef ENABLE_NLS
735 static int
736 undelfs_init (struct vfs_class *me)
738 (void) me;
740 undelfserr = _(undelfserr);
741 return 1;
743 #else
744 #define undelfs_init NULL
745 #endif
747 void
748 init_undelfs (void)
750 vfs_undelfs_ops.name = "undelfs";
751 vfs_undelfs_ops.prefix = "undel:";
752 vfs_undelfs_ops.init = undelfs_init;
753 vfs_undelfs_ops.open = undelfs_open;
754 vfs_undelfs_ops.close = undelfs_close;
755 vfs_undelfs_ops.read = undelfs_read;
756 vfs_undelfs_ops.opendir = undelfs_opendir;
757 vfs_undelfs_ops.readdir = undelfs_readdir;
758 vfs_undelfs_ops.closedir = undelfs_closedir;
759 vfs_undelfs_ops.stat = undelfs_stat;
760 vfs_undelfs_ops.lstat = undelfs_lstat;
761 vfs_undelfs_ops.fstat = undelfs_fstat;
762 vfs_undelfs_ops.chdir = undelfs_chdir;
763 vfs_undelfs_ops.lseek = undelfs_lseek;
764 vfs_undelfs_ops.getid = undelfs_getid;
765 vfs_undelfs_ops.nothingisopen = undelfs_nothingisopen;
766 vfs_undelfs_ops.free = undelfs_free;
767 vfs_register_class (&vfs_undelfs_ops);