root/src/vfs/extfs/extfs.c

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

DEFINITIONS

This source file includes following definitions.
  1. extfs_super_new
  2. extfs_entry_new
  3. extfs_fill_name
  4. extfs_cmp_archive
  5. extfs_generate_entry
  6. extfs_find_entry_int
  7. extfs_find_entry
  8. extfs_fill_names
  9. extfs_free_inode
  10. extfs_free_archive
  11. extfs_skip_leading_dotslash
  12. extfs_add_file
  13. extfs_open_archive
  14. extfs_read_archive
  15. extfs_which
  16. extfs_open_and_read_archive
  17. extfs_get_path
  18. extfs_get_path_from_entry
  19. extfs_resolve_symlinks_int
  20. extfs_resolve_symlinks
  21. extfs_get_archive_name
  22. extfs_cmd
  23. extfs_run
  24. extfs_open
  25. extfs_read
  26. extfs_close
  27. extfs_errno
  28. extfs_opendir
  29. extfs_readdir
  30. extfs_closedir
  31. extfs_stat_move
  32. extfs_internal_stat
  33. extfs_stat
  34. extfs_lstat
  35. extfs_fstat
  36. extfs_readlink
  37. extfs_chown
  38. extfs_chmod
  39. extfs_write
  40. extfs_unlink
  41. extfs_mkdir
  42. extfs_rmdir
  43. extfs_chdir
  44. extfs_lseek
  45. extfs_getid
  46. extfs_getlocalcopy
  47. extfs_ungetlocalcopy
  48. extfs_get_plugins
  49. extfs_init
  50. extfs_done
  51. extfs_setctl
  52. vfs_init_extfs

   1 /*
   2    Virtual File System: External file system.
   3 
   4    Copyright (C) 1995-2021
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Jakub Jelinek, 1995
   9    Pavel Machek, 1998
  10    Andrew T. Veliath, 1999
  11    Slava Zanko <slavazanko@gmail.com>, 2013
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27  */
  28 
  29 /**
  30  * \file
  31  * \brief Source: Virtual File System: External file system
  32  * \author Jakub Jelinek
  33  * \author Pavel Machek
  34  * \author Andrew T. Veliath
  35  * \date 1995, 1998, 1999
  36  */
  37 
  38 /* Namespace: init_extfs */
  39 
  40 #include <config.h>
  41 
  42 #include <stdio.h>
  43 #include <ctype.h>
  44 #include <string.h>
  45 #include <stdlib.h>
  46 #include <sys/types.h>
  47 #include <sys/stat.h>
  48 #include <unistd.h>
  49 #include <signal.h>
  50 #include <errno.h>
  51 #include <sys/wait.h>
  52 
  53 #include "lib/global.h"
  54 #include "lib/fileloc.h"
  55 #include "lib/mcconfig.h"
  56 #include "lib/util.h"
  57 #include "lib/widget.h"         /* message() */
  58 
  59 #include "src/execute.h"        /* For shell_execute */
  60 
  61 #include "lib/vfs/vfs.h"
  62 #include "lib/vfs/utilvfs.h"
  63 #include "lib/vfs/xdirentry.h"
  64 #include "lib/vfs/gc.h"         /* vfs_rmstamp */
  65 
  66 #include "extfs.h"
  67 
  68 /*** global variables ****************************************************************************/
  69 
  70 /*** file scope macro definitions ****************************************************************/
  71 
  72 #undef ERRNOR
  73 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
  74 
  75 #define RECORDSIZE 512
  76 
  77 #define EXTFS_SUPER(a) ((struct extfs_super_t *) (a))
  78 
  79 /*** file scope type declarations ****************************************************************/
  80 
  81 struct extfs_super_t
  82 {
  83     struct vfs_s_super base;    /* base class */
  84 
  85     int fstype;
  86     char *local_name;
  87     struct stat local_stat;
  88     dev_t rdev;
  89 };
  90 
  91 typedef struct
  92 {
  93     char *path;
  94     char *prefix;
  95     gboolean need_archive;
  96 } extfs_plugin_info_t;
  97 
  98 /*** file scope variables ************************************************************************/
  99 
 100 static GArray *extfs_plugins = NULL;
 101 
 102 static gboolean errloop;
 103 static gboolean notadir;
 104 
 105 static struct vfs_s_subclass extfs_subclass;
 106 static struct vfs_class *vfs_extfs_ops = VFS_CLASS (&extfs_subclass);
 107 
 108 static int my_errno = 0;
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 /*** file scope functions ************************************************************************/
 112 /* --------------------------------------------------------------------------------------------- */
 113 
 114 static struct vfs_s_entry *extfs_resolve_symlinks_int (struct vfs_s_entry *entry, GSList * list);
 115 
 116 /* --------------------------------------------------------------------------------------------- */
 117 
 118 static struct extfs_super_t *
 119 extfs_super_new (struct vfs_class *me, const char *name, const vfs_path_t * local_name_vpath,
     /* [previous][next][first][last][top][bottom][index][help]  */
 120                  int fstype)
 121 {
 122     struct extfs_super_t *super;
 123     struct vfs_s_super *vsuper;
 124 
 125     super = g_new0 (struct extfs_super_t, 1);
 126     vsuper = VFS_SUPER (super);
 127 
 128     vsuper->me = me;
 129     vsuper->name = g_strdup (name);
 130 
 131     super->fstype = fstype;
 132 
 133     if (local_name_vpath != NULL)
 134     {
 135         super->local_name = g_strdup (vfs_path_get_last_path_str (local_name_vpath));
 136         mc_stat (local_name_vpath, &super->local_stat);
 137     }
 138 
 139     VFS_SUBCLASS (me)->supers = g_list_prepend (VFS_SUBCLASS (me)->supers, super);
 140 
 141     return super;
 142 }
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 /* unlike vfs_s_new_entry(), inode->ent is kept */
 147 static struct vfs_s_entry *
 148 extfs_entry_new (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     struct vfs_s_entry *entry;
 151 
 152     (void) me;
 153 
 154     entry = g_new0 (struct vfs_s_entry, 1);
 155 
 156     entry->name = g_strdup (name);
 157     entry->ino = inode;
 158 
 159     return entry;
 160 }
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 
 164 static void
 165 extfs_fill_name (void *data, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 166 {
 167     struct vfs_s_super *a = VFS_SUPER (data);
 168     fill_names_f func = (fill_names_f) user_data;
 169     extfs_plugin_info_t *info;
 170     char *name;
 171 
 172     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, EXTFS_SUPER (a)->fstype);
 173     name =
 174         g_strconcat (a->name != NULL ? a->name : "", PATH_SEP_STR, info->prefix,
 175                      VFS_PATH_URL_DELIMITER, (char *) NULL);
 176     func (name);
 177     g_free (name);
 178 }
 179 
 180 /* --------------------------------------------------------------------------------------------- */
 181 
 182 static gint
 183 extfs_cmp_archive (const void *a, const void *b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 184 {
 185     const struct vfs_s_super *ar = (const struct vfs_s_super *) a;
 186     const char *archive_name = (const char *) b;
 187 
 188     return (ar->name != NULL && strcmp (ar->name, archive_name) == 0) ? 0 : 1;
 189 }
 190 
 191 /* --------------------------------------------------------------------------------------------- */
 192 
 193 static struct vfs_s_entry *
 194 extfs_generate_entry (struct extfs_super_t *archive, const char *name, struct vfs_s_inode *parent,
     /* [previous][next][first][last][top][bottom][index][help]  */
 195                       mode_t mode)
 196 {
 197     struct vfs_class *me = VFS_SUPER (archive)->me;
 198     struct stat st;
 199     mode_t myumask;
 200     struct vfs_s_inode *inode;
 201     struct vfs_s_entry *entry;
 202 
 203     memset (&st, 0, sizeof (st));
 204     st.st_ino = VFS_SUPER (archive)->ino_usage++;
 205     st.st_dev = archive->rdev;
 206     myumask = umask (022);
 207     umask (myumask);
 208     st.st_mode = mode & ~myumask;
 209     st.st_uid = getuid ();
 210     st.st_gid = getgid ();
 211     st.st_mtime = time (NULL);
 212     st.st_atime = st.st_mtime;
 213     st.st_ctime = st.st_mtime;
 214     st.st_nlink = 1;
 215 
 216     inode = vfs_s_new_inode (me, VFS_SUPER (archive), &st);
 217     entry = vfs_s_new_entry (me, name, inode);
 218     if (parent != NULL)
 219         vfs_s_insert_entry (me, parent, entry);
 220 
 221     return entry;
 222 }
 223 
 224 /* --------------------------------------------------------------------------------------------- */
 225 
 226 static struct vfs_s_entry *
 227 extfs_find_entry_int (struct vfs_s_inode *dir, const char *name, GSList * list, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 228 {
 229     struct vfs_s_entry *pent, *pdir;
 230     const char *p, *name_end;
 231     char *q;
 232     char c = PATH_SEP;
 233     struct extfs_super_t *super;
 234 
 235     if (g_path_is_absolute (name))
 236     {
 237         /* Handle absolute paths */
 238         name = g_path_skip_root (name);
 239         dir = dir->super->root;
 240     }
 241 
 242     super = EXTFS_SUPER (dir->super);
 243     pent = dir->ent;
 244     p = name;
 245     name_end = name + strlen (name);
 246 
 247     while ((pent != NULL) && (c != '\0') && (*p != '\0'))
 248     {
 249         q = strchr (p, PATH_SEP);
 250         if (q == NULL)
 251             q = (char *) name_end;
 252 
 253         c = *q;
 254         *q = '\0';
 255 
 256         if (DIR_IS_DOTDOT (p))
 257             pent = pent->dir->ent;
 258         else
 259         {
 260             GList *pl;
 261 
 262             pent = extfs_resolve_symlinks_int (pent, list);
 263             if (pent == NULL)
 264             {
 265                 *q = c;
 266                 return NULL;
 267             }
 268 
 269             if (!S_ISDIR (pent->ino->st.st_mode))
 270             {
 271                 *q = c;
 272                 notadir = TRUE;
 273                 return NULL;
 274             }
 275 
 276             pdir = pent;
 277             pl = g_queue_find_custom (pent->ino->subdir, p, vfs_s_entry_compare);
 278             pent = pl != NULL ? VFS_ENTRY (pl->data) : NULL;
 279             if (pent != NULL && q + 1 > name_end)
 280             {
 281                 /* Hack: I keep the original semanthic unless q+1 would break in the strchr */
 282                 *q = c;
 283                 notadir = !S_ISDIR (pent->ino->st.st_mode);
 284                 return pent;
 285             }
 286 
 287             /* When we load archive, we create automagically non-existent directories */
 288             if (pent == NULL && (flags & FL_MKDIR) != 0)
 289                 pent = extfs_generate_entry (super, p, pdir->ino, S_IFDIR | 0777);
 290             if (pent == NULL && (flags & FL_MKFILE) != 0)
 291                 pent = extfs_generate_entry (super, p, pdir->ino, S_IFREG | 0666);
 292         }
 293 
 294         /* Next iteration */
 295         *q = c;
 296         if (c != '\0')
 297             p = q + 1;
 298     }
 299     if (pent == NULL)
 300         my_errno = ENOENT;
 301     return pent;
 302 }
 303 
 304 /* --------------------------------------------------------------------------------------------- */
 305 
 306 static struct vfs_s_entry *
 307 extfs_find_entry (struct vfs_s_inode *dir, const char *name, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 308 {
 309     struct vfs_s_entry *res;
 310 
 311     errloop = FALSE;
 312     notadir = FALSE;
 313 
 314     res = extfs_find_entry_int (dir, name, NULL, flags);
 315     if (res == NULL)
 316     {
 317         if (errloop)
 318             my_errno = ELOOP;
 319         else if (notadir)
 320             my_errno = ENOTDIR;
 321     }
 322     return res;
 323 }
 324 
 325 /* --------------------------------------------------------------------------------------------- */
 326 
 327 static void
 328 extfs_fill_names (struct vfs_class *me, fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
 329 {
 330     g_list_foreach (VFS_SUBCLASS (me)->supers, extfs_fill_name, func);
 331 }
 332 
 333 /* --------------------------------------------------------------------------------------------- */
 334 
 335 /* Create this function because VFSF_USETMP flag is not used in extfs */
 336 static void
 337 extfs_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
     /* [previous][next][first][last][top][bottom][index][help]  */
 338 {
 339     (void) me;
 340 
 341     if (ino->localname != NULL)
 342     {
 343         unlink (ino->localname);
 344         MC_PTR_FREE (ino->localname);
 345     }
 346 }
 347 
 348 /* --------------------------------------------------------------------------------------------- */
 349 
 350 static void
 351 extfs_free_archive (struct vfs_class *me, struct vfs_s_super *psup)
     /* [previous][next][first][last][top][bottom][index][help]  */
 352 {
 353     struct extfs_super_t *archive = EXTFS_SUPER (psup);
 354 
 355     (void) me;
 356 
 357     if (archive->local_name != NULL)
 358     {
 359         struct stat my;
 360         vfs_path_t *local_name_vpath, *name_vpath;
 361 
 362         local_name_vpath = vfs_path_from_str (archive->local_name);
 363         name_vpath = vfs_path_from_str (psup->name);
 364         mc_stat (local_name_vpath, &my);
 365         mc_ungetlocalcopy (name_vpath, local_name_vpath,
 366                            archive->local_stat.st_mtime != my.st_mtime);
 367         vfs_path_free (local_name_vpath, TRUE);
 368         vfs_path_free (name_vpath, TRUE);
 369         g_free (archive->local_name);
 370     }
 371 }
 372 
 373 /* --------------------------------------------------------------------------------------------- */
 374 
 375 static inline char *
 376 extfs_skip_leading_dotslash (char *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
 377 {
 378     /* Skip leading "./" (if present).
 379      * Some programs don't understand it:
 380      *
 381      * $ zip file.zip ./-file2.txt file1.txt
 382      *   adding: -file2.txt (stored 0%)
 383      *   adding: file1.txt (stored 0%)
 384      * $ /usr/lib/mc/extfs.d/uzip copyout file.zip ./-file2.txt ./tmp-file2.txt
 385      * caution: filename not matched:  ./-file2.txt
 386      */
 387     if (s[0] == '.' && s[1] == PATH_SEP)
 388         s += 2;
 389 
 390     return s;
 391 }
 392 
 393 /* --------------------------------------------------------------------------------------------- */
 394 
 395 static int
 396 extfs_add_file (struct extfs_super_t *archive, const char *file_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 397 {
 398     struct vfs_s_super *super = VFS_SUPER (archive);
 399     struct stat hstat;
 400     char *current_file_name = NULL, *current_link_name = NULL;
 401     int ret = 0;
 402 
 403     if (vfs_parse_ls_lga (file_name, &hstat, &current_file_name, &current_link_name, NULL))
 404     {
 405         char *cfn = current_file_name;
 406 
 407         if (*cfn != '\0')
 408         {
 409             struct vfs_s_entry *entry;
 410             struct vfs_s_entry *pent = NULL;
 411             struct vfs_s_inode *inode;
 412             char *p, *q;
 413 
 414             cfn = extfs_skip_leading_dotslash (cfn);
 415             if (IS_PATH_SEP (*cfn))
 416                 cfn++;
 417             p = strchr (cfn, '\0');
 418             if (p != cfn && IS_PATH_SEP (p[-1]))
 419                 p[-1] = '\0';
 420             p = strrchr (cfn, PATH_SEP);
 421             if (p == NULL)
 422             {
 423                 p = cfn;
 424                 q = strchr (cfn, '\0');
 425             }
 426             else
 427             {
 428                 *(p++) = '\0';
 429                 q = cfn;
 430             }
 431 
 432             if (*q != '\0')
 433             {
 434                 pent = extfs_find_entry (super->root, q, FL_MKDIR);
 435                 if (pent == NULL)
 436                 {
 437                     ret = -1;
 438                     goto done;
 439                 }
 440             }
 441 
 442             if (pent != NULL)
 443             {
 444                 entry = extfs_entry_new (super->me, p, pent->ino);
 445                 entry->dir = pent->ino;
 446                 g_queue_push_tail (pent->ino->subdir, entry);
 447             }
 448             else
 449             {
 450                 entry = extfs_entry_new (super->me, p, super->root);
 451                 entry->dir = super->root;
 452                 g_queue_push_tail (super->root->subdir, entry);
 453             }
 454 
 455             if (!S_ISLNK (hstat.st_mode) && (current_link_name != NULL))
 456             {
 457                 pent = extfs_find_entry (super->root, current_link_name, FL_NONE);
 458                 if (pent == NULL)
 459                 {
 460                     ret = -1;
 461                     goto done;
 462                 }
 463 
 464                 pent->ino->st.st_nlink++;
 465                 entry->ino = pent->ino;
 466             }
 467             else
 468             {
 469                 struct stat st;
 470 
 471                 memset (&st, 0, sizeof (st));
 472                 st.st_ino = super->ino_usage++;
 473                 st.st_nlink = 1;
 474                 st.st_dev = archive->rdev;
 475                 st.st_mode = hstat.st_mode;
 476 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 477                 st.st_rdev = hstat.st_rdev;
 478 #endif
 479                 st.st_uid = hstat.st_uid;
 480                 st.st_gid = hstat.st_gid;
 481                 st.st_size = hstat.st_size;
 482                 st.st_mtime = hstat.st_mtime;
 483                 st.st_atime = hstat.st_atime;
 484                 st.st_ctime = hstat.st_ctime;
 485 
 486                 if (current_link_name == NULL && S_ISLNK (hstat.st_mode))
 487                     st.st_mode &= ~S_IFLNK;     /* You *DON'T* want to do this always */
 488 
 489                 inode = vfs_s_new_inode (super->me, super, &st);
 490                 inode->ent = entry;
 491                 entry->ino = inode;
 492 
 493                 if (current_link_name != NULL && S_ISLNK (hstat.st_mode))
 494                 {
 495                     inode->linkname = current_link_name;
 496                     current_link_name = NULL;
 497                 }
 498             }
 499         }
 500 
 501       done:
 502         g_free (current_file_name);
 503         g_free (current_link_name);
 504     }
 505 
 506     return ret;
 507 }
 508 
 509 /* --------------------------------------------------------------------------------------------- */
 510 
 511 static mc_pipe_t *
 512 extfs_open_archive (int fstype, const char *name, struct extfs_super_t **pparc, GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 513 {
 514     const extfs_plugin_info_t *info;
 515     static dev_t archive_counter = 0;
 516     mc_pipe_t *result = NULL;
 517     mode_t mode;
 518     char *cmd = NULL;
 519     struct stat mystat;
 520     struct extfs_super_t *current_archive;
 521     struct vfs_s_entry *root_entry;
 522     char *tmp = NULL;
 523     vfs_path_t *local_name_vpath = NULL;
 524     const char *local_last_path = NULL;
 525     vfs_path_t *name_vpath;
 526 
 527     memset (&mystat, 0, sizeof (mystat));
 528 
 529     name_vpath = vfs_path_from_str (name);
 530     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
 531 
 532     if (info->need_archive)
 533     {
 534         if (mc_stat (name_vpath, &mystat) == -1)
 535             goto ret;
 536 
 537         if (!vfs_file_is_local (name_vpath))
 538         {
 539             local_name_vpath = mc_getlocalcopy (name_vpath);
 540             if (local_name_vpath == NULL)
 541                 goto ret;
 542         }
 543 
 544         local_last_path = vfs_path_get_last_path_str (local_name_vpath);
 545         if (local_last_path == NULL)
 546             tmp = name_quote (vfs_path_get_last_path_str (name_vpath), FALSE);
 547     }
 548 
 549     if (local_last_path != NULL)
 550         cmd = g_strconcat (info->path, info->prefix, " list ", local_last_path, (char *) NULL);
 551     else if (tmp != NULL)
 552     {
 553         cmd = g_strconcat (info->path, info->prefix, " list ", tmp, (char *) NULL);
 554         g_free (tmp);
 555     }
 556 
 557     if (cmd != NULL)
 558     {
 559         result = mc_popen (cmd, TRUE, TRUE, error);
 560         g_free (cmd);
 561     }
 562 
 563     if (result == NULL)
 564     {
 565         if (local_name_vpath != NULL)
 566         {
 567             mc_ungetlocalcopy (name_vpath, local_name_vpath, FALSE);
 568             vfs_path_free (local_name_vpath, TRUE);
 569         }
 570         goto ret;
 571     }
 572 
 573     current_archive = extfs_super_new (vfs_extfs_ops, name, local_name_vpath, fstype);
 574     current_archive->rdev = archive_counter++;
 575     vfs_path_free (local_name_vpath, TRUE);
 576 
 577     mode = mystat.st_mode & 07777;
 578     if (mode & 0400)
 579         mode |= 0100;
 580     if (mode & 0040)
 581         mode |= 0010;
 582     if (mode & 0004)
 583         mode |= 0001;
 584     mode |= S_IFDIR;
 585 
 586     root_entry = extfs_generate_entry (current_archive, PATH_SEP_STR, NULL, mode);
 587     root_entry->ino->st.st_uid = mystat.st_uid;
 588     root_entry->ino->st.st_gid = mystat.st_gid;
 589     root_entry->ino->st.st_atime = mystat.st_atime;
 590     root_entry->ino->st.st_ctime = mystat.st_ctime;
 591     root_entry->ino->st.st_mtime = mystat.st_mtime;
 592     root_entry->ino->ent = root_entry;
 593     VFS_SUPER (current_archive)->root = root_entry->ino;
 594 
 595     *pparc = current_archive;
 596 
 597   ret:
 598     vfs_path_free (name_vpath, TRUE);
 599     return result;
 600 }
 601 
 602 /* --------------------------------------------------------------------------------------------- */
 603 /**
 604  * Main loop for reading an archive.
 605  * Return 0 on success, -1 on error.
 606  */
 607 
 608 static int
 609 extfs_read_archive (mc_pipe_t * pip, struct extfs_super_t *archive, GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 610 {
 611     int ret = 0;
 612     GString *buffer;
 613     GString *err_msg = NULL;
 614     GString *remain_file_name = NULL;
 615 
 616     while (ret != -1)
 617     {
 618         /* init buffers before call of mc_pread() */
 619         pip->out.len = MC_PIPE_BUFSIZE;
 620         pip->err.len = MC_PIPE_BUFSIZE;
 621 
 622         mc_pread (pip, error);
 623 
 624         if (*error != NULL)
 625             return (-1);
 626 
 627         if (pip->err.len > 0)
 628         {
 629             /* join errors/warnings */
 630             if (err_msg == NULL)
 631                 err_msg = g_string_new_len (pip->err.buf, pip->err.len);
 632             else
 633             {
 634                 if (err_msg->str[err_msg->len - 1] != '\n')
 635                     g_string_append_c (err_msg, '\n');
 636                 g_string_append_len (err_msg, pip->err.buf, pip->err.len);
 637             }
 638         }
 639 
 640         if (pip->out.len == MC_PIPE_STREAM_EOF)
 641             break;
 642 
 643         if (pip->out.len == 0)
 644             continue;
 645 
 646         if (pip->out.len == MC_PIPE_ERROR_READ)
 647             return (-1);
 648 
 649         while (ret != -1 && (buffer = mc_pstream_get_string (&pip->out)) != NULL)
 650         {
 651             /* handle a \n-separated file list */
 652 
 653             if (buffer->str[buffer->len - 1] == '\n')
 654             {
 655                 /* entire file name or last chunk */
 656 
 657                 g_string_truncate (buffer, buffer->len - 1);
 658 
 659                 /* join filename chunks */
 660                 if (remain_file_name != NULL)
 661                 {
 662                     g_string_append_len (remain_file_name, buffer->str, buffer->len);
 663                     g_string_free (buffer, TRUE);
 664                     buffer = remain_file_name;
 665                     remain_file_name = NULL;
 666                 }
 667             }
 668             else
 669             {
 670                 /* first or middle chunk of file name */
 671 
 672                 if (remain_file_name == NULL)
 673                     remain_file_name = buffer;
 674                 else
 675                 {
 676                     g_string_append_len (remain_file_name, buffer->str, buffer->len);
 677                     g_string_free (buffer, TRUE);
 678                 }
 679 
 680                 continue;
 681             }
 682 
 683             ret = extfs_add_file (archive, buffer->str);
 684 
 685             g_string_free (buffer, TRUE);
 686         }
 687     }
 688 
 689     if (remain_file_name != NULL)
 690         g_string_free (remain_file_name, TRUE);
 691 
 692     if (err_msg != NULL)
 693     {
 694         if (*error == NULL)
 695             mc_propagate_error (error, 0, "%s", err_msg->str);
 696 
 697         g_string_free (err_msg, TRUE);
 698     }
 699 
 700     return ret;
 701 }
 702 
 703 /* --------------------------------------------------------------------------------------------- */
 704 
 705 static int
 706 extfs_which (struct vfs_class *me, const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 707 {
 708     size_t path_len;
 709     size_t i;
 710 
 711     (void) me;
 712 
 713     path_len = strlen (path);
 714 
 715     for (i = 0; i < extfs_plugins->len; i++)
 716     {
 717         extfs_plugin_info_t *info;
 718 
 719         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
 720 
 721         if ((strncmp (path, info->prefix, path_len) == 0)
 722             && ((info->prefix[path_len] == '\0') || (info->prefix[path_len] == '+')))
 723             return i;
 724     }
 725     return -1;
 726 }
 727 
 728 /* --------------------------------------------------------------------------------------------- */
 729 
 730 static int
 731 extfs_open_and_read_archive (int fstype, const char *name, struct extfs_super_t **archive)
     /* [previous][next][first][last][top][bottom][index][help]  */
 732 {
 733     int result = -1;
 734     struct extfs_super_t *a;
 735     mc_pipe_t *pip;
 736     GError *error = NULL;
 737 
 738     pip = extfs_open_archive (fstype, name, archive, &error);
 739 
 740     a = *archive;
 741 
 742     if (pip == NULL)
 743     {
 744         const extfs_plugin_info_t *info;
 745 
 746         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
 747         message (D_ERROR, MSG_ERROR, _("Cannot open %s archive\n%s:\n%s"), info->prefix, name,
 748                  error->message);
 749         g_error_free (error);
 750     }
 751     else
 752     {
 753         result = extfs_read_archive (pip, a, &error);
 754 
 755         if (result != 0)
 756             VFS_SUPER (a)->me->free (VFS_SUPER (a));
 757 
 758         if (error != NULL)
 759         {
 760             message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 761             g_error_free (error);
 762         }
 763 
 764         mc_pclose (pip, NULL);
 765     }
 766 
 767     return result;
 768 }
 769 
 770 /* --------------------------------------------------------------------------------------------- */
 771 /**
 772  * Dissect the path and create corresponding superblock.
 773  */
 774 static const char *
 775 extfs_get_path (const vfs_path_t * vpath, struct extfs_super_t **archive, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 776 {
 777     char *archive_name;
 778     int result = -1;
 779     GList *parc;
 780     int fstype;
 781     const vfs_path_element_t *path_element;
 782     struct extfs_super_t *a = NULL;
 783 
 784     path_element = vfs_path_get_by_index (vpath, -1);
 785 
 786     fstype = extfs_which (path_element->class, path_element->vfs_prefix);
 787     if (fstype == -1)
 788         return NULL;
 789 
 790     archive_name = vfs_path_to_str_elements_count (vpath, -1);
 791 
 792     /* All filesystems should have some local archive, at least it can be PATH_SEP ('/'). */
 793     parc = g_list_find_custom (extfs_subclass.supers, archive_name, extfs_cmp_archive);
 794     if (parc != NULL)
 795     {
 796         a = EXTFS_SUPER (parc->data);
 797         vfs_stamp (vfs_extfs_ops, (vfsid) a);
 798         g_free (archive_name);
 799     }
 800     else
 801     {
 802         if ((flags & FL_NO_OPEN) == 0)
 803             result = extfs_open_and_read_archive (fstype, archive_name, &a);
 804 
 805         g_free (archive_name);
 806 
 807         if (result == -1)
 808         {
 809             path_element->class->verrno = EIO;
 810             return NULL;
 811         }
 812     }
 813 
 814     *archive = a;
 815     return path_element->path;
 816 }
 817 
 818 /* --------------------------------------------------------------------------------------------- */
 819 /* Return allocated path (without leading slash) inside the archive  */
 820 
 821 static char *
 822 extfs_get_path_from_entry (const struct vfs_s_entry *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 823 {
 824     const struct vfs_s_entry *e;
 825     GString *localpath;
 826 
 827     localpath = g_string_new ("");
 828 
 829     for (e = entry; e->dir != NULL; e = e->dir->ent)
 830     {
 831         g_string_prepend (localpath, e->name);
 832         if (e->dir->ent->dir != NULL)
 833             g_string_prepend_c (localpath, PATH_SEP);
 834     }
 835 
 836     return g_string_free (localpath, FALSE);
 837 }
 838 
 839 /* --------------------------------------------------------------------------------------------- */
 840 
 841 static struct vfs_s_entry *
 842 extfs_resolve_symlinks_int (struct vfs_s_entry *entry, GSList * list)
     /* [previous][next][first][last][top][bottom][index][help]  */
 843 {
 844     struct vfs_s_entry *pent = NULL;
 845 
 846     if (!S_ISLNK (entry->ino->st.st_mode))
 847         return entry;
 848 
 849     if (g_slist_find (list, entry) != NULL)
 850     {
 851         /* Here we protect us against symlink looping */
 852         errloop = TRUE;
 853     }
 854     else
 855     {
 856         GSList *looping;
 857 
 858         looping = g_slist_prepend (list, entry);
 859         pent = extfs_find_entry_int (entry->dir, entry->ino->linkname, looping, FL_NONE);
 860         looping = g_slist_delete_link (looping, looping);
 861 
 862         if (pent == NULL)
 863             my_errno = ENOENT;
 864     }
 865 
 866     return pent;
 867 }
 868 
 869 /* --------------------------------------------------------------------------------------------- */
 870 
 871 static struct vfs_s_entry *
 872 extfs_resolve_symlinks (struct vfs_s_entry *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 873 {
 874     struct vfs_s_entry *res;
 875 
 876     errloop = FALSE;
 877     notadir = FALSE;
 878     res = extfs_resolve_symlinks_int (entry, NULL);
 879     if (res == NULL)
 880     {
 881         if (errloop)
 882             my_errno = ELOOP;
 883         else if (notadir)
 884             my_errno = ENOTDIR;
 885     }
 886     return res;
 887 }
 888 
 889 /* --------------------------------------------------------------------------------------------- */
 890 
 891 static char *
 892 extfs_get_archive_name (const struct extfs_super_t *archive)
     /* [previous][next][first][last][top][bottom][index][help]  */
 893 {
 894     const char *archive_name;
 895 
 896     if (archive->local_name != NULL)
 897         archive_name = archive->local_name;
 898     else
 899         archive_name = CONST_VFS_SUPER (archive)->name;
 900 
 901     if (archive_name == NULL || *archive_name == '\0')
 902         return g_strdup ("no_archive_name");
 903     else
 904     {
 905         char *ret_str;
 906         vfs_path_t *vpath;
 907         const vfs_path_element_t *path_element;
 908 
 909         vpath = vfs_path_from_str (archive_name);
 910         path_element = vfs_path_get_by_index (vpath, -1);
 911         ret_str = g_strdup (path_element->path);
 912         vfs_path_free (vpath, TRUE);
 913         return ret_str;
 914     }
 915 }
 916 
 917 /* --------------------------------------------------------------------------------------------- */
 918 /** Don't pass localname as NULL */
 919 
 920 static int
 921 extfs_cmd (const char *str_extfs_cmd, const struct extfs_super_t *archive,
     /* [previous][next][first][last][top][bottom][index][help]  */
 922            const struct vfs_s_entry *entry, const char *localname)
 923 {
 924     char *file;
 925     char *quoted_file;
 926     char *quoted_localname;
 927     char *archive_name, *quoted_archive_name;
 928     const extfs_plugin_info_t *info;
 929     char *cmd;
 930     int retval = 0;
 931     GError *error = NULL;
 932     mc_pipe_t *pip;
 933 
 934     file = extfs_get_path_from_entry (entry);
 935     quoted_file = name_quote (file, FALSE);
 936     g_free (file);
 937 
 938     /* Skip leading "./" (if present) added in name_quote() */
 939     file = extfs_skip_leading_dotslash (quoted_file);
 940 
 941     archive_name = extfs_get_archive_name (archive);
 942     quoted_archive_name = name_quote (archive_name, FALSE);
 943     g_free (archive_name);
 944     quoted_localname = name_quote (localname, FALSE);
 945     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
 946     cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
 947                        quoted_archive_name, " ", file, " ", quoted_localname, (char *) NULL);
 948     g_free (quoted_file);
 949     g_free (quoted_localname);
 950     g_free (quoted_archive_name);
 951 
 952     /* don't read stdout */
 953     pip = mc_popen (cmd, FALSE, TRUE, &error);
 954     g_free (cmd);
 955 
 956     if (pip == NULL)
 957     {
 958         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 959         g_error_free (error);
 960         return (-1);
 961     }
 962 
 963     mc_pread (pip, &error);
 964     if (error != NULL)
 965     {
 966         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 967         g_error_free (error);
 968         retval = -1;
 969     }
 970     else if (pip->err.len > 0)
 971         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), pip->err.buf);
 972 
 973     mc_pclose (pip, NULL);
 974 
 975     return retval;
 976 }
 977 
 978 /* --------------------------------------------------------------------------------------------- */
 979 
 980 static void
 981 extfs_run (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 982 {
 983     struct extfs_super_t *archive = NULL;
 984     const char *p;
 985     char *q, *archive_name, *quoted_archive_name;
 986     char *cmd;
 987     const extfs_plugin_info_t *info;
 988 
 989     p = extfs_get_path (vpath, &archive, FL_NONE);
 990     if (p == NULL)
 991         return;
 992     q = name_quote (p, FALSE);
 993 
 994     archive_name = extfs_get_archive_name (archive);
 995     quoted_archive_name = name_quote (archive_name, FALSE);
 996     g_free (archive_name);
 997     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
 998     cmd =
 999         g_strconcat (info->path, info->prefix, " run ", quoted_archive_name, " ", q, (char *) NULL);
1000     g_free (quoted_archive_name);
1001     g_free (q);
1002     shell_execute (cmd, 0);
1003     g_free (cmd);
1004 }
1005 
1006 /* --------------------------------------------------------------------------------------------- */
1007 
1008 static void *
1009 extfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1010 {
1011     vfs_file_handler_t *extfs_info;
1012     struct extfs_super_t *archive = NULL;
1013     const char *q;
1014     struct vfs_s_entry *entry;
1015     int local_handle;
1016     gboolean created = FALSE;
1017 
1018     q = extfs_get_path (vpath, &archive, FL_NONE);
1019     if (q == NULL)
1020         return NULL;
1021     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1022     if ((entry == NULL) && ((flags & O_CREAT) != 0))
1023     {
1024         /* Create new entry */
1025         entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_MKFILE);
1026         created = (entry != NULL);
1027     }
1028 
1029     if (entry == NULL)
1030         return NULL;
1031     entry = extfs_resolve_symlinks (entry);
1032     if (entry == NULL)
1033         return NULL;
1034 
1035     if (S_ISDIR (entry->ino->st.st_mode))
1036         ERRNOR (EISDIR, NULL);
1037 
1038     if (entry->ino->localname == NULL)
1039     {
1040         vfs_path_t *local_filename_vpath;
1041         const char *local_filename;
1042 
1043         local_handle = vfs_mkstemps (&local_filename_vpath, "extfs", entry->name);
1044 
1045         if (local_handle == -1)
1046             return NULL;
1047         close (local_handle);
1048         local_filename = vfs_path_get_by_index (local_filename_vpath, -1)->path;
1049 
1050         if (!created && ((flags & O_TRUNC) == 0)
1051             && extfs_cmd (" copyout ", archive, entry, local_filename))
1052         {
1053             unlink (local_filename);
1054             vfs_path_free (local_filename_vpath, TRUE);
1055             my_errno = EIO;
1056             return NULL;
1057         }
1058         entry->ino->localname = g_strdup (local_filename);
1059         vfs_path_free (local_filename_vpath, TRUE);
1060     }
1061 
1062     local_handle = open (entry->ino->localname, NO_LINEAR (flags), mode);
1063 
1064     if (local_handle == -1)
1065     {
1066         /* file exists(may be). Need to drop O_CREAT flag and truncate file content */
1067         flags = ~O_CREAT & (NO_LINEAR (flags) | O_TRUNC);
1068         local_handle = open (entry->ino->localname, flags, mode);
1069     }
1070 
1071     if (local_handle == -1)
1072         ERRNOR (EIO, NULL);
1073 
1074     extfs_info = g_new (vfs_file_handler_t, 1);
1075     vfs_s_init_fh (extfs_info, entry->ino, created);
1076     extfs_info->handle = local_handle;
1077 
1078     /* i.e. we had no open files and now we have one */
1079     vfs_rmstamp (vfs_extfs_ops, (vfsid) archive);
1080     VFS_SUPER (archive)->fd_usage++;
1081     return extfs_info;
1082 }
1083 
1084 /* --------------------------------------------------------------------------------------------- */
1085 
1086 static ssize_t
1087 extfs_read (void *fh, char *buffer, size_t count)
     /* [previous][next][first][last][top][bottom][index][help]  */
1088 {
1089     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1090 
1091     return read (file->handle, buffer, count);
1092 }
1093 
1094 /* --------------------------------------------------------------------------------------------- */
1095 
1096 static int
1097 extfs_close (void *fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1098 {
1099     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1100     int errno_code = 0;
1101 
1102     close (file->handle);
1103     file->handle = -1;
1104 
1105     /* Commit the file if it has changed */
1106     if (file->changed)
1107     {
1108         struct stat file_status;
1109 
1110         if (extfs_cmd
1111             (" copyin ", EXTFS_SUPER (VFS_FILE_HANDLER_SUPER (fh)), file->ino->ent,
1112              file->ino->localname))
1113             errno_code = EIO;
1114 
1115         if (stat (file->ino->localname, &file_status) != 0)
1116             errno_code = EIO;
1117         else
1118             file->ino->st.st_size = file_status.st_size;
1119 
1120         file->ino->st.st_mtime = time (NULL);
1121     }
1122 
1123     if (--VFS_FILE_HANDLER_SUPER (fh)->fd_usage == 0)
1124         vfs_stamp_create (vfs_extfs_ops, VFS_FILE_HANDLER_SUPER (fh));
1125 
1126     g_free (fh);
1127     if (errno_code != 0)
1128         ERRNOR (EIO, -1);
1129     return 0;
1130 }
1131 
1132 /* --------------------------------------------------------------------------------------------- */
1133 
1134 static int
1135 extfs_errno (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1136 {
1137     (void) me;
1138     return my_errno;
1139 }
1140 
1141 /* --------------------------------------------------------------------------------------------- */
1142 
1143 static void *
1144 extfs_opendir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1145 {
1146     struct extfs_super_t *archive = NULL;
1147     const char *q;
1148     struct vfs_s_entry *entry;
1149     GList **info;
1150 
1151     q = extfs_get_path (vpath, &archive, FL_NONE);
1152     if (q == NULL)
1153         return NULL;
1154     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1155     if (entry == NULL)
1156         return NULL;
1157     entry = extfs_resolve_symlinks (entry);
1158     if (entry == NULL)
1159         return NULL;
1160     if (!S_ISDIR (entry->ino->st.st_mode))
1161         ERRNOR (ENOTDIR, NULL);
1162 
1163     info = g_new (GList *, 1);
1164     *info = g_queue_peek_head_link (entry->ino->subdir);
1165 
1166     return info;
1167 }
1168 
1169 /* --------------------------------------------------------------------------------------------- */
1170 
1171 static struct vfs_dirent *
1172 extfs_readdir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1173 {
1174     struct vfs_dirent *dir;
1175     GList **info = (GList **) data;
1176 
1177     if (*info == NULL)
1178         return NULL;
1179 
1180     dir = vfs_dirent_init (NULL, VFS_ENTRY ((*info)->data)->name, 0);   /* FIXME: inode */
1181 
1182     *info = g_list_next (*info);
1183 
1184     return dir;
1185 }
1186 
1187 /* --------------------------------------------------------------------------------------------- */
1188 
1189 static int
1190 extfs_closedir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1191 {
1192     g_free (data);
1193     return 0;
1194 }
1195 
1196 /* --------------------------------------------------------------------------------------------- */
1197 
1198 
1199 static void
1200 extfs_stat_move (struct stat *buf, const struct vfs_s_inode *inode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1201 {
1202     *buf = inode->st;
1203 
1204 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1205     buf->st_blksize = RECORDSIZE;
1206 #endif
1207     vfs_adjust_stat (buf);
1208 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1209     buf->st_atim.tv_nsec = buf->st_mtim.tv_nsec = buf->st_ctim.tv_nsec = 0;
1210 #endif
1211 }
1212 
1213 /* --------------------------------------------------------------------------------------------- */
1214 
1215 static int
1216 extfs_internal_stat (const vfs_path_t * vpath, struct stat *buf, gboolean resolve)
     /* [previous][next][first][last][top][bottom][index][help]  */
1217 {
1218     struct extfs_super_t *archive;
1219     const char *q;
1220     struct vfs_s_entry *entry;
1221     int result = -1;
1222 
1223     q = extfs_get_path (vpath, &archive, FL_NONE);
1224     if (q == NULL)
1225         goto cleanup;
1226     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1227     if (entry == NULL)
1228         goto cleanup;
1229     if (resolve)
1230     {
1231         entry = extfs_resolve_symlinks (entry);
1232         if (entry == NULL)
1233             goto cleanup;
1234     }
1235     extfs_stat_move (buf, entry->ino);
1236     result = 0;
1237   cleanup:
1238     return result;
1239 }
1240 
1241 /* --------------------------------------------------------------------------------------------- */
1242 
1243 static int
1244 extfs_stat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1245 {
1246     return extfs_internal_stat (vpath, buf, TRUE);
1247 }
1248 
1249 /* --------------------------------------------------------------------------------------------- */
1250 
1251 static int
1252 extfs_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1253 {
1254     return extfs_internal_stat (vpath, buf, FALSE);
1255 }
1256 
1257 /* --------------------------------------------------------------------------------------------- */
1258 
1259 static int
1260 extfs_fstat (void *fh, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1261 {
1262     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1263 
1264     extfs_stat_move (buf, file->ino);
1265     return 0;
1266 }
1267 
1268 /* --------------------------------------------------------------------------------------------- */
1269 
1270 static int
1271 extfs_readlink (const vfs_path_t * vpath, char *buf, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
1272 {
1273     struct extfs_super_t *archive;
1274     const char *q;
1275     size_t len;
1276     struct vfs_s_entry *entry;
1277     int result = -1;
1278 
1279     q = extfs_get_path (vpath, &archive, FL_NONE);
1280     if (q == NULL)
1281         goto cleanup;
1282     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1283     if (entry == NULL)
1284         goto cleanup;
1285     if (!S_ISLNK (entry->ino->st.st_mode))
1286     {
1287         const vfs_path_element_t *path_element;
1288 
1289         path_element = vfs_path_get_by_index (vpath, -1);
1290         path_element->class->verrno = EINVAL;
1291         goto cleanup;
1292     }
1293     len = strlen (entry->ino->linkname);
1294     if (size < len)
1295         len = size;
1296     /* readlink() does not append a NUL character to buf */
1297     result = len;
1298     memcpy (buf, entry->ino->linkname, result);
1299   cleanup:
1300     return result;
1301 }
1302 
1303 /* --------------------------------------------------------------------------------------------- */
1304 
1305 static int
1306 extfs_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
1307 {
1308     (void) vpath;
1309     (void) owner;
1310     (void) group;
1311     return 0;
1312 }
1313 
1314 /* --------------------------------------------------------------------------------------------- */
1315 
1316 static int
1317 extfs_chmod (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1318 {
1319     (void) vpath;
1320     (void) mode;
1321     return 0;
1322 }
1323 
1324 /* --------------------------------------------------------------------------------------------- */
1325 
1326 static ssize_t
1327 extfs_write (void *fh, const char *buf, size_t nbyte)
     /* [previous][next][first][last][top][bottom][index][help]  */
1328 {
1329     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1330 
1331     file->changed = TRUE;
1332     return write (file->handle, buf, nbyte);
1333 }
1334 
1335 /* --------------------------------------------------------------------------------------------- */
1336 
1337 static int
1338 extfs_unlink (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1339 {
1340     struct extfs_super_t *archive;
1341     const char *q;
1342     struct vfs_s_entry *entry;
1343     int result = -1;
1344 
1345     q = extfs_get_path (vpath, &archive, FL_NONE);
1346     if (q == NULL)
1347         goto cleanup;
1348     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1349     if (entry == NULL)
1350         goto cleanup;
1351     entry = extfs_resolve_symlinks (entry);
1352     if (entry == NULL)
1353         goto cleanup;
1354     if (S_ISDIR (entry->ino->st.st_mode))
1355     {
1356         const vfs_path_element_t *path_element;
1357 
1358         path_element = vfs_path_get_by_index (vpath, -1);
1359         path_element->class->verrno = EISDIR;
1360         goto cleanup;
1361     }
1362     if (extfs_cmd (" rm ", archive, entry, ""))
1363     {
1364         my_errno = EIO;
1365         goto cleanup;
1366     }
1367     vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1368     result = 0;
1369   cleanup:
1370     return result;
1371 }
1372 
1373 /* --------------------------------------------------------------------------------------------- */
1374 
1375 static int
1376 extfs_mkdir (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1377 {
1378     struct extfs_super_t *archive;
1379     const char *q;
1380     struct vfs_s_entry *entry;
1381     int result = -1;
1382     const vfs_path_element_t *path_element;
1383 
1384     (void) mode;
1385 
1386     path_element = vfs_path_get_by_index (vpath, -1);
1387     q = extfs_get_path (vpath, &archive, FL_NONE);
1388     if (q == NULL)
1389         goto cleanup;
1390     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1391     if (entry != NULL)
1392     {
1393         path_element->class->verrno = EEXIST;
1394         goto cleanup;
1395     }
1396     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_MKDIR);
1397     if (entry == NULL)
1398         goto cleanup;
1399     entry = extfs_resolve_symlinks (entry);
1400     if (entry == NULL)
1401         goto cleanup;
1402     if (!S_ISDIR (entry->ino->st.st_mode))
1403     {
1404         path_element->class->verrno = ENOTDIR;
1405         goto cleanup;
1406     }
1407 
1408     if (extfs_cmd (" mkdir ", archive, entry, ""))
1409     {
1410         my_errno = EIO;
1411         vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1412         goto cleanup;
1413     }
1414     result = 0;
1415   cleanup:
1416     return result;
1417 }
1418 
1419 /* --------------------------------------------------------------------------------------------- */
1420 
1421 static int
1422 extfs_rmdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1423 {
1424     struct extfs_super_t *archive;
1425     const char *q;
1426     struct vfs_s_entry *entry;
1427     int result = -1;
1428 
1429     q = extfs_get_path (vpath, &archive, FL_NONE);
1430     if (q == NULL)
1431         goto cleanup;
1432     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1433     if (entry == NULL)
1434         goto cleanup;
1435     entry = extfs_resolve_symlinks (entry);
1436     if (entry == NULL)
1437         goto cleanup;
1438     if (!S_ISDIR (entry->ino->st.st_mode))
1439     {
1440         const vfs_path_element_t *path_element;
1441 
1442         path_element = vfs_path_get_by_index (vpath, -1);
1443         path_element->class->verrno = ENOTDIR;
1444         goto cleanup;
1445     }
1446 
1447     if (extfs_cmd (" rmdir ", archive, entry, ""))
1448     {
1449         my_errno = EIO;
1450         goto cleanup;
1451     }
1452     vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1453     result = 0;
1454   cleanup:
1455     return result;
1456 }
1457 
1458 /* --------------------------------------------------------------------------------------------- */
1459 
1460 static int
1461 extfs_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1462 {
1463     void *data;
1464 
1465     my_errno = ENOTDIR;
1466     data = extfs_opendir (vpath);
1467     if (data == NULL)
1468         return (-1);
1469     extfs_closedir (data);
1470     my_errno = 0;
1471     return 0;
1472 }
1473 
1474 /* --------------------------------------------------------------------------------------------- */
1475 
1476 static off_t
1477 extfs_lseek (void *fh, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
1478 {
1479     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1480 
1481     return lseek (file->handle, offset, whence);
1482 }
1483 
1484 /* --------------------------------------------------------------------------------------------- */
1485 
1486 static vfsid
1487 extfs_getid (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1488 {
1489     struct extfs_super_t *archive = NULL;
1490     const char *p;
1491 
1492     p = extfs_get_path (vpath, &archive, FL_NO_OPEN);
1493     return (p == NULL ? NULL : (vfsid) archive);
1494 }
1495 
1496 /* --------------------------------------------------------------------------------------------- */
1497 
1498 static vfs_path_t *
1499 extfs_getlocalcopy (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1500 {
1501     vfs_file_handler_t *fh;
1502     vfs_path_t *p;
1503 
1504     fh = VFS_FILE_HANDLER (extfs_open (vpath, O_RDONLY, 0));
1505     if (fh == NULL)
1506         return NULL;
1507     if (fh->ino->localname == NULL)
1508     {
1509         extfs_close ((void *) fh);
1510         return NULL;
1511     }
1512     p = vfs_path_from_str (fh->ino->localname);
1513     VFS_FILE_HANDLER_SUPER (fh)->fd_usage++;
1514     extfs_close ((void *) fh);
1515     return p;
1516 }
1517 
1518 /* --------------------------------------------------------------------------------------------- */
1519 
1520 static int
1521 extfs_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
1522 {
1523     vfs_file_handler_t *fh;
1524 
1525     fh = VFS_FILE_HANDLER (extfs_open (vpath, O_RDONLY, 0));
1526     if (fh == NULL)
1527         return 0;
1528 
1529     if (strcmp (fh->ino->localname, vfs_path_get_last_path_str (local)) == 0)
1530     {
1531         VFS_FILE_HANDLER_SUPER (fh)->fd_usage--;
1532         if (has_changed)
1533             fh->changed = TRUE;
1534         extfs_close ((void *) fh);
1535         return 0;
1536     }
1537     else
1538     {
1539         /* Should not happen */
1540         extfs_close ((void *) fh);
1541         return 0;
1542     }
1543 }
1544 
1545 /* --------------------------------------------------------------------------------------------- */
1546 
1547 static gboolean
1548 extfs_get_plugins (const char *where, gboolean silent)
     /* [previous][next][first][last][top][bottom][index][help]  */
1549 {
1550     char *dirname;
1551     GDir *dir;
1552     const char *filename;
1553 
1554     dirname = g_build_path (PATH_SEP_STR, where, MC_EXTFS_DIR, (char *) NULL);
1555     dir = g_dir_open (dirname, 0, NULL);
1556 
1557     /* We may not use vfs_die() message or message or similar,
1558      * UI is not initialized at this time and message would not
1559      * appear on screen. */
1560     if (dir == NULL)
1561     {
1562         if (!silent)
1563             fprintf (stderr, _("Warning: cannot open %s directory\n"), dirname);
1564         g_free (dirname);
1565         return FALSE;
1566     }
1567 
1568     if (extfs_plugins == NULL)
1569         extfs_plugins = g_array_sized_new (FALSE, TRUE, sizeof (extfs_plugin_info_t), 32);
1570 
1571     while ((filename = g_dir_read_name (dir)) != NULL)
1572     {
1573         char fullname[MC_MAXPATHLEN];
1574         struct stat s;
1575 
1576         g_snprintf (fullname, sizeof (fullname), "%s" PATH_SEP_STR "%s", dirname, filename);
1577 
1578         if ((stat (fullname, &s) == 0) && S_ISREG (s.st_mode) && !S_ISDIR (s.st_mode)
1579             && is_exe (s.st_mode))
1580         {
1581             int f;
1582 
1583             f = open (fullname, O_RDONLY);
1584 
1585             if (f >= 0)
1586             {
1587                 size_t len, i;
1588                 extfs_plugin_info_t info;
1589                 gboolean found = FALSE;
1590 
1591                 close (f);
1592 
1593                 /* Handle those with a trailing '+', those flag that the
1594                  * file system does not require an archive to work
1595                  */
1596                 len = strlen (filename);
1597                 info.need_archive = (filename[len - 1] != '+');
1598                 info.path = g_strconcat (dirname, PATH_SEP_STR, (char *) NULL);
1599                 info.prefix = g_strdup (filename);
1600 
1601                 /* prepare to compare file names without trailing '+' */
1602                 if (!info.need_archive)
1603                     info.prefix[len - 1] = '\0';
1604 
1605                 /* don't overload already found plugin */
1606                 for (i = 0; i < extfs_plugins->len; i++)
1607                 {
1608                     extfs_plugin_info_t *p;
1609 
1610                     p = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
1611 
1612                     /* 2 files with same names cannot be in a directory */
1613                     if ((strcmp (info.path, p->path) != 0)
1614                         && (strcmp (info.prefix, p->prefix) == 0))
1615                     {
1616                         found = TRUE;
1617                         break;
1618                     }
1619                 }
1620 
1621                 if (found)
1622                 {
1623                     g_free (info.path);
1624                     g_free (info.prefix);
1625                 }
1626                 else
1627                 {
1628                     /* restore file name */
1629                     if (!info.need_archive)
1630                         info.prefix[len - 1] = '+';
1631                     g_array_append_val (extfs_plugins, info);
1632                 }
1633             }
1634         }
1635     }
1636 
1637     g_dir_close (dir);
1638     g_free (dirname);
1639 
1640     return TRUE;
1641 }
1642 
1643 /* --------------------------------------------------------------------------------------------- */
1644 
1645 static int
1646 extfs_init (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1647 {
1648     gboolean d1, d2;
1649 
1650     (void) me;
1651 
1652     /* 1st: scan user directory */
1653     d1 = extfs_get_plugins (mc_config_get_data_path (), TRUE);  /* silent about user dir */
1654     /* 2nd: scan system dir */
1655     d2 = extfs_get_plugins (LIBEXECDIR, d1);
1656 
1657     return (d1 || d2 ? 1 : 0);
1658 }
1659 
1660 /* --------------------------------------------------------------------------------------------- */
1661 
1662 static void
1663 extfs_done (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1664 {
1665     size_t i;
1666 
1667     while (VFS_SUBCLASS (me)->supers != NULL)
1668         me->free ((vfsid) VFS_SUBCLASS (me)->supers->data);
1669 
1670     if (extfs_plugins == NULL)
1671         return;
1672 
1673     for (i = 0; i < extfs_plugins->len; i++)
1674     {
1675         extfs_plugin_info_t *info;
1676 
1677         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
1678         g_free (info->path);
1679         g_free (info->prefix);
1680     }
1681 
1682     g_array_free (extfs_plugins, TRUE);
1683 }
1684 
1685 /* --------------------------------------------------------------------------------------------- */
1686 
1687 static int
1688 extfs_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
1689 {
1690     (void) arg;
1691 
1692     if (ctlop == VFS_SETCTL_RUN)
1693     {
1694         extfs_run (vpath);
1695         return 1;
1696     }
1697     return 0;
1698 }
1699 
1700 /* --------------------------------------------------------------------------------------------- */
1701 /*** public functions ****************************************************************************/
1702 /* --------------------------------------------------------------------------------------------- */
1703 
1704 void
1705 vfs_init_extfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1706 {
1707     vfs_init_subclass (&extfs_subclass, "extfs", VFSF_UNKNOWN, NULL);
1708     vfs_extfs_ops->init = extfs_init;
1709     vfs_extfs_ops->done = extfs_done;
1710     vfs_extfs_ops->fill_names = extfs_fill_names;
1711     vfs_extfs_ops->which = extfs_which;
1712     vfs_extfs_ops->open = extfs_open;
1713     vfs_extfs_ops->close = extfs_close;
1714     vfs_extfs_ops->read = extfs_read;
1715     vfs_extfs_ops->write = extfs_write;
1716     vfs_extfs_ops->opendir = extfs_opendir;
1717     vfs_extfs_ops->readdir = extfs_readdir;
1718     vfs_extfs_ops->closedir = extfs_closedir;
1719     vfs_extfs_ops->stat = extfs_stat;
1720     vfs_extfs_ops->lstat = extfs_lstat;
1721     vfs_extfs_ops->fstat = extfs_fstat;
1722     vfs_extfs_ops->chmod = extfs_chmod;
1723     vfs_extfs_ops->chown = extfs_chown;
1724     vfs_extfs_ops->readlink = extfs_readlink;
1725     vfs_extfs_ops->unlink = extfs_unlink;
1726     vfs_extfs_ops->chdir = extfs_chdir;
1727     vfs_extfs_ops->ferrno = extfs_errno;
1728     vfs_extfs_ops->lseek = extfs_lseek;
1729     vfs_extfs_ops->getid = extfs_getid;
1730     vfs_extfs_ops->getlocalcopy = extfs_getlocalcopy;
1731     vfs_extfs_ops->ungetlocalcopy = extfs_ungetlocalcopy;
1732     vfs_extfs_ops->mkdir = extfs_mkdir;
1733     vfs_extfs_ops->rmdir = extfs_rmdir;
1734     vfs_extfs_ops->setctl = extfs_setctl;
1735     extfs_subclass.free_inode = extfs_free_inode;
1736     extfs_subclass.free_archive = extfs_free_archive;
1737     vfs_register_class (vfs_extfs_ops);
1738 }
1739 
1740 /* --------------------------------------------------------------------------------------------- */

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