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

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