Add src/execute.c:execute_external_editor_or_viewer() function.
[midnight-commander.git] / src / vfs / undelfs / undelfs.c
blobf03492f1c8f817317fd815e181d35daf678b64a5
1 /*
2 UnDel File System: Midnight Commander file system.
4 This file system is intended to be used together with the
5 ext2fs library to recover files from ext2fs file systems.
7 Parts of this program were taken from the lsdel.c and dump.c files
8 written by Ted Ts'o (tytso@mit.edu) for the ext2fs package.
10 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
11 2005, 2007, 2011
12 The Free Software Foundation, Inc.
14 Written by:
15 Miguel de Icaza, 1995
16 Norbert Warmuth, 1997
17 Pavel Machek, 2000
19 This file is part of the Midnight Commander.
21 The Midnight Commander is free software: you can redistribute it
22 and/or modify it under the terms of the GNU General Public License as
23 published by the Free Software Foundation, either version 3 of the License,
24 or (at your option) any later version.
26 The Midnight Commander is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 /**
36 * \file
37 * \brief Source: UnDel File System
39 * Assumptions:
41 * 1. We don't handle directories (thus undelfs_get_path is easy to write).
42 * 2. Files are on the local file system (we do not support vfs files
43 * because we would have to provide an io_manager for the ext2fs tools,
44 * and I don't think it would be too useful to undelete files
47 #include <config.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
56 #else
57 /* asm/types.h defines its own umode_t */
58 #undef umode_t
59 #include <linux/ext2_fs.h>
60 #endif
62 #include <ext2fs/ext2fs.h>
63 #include <ctype.h>
65 #include "lib/global.h"
67 #include "lib/widget.h" /* message() */
68 #include "lib/vfs/utilvfs.h"
69 #include "lib/vfs/vfs.h"
71 #include "undelfs.h"
73 /*** global variables ****************************************************************************/
75 /*** file scope macro definitions ****************************************************************/
77 /* To generate the . and .. entries use -2 */
78 #define READDIR_PTR_INIT 0
80 #define undelfs_stat undelfs_lstat
82 /*** file scope type declarations ****************************************************************/
84 struct deleted_info
86 ext2_ino_t ino;
87 unsigned short mode;
88 unsigned short uid;
89 unsigned short gid;
90 unsigned long size;
91 time_t dtime;
92 int num_blocks;
93 int free_blocks;
96 struct lsdel_struct
98 ext2_ino_t inode;
99 int num_blocks;
100 int free_blocks;
101 int bad_blocks;
104 typedef struct
106 int f_index; /* file index into delarray */
107 char *buf;
108 int error_code; /* */
109 off_t pos; /* file position */
110 off_t current; /* used to determine current position in itereate */
111 gboolean finished;
112 ext2_ino_t inode;
113 int bytes_read;
114 off_t size;
116 /* Used by undelfs_read: */
117 char *dest_buffer; /* destination buffer */
118 size_t count; /* bytes to read */
119 } undelfs_file;
121 /*** file scope variables ************************************************************************/
123 /* We only allow one opened ext2fs */
124 static char *ext2_fname;
125 static ext2_filsys fs = NULL;
126 static struct lsdel_struct lsd;
127 static struct deleted_info *delarray;
128 static int num_delarray, max_delarray;
129 static char *block_buf;
130 static const char *undelfserr = N_("undelfs: error");
131 static int readdir_ptr;
132 static int undelfs_usage;
133 static struct vfs_class vfs_undelfs_ops;
135 /*** file scope functions ************************************************************************/
136 /* --------------------------------------------------------------------------------------------- */
138 static void
139 undelfs_shutdown (void)
141 if (fs)
142 ext2fs_close (fs);
143 fs = NULL;
144 g_free (ext2_fname);
145 ext2_fname = NULL;
146 g_free (delarray);
147 delarray = NULL;
148 g_free (block_buf);
149 block_buf = NULL;
152 /* --------------------------------------------------------------------------------------------- */
154 static void
155 undelfs_get_path (const vfs_path_t * vpath, char **fsname, char **file)
157 const char *p, *dirname;
158 const vfs_path_element_t *path_element;
160 path_element = vfs_path_get_by_index (vpath, -1);
162 /* To look like filesystem, we have virtual directories
163 undel://XXX, which have no subdirectories. XXX is replaced with
164 hda5, sdb8 etc, which is assumed to live under /dev.
165 -- pavel@ucw.cz */
167 dirname = path_element->path;
169 *fsname = NULL;
171 if (strncmp (dirname, "undel://", 8) != 0)
172 return;
174 dirname += 8;
176 /* Since we don't allow subdirectories, it's easy to get a filename,
177 * just scan backwards for a slash */
178 if (*dirname == 0)
179 return;
181 p = dirname + strlen (dirname);
182 #if 0
183 /* Strip trailing ./
185 if (p - dirname > 2 && *(p - 1) == '/' && *(p - 2) == '.')
186 *(p = p - 2) = 0;
187 #endif
189 while (p > dirname)
191 if (*p == '/')
193 char *tmp;
195 *file = g_strdup (p + 1);
196 tmp = g_strndup (dirname, p - dirname);
197 *fsname = g_strconcat ("/dev/", tmp, (char *) NULL);
198 g_free (tmp);
199 return;
201 p--;
203 *file = g_strdup ("");
204 *fsname = g_strconcat ("/dev/", dirname, (char *) NULL);
207 /* --------------------------------------------------------------------------------------------- */
209 static int
210 undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *private)
212 struct lsdel_struct *_lsd = (struct lsdel_struct *) private;
213 (void) blockcnt;
214 _lsd->num_blocks++;
216 if (*block_nr < _fs->super->s_first_data_block || *block_nr >= _fs->super->s_blocks_count)
218 _lsd->bad_blocks++;
219 return BLOCK_ABORT;
222 if (!ext2fs_test_block_bitmap (_fs->block_map, *block_nr))
223 _lsd->free_blocks++;
225 return 0;
228 /* --------------------------------------------------------------------------------------------- */
230 * Load information about deleted files.
231 * Don't abort if there is not enough memory - load as much as we can.
234 static int
235 undelfs_loaddel (void)
237 int retval, count;
238 ext2_ino_t ino;
239 struct ext2_inode inode;
240 ext2_inode_scan scan;
242 max_delarray = 100;
243 num_delarray = 0;
244 delarray = g_try_malloc (sizeof (struct deleted_info) * max_delarray);
245 if (!delarray)
247 message (D_ERROR, undelfserr, _("not enough memory"));
248 return 0;
250 block_buf = g_try_malloc (fs->blocksize * 3);
251 if (!block_buf)
253 message (D_ERROR, undelfserr, _("while allocating block buffer"));
254 goto free_delarray;
256 retval = ext2fs_open_inode_scan (fs, 0, &scan);
257 if (retval != 0)
259 message (D_ERROR, undelfserr, _("open_inode_scan: %d"), retval);
260 goto free_block_buf;
262 retval = ext2fs_get_next_inode (scan, &ino, &inode);
263 if (retval != 0)
265 message (D_ERROR, undelfserr, _("while starting inode scan %d"), retval);
266 goto error_out;
268 count = 0;
269 while (ino)
271 if ((count++ % 1024) == 0)
272 vfs_print_message (_("undelfs: loading deleted files information %d inodes"), count);
273 if (inode.i_dtime == 0)
274 goto next;
276 if (S_ISDIR (inode.i_mode))
277 goto next;
279 lsd.inode = ino;
280 lsd.num_blocks = 0;
281 lsd.free_blocks = 0;
282 lsd.bad_blocks = 0;
284 retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
285 if (retval)
287 message (D_ERROR, undelfserr, _("while calling ext2_block_iterate %d"), retval);
288 goto next;
290 if (lsd.free_blocks && !lsd.bad_blocks)
292 if (num_delarray >= max_delarray)
294 struct deleted_info *delarray_new = g_try_realloc (delarray,
295 sizeof (struct deleted_info) *
296 (max_delarray + 50));
297 if (!delarray_new)
299 message (D_ERROR, undelfserr, _("no more memory while reallocating array"));
300 goto error_out;
302 delarray = delarray_new;
303 max_delarray += 50;
306 delarray[num_delarray].ino = ino;
307 delarray[num_delarray].mode = inode.i_mode;
308 delarray[num_delarray].uid = inode.i_uid;
309 delarray[num_delarray].gid = inode.i_gid;
310 delarray[num_delarray].size = inode.i_size;
311 delarray[num_delarray].dtime = inode.i_dtime;
312 delarray[num_delarray].num_blocks = lsd.num_blocks;
313 delarray[num_delarray].free_blocks = lsd.free_blocks;
314 num_delarray++;
317 next:
318 retval = ext2fs_get_next_inode (scan, &ino, &inode);
319 if (retval)
321 message (D_ERROR, undelfserr, _("while doing inode scan %d"), retval);
322 goto error_out;
325 readdir_ptr = READDIR_PTR_INIT;
326 ext2fs_close_inode_scan (scan);
327 return 1;
329 error_out:
330 ext2fs_close_inode_scan (scan);
331 free_block_buf:
332 g_free (block_buf);
333 block_buf = NULL;
334 free_delarray:
335 g_free (delarray);
336 delarray = NULL;
337 return 0;
340 /* --------------------------------------------------------------------------------------------- */
342 static void *
343 undelfs_opendir (const vfs_path_t * vpath)
345 char *file, *f;
346 const vfs_path_element_t *path_element;
348 path_element = vfs_path_get_by_index (vpath, -1);
349 undelfs_get_path (vpath, &file, &f);
350 if (!file)
351 return 0;
353 /* We don't use the file name */
354 g_free (f);
356 if (!ext2_fname || strcmp (ext2_fname, file))
358 undelfs_shutdown ();
359 ext2_fname = file;
361 else
363 /* To avoid expensive re-scannings */
364 readdir_ptr = READDIR_PTR_INIT;
365 g_free (file);
366 return fs;
369 if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
371 message (D_ERROR, undelfserr, _("Cannot open file %s"), ext2_fname);
372 return 0;
374 vfs_print_message (_("undelfs: reading inode bitmap..."));
375 if (ext2fs_read_inode_bitmap (fs))
377 message (D_ERROR, undelfserr, _("Cannot load inode bitmap from:\n%s"), ext2_fname);
378 goto quit_opendir;
380 vfs_print_message (_("undelfs: reading block bitmap..."));
381 if (ext2fs_read_block_bitmap (fs))
383 message (D_ERROR, undelfserr, _("Cannot load block bitmap from:\n%s"), ext2_fname);
384 goto quit_opendir;
386 /* Now load the deleted information */
387 if (!undelfs_loaddel ())
388 goto quit_opendir;
389 vfs_print_message (_("%s: done."), path_element->class->name);
390 return fs;
391 quit_opendir:
392 vfs_print_message (_("%s: failure"), path_element->class->name);
393 ext2fs_close (fs);
394 fs = NULL;
395 return 0;
398 /* --------------------------------------------------------------------------------------------- */
400 static void *
401 undelfs_readdir (void *vfs_info)
403 static union vfs_dirent undelfs_readdir_data;
404 static char *const dirent_dest = undelfs_readdir_data.dent.d_name;
406 if (vfs_info != fs)
408 message (D_ERROR, undelfserr, _("vfs_info is not fs!"));
409 return NULL;
411 if (readdir_ptr == num_delarray)
412 return NULL;
413 if (readdir_ptr < 0)
414 strcpy (dirent_dest, readdir_ptr == -2 ? "." : "..");
415 else
416 g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
417 (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
418 readdir_ptr++;
420 compute_namelen (&undelfs_readdir_data.dent);
422 return &undelfs_readdir_data;
425 /* --------------------------------------------------------------------------------------------- */
427 static int
428 undelfs_closedir (void *vfs_info)
430 (void) vfs_info;
431 return 0;
434 /* --------------------------------------------------------------------------------------------- */
435 /* We do not support lseek */
437 static void *
438 undelfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
440 char *file, *f;
441 ext2_ino_t inode, i;
442 undelfs_file *p = NULL;
443 (void) flags;
444 (void) mode;
446 /* Only allow reads on this file system */
447 undelfs_get_path (vpath, &file, &f);
448 if (!file)
449 return 0;
451 if (!ext2_fname || strcmp (ext2_fname, file))
453 message (D_ERROR, undelfserr, _("You have to chdir to extract files first"));
454 g_free (file);
455 g_free (f);
456 return 0;
458 inode = atol (f);
460 /* Search the file into delarray */
461 for (i = 0; i < (ext2_ino_t) num_delarray; i++)
463 if (inode != delarray[i].ino)
464 continue;
466 /* Found: setup all the structures needed by read */
467 p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
468 if (!p)
470 g_free (file);
471 g_free (f);
472 return 0;
474 p->buf = g_try_malloc (fs->blocksize);
475 if (!p->buf)
477 g_free (p);
478 g_free (file);
479 g_free (f);
480 return 0;
482 p->inode = inode;
483 p->finished = FALSE;
484 p->f_index = i;
485 p->error_code = 0;
486 p->pos = 0;
487 p->size = delarray[i].size;
489 g_free (file);
490 g_free (f);
491 undelfs_usage++;
492 return p;
495 /* --------------------------------------------------------------------------------------------- */
497 static int
498 undelfs_close (void *vfs_info)
500 undelfs_file *p = vfs_info;
501 g_free (p->buf);
502 g_free (p);
503 undelfs_usage--;
504 return 0;
507 /* --------------------------------------------------------------------------------------------- */
509 static int
510 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
512 int copy_count;
513 undelfs_file *p = (undelfs_file *) private;
515 if (blockcnt < 0)
516 return 0;
518 if (*blocknr)
520 p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
521 if (p->error_code)
522 return BLOCK_ABORT;
524 else
525 memset (p->buf, 0, param_fs->blocksize);
527 if (p->pos + (off_t) p->count < p->current)
529 p->finished = TRUE;
530 return BLOCK_ABORT;
532 if (p->pos > p->current + param_fs->blocksize)
534 p->current += param_fs->blocksize;
535 return 0; /* we have not arrived yet */
538 /* Now, we know we have to extract some data */
539 if (p->pos >= p->current)
542 /* First case: starting pointer inside this block */
543 if (p->pos + (off_t) p->count <= p->current + param_fs->blocksize)
545 /* Fully contained */
546 copy_count = p->count;
547 p->finished = (p->count != 0);
549 else
551 /* Still some more data */
552 copy_count = param_fs->blocksize - (p->pos - p->current);
554 memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
556 else
558 /* Second case: we already have passed p->pos */
559 if (p->pos + (off_t) p->count < p->current + param_fs->blocksize)
561 copy_count = (p->pos + p->count) - p->current;
562 p->finished = (p->count != 0);
564 else
566 copy_count = param_fs->blocksize;
568 memcpy (p->dest_buffer, p->buf, copy_count);
570 p->dest_buffer += copy_count;
571 p->current += param_fs->blocksize;
572 if (p->finished)
574 return BLOCK_ABORT;
576 return 0;
579 /* --------------------------------------------------------------------------------------------- */
581 static ssize_t
582 undelfs_read (void *vfs_info, char *buffer, size_t count)
584 undelfs_file *p = vfs_info;
585 int retval;
587 p->dest_buffer = buffer;
588 p->current = 0;
589 p->finished = FALSE;
590 p->count = count;
592 if (p->pos + (off_t) p->count > p->size)
594 p->count = p->size - p->pos;
596 retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
597 if (retval)
599 message (D_ERROR, undelfserr, _("while iterating over blocks"));
600 return -1;
602 if (p->error_code && !p->finished)
603 return 0;
604 p->pos = p->pos + (p->dest_buffer - buffer);
605 return p->dest_buffer - buffer;
608 /* --------------------------------------------------------------------------------------------- */
610 static long
611 undelfs_getindex (char *path)
613 ext2_ino_t inode = atol (path);
614 int i;
616 for (i = 0; i < num_delarray; i++)
618 if (delarray[i].ino == inode)
619 return i;
621 return -1;
624 /* --------------------------------------------------------------------------------------------- */
626 static int
627 undelfs_stat_int (int inode_index, struct stat *buf)
629 buf->st_dev = 0;
630 buf->st_ino = delarray[inode_index].ino;
631 buf->st_mode = delarray[inode_index].mode;
632 buf->st_nlink = 1;
633 buf->st_uid = delarray[inode_index].uid;
634 buf->st_gid = delarray[inode_index].gid;
635 buf->st_size = delarray[inode_index].size;
636 buf->st_atime = delarray[inode_index].dtime;
637 buf->st_ctime = delarray[inode_index].dtime;
638 buf->st_mtime = delarray[inode_index].dtime;
639 return 0;
642 /* --------------------------------------------------------------------------------------------- */
644 static int
645 undelfs_lstat (const vfs_path_t * vpath, struct stat *buf)
647 int inode_index;
648 char *file, *f;
650 undelfs_get_path (vpath, &file, &f);
651 if (!file)
652 return 0;
654 /* When called from save_cwd_stats we get an incorrect file and f here:
655 e.g. incorrect correct
656 path = "undel:/dev/sda1" path="undel:/dev/sda1/401:1"
657 file = "/dev" file="/dev/sda1"
658 f = "sda1" f ="401:1"
659 If the first char in f is no digit -> return error */
660 if (!isdigit (*f))
662 g_free (file);
663 g_free (f);
664 return -1;
667 if (!ext2_fname || strcmp (ext2_fname, file))
669 message (D_ERROR, undelfserr, _("You have to chdir to extract files first"));
670 g_free (file);
671 g_free (f);
672 return 0;
674 inode_index = undelfs_getindex (f);
675 g_free (file);
676 g_free (f);
678 if (inode_index == -1)
679 return -1;
681 return undelfs_stat_int (inode_index, buf);
684 /* --------------------------------------------------------------------------------------------- */
686 static int
687 undelfs_fstat (void *vfs_info, struct stat *buf)
689 undelfs_file *p = vfs_info;
691 return undelfs_stat_int (p->f_index, buf);
694 /* --------------------------------------------------------------------------------------------- */
696 static int
697 undelfs_chdir (const vfs_path_t * vpath)
699 char *file, *f;
700 int fd;
702 undelfs_get_path (vpath, &file, &f);
703 if (!file)
704 return -1;
706 /* We may use access because ext2 file systems are local */
707 /* this could be fixed by making an ext2fs io manager to use */
708 /* our vfs, but that is left as an excercise for the reader */
709 fd = open (file, O_RDONLY);
710 if (fd == -1)
712 message (D_ERROR, undelfserr, _("Cannot open file \"%s\""), file);
713 g_free (f);
714 g_free (file);
715 return -1;
717 close (fd);
718 g_free (f);
719 g_free (file);
720 return 0;
723 /* --------------------------------------------------------------------------------------------- */
725 /* this has to stay here for now: vfs layer does not know how to emulate it */
726 static off_t
727 undelfs_lseek (void *vfs_info, off_t offset, int whence)
729 (void) vfs_info;
730 (void) offset;
731 (void) whence;
733 return -1;
736 /* --------------------------------------------------------------------------------------------- */
738 static vfsid
739 undelfs_getid (const vfs_path_t * vpath)
741 char *fname, *fsname;
743 undelfs_get_path (vpath, &fsname, &fname);
745 if (!fsname)
746 return NULL;
747 g_free (fname);
748 g_free (fsname);
749 return (vfsid) fs;
752 /* --------------------------------------------------------------------------------------------- */
754 static int
755 undelfs_nothingisopen (vfsid id)
757 (void) id;
759 return !undelfs_usage;
762 /* --------------------------------------------------------------------------------------------- */
764 static void
765 undelfs_free (vfsid id)
767 (void) id;
769 undelfs_shutdown ();
772 /* --------------------------------------------------------------------------------------------- */
774 #ifdef ENABLE_NLS
775 static int
776 undelfs_init (struct vfs_class *me)
778 (void) me;
780 undelfserr = _(undelfserr);
781 return 1;
783 #else
784 #define undelfs_init NULL
785 #endif
787 /* --------------------------------------------------------------------------------------------- */
788 /*** public functions ****************************************************************************/
789 /* --------------------------------------------------------------------------------------------- */
791 * This function overrides com_err() from libcom_err library.
792 * It is used in libext2fs to report errors.
795 void
796 com_err (const char *whoami, long err_code, const char *fmt, ...)
798 va_list ap;
799 char *str;
801 va_start (ap, fmt);
802 str = g_strdup_vprintf (fmt, ap);
803 va_end (ap);
805 message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code);
806 g_free (str);
809 /* --------------------------------------------------------------------------------------------- */
811 void
812 init_undelfs (void)
814 vfs_undelfs_ops.name = "undelfs";
815 vfs_undelfs_ops.prefix = "undel";
816 vfs_undelfs_ops.init = undelfs_init;
817 vfs_undelfs_ops.open = undelfs_open;
818 vfs_undelfs_ops.close = undelfs_close;
819 vfs_undelfs_ops.read = undelfs_read;
820 vfs_undelfs_ops.opendir = undelfs_opendir;
821 vfs_undelfs_ops.readdir = undelfs_readdir;
822 vfs_undelfs_ops.closedir = undelfs_closedir;
823 vfs_undelfs_ops.stat = undelfs_stat;
824 vfs_undelfs_ops.lstat = undelfs_lstat;
825 vfs_undelfs_ops.fstat = undelfs_fstat;
826 vfs_undelfs_ops.chdir = undelfs_chdir;
827 vfs_undelfs_ops.lseek = undelfs_lseek;
828 vfs_undelfs_ops.getid = undelfs_getid;
829 vfs_undelfs_ops.nothingisopen = undelfs_nothingisopen;
830 vfs_undelfs_ops.free = undelfs_free;
831 vfs_register_class (&vfs_undelfs_ops);
834 /* --------------------------------------------------------------------------------------------- */