undelfs.c 22 KB

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