root/src/vfs/undelfs/undelfs.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. undelfs_shutdown
  2. undelfs_get_path
  3. undelfs_lsdel_proc
  4. undelfs_loaddel
  5. undelfs_opendir
  6. undelfs_readdir
  7. undelfs_closedir
  8. undelfs_open
  9. undelfs_close
  10. undelfs_dump_read
  11. undelfs_read
  12. undelfs_getindex
  13. undelfs_stat_int
  14. undelfs_lstat
  15. undelfs_fstat
  16. undelfs_chdir
  17. undelfs_lseek
  18. undelfs_getid
  19. undelfs_nothingisopen
  20. undelfs_free
  21. undelfs_init
  22. com_err
  23. vfs_init_undelfs

   1 /*
   2    UnDel File System: Midnight Commander file system.
   3 
   4    This file system is intended to be used together with the
   5    ext2fs library to recover files from ext2fs file systems.
   6 
   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.
   9 
  10    Copyright (C) 1995-2019
  11    Free Software Foundation, Inc.
  12 
  13    Written by:
  14    Miguel de Icaza, 1995
  15    Norbert Warmuth, 1997
  16    Pavel Machek, 2000
  17 
  18    This file is part of the Midnight Commander.
  19 
  20    The Midnight Commander is free software: you can redistribute it
  21    and/or modify it under the terms of the GNU General Public License as
  22    published by the Free Software Foundation, either version 3 of the License,
  23    or (at your option) any later version.
  24 
  25    The Midnight Commander is distributed in the hope that it will be useful,
  26    but WITHOUT ANY WARRANTY; without even the implied warranty of
  27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28    GNU General Public License for more details.
  29 
  30    You should have received a copy of the GNU General Public License
  31    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  32  */
  33 
  34 /**
  35  * \file
  36  * \brief Source: UnDel File System
  37  *
  38  * Assumptions:
  39  *
  40  * 1. We don't handle directories (thus undelfs_get_path is easy to write).
  41  * 2. Files are on the local file system (we do not support vfs files
  42  *    because we would have to provide an io_manager for the ext2fs tools,
  43  *    and I don't think it would be too useful to undelete files
  44  */
  45 
  46 #include <config.h>
  47 
  48 #include <errno.h>
  49 #include <stdio.h>
  50 #include <stdlib.h>
  51 #include <string.h>             /* memset() */
  52 
  53 #ifdef HAVE_EXT2FS_EXT2_FS_H
  54 #include <ext2fs/ext2_fs.h>
  55 #else
  56 /* asm/types.h defines its own umode_t */
  57 #undef umode_t
  58 #include <linux/ext2_fs.h>
  59 #endif
  60 
  61 #include <ext2fs/ext2fs.h>
  62 #include <ctype.h>
  63 
  64 #include "lib/global.h"
  65 
  66 #include "lib/util.h"
  67 #include "lib/widget.h"         /* message() */
  68 #include "lib/vfs/xdirentry.h"
  69 #include "lib/vfs/utilvfs.h"
  70 #include "lib/vfs/vfs.h"
  71 
  72 #include "undelfs.h"
  73 
  74 /*** global variables ****************************************************************************/
  75 
  76 /*** file scope macro definitions ****************************************************************/
  77 
  78 /* To generate the . and .. entries use -2 */
  79 #define READDIR_PTR_INIT 0
  80 
  81 #define undelfs_stat undelfs_lstat
  82 
  83 /*** file scope type declarations ****************************************************************/
  84 
  85 struct deleted_info
  86 {
  87     ext2_ino_t ino;
  88     unsigned short mode;
  89     unsigned short uid;
  90     unsigned short gid;
  91     unsigned long size;
  92     time_t dtime;
  93     int num_blocks;
  94     int free_blocks;
  95 };
  96 
  97 struct lsdel_struct
  98 {
  99     ext2_ino_t inode;
 100     int num_blocks;
 101     int free_blocks;
 102     int bad_blocks;
 103 };
 104 
 105 typedef struct
 106 {
 107     int f_index;                /* file index into delarray */
 108     char *buf;
 109     int error_code;             /*  */
 110     off_t pos;                  /* file position */
 111     off_t current;              /* used to determine current position in itereate */
 112     gboolean finished;
 113     ext2_ino_t inode;
 114     int bytes_read;
 115     off_t size;
 116 
 117     /* Used by undelfs_read: */
 118     char *dest_buffer;          /* destination buffer */
 119     size_t count;               /* bytes to read */
 120 } undelfs_file;
 121 
 122 /*** file scope variables ************************************************************************/
 123 
 124 /* We only allow one opened ext2fs */
 125 static char *ext2_fname;
 126 static ext2_filsys fs = NULL;
 127 static struct lsdel_struct lsd;
 128 static struct deleted_info *delarray;
 129 static int num_delarray, max_delarray;
 130 static char *block_buf;
 131 static const char *undelfserr = N_("undelfs: error");
 132 static int readdir_ptr;
 133 static int undelfs_usage;
 134 
 135 static struct vfs_s_subclass undelfs_subclass;
 136 static struct vfs_class *vfs_undelfs_ops = VFS_CLASS (&undelfs_subclass);
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 /*** file scope functions ************************************************************************/
 140 /* --------------------------------------------------------------------------------------------- */
 141 
 142 static void
 143 undelfs_shutdown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 144 {
 145     if (fs)
 146         ext2fs_close (fs);
 147     fs = NULL;
 148     MC_PTR_FREE (ext2_fname);
 149     MC_PTR_FREE (delarray);
 150     MC_PTR_FREE (block_buf);
 151 }
 152 
 153 /* --------------------------------------------------------------------------------------------- */
 154 
 155 static void
 156 undelfs_get_path (const vfs_path_t * vpath, char **fsname, char **file)
     /* [previous][next][first][last][top][bottom][index][help]  */
 157 {
 158     const char *p, *dirname;
 159     const vfs_path_element_t *path_element;
 160 
 161     path_element = vfs_path_get_by_index (vpath, -1);
 162 
 163     /* To look like filesystem, we have virtual directories
 164        undel://XXX, which have no subdirectories. XXX is replaced with
 165        hda5, sdb8 etc, which is assumed to live under /dev. 
 166        -- pavel@ucw.cz */
 167 
 168     dirname = path_element->path;
 169 
 170     *fsname = NULL;
 171 
 172     if (strncmp (dirname, "undel://", 8) != 0)
 173         return;
 174 
 175     dirname += 8;
 176 
 177     /* Since we don't allow subdirectories, it's easy to get a filename,
 178      * just scan backwards for a slash */
 179     if (*dirname == 0)
 180         return;
 181 
 182     p = dirname + strlen (dirname);
 183 #if 0
 184     /* Strip trailing ./
 185      */
 186     if (p - dirname > 2 && IS_PATH_SEP (p[-1]) && p[-2] == '.')
 187         *(p = p - 2) = 0;
 188 #endif
 189 
 190     while (p > dirname)
 191     {
 192         if (IS_PATH_SEP (*p))
 193         {
 194             char *tmp;
 195 
 196             *file = g_strdup (p + 1);
 197             tmp = g_strndup (dirname, p - dirname);
 198             *fsname = g_strconcat ("/dev/", tmp, (char *) NULL);
 199             g_free (tmp);
 200             return;
 201         }
 202         p--;
 203     }
 204     *file = g_strdup ("");
 205     *fsname = g_strconcat ("/dev/", dirname, (char *) NULL);
 206 }
 207 
 208 /* --------------------------------------------------------------------------------------------- */
 209 
 210 static int
 211 undelfs_lsdel_proc (ext2_filsys _fs, blk_t * block_nr, int blockcnt, void *private)
     /* [previous][next][first][last][top][bottom][index][help]  */
 212 {
 213     struct lsdel_struct *_lsd = (struct lsdel_struct *) private;
 214     (void) blockcnt;
 215     _lsd->num_blocks++;
 216 
 217     if (*block_nr < _fs->super->s_first_data_block || *block_nr >= _fs->super->s_blocks_count)
 218     {
 219         _lsd->bad_blocks++;
 220         return BLOCK_ABORT;
 221     }
 222 
 223     if (!ext2fs_test_block_bitmap (_fs->block_map, *block_nr))
 224         _lsd->free_blocks++;
 225 
 226     return 0;
 227 }
 228 
 229 /* --------------------------------------------------------------------------------------------- */
 230 /**
 231  * Load information about deleted files.
 232  * Don't abort if there is not enough memory - load as much as we can.
 233  */
 234 
 235 static int
 236 undelfs_loaddel (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 237 {
 238     int retval, count;
 239     ext2_ino_t ino;
 240     struct ext2_inode inode;
 241     ext2_inode_scan scan;
 242 
 243     max_delarray = 100;
 244     num_delarray = 0;
 245     delarray = g_try_malloc (sizeof (struct deleted_info) * max_delarray);
 246     if (!delarray)
 247     {
 248         message (D_ERROR, undelfserr, "%s", _("not enough memory"));
 249         return 0;
 250     }
 251     block_buf = g_try_malloc (fs->blocksize * 3);
 252     if (!block_buf)
 253     {
 254         message (D_ERROR, undelfserr, "%s", _("while allocating block buffer"));
 255         goto free_delarray;
 256     }
 257     retval = ext2fs_open_inode_scan (fs, 0, &scan);
 258     if (retval != 0)
 259     {
 260         message (D_ERROR, undelfserr, _("open_inode_scan: %d"), retval);
 261         goto free_block_buf;
 262     }
 263     retval = ext2fs_get_next_inode (scan, &ino, &inode);
 264     if (retval != 0)
 265     {
 266         message (D_ERROR, undelfserr, _("while starting inode scan %d"), retval);
 267         goto error_out;
 268     }
 269     count = 0;
 270     while (ino)
 271     {
 272         if ((count++ % 1024) == 0)
 273             vfs_print_message (_("undelfs: loading deleted files information %d inodes"), count);
 274         if (inode.i_dtime == 0)
 275             goto next;
 276 
 277         if (S_ISDIR (inode.i_mode))
 278             goto next;
 279 
 280         lsd.inode = ino;
 281         lsd.num_blocks = 0;
 282         lsd.free_blocks = 0;
 283         lsd.bad_blocks = 0;
 284 
 285         retval = ext2fs_block_iterate (fs, ino, 0, block_buf, undelfs_lsdel_proc, &lsd);
 286         if (retval)
 287         {
 288             message (D_ERROR, undelfserr, _("while calling ext2_block_iterate %d"), retval);
 289             goto next;
 290         }
 291         if (lsd.free_blocks && !lsd.bad_blocks)
 292         {
 293             if (num_delarray >= max_delarray)
 294             {
 295                 struct deleted_info *delarray_new = g_try_realloc (delarray,
 296                                                                    sizeof (struct deleted_info) *
 297                                                                    (max_delarray + 50));
 298                 if (!delarray_new)
 299                 {
 300                     message (D_ERROR, undelfserr, "%s",
 301                              _("no more memory while reallocating array"));
 302                     goto error_out;
 303                 }
 304                 delarray = delarray_new;
 305                 max_delarray += 50;
 306             }
 307 
 308             delarray[num_delarray].ino = ino;
 309             delarray[num_delarray].mode = inode.i_mode;
 310             delarray[num_delarray].uid = inode.i_uid;
 311             delarray[num_delarray].gid = inode.i_gid;
 312             delarray[num_delarray].size = inode.i_size;
 313             delarray[num_delarray].dtime = inode.i_dtime;
 314             delarray[num_delarray].num_blocks = lsd.num_blocks;
 315             delarray[num_delarray].free_blocks = lsd.free_blocks;
 316             num_delarray++;
 317         }
 318 
 319       next:
 320         retval = ext2fs_get_next_inode (scan, &ino, &inode);
 321         if (retval)
 322         {
 323             message (D_ERROR, undelfserr, _("while doing inode scan %d"), retval);
 324             goto error_out;
 325         }
 326     }
 327     readdir_ptr = READDIR_PTR_INIT;
 328     ext2fs_close_inode_scan (scan);
 329     return 1;
 330 
 331   error_out:
 332     ext2fs_close_inode_scan (scan);
 333   free_block_buf:
 334     MC_PTR_FREE (block_buf);
 335   free_delarray:
 336     MC_PTR_FREE (delarray);
 337     return 0;
 338 }
 339 
 340 /* --------------------------------------------------------------------------------------------- */
 341 
 342 static void *
 343 undelfs_opendir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 344 {
 345     char *file, *f = NULL;
 346     const vfs_path_element_t *path_element;
 347 
 348     path_element = vfs_path_get_by_index (vpath, -1);
 349     undelfs_get_path (vpath, &file, &f);
 350     if (file == NULL)
 351     {
 352         g_free (f);
 353         return 0;
 354     }
 355 
 356     /* We don't use the file name */
 357     g_free (f);
 358 
 359     if (!ext2_fname || strcmp (ext2_fname, file))
 360     {
 361         undelfs_shutdown ();
 362         ext2_fname = file;
 363     }
 364     else
 365     {
 366         /* To avoid expensive re-scannings */
 367         readdir_ptr = READDIR_PTR_INIT;
 368         g_free (file);
 369         return fs;
 370     }
 371 
 372     if (ext2fs_open (ext2_fname, 0, 0, 0, unix_io_manager, &fs))
 373     {
 374         message (D_ERROR, undelfserr, _("Cannot open file %s"), ext2_fname);
 375         return 0;
 376     }
 377     vfs_print_message ("%s", _("undelfs: reading inode bitmap..."));
 378     if (ext2fs_read_inode_bitmap (fs))
 379     {
 380         message (D_ERROR, undelfserr, _("Cannot load inode bitmap from:\n%s"), ext2_fname);
 381         goto quit_opendir;
 382     }
 383     vfs_print_message ("%s", _("undelfs: reading block bitmap..."));
 384     if (ext2fs_read_block_bitmap (fs))
 385     {
 386         message (D_ERROR, undelfserr, _("Cannot load block bitmap from:\n%s"), ext2_fname);
 387         goto quit_opendir;
 388     }
 389     /* Now load the deleted information */
 390     if (!undelfs_loaddel ())
 391         goto quit_opendir;
 392     vfs_print_message (_("%s: done."), path_element->class->name);
 393     return fs;
 394   quit_opendir:
 395     vfs_print_message (_("%s: failure"), path_element->class->name);
 396     ext2fs_close (fs);
 397     fs = NULL;
 398     return 0;
 399 }
 400 
 401 /* --------------------------------------------------------------------------------------------- */
 402 
 403 static void *
 404 undelfs_readdir (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 405 {
 406     static union vfs_dirent undelfs_readdir_data;
 407     static char *const dirent_dest = undelfs_readdir_data.dent.d_name;
 408 
 409     if (vfs_info != fs)
 410     {
 411         message (D_ERROR, undelfserr, "%s", _("vfs_info is not fs!"));
 412         return NULL;
 413     }
 414     if (readdir_ptr == num_delarray)
 415         return NULL;
 416     if (readdir_ptr < 0)
 417         strcpy (dirent_dest, readdir_ptr == -2 ? "." : "..");
 418     else
 419         g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
 420                     (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
 421     readdir_ptr++;
 422 
 423     return &undelfs_readdir_data;
 424 }
 425 
 426 /* --------------------------------------------------------------------------------------------- */
 427 
 428 static int
 429 undelfs_closedir (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 430 {
 431     (void) vfs_info;
 432     return 0;
 433 }
 434 
 435 /* --------------------------------------------------------------------------------------------- */
 436 /* We do not support lseek */
 437 
 438 static void *
 439 undelfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
 440 {
 441     char *file, *f = NULL;
 442     ext2_ino_t inode, i;
 443     undelfs_file *p = NULL;
 444     (void) flags;
 445     (void) mode;
 446 
 447     /* Only allow reads on this file system */
 448     undelfs_get_path (vpath, &file, &f);
 449     if (file == NULL)
 450     {
 451         g_free (f);
 452         return 0;
 453     }
 454 
 455     if (!ext2_fname || strcmp (ext2_fname, file))
 456     {
 457         message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
 458         g_free (file);
 459         g_free (f);
 460         return 0;
 461     }
 462     inode = atol (f);
 463 
 464     /* Search the file into delarray */
 465     for (i = 0; i < (ext2_ino_t) num_delarray; i++)
 466     {
 467         if (inode != delarray[i].ino)
 468             continue;
 469 
 470         /* Found: setup all the structures needed by read */
 471         p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
 472         if (!p)
 473         {
 474             g_free (file);
 475             g_free (f);
 476             return 0;
 477         }
 478         p->buf = g_try_malloc (fs->blocksize);
 479         if (!p->buf)
 480         {
 481             g_free (p);
 482             g_free (file);
 483             g_free (f);
 484             return 0;
 485         }
 486         p->inode = inode;
 487         p->finished = FALSE;
 488         p->f_index = i;
 489         p->error_code = 0;
 490         p->pos = 0;
 491         p->size = delarray[i].size;
 492     }
 493     g_free (file);
 494     g_free (f);
 495     undelfs_usage++;
 496     return p;
 497 }
 498 
 499 /* --------------------------------------------------------------------------------------------- */
 500 
 501 static int
 502 undelfs_close (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 503 {
 504     undelfs_file *p = vfs_info;
 505     g_free (p->buf);
 506     g_free (p);
 507     undelfs_usage--;
 508     return 0;
 509 }
 510 
 511 /* --------------------------------------------------------------------------------------------- */
 512 
 513 static int
 514 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
     /* [previous][next][first][last][top][bottom][index][help]  */
 515 {
 516     int copy_count;
 517     undelfs_file *p = (undelfs_file *) private;
 518 
 519     if (blockcnt < 0)
 520         return 0;
 521 
 522     if (*blocknr)
 523     {
 524         p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
 525         if (p->error_code)
 526             return BLOCK_ABORT;
 527     }
 528     else
 529         memset (p->buf, 0, param_fs->blocksize);
 530 
 531     if (p->pos + (off_t) p->count < p->current)
 532     {
 533         p->finished = TRUE;
 534         return BLOCK_ABORT;
 535     }
 536     if (p->pos > p->current + param_fs->blocksize)
 537     {
 538         p->current += param_fs->blocksize;
 539         return 0;               /* we have not arrived yet */
 540     }
 541 
 542     /* Now, we know we have to extract some data */
 543     if (p->pos >= p->current)
 544     {
 545 
 546         /* First case: starting pointer inside this block */
 547         if (p->pos + (off_t) p->count <= p->current + param_fs->blocksize)
 548         {
 549             /* Fully contained */
 550             copy_count = p->count;
 551             p->finished = (p->count != 0);
 552         }
 553         else
 554         {
 555             /* Still some more data */
 556             copy_count = param_fs->blocksize - (p->pos - p->current);
 557         }
 558         memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
 559     }
 560     else
 561     {
 562         /* Second case: we already have passed p->pos */
 563         if (p->pos + (off_t) p->count < p->current + param_fs->blocksize)
 564         {
 565             copy_count = (p->pos + p->count) - p->current;
 566             p->finished = (p->count != 0);
 567         }
 568         else
 569         {
 570             copy_count = param_fs->blocksize;
 571         }
 572         memcpy (p->dest_buffer, p->buf, copy_count);
 573     }
 574     p->dest_buffer += copy_count;
 575     p->current += param_fs->blocksize;
 576     if (p->finished)
 577     {
 578         return BLOCK_ABORT;
 579     }
 580     return 0;
 581 }
 582 
 583 /* --------------------------------------------------------------------------------------------- */
 584 
 585 static ssize_t
 586 undelfs_read (void *vfs_info, char *buffer, size_t count)
     /* [previous][next][first][last][top][bottom][index][help]  */
 587 {
 588     undelfs_file *p = vfs_info;
 589     int retval;
 590 
 591     p->dest_buffer = buffer;
 592     p->current = 0;
 593     p->finished = FALSE;
 594     p->count = count;
 595 
 596     if (p->pos + (off_t) p->count > p->size)
 597     {
 598         p->count = p->size - p->pos;
 599     }
 600     retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
 601     if (retval)
 602     {
 603         message (D_ERROR, undelfserr, "%s", _("while iterating over blocks"));
 604         return -1;
 605     }
 606     if (p->error_code && !p->finished)
 607         return 0;
 608     p->pos = p->pos + (p->dest_buffer - buffer);
 609     return p->dest_buffer - buffer;
 610 }
 611 
 612 /* --------------------------------------------------------------------------------------------- */
 613 
 614 static long
 615 undelfs_getindex (char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 616 {
 617     ext2_ino_t inode = atol (path);
 618     int i;
 619 
 620     for (i = 0; i < num_delarray; i++)
 621     {
 622         if (delarray[i].ino == inode)
 623             return i;
 624     }
 625     return -1;
 626 }
 627 
 628 /* --------------------------------------------------------------------------------------------- */
 629 
 630 static int
 631 undelfs_stat_int (int inode_index, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 632 {
 633     buf->st_dev = 0;
 634     buf->st_ino = delarray[inode_index].ino;
 635     buf->st_mode = delarray[inode_index].mode;
 636     buf->st_nlink = 1;
 637     buf->st_uid = delarray[inode_index].uid;
 638     buf->st_gid = delarray[inode_index].gid;
 639     buf->st_size = delarray[inode_index].size;
 640     buf->st_atime = delarray[inode_index].dtime;
 641     buf->st_ctime = delarray[inode_index].dtime;
 642     buf->st_mtime = delarray[inode_index].dtime;
 643 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 644     buf->st_atim.tv_nsec = buf->st_mtim.tv_nsec = buf->st_ctim.tv_nsec = 0;
 645 #endif
 646     return 0;
 647 }
 648 
 649 /* --------------------------------------------------------------------------------------------- */
 650 
 651 static int
 652 undelfs_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 653 {
 654     int inode_index;
 655     char *file, *f = NULL;
 656 
 657     undelfs_get_path (vpath, &file, &f);
 658     if (file == NULL)
 659     {
 660         g_free (f);
 661         return 0;
 662     }
 663 
 664     /* When called from save_cwd_stats we get an incorrect file and f here:
 665        e.g. incorrect                         correct
 666        path = "undel:/dev/sda1"          path="undel:/dev/sda1/401:1"
 667        file = "/dev"                     file="/dev/sda1"
 668        f    = "sda1"                     f   ="401:1"
 669        If the first char in f is no digit -> return error */
 670     if (!isdigit (*f))
 671     {
 672         g_free (file);
 673         g_free (f);
 674         return -1;
 675     }
 676 
 677     if (!ext2_fname || strcmp (ext2_fname, file))
 678     {
 679         g_free (file);
 680         g_free (f);
 681         message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
 682         return 0;
 683     }
 684     inode_index = undelfs_getindex (f);
 685     g_free (file);
 686     g_free (f);
 687 
 688     if (inode_index == -1)
 689         return -1;
 690 
 691     return undelfs_stat_int (inode_index, buf);
 692 }
 693 
 694 /* --------------------------------------------------------------------------------------------- */
 695 
 696 static int
 697 undelfs_fstat (void *vfs_info, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 698 {
 699     undelfs_file *p = vfs_info;
 700 
 701     return undelfs_stat_int (p->f_index, buf);
 702 }
 703 
 704 /* --------------------------------------------------------------------------------------------- */
 705 
 706 static int
 707 undelfs_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 708 {
 709     char *file, *f = NULL;
 710     int fd;
 711 
 712     undelfs_get_path (vpath, &file, &f);
 713     if (file == NULL)
 714     {
 715         g_free (f);
 716         return (-1);
 717     }
 718 
 719     /* We may use access because ext2 file systems are local */
 720     /* this could be fixed by making an ext2fs io manager to use */
 721     /* our vfs, but that is left as an exercise for the reader */
 722     fd = open (file, O_RDONLY);
 723     if (fd == -1)
 724     {
 725         message (D_ERROR, undelfserr, _("Cannot open file \"%s\""), file);
 726         g_free (f);
 727         g_free (file);
 728         return -1;
 729     }
 730     close (fd);
 731     g_free (f);
 732     g_free (file);
 733     return 0;
 734 }
 735 
 736 /* --------------------------------------------------------------------------------------------- */
 737 
 738 /* this has to stay here for now: vfs layer does not know how to emulate it */
 739 static off_t
 740 undelfs_lseek (void *vfs_info, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
 741 {
 742     (void) vfs_info;
 743     (void) offset;
 744     (void) whence;
 745 
 746     return -1;
 747 }
 748 
 749 /* --------------------------------------------------------------------------------------------- */
 750 
 751 static vfsid
 752 undelfs_getid (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 753 {
 754     char *fname = NULL, *fsname;
 755     gboolean ok;
 756 
 757     undelfs_get_path (vpath, &fsname, &fname);
 758     ok = fsname != NULL;
 759 
 760     g_free (fname);
 761     g_free (fsname);
 762 
 763     return ok ? (vfsid) fs : NULL;
 764 }
 765 
 766 /* --------------------------------------------------------------------------------------------- */
 767 
 768 static gboolean
 769 undelfs_nothingisopen (vfsid id)
     /* [previous][next][first][last][top][bottom][index][help]  */
 770 {
 771     (void) id;
 772 
 773     return (undelfs_usage == 0);
 774 }
 775 
 776 /* --------------------------------------------------------------------------------------------- */
 777 
 778 static void
 779 undelfs_free (vfsid id)
     /* [previous][next][first][last][top][bottom][index][help]  */
 780 {
 781     (void) id;
 782 
 783     undelfs_shutdown ();
 784 }
 785 
 786 /* --------------------------------------------------------------------------------------------- */
 787 
 788 #ifdef ENABLE_NLS
 789 static int
 790 undelfs_init (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 791 {
 792     (void) me;
 793 
 794     undelfserr = _(undelfserr);
 795     return 1;
 796 }
 797 #else
 798 #define undelfs_init NULL
 799 #endif
 800 
 801 /* --------------------------------------------------------------------------------------------- */
 802 /*** public functions ****************************************************************************/
 803 /* --------------------------------------------------------------------------------------------- */
 804 /**
 805  * This function overrides com_err() from libcom_err library.
 806  * It is used in libext2fs to report errors.
 807  */
 808 
 809 void
 810 com_err (const char *whoami, long err_code, const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 811 {
 812     va_list ap;
 813     char *str;
 814 
 815     va_start (ap, fmt);
 816     str = g_strdup_vprintf (fmt, ap);
 817     va_end (ap);
 818 
 819     message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code);
 820     g_free (str);
 821 }
 822 
 823 /* --------------------------------------------------------------------------------------------- */
 824 
 825 void
 826 vfs_init_undelfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 827 {
 828     /* NULLize vfs_s_subclass members */
 829     memset (&undelfs_subclass, 0, sizeof (undelfs_subclass));
 830 
 831     vfs_init_class (vfs_undelfs_ops, "undelfs", VFS_UNKNOWN, "undel");
 832     vfs_undelfs_ops->init = undelfs_init;
 833     vfs_undelfs_ops->open = undelfs_open;
 834     vfs_undelfs_ops->close = undelfs_close;
 835     vfs_undelfs_ops->read = undelfs_read;
 836     vfs_undelfs_ops->opendir = undelfs_opendir;
 837     vfs_undelfs_ops->readdir = undelfs_readdir;
 838     vfs_undelfs_ops->closedir = undelfs_closedir;
 839     vfs_undelfs_ops->stat = undelfs_stat;
 840     vfs_undelfs_ops->lstat = undelfs_lstat;
 841     vfs_undelfs_ops->fstat = undelfs_fstat;
 842     vfs_undelfs_ops->chdir = undelfs_chdir;
 843     vfs_undelfs_ops->lseek = undelfs_lseek;
 844     vfs_undelfs_ops->getid = undelfs_getid;
 845     vfs_undelfs_ops->nothingisopen = undelfs_nothingisopen;
 846     vfs_undelfs_ops->free = undelfs_free;
 847     vfs_register_class (vfs_undelfs_ops);
 848 }
 849 
 850 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */