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-2020
  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 struct vfs_dirent *
 404 undelfs_readdir (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 405 {
 406     struct vfs_dirent *dirent;
 407 
 408     if (vfs_info != fs)
 409     {
 410         message (D_ERROR, undelfserr, "%s", _("vfs_info is not fs!"));
 411         return NULL;
 412     }
 413     if (readdir_ptr == num_delarray)
 414         return NULL;
 415     if (readdir_ptr < 0)
 416         dirent = vfs_dirent_init (NULL, readdir_ptr == -2 ? "." : "..", 0);     /* FIXME: inode */
 417     else
 418     {
 419         char dirent_dest[MC_MAXPATHLEN];
 420 
 421         g_snprintf (dirent_dest, MC_MAXPATHLEN, "%ld:%d",
 422                     (long) delarray[readdir_ptr].ino, delarray[readdir_ptr].num_blocks);
 423         dirent = vfs_dirent_init (NULL, dirent_dest, 0);        /* FIXME: inode */
 424     }
 425     readdir_ptr++;
 426 
 427     return dirent;
 428 }
 429 
 430 /* --------------------------------------------------------------------------------------------- */
 431 
 432 static int
 433 undelfs_closedir (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 434 {
 435     (void) vfs_info;
 436     return 0;
 437 }
 438 
 439 /* --------------------------------------------------------------------------------------------- */
 440 /* We do not support lseek */
 441 
 442 static void *
 443 undelfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
 444 {
 445     char *file, *f = NULL;
 446     ext2_ino_t inode, i;
 447     undelfs_file *p = NULL;
 448     (void) flags;
 449     (void) mode;
 450 
 451     /* Only allow reads on this file system */
 452     undelfs_get_path (vpath, &file, &f);
 453     if (file == NULL)
 454     {
 455         g_free (f);
 456         return 0;
 457     }
 458 
 459     if (!ext2_fname || strcmp (ext2_fname, file))
 460     {
 461         message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
 462         g_free (file);
 463         g_free (f);
 464         return 0;
 465     }
 466     inode = atol (f);
 467 
 468     /* Search the file into delarray */
 469     for (i = 0; i < (ext2_ino_t) num_delarray; i++)
 470     {
 471         if (inode != delarray[i].ino)
 472             continue;
 473 
 474         /* Found: setup all the structures needed by read */
 475         p = (undelfs_file *) g_try_malloc (((gsize) sizeof (undelfs_file)));
 476         if (!p)
 477         {
 478             g_free (file);
 479             g_free (f);
 480             return 0;
 481         }
 482         p->buf = g_try_malloc (fs->blocksize);
 483         if (!p->buf)
 484         {
 485             g_free (p);
 486             g_free (file);
 487             g_free (f);
 488             return 0;
 489         }
 490         p->inode = inode;
 491         p->finished = FALSE;
 492         p->f_index = i;
 493         p->error_code = 0;
 494         p->pos = 0;
 495         p->size = delarray[i].size;
 496     }
 497     g_free (file);
 498     g_free (f);
 499     undelfs_usage++;
 500     return p;
 501 }
 502 
 503 /* --------------------------------------------------------------------------------------------- */
 504 
 505 static int
 506 undelfs_close (void *vfs_info)
     /* [previous][next][first][last][top][bottom][index][help]  */
 507 {
 508     undelfs_file *p = vfs_info;
 509     g_free (p->buf);
 510     g_free (p);
 511     undelfs_usage--;
 512     return 0;
 513 }
 514 
 515 /* --------------------------------------------------------------------------------------------- */
 516 
 517 static int
 518 undelfs_dump_read (ext2_filsys param_fs, blk_t * blocknr, int blockcnt, void *private)
     /* [previous][next][first][last][top][bottom][index][help]  */
 519 {
 520     int copy_count;
 521     undelfs_file *p = (undelfs_file *) private;
 522 
 523     if (blockcnt < 0)
 524         return 0;
 525 
 526     if (*blocknr)
 527     {
 528         p->error_code = io_channel_read_blk (param_fs->io, *blocknr, 1, p->buf);
 529         if (p->error_code)
 530             return BLOCK_ABORT;
 531     }
 532     else
 533         memset (p->buf, 0, param_fs->blocksize);
 534 
 535     if (p->pos + (off_t) p->count < p->current)
 536     {
 537         p->finished = TRUE;
 538         return BLOCK_ABORT;
 539     }
 540     if (p->pos > p->current + param_fs->blocksize)
 541     {
 542         p->current += param_fs->blocksize;
 543         return 0;               /* we have not arrived yet */
 544     }
 545 
 546     /* Now, we know we have to extract some data */
 547     if (p->pos >= p->current)
 548     {
 549 
 550         /* First case: starting pointer inside this block */
 551         if (p->pos + (off_t) p->count <= p->current + param_fs->blocksize)
 552         {
 553             /* Fully contained */
 554             copy_count = p->count;
 555             p->finished = (p->count != 0);
 556         }
 557         else
 558         {
 559             /* Still some more data */
 560             copy_count = param_fs->blocksize - (p->pos - p->current);
 561         }
 562         memcpy (p->dest_buffer, p->buf + (p->pos - p->current), copy_count);
 563     }
 564     else
 565     {
 566         /* Second case: we already have passed p->pos */
 567         if (p->pos + (off_t) p->count < p->current + param_fs->blocksize)
 568         {
 569             copy_count = (p->pos + p->count) - p->current;
 570             p->finished = (p->count != 0);
 571         }
 572         else
 573         {
 574             copy_count = param_fs->blocksize;
 575         }
 576         memcpy (p->dest_buffer, p->buf, copy_count);
 577     }
 578     p->dest_buffer += copy_count;
 579     p->current += param_fs->blocksize;
 580     if (p->finished)
 581     {
 582         return BLOCK_ABORT;
 583     }
 584     return 0;
 585 }
 586 
 587 /* --------------------------------------------------------------------------------------------- */
 588 
 589 static ssize_t
 590 undelfs_read (void *vfs_info, char *buffer, size_t count)
     /* [previous][next][first][last][top][bottom][index][help]  */
 591 {
 592     undelfs_file *p = vfs_info;
 593     int retval;
 594 
 595     p->dest_buffer = buffer;
 596     p->current = 0;
 597     p->finished = FALSE;
 598     p->count = count;
 599 
 600     if (p->pos + (off_t) p->count > p->size)
 601     {
 602         p->count = p->size - p->pos;
 603     }
 604     retval = ext2fs_block_iterate (fs, p->inode, 0, NULL, undelfs_dump_read, p);
 605     if (retval)
 606     {
 607         message (D_ERROR, undelfserr, "%s", _("while iterating over blocks"));
 608         return -1;
 609     }
 610     if (p->error_code && !p->finished)
 611         return 0;
 612     p->pos = p->pos + (p->dest_buffer - buffer);
 613     return p->dest_buffer - buffer;
 614 }
 615 
 616 /* --------------------------------------------------------------------------------------------- */
 617 
 618 static long
 619 undelfs_getindex (char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 620 {
 621     ext2_ino_t inode = atol (path);
 622     int i;
 623 
 624     for (i = 0; i < num_delarray; i++)
 625     {
 626         if (delarray[i].ino == inode)
 627             return i;
 628     }
 629     return -1;
 630 }
 631 
 632 /* --------------------------------------------------------------------------------------------- */
 633 
 634 static int
 635 undelfs_stat_int (int inode_index, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 636 {
 637     buf->st_dev = 0;
 638     buf->st_ino = delarray[inode_index].ino;
 639     buf->st_mode = delarray[inode_index].mode;
 640     buf->st_nlink = 1;
 641     buf->st_uid = delarray[inode_index].uid;
 642     buf->st_gid = delarray[inode_index].gid;
 643     buf->st_size = delarray[inode_index].size;
 644     buf->st_atime = delarray[inode_index].dtime;
 645     buf->st_ctime = delarray[inode_index].dtime;
 646     buf->st_mtime = delarray[inode_index].dtime;
 647 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 648     buf->st_atim.tv_nsec = buf->st_mtim.tv_nsec = buf->st_ctim.tv_nsec = 0;
 649 #endif
 650     return 0;
 651 }
 652 
 653 /* --------------------------------------------------------------------------------------------- */
 654 
 655 static int
 656 undelfs_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 657 {
 658     int inode_index;
 659     char *file, *f = NULL;
 660 
 661     undelfs_get_path (vpath, &file, &f);
 662     if (file == NULL)
 663     {
 664         g_free (f);
 665         return 0;
 666     }
 667 
 668     /* When called from save_cwd_stats we get an incorrect file and f here:
 669        e.g. incorrect                         correct
 670        path = "undel:/dev/sda1"          path="undel:/dev/sda1/401:1"
 671        file = "/dev"                     file="/dev/sda1"
 672        f    = "sda1"                     f   ="401:1"
 673        If the first char in f is no digit -> return error */
 674     if (!isdigit (*f))
 675     {
 676         g_free (file);
 677         g_free (f);
 678         return -1;
 679     }
 680 
 681     if (!ext2_fname || strcmp (ext2_fname, file))
 682     {
 683         g_free (file);
 684         g_free (f);
 685         message (D_ERROR, undelfserr, "%s", _("You have to chdir to extract files first"));
 686         return 0;
 687     }
 688     inode_index = undelfs_getindex (f);
 689     g_free (file);
 690     g_free (f);
 691 
 692     if (inode_index == -1)
 693         return -1;
 694 
 695     return undelfs_stat_int (inode_index, buf);
 696 }
 697 
 698 /* --------------------------------------------------------------------------------------------- */
 699 
 700 static int
 701 undelfs_fstat (void *vfs_info, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 702 {
 703     undelfs_file *p = vfs_info;
 704 
 705     return undelfs_stat_int (p->f_index, buf);
 706 }
 707 
 708 /* --------------------------------------------------------------------------------------------- */
 709 
 710 static int
 711 undelfs_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 712 {
 713     char *file, *f = NULL;
 714     int fd;
 715 
 716     undelfs_get_path (vpath, &file, &f);
 717     if (file == NULL)
 718     {
 719         g_free (f);
 720         return (-1);
 721     }
 722 
 723     /* We may use access because ext2 file systems are local */
 724     /* this could be fixed by making an ext2fs io manager to use */
 725     /* our vfs, but that is left as an exercise for the reader */
 726     fd = open (file, O_RDONLY);
 727     if (fd == -1)
 728     {
 729         message (D_ERROR, undelfserr, _("Cannot open file \"%s\""), file);
 730         g_free (f);
 731         g_free (file);
 732         return -1;
 733     }
 734     close (fd);
 735     g_free (f);
 736     g_free (file);
 737     return 0;
 738 }
 739 
 740 /* --------------------------------------------------------------------------------------------- */
 741 
 742 /* this has to stay here for now: vfs layer does not know how to emulate it */
 743 static off_t
 744 undelfs_lseek (void *vfs_info, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
 745 {
 746     (void) vfs_info;
 747     (void) offset;
 748     (void) whence;
 749 
 750     return -1;
 751 }
 752 
 753 /* --------------------------------------------------------------------------------------------- */
 754 
 755 static vfsid
 756 undelfs_getid (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 757 {
 758     char *fname = NULL, *fsname;
 759     gboolean ok;
 760 
 761     undelfs_get_path (vpath, &fsname, &fname);
 762     ok = fsname != NULL;
 763 
 764     g_free (fname);
 765     g_free (fsname);
 766 
 767     return ok ? (vfsid) fs : NULL;
 768 }
 769 
 770 /* --------------------------------------------------------------------------------------------- */
 771 
 772 static gboolean
 773 undelfs_nothingisopen (vfsid id)
     /* [previous][next][first][last][top][bottom][index][help]  */
 774 {
 775     (void) id;
 776 
 777     return (undelfs_usage == 0);
 778 }
 779 
 780 /* --------------------------------------------------------------------------------------------- */
 781 
 782 static void
 783 undelfs_free (vfsid id)
     /* [previous][next][first][last][top][bottom][index][help]  */
 784 {
 785     (void) id;
 786 
 787     undelfs_shutdown ();
 788 }
 789 
 790 /* --------------------------------------------------------------------------------------------- */
 791 
 792 #ifdef ENABLE_NLS
 793 static int
 794 undelfs_init (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 795 {
 796     (void) me;
 797 
 798     undelfserr = _(undelfserr);
 799     return 1;
 800 }
 801 #else
 802 #define undelfs_init NULL
 803 #endif
 804 
 805 /* --------------------------------------------------------------------------------------------- */
 806 /*** public functions ****************************************************************************/
 807 /* --------------------------------------------------------------------------------------------- */
 808 /**
 809  * This function overrides com_err() from libcom_err library.
 810  * It is used in libext2fs to report errors.
 811  */
 812 
 813 void
 814 com_err (const char *whoami, long err_code, const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 815 {
 816     va_list ap;
 817     char *str;
 818 
 819     va_start (ap, fmt);
 820     str = g_strdup_vprintf (fmt, ap);
 821     va_end (ap);
 822 
 823     message (D_ERROR, _("Ext2lib error"), "%s (%s: %ld)", str, whoami, err_code);
 824     g_free (str);
 825 }
 826 
 827 /* --------------------------------------------------------------------------------------------- */
 828 
 829 void
 830 vfs_init_undelfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 831 {
 832     /* NULLize vfs_s_subclass members */
 833     memset (&undelfs_subclass, 0, sizeof (undelfs_subclass));
 834 
 835     vfs_init_class (vfs_undelfs_ops, "undelfs", VFSF_UNKNOWN, "undel");
 836     vfs_undelfs_ops->init = undelfs_init;
 837     vfs_undelfs_ops->open = undelfs_open;
 838     vfs_undelfs_ops->close = undelfs_close;
 839     vfs_undelfs_ops->read = undelfs_read;
 840     vfs_undelfs_ops->opendir = undelfs_opendir;
 841     vfs_undelfs_ops->readdir = undelfs_readdir;
 842     vfs_undelfs_ops->closedir = undelfs_closedir;
 843     vfs_undelfs_ops->stat = undelfs_stat;
 844     vfs_undelfs_ops->lstat = undelfs_lstat;
 845     vfs_undelfs_ops->fstat = undelfs_fstat;
 846     vfs_undelfs_ops->chdir = undelfs_chdir;
 847     vfs_undelfs_ops->lseek = undelfs_lseek;
 848     vfs_undelfs_ops->getid = undelfs_getid;
 849     vfs_undelfs_ops->nothingisopen = undelfs_nothingisopen;
 850     vfs_undelfs_ops->free = undelfs_free;
 851     vfs_register_class (vfs_undelfs_ops);
 852 }
 853 
 854 /* --------------------------------------------------------------------------------------------- */

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