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;
 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     vfs_path_t *name_vpath;
 525 
 526     memset (&mystat, 0, sizeof (mystat));
 527 
 528     name_vpath = vfs_path_from_str (name);
 529     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
 530 
 531     if (info->need_archive)
 532     {
 533         if (mc_stat (name_vpath, &mystat) == -1)
 534             goto ret;
 535 
 536         if (!vfs_file_is_local (name_vpath))
 537         {
 538             local_name_vpath = mc_getlocalcopy (name_vpath);
 539             if (local_name_vpath == NULL)
 540                 goto ret;
 541         }
 542 
 543         tmp = name_quote (vfs_path_get_last_path_str (name_vpath), FALSE);
 544     }
 545 
 546     cmd = g_strconcat (info->path, info->prefix, " list ",
 547                        vfs_path_get_last_path_str (local_name_vpath) != NULL ?
 548                        vfs_path_get_last_path_str (local_name_vpath) : tmp, (char *) NULL);
 549     g_free (tmp);
 550 
 551     result = mc_popen (cmd, TRUE, TRUE, error);
 552     g_free (cmd);
 553 
 554     if (result == NULL)
 555     {
 556         if (local_name_vpath != NULL)
 557         {
 558             mc_ungetlocalcopy (name_vpath, local_name_vpath, FALSE);
 559             vfs_path_free (local_name_vpath, TRUE);
 560         }
 561         goto ret;
 562     }
 563 
 564     current_archive = extfs_super_new (vfs_extfs_ops, name, local_name_vpath, fstype);
 565     current_archive->rdev = archive_counter++;
 566     vfs_path_free (local_name_vpath, TRUE);
 567 
 568     mode = mystat.st_mode & 07777;
 569     if (mode & 0400)
 570         mode |= 0100;
 571     if (mode & 0040)
 572         mode |= 0010;
 573     if (mode & 0004)
 574         mode |= 0001;
 575     mode |= S_IFDIR;
 576 
 577     root_entry = extfs_generate_entry (current_archive, PATH_SEP_STR, NULL, mode);
 578     root_entry->ino->st.st_uid = mystat.st_uid;
 579     root_entry->ino->st.st_gid = mystat.st_gid;
 580     root_entry->ino->st.st_atime = mystat.st_atime;
 581     root_entry->ino->st.st_ctime = mystat.st_ctime;
 582     root_entry->ino->st.st_mtime = mystat.st_mtime;
 583     root_entry->ino->ent = root_entry;
 584     VFS_SUPER (current_archive)->root = root_entry->ino;
 585 
 586     *pparc = current_archive;
 587 
 588   ret:
 589     vfs_path_free (name_vpath, TRUE);
 590     return result;
 591 }
 592 
 593 /* --------------------------------------------------------------------------------------------- */
 594 /**
 595  * Main loop for reading an archive.
 596  * Return 0 on success, -1 on error.
 597  */
 598 
 599 static int
 600 extfs_read_archive (mc_pipe_t * pip, struct extfs_super_t *archive, GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 601 {
 602     int ret = 0;
 603     GString *buffer;
 604     GString *err_msg = NULL;
 605     GString *remain_file_name = NULL;
 606 
 607     while (ret != -1)
 608     {
 609         /* init buffers before call of mc_pread() */
 610         pip->out.len = MC_PIPE_BUFSIZE;
 611         pip->err.len = MC_PIPE_BUFSIZE;
 612 
 613         mc_pread (pip, error);
 614 
 615         if (*error != NULL)
 616             return (-1);
 617 
 618         if (pip->err.len > 0)
 619         {
 620             /* join errors/warnings */
 621             if (err_msg == NULL)
 622                 err_msg = g_string_new_len (pip->err.buf, pip->err.len);
 623             else
 624             {
 625                 if (err_msg->str[err_msg->len - 1] != '\n')
 626                     g_string_append_c (err_msg, '\n');
 627                 g_string_append_len (err_msg, pip->err.buf, pip->err.len);
 628             }
 629         }
 630 
 631         if (pip->out.len == MC_PIPE_STREAM_EOF)
 632             break;
 633 
 634         if (pip->out.len == 0)
 635             continue;
 636 
 637         if (pip->out.len == MC_PIPE_ERROR_READ)
 638             return (-1);
 639 
 640         while (ret != -1 && (buffer = mc_pstream_get_string (&pip->out)) != NULL)
 641         {
 642             /* handle a \n-separated file list */
 643 
 644             if (buffer->str[buffer->len - 1] == '\n')
 645             {
 646                 /* entire file name or last chunk */
 647 
 648                 g_string_truncate (buffer, buffer->len - 1);
 649 
 650                 /* join filename chunks */
 651                 if (remain_file_name != NULL)
 652                 {
 653                     g_string_append_len (remain_file_name, buffer->str, buffer->len);
 654                     g_string_free (buffer, TRUE);
 655                     buffer = remain_file_name;
 656                     remain_file_name = NULL;
 657                 }
 658             }
 659             else
 660             {
 661                 /* first or middle chunk of file name */
 662 
 663                 if (remain_file_name == NULL)
 664                     remain_file_name = buffer;
 665                 else
 666                 {
 667                     g_string_append_len (remain_file_name, buffer->str, buffer->len);
 668                     g_string_free (buffer, TRUE);
 669                 }
 670 
 671                 continue;
 672             }
 673 
 674             ret = extfs_add_file (archive, buffer->str);
 675 
 676             g_string_free (buffer, TRUE);
 677         }
 678     }
 679 
 680     if (remain_file_name != NULL)
 681         g_string_free (remain_file_name, TRUE);
 682 
 683     if (err_msg != NULL)
 684     {
 685         if (*error == NULL)
 686             mc_propagate_error (error, 0, "%s", err_msg->str);
 687 
 688         g_string_free (err_msg, TRUE);
 689     }
 690 
 691     return ret;
 692 }
 693 
 694 /* --------------------------------------------------------------------------------------------- */
 695 
 696 static int
 697 extfs_which (struct vfs_class *me, const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 698 {
 699     size_t path_len;
 700     size_t i;
 701 
 702     (void) me;
 703 
 704     path_len = strlen (path);
 705 
 706     for (i = 0; i < extfs_plugins->len; i++)
 707     {
 708         extfs_plugin_info_t *info;
 709 
 710         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
 711 
 712         if ((strncmp (path, info->prefix, path_len) == 0)
 713             && ((info->prefix[path_len] == '\0') || (info->prefix[path_len] == '+')))
 714             return i;
 715     }
 716     return -1;
 717 }
 718 
 719 /* --------------------------------------------------------------------------------------------- */
 720 
 721 static int
 722 extfs_open_and_read_archive (int fstype, const char *name, struct extfs_super_t **archive)
     /* [previous][next][first][last][top][bottom][index][help]  */
 723 {
 724     int result = -1;
 725     struct extfs_super_t *a;
 726     mc_pipe_t *pip;
 727     GError *error = NULL;
 728 
 729     pip = extfs_open_archive (fstype, name, archive, &error);
 730 
 731     a = *archive;
 732 
 733     if (pip == NULL)
 734     {
 735         const extfs_plugin_info_t *info;
 736 
 737         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
 738         message (D_ERROR, MSG_ERROR, _("Cannot open %s archive\n%s:\n%s"), info->prefix, name,
 739                  error->message);
 740         g_error_free (error);
 741     }
 742     else
 743     {
 744         result = extfs_read_archive (pip, a, &error);
 745 
 746         if (result != 0)
 747             VFS_SUPER (a)->me->free (VFS_SUPER (a));
 748 
 749         if (error != NULL)
 750         {
 751             message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 752             g_error_free (error);
 753         }
 754     }
 755 
 756     mc_pclose (pip, NULL);
 757 
 758     return result;
 759 }
 760 
 761 /* --------------------------------------------------------------------------------------------- */
 762 /**
 763  * Dissect the path and create corresponding superblock.
 764  */
 765 static const char *
 766 extfs_get_path (const vfs_path_t * vpath, struct extfs_super_t **archive, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 767 {
 768     char *archive_name;
 769     int result = -1;
 770     GList *parc;
 771     int fstype;
 772     const vfs_path_element_t *path_element;
 773     struct extfs_super_t *a = NULL;
 774 
 775     path_element = vfs_path_get_by_index (vpath, -1);
 776 
 777     fstype = extfs_which (path_element->class, path_element->vfs_prefix);
 778     if (fstype == -1)
 779         return NULL;
 780 
 781     archive_name = vfs_path_to_str_elements_count (vpath, -1);
 782 
 783     /* All filesystems should have some local archive, at least it can be PATH_SEP ('/'). */
 784     parc = g_list_find_custom (extfs_subclass.supers, archive_name, extfs_cmp_archive);
 785     if (parc != NULL)
 786     {
 787         a = EXTFS_SUPER (parc->data);
 788         vfs_stamp (vfs_extfs_ops, (vfsid) a);
 789         g_free (archive_name);
 790     }
 791     else
 792     {
 793         if ((flags & FL_NO_OPEN) == 0)
 794             result = extfs_open_and_read_archive (fstype, archive_name, &a);
 795 
 796         g_free (archive_name);
 797 
 798         if (result == -1)
 799         {
 800             path_element->class->verrno = EIO;
 801             return NULL;
 802         }
 803     }
 804 
 805     *archive = a;
 806     return path_element->path;
 807 }
 808 
 809 /* --------------------------------------------------------------------------------------------- */
 810 /* Return allocated path (without leading slash) inside the archive  */
 811 
 812 static char *
 813 extfs_get_path_from_entry (const struct vfs_s_entry *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 814 {
 815     const struct vfs_s_entry *e;
 816     GString *localpath;
 817 
 818     localpath = g_string_new ("");
 819 
 820     for (e = entry; e->dir != NULL; e = e->dir->ent)
 821     {
 822         g_string_prepend (localpath, e->name);
 823         if (e->dir->ent->dir != NULL)
 824             g_string_prepend_c (localpath, PATH_SEP);
 825     }
 826 
 827     return g_string_free (localpath, FALSE);
 828 }
 829 
 830 /* --------------------------------------------------------------------------------------------- */
 831 
 832 static struct vfs_s_entry *
 833 extfs_resolve_symlinks_int (struct vfs_s_entry *entry, GSList * list)
     /* [previous][next][first][last][top][bottom][index][help]  */
 834 {
 835     struct vfs_s_entry *pent = NULL;
 836 
 837     if (!S_ISLNK (entry->ino->st.st_mode))
 838         return entry;
 839 
 840     if (g_slist_find (list, entry) != NULL)
 841     {
 842         /* Here we protect us against symlink looping */
 843         errloop = TRUE;
 844     }
 845     else
 846     {
 847         GSList *looping;
 848 
 849         looping = g_slist_prepend (list, entry);
 850         pent = extfs_find_entry_int (entry->dir, entry->ino->linkname, looping, FL_NONE);
 851         looping = g_slist_delete_link (looping, looping);
 852 
 853         if (pent == NULL)
 854             my_errno = ENOENT;
 855     }
 856 
 857     return pent;
 858 }
 859 
 860 /* --------------------------------------------------------------------------------------------- */
 861 
 862 static struct vfs_s_entry *
 863 extfs_resolve_symlinks (struct vfs_s_entry *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 864 {
 865     struct vfs_s_entry *res;
 866 
 867     errloop = FALSE;
 868     notadir = FALSE;
 869     res = extfs_resolve_symlinks_int (entry, NULL);
 870     if (res == NULL)
 871     {
 872         if (errloop)
 873             my_errno = ELOOP;
 874         else if (notadir)
 875             my_errno = ENOTDIR;
 876     }
 877     return res;
 878 }
 879 
 880 /* --------------------------------------------------------------------------------------------- */
 881 
 882 static char *
 883 extfs_get_archive_name (const struct extfs_super_t *archive)
     /* [previous][next][first][last][top][bottom][index][help]  */
 884 {
 885     const char *archive_name;
 886 
 887     if (archive->local_name != NULL)
 888         archive_name = archive->local_name;
 889     else
 890         archive_name = VFS_SUPER (archive)->name;
 891 
 892     if (archive_name == NULL || *archive_name == '\0')
 893         return g_strdup ("no_archive_name");
 894     else
 895     {
 896         char *ret_str;
 897         vfs_path_t *vpath;
 898         const vfs_path_element_t *path_element;
 899 
 900         vpath = vfs_path_from_str (archive_name);
 901         path_element = vfs_path_get_by_index (vpath, -1);
 902         ret_str = g_strdup (path_element->path);
 903         vfs_path_free (vpath, TRUE);
 904         return ret_str;
 905     }
 906 }
 907 
 908 /* --------------------------------------------------------------------------------------------- */
 909 /** Don't pass localname as NULL */
 910 
 911 static int
 912 extfs_cmd (const char *str_extfs_cmd, const struct extfs_super_t *archive,
     /* [previous][next][first][last][top][bottom][index][help]  */
 913            const struct vfs_s_entry *entry, const char *localname)
 914 {
 915     char *file;
 916     char *quoted_file;
 917     char *quoted_localname;
 918     char *archive_name, *quoted_archive_name;
 919     const extfs_plugin_info_t *info;
 920     char *cmd;
 921     int retval = 0;
 922     GError *error = NULL;
 923     mc_pipe_t *pip;
 924 
 925     file = extfs_get_path_from_entry (entry);
 926     quoted_file = name_quote (file, FALSE);
 927     g_free (file);
 928 
 929     /* Skip leading "./" (if present) added in name_quote() */
 930     file = extfs_skip_leading_dotslash (quoted_file);
 931 
 932     archive_name = extfs_get_archive_name (archive);
 933     quoted_archive_name = name_quote (archive_name, FALSE);
 934     g_free (archive_name);
 935     quoted_localname = name_quote (localname, FALSE);
 936     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
 937     cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
 938                        quoted_archive_name, " ", file, " ", quoted_localname, (char *) NULL);
 939     g_free (quoted_file);
 940     g_free (quoted_localname);
 941     g_free (quoted_archive_name);
 942 
 943     /* don't read stdout */
 944     pip = mc_popen (cmd, FALSE, TRUE, &error);
 945     g_free (cmd);
 946 
 947     if (pip == NULL)
 948     {
 949         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 950         g_error_free (error);
 951         return (-1);
 952     }
 953 
 954     mc_pread (pip, &error);
 955     if (error != NULL)
 956     {
 957         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), error->message);
 958         g_error_free (error);
 959         retval = -1;
 960     }
 961     else if (pip->err.len > 0)
 962         message (D_ERROR, MSG_ERROR, _("EXTFS virtual file system:\n%s"), pip->err.buf);
 963 
 964     mc_pclose (pip, NULL);
 965 
 966     return retval;
 967 }
 968 
 969 /* --------------------------------------------------------------------------------------------- */
 970 
 971 static void
 972 extfs_run (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 973 {
 974     struct extfs_super_t *archive = NULL;
 975     const char *p;
 976     char *q, *archive_name, *quoted_archive_name;
 977     char *cmd;
 978     const extfs_plugin_info_t *info;
 979 
 980     p = extfs_get_path (vpath, &archive, FL_NONE);
 981     if (p == NULL)
 982         return;
 983     q = name_quote (p, FALSE);
 984 
 985     archive_name = extfs_get_archive_name (archive);
 986     quoted_archive_name = name_quote (archive_name, FALSE);
 987     g_free (archive_name);
 988     info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
 989     cmd =
 990         g_strconcat (info->path, info->prefix, " run ", quoted_archive_name, " ", q, (char *) NULL);
 991     g_free (quoted_archive_name);
 992     g_free (q);
 993     shell_execute (cmd, 0);
 994     g_free (cmd);
 995 }
 996 
 997 /* --------------------------------------------------------------------------------------------- */
 998 
 999 static void *
1000 extfs_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1001 {
1002     vfs_file_handler_t *extfs_info;
1003     struct extfs_super_t *archive = NULL;
1004     const char *q;
1005     struct vfs_s_entry *entry;
1006     int local_handle;
1007     gboolean created = FALSE;
1008 
1009     q = extfs_get_path (vpath, &archive, FL_NONE);
1010     if (q == NULL)
1011         return NULL;
1012     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1013     if ((entry == NULL) && ((flags & O_CREAT) != 0))
1014     {
1015         /* Create new entry */
1016         entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_MKFILE);
1017         created = (entry != NULL);
1018     }
1019 
1020     if (entry == NULL)
1021         return NULL;
1022     entry = extfs_resolve_symlinks (entry);
1023     if (entry == NULL)
1024         return NULL;
1025 
1026     if (S_ISDIR (entry->ino->st.st_mode))
1027         ERRNOR (EISDIR, NULL);
1028 
1029     if (entry->ino->localname == NULL)
1030     {
1031         vfs_path_t *local_filename_vpath;
1032         const char *local_filename;
1033 
1034         local_handle = vfs_mkstemps (&local_filename_vpath, "extfs", entry->name);
1035 
1036         if (local_handle == -1)
1037             return NULL;
1038         close (local_handle);
1039         local_filename = vfs_path_get_by_index (local_filename_vpath, -1)->path;
1040 
1041         if (!created && ((flags & O_TRUNC) == 0)
1042             && extfs_cmd (" copyout ", archive, entry, local_filename))
1043         {
1044             unlink (local_filename);
1045             vfs_path_free (local_filename_vpath, TRUE);
1046             my_errno = EIO;
1047             return NULL;
1048         }
1049         entry->ino->localname = g_strdup (local_filename);
1050         vfs_path_free (local_filename_vpath, TRUE);
1051     }
1052 
1053     local_handle = open (entry->ino->localname, NO_LINEAR (flags), mode);
1054 
1055     if (local_handle == -1)
1056     {
1057         /* file exists(may be). Need to drop O_CREAT flag and truncate file content */
1058         flags = ~O_CREAT & (NO_LINEAR (flags) | O_TRUNC);
1059         local_handle = open (entry->ino->localname, flags, mode);
1060     }
1061 
1062     if (local_handle == -1)
1063         ERRNOR (EIO, NULL);
1064 
1065     extfs_info = g_new (vfs_file_handler_t, 1);
1066     vfs_s_init_fh (extfs_info, entry->ino, created);
1067     extfs_info->handle = local_handle;
1068 
1069     /* i.e. we had no open files and now we have one */
1070     vfs_rmstamp (vfs_extfs_ops, (vfsid) archive);
1071     VFS_SUPER (archive)->fd_usage++;
1072     return extfs_info;
1073 }
1074 
1075 /* --------------------------------------------------------------------------------------------- */
1076 
1077 static ssize_t
1078 extfs_read (void *fh, char *buffer, size_t count)
     /* [previous][next][first][last][top][bottom][index][help]  */
1079 {
1080     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1081 
1082     return read (file->handle, buffer, count);
1083 }
1084 
1085 /* --------------------------------------------------------------------------------------------- */
1086 
1087 static int
1088 extfs_close (void *fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1089 {
1090     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1091     int errno_code = 0;
1092 
1093     close (file->handle);
1094     file->handle = -1;
1095 
1096     /* Commit the file if it has changed */
1097     if (file->changed)
1098     {
1099         struct stat file_status;
1100 
1101         if (extfs_cmd
1102             (" copyin ", EXTFS_SUPER (VFS_FILE_HANDLER_SUPER (fh)), file->ino->ent,
1103              file->ino->localname))
1104             errno_code = EIO;
1105 
1106         if (stat (file->ino->localname, &file_status) != 0)
1107             errno_code = EIO;
1108         else
1109             file->ino->st.st_size = file_status.st_size;
1110 
1111         file->ino->st.st_mtime = time (NULL);
1112     }
1113 
1114     if (--VFS_FILE_HANDLER_SUPER (fh)->fd_usage == 0)
1115         vfs_stamp_create (vfs_extfs_ops, VFS_FILE_HANDLER_SUPER (fh));
1116 
1117     g_free (fh);
1118     if (errno_code != 0)
1119         ERRNOR (EIO, -1);
1120     return 0;
1121 }
1122 
1123 /* --------------------------------------------------------------------------------------------- */
1124 
1125 static int
1126 extfs_errno (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1127 {
1128     (void) me;
1129     return my_errno;
1130 }
1131 
1132 /* --------------------------------------------------------------------------------------------- */
1133 
1134 static void *
1135 extfs_opendir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1136 {
1137     struct extfs_super_t *archive = NULL;
1138     const char *q;
1139     struct vfs_s_entry *entry;
1140     GList **info;
1141 
1142     q = extfs_get_path (vpath, &archive, FL_NONE);
1143     if (q == NULL)
1144         return NULL;
1145     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1146     if (entry == NULL)
1147         return NULL;
1148     entry = extfs_resolve_symlinks (entry);
1149     if (entry == NULL)
1150         return NULL;
1151     if (!S_ISDIR (entry->ino->st.st_mode))
1152         ERRNOR (ENOTDIR, NULL);
1153 
1154     info = g_new (GList *, 1);
1155     *info = g_queue_peek_head_link (entry->ino->subdir);
1156 
1157     return info;
1158 }
1159 
1160 /* --------------------------------------------------------------------------------------------- */
1161 
1162 static struct vfs_dirent *
1163 extfs_readdir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1164 {
1165     struct vfs_dirent *dir;
1166     GList **info = (GList **) data;
1167 
1168     if (*info == NULL)
1169         return NULL;
1170 
1171     dir = vfs_dirent_init (NULL, VFS_ENTRY ((*info)->data)->name, 0);   /* FIXME: inode */
1172 
1173     *info = g_list_next (*info);
1174 
1175     return dir;
1176 }
1177 
1178 /* --------------------------------------------------------------------------------------------- */
1179 
1180 static int
1181 extfs_closedir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1182 {
1183     g_free (data);
1184     return 0;
1185 }
1186 
1187 /* --------------------------------------------------------------------------------------------- */
1188 
1189 
1190 static void
1191 extfs_stat_move (struct stat *buf, const struct vfs_s_inode *inode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1192 {
1193     *buf = inode->st;
1194 
1195 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1196     buf->st_blksize = RECORDSIZE;
1197 #endif
1198     vfs_adjust_stat (buf);
1199 #ifdef HAVE_STRUCT_STAT_ST_MTIM
1200     buf->st_atim.tv_nsec = buf->st_mtim.tv_nsec = buf->st_ctim.tv_nsec = 0;
1201 #endif
1202 }
1203 
1204 /* --------------------------------------------------------------------------------------------- */
1205 
1206 static int
1207 extfs_internal_stat (const vfs_path_t * vpath, struct stat *buf, gboolean resolve)
     /* [previous][next][first][last][top][bottom][index][help]  */
1208 {
1209     struct extfs_super_t *archive;
1210     const char *q;
1211     struct vfs_s_entry *entry;
1212     int result = -1;
1213 
1214     q = extfs_get_path (vpath, &archive, FL_NONE);
1215     if (q == NULL)
1216         goto cleanup;
1217     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1218     if (entry == NULL)
1219         goto cleanup;
1220     if (resolve)
1221     {
1222         entry = extfs_resolve_symlinks (entry);
1223         if (entry == NULL)
1224             goto cleanup;
1225     }
1226     extfs_stat_move (buf, entry->ino);
1227     result = 0;
1228   cleanup:
1229     return result;
1230 }
1231 
1232 /* --------------------------------------------------------------------------------------------- */
1233 
1234 static int
1235 extfs_stat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1236 {
1237     return extfs_internal_stat (vpath, buf, TRUE);
1238 }
1239 
1240 /* --------------------------------------------------------------------------------------------- */
1241 
1242 static int
1243 extfs_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1244 {
1245     return extfs_internal_stat (vpath, buf, FALSE);
1246 }
1247 
1248 /* --------------------------------------------------------------------------------------------- */
1249 
1250 static int
1251 extfs_fstat (void *fh, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1252 {
1253     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1254 
1255     extfs_stat_move (buf, file->ino);
1256     return 0;
1257 }
1258 
1259 /* --------------------------------------------------------------------------------------------- */
1260 
1261 static int
1262 extfs_readlink (const vfs_path_t * vpath, char *buf, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
1263 {
1264     struct extfs_super_t *archive;
1265     const char *q;
1266     size_t len;
1267     struct vfs_s_entry *entry;
1268     int result = -1;
1269 
1270     q = extfs_get_path (vpath, &archive, FL_NONE);
1271     if (q == NULL)
1272         goto cleanup;
1273     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1274     if (entry == NULL)
1275         goto cleanup;
1276     if (!S_ISLNK (entry->ino->st.st_mode))
1277     {
1278         const vfs_path_element_t *path_element;
1279 
1280         path_element = vfs_path_get_by_index (vpath, -1);
1281         path_element->class->verrno = EINVAL;
1282         goto cleanup;
1283     }
1284     len = strlen (entry->ino->linkname);
1285     if (size < len)
1286         len = size;
1287     /* readlink() does not append a NUL character to buf */
1288     result = len;
1289     memcpy (buf, entry->ino->linkname, result);
1290   cleanup:
1291     return result;
1292 }
1293 
1294 /* --------------------------------------------------------------------------------------------- */
1295 
1296 static int
1297 extfs_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
1298 {
1299     (void) vpath;
1300     (void) owner;
1301     (void) group;
1302     return 0;
1303 }
1304 
1305 /* --------------------------------------------------------------------------------------------- */
1306 
1307 static int
1308 extfs_chmod (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1309 {
1310     (void) vpath;
1311     (void) mode;
1312     return 0;
1313 }
1314 
1315 /* --------------------------------------------------------------------------------------------- */
1316 
1317 static ssize_t
1318 extfs_write (void *fh, const char *buf, size_t nbyte)
     /* [previous][next][first][last][top][bottom][index][help]  */
1319 {
1320     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1321 
1322     file->changed = TRUE;
1323     return write (file->handle, buf, nbyte);
1324 }
1325 
1326 /* --------------------------------------------------------------------------------------------- */
1327 
1328 static int
1329 extfs_unlink (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1330 {
1331     struct extfs_super_t *archive;
1332     const char *q;
1333     struct vfs_s_entry *entry;
1334     int result = -1;
1335 
1336     q = extfs_get_path (vpath, &archive, FL_NONE);
1337     if (q == NULL)
1338         goto cleanup;
1339     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1340     if (entry == NULL)
1341         goto cleanup;
1342     entry = extfs_resolve_symlinks (entry);
1343     if (entry == NULL)
1344         goto cleanup;
1345     if (S_ISDIR (entry->ino->st.st_mode))
1346     {
1347         const vfs_path_element_t *path_element;
1348 
1349         path_element = vfs_path_get_by_index (vpath, -1);
1350         path_element->class->verrno = EISDIR;
1351         goto cleanup;
1352     }
1353     if (extfs_cmd (" rm ", archive, entry, ""))
1354     {
1355         my_errno = EIO;
1356         goto cleanup;
1357     }
1358     vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1359     result = 0;
1360   cleanup:
1361     return result;
1362 }
1363 
1364 /* --------------------------------------------------------------------------------------------- */
1365 
1366 static int
1367 extfs_mkdir (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1368 {
1369     struct extfs_super_t *archive;
1370     const char *q;
1371     struct vfs_s_entry *entry;
1372     int result = -1;
1373     const vfs_path_element_t *path_element;
1374 
1375     (void) mode;
1376 
1377     path_element = vfs_path_get_by_index (vpath, -1);
1378     q = extfs_get_path (vpath, &archive, FL_NONE);
1379     if (q == NULL)
1380         goto cleanup;
1381     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1382     if (entry != NULL)
1383     {
1384         path_element->class->verrno = EEXIST;
1385         goto cleanup;
1386     }
1387     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_MKDIR);
1388     if (entry == NULL)
1389         goto cleanup;
1390     entry = extfs_resolve_symlinks (entry);
1391     if (entry == NULL)
1392         goto cleanup;
1393     if (!S_ISDIR (entry->ino->st.st_mode))
1394     {
1395         path_element->class->verrno = ENOTDIR;
1396         goto cleanup;
1397     }
1398 
1399     if (extfs_cmd (" mkdir ", archive, entry, ""))
1400     {
1401         my_errno = EIO;
1402         vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1403         goto cleanup;
1404     }
1405     result = 0;
1406   cleanup:
1407     return result;
1408 }
1409 
1410 /* --------------------------------------------------------------------------------------------- */
1411 
1412 static int
1413 extfs_rmdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1414 {
1415     struct extfs_super_t *archive;
1416     const char *q;
1417     struct vfs_s_entry *entry;
1418     int result = -1;
1419 
1420     q = extfs_get_path (vpath, &archive, FL_NONE);
1421     if (q == NULL)
1422         goto cleanup;
1423     entry = extfs_find_entry (VFS_SUPER (archive)->root, q, FL_NONE);
1424     if (entry == NULL)
1425         goto cleanup;
1426     entry = extfs_resolve_symlinks (entry);
1427     if (entry == NULL)
1428         goto cleanup;
1429     if (!S_ISDIR (entry->ino->st.st_mode))
1430     {
1431         const vfs_path_element_t *path_element;
1432 
1433         path_element = vfs_path_get_by_index (vpath, -1);
1434         path_element->class->verrno = ENOTDIR;
1435         goto cleanup;
1436     }
1437 
1438     if (extfs_cmd (" rmdir ", archive, entry, ""))
1439     {
1440         my_errno = EIO;
1441         goto cleanup;
1442     }
1443     vfs_s_free_entry (VFS_SUPER (archive)->me, entry);
1444     result = 0;
1445   cleanup:
1446     return result;
1447 }
1448 
1449 /* --------------------------------------------------------------------------------------------- */
1450 
1451 static int
1452 extfs_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1453 {
1454     void *data;
1455 
1456     my_errno = ENOTDIR;
1457     data = extfs_opendir (vpath);
1458     if (data == NULL)
1459         return (-1);
1460     extfs_closedir (data);
1461     my_errno = 0;
1462     return 0;
1463 }
1464 
1465 /* --------------------------------------------------------------------------------------------- */
1466 
1467 static off_t
1468 extfs_lseek (void *fh, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
1469 {
1470     vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1471 
1472     return lseek (file->handle, offset, whence);
1473 }
1474 
1475 /* --------------------------------------------------------------------------------------------- */
1476 
1477 static vfsid
1478 extfs_getid (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1479 {
1480     struct extfs_super_t *archive = NULL;
1481     const char *p;
1482 
1483     p = extfs_get_path (vpath, &archive, FL_NO_OPEN);
1484     return (p == NULL ? NULL : (vfsid) archive);
1485 }
1486 
1487 /* --------------------------------------------------------------------------------------------- */
1488 
1489 static vfs_path_t *
1490 extfs_getlocalcopy (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1491 {
1492     vfs_file_handler_t *fh;
1493     vfs_path_t *p;
1494 
1495     fh = VFS_FILE_HANDLER (extfs_open (vpath, O_RDONLY, 0));
1496     if (fh == NULL)
1497         return NULL;
1498     if (fh->ino->localname == NULL)
1499     {
1500         extfs_close ((void *) fh);
1501         return NULL;
1502     }
1503     p = vfs_path_from_str (fh->ino->localname);
1504     VFS_FILE_HANDLER_SUPER (fh)->fd_usage++;
1505     extfs_close ((void *) fh);
1506     return p;
1507 }
1508 
1509 /* --------------------------------------------------------------------------------------------- */
1510 
1511 static int
1512 extfs_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
1513 {
1514     vfs_file_handler_t *fh;
1515 
1516     fh = VFS_FILE_HANDLER (extfs_open (vpath, O_RDONLY, 0));
1517     if (fh == NULL)
1518         return 0;
1519 
1520     if (strcmp (fh->ino->localname, vfs_path_get_last_path_str (local)) == 0)
1521     {
1522         VFS_FILE_HANDLER_SUPER (fh)->fd_usage--;
1523         if (has_changed)
1524             fh->changed = TRUE;
1525         extfs_close ((void *) fh);
1526         return 0;
1527     }
1528     else
1529     {
1530         /* Should not happen */
1531         extfs_close ((void *) fh);
1532         return 0;
1533     }
1534 }
1535 
1536 /* --------------------------------------------------------------------------------------------- */
1537 
1538 static gboolean
1539 extfs_get_plugins (const char *where, gboolean silent)
     /* [previous][next][first][last][top][bottom][index][help]  */
1540 {
1541     char *dirname;
1542     GDir *dir;
1543     const char *filename;
1544 
1545     dirname = g_build_path (PATH_SEP_STR, where, MC_EXTFS_DIR, (char *) NULL);
1546     dir = g_dir_open (dirname, 0, NULL);
1547 
1548     /* We may not use vfs_die() message or message or similar,
1549      * UI is not initialized at this time and message would not
1550      * appear on screen. */
1551     if (dir == NULL)
1552     {
1553         if (!silent)
1554             fprintf (stderr, _("Warning: cannot open %s directory\n"), dirname);
1555         g_free (dirname);
1556         return FALSE;
1557     }
1558 
1559     if (extfs_plugins == NULL)
1560         extfs_plugins = g_array_sized_new (FALSE, TRUE, sizeof (extfs_plugin_info_t), 32);
1561 
1562     while ((filename = g_dir_read_name (dir)) != NULL)
1563     {
1564         char fullname[MC_MAXPATHLEN];
1565         struct stat s;
1566 
1567         g_snprintf (fullname, sizeof (fullname), "%s" PATH_SEP_STR "%s", dirname, filename);
1568 
1569         if ((stat (fullname, &s) == 0)
1570             && S_ISREG (s.st_mode) && !S_ISDIR (s.st_mode)
1571             && (((s.st_mode & S_IXOTH) != 0) ||
1572                 ((s.st_mode & S_IXUSR) != 0) || ((s.st_mode & S_IXGRP) != 0)))
1573         {
1574             int f;
1575 
1576             f = open (fullname, O_RDONLY);
1577 
1578             if (f >= 0)
1579             {
1580                 size_t len, i;
1581                 extfs_plugin_info_t info;
1582                 gboolean found = FALSE;
1583 
1584                 close (f);
1585 
1586                 /* Handle those with a trailing '+', those flag that the
1587                  * file system does not require an archive to work
1588                  */
1589                 len = strlen (filename);
1590                 info.need_archive = (filename[len - 1] != '+');
1591                 info.path = g_strconcat (dirname, PATH_SEP_STR, (char *) NULL);
1592                 info.prefix = g_strdup (filename);
1593 
1594                 /* prepare to compare file names without trailing '+' */
1595                 if (!info.need_archive)
1596                     info.prefix[len - 1] = '\0';
1597 
1598                 /* don't overload already found plugin */
1599                 for (i = 0; i < extfs_plugins->len; i++)
1600                 {
1601                     extfs_plugin_info_t *p;
1602 
1603                     p = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
1604 
1605                     /* 2 files with same names cannot be in a directory */
1606                     if ((strcmp (info.path, p->path) != 0)
1607                         && (strcmp (info.prefix, p->prefix) == 0))
1608                     {
1609                         found = TRUE;
1610                         break;
1611                     }
1612                 }
1613 
1614                 if (found)
1615                 {
1616                     g_free (info.path);
1617                     g_free (info.prefix);
1618                 }
1619                 else
1620                 {
1621                     /* restore file name */
1622                     if (!info.need_archive)
1623                         info.prefix[len - 1] = '+';
1624                     g_array_append_val (extfs_plugins, info);
1625                 }
1626             }
1627         }
1628     }
1629 
1630     g_dir_close (dir);
1631     g_free (dirname);
1632 
1633     return TRUE;
1634 }
1635 
1636 /* --------------------------------------------------------------------------------------------- */
1637 
1638 static int
1639 extfs_init (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1640 {
1641     gboolean d1, d2;
1642 
1643     (void) me;
1644 
1645     /* 1st: scan user directory */
1646     d1 = extfs_get_plugins (mc_config_get_data_path (), TRUE);  /* silent about user dir */
1647     /* 2nd: scan system dir */
1648     d2 = extfs_get_plugins (LIBEXECDIR, d1);
1649 
1650     return (d1 || d2 ? 1 : 0);
1651 }
1652 
1653 /* --------------------------------------------------------------------------------------------- */
1654 
1655 static void
1656 extfs_done (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
1657 {
1658     size_t i;
1659 
1660     while (VFS_SUBCLASS (me)->supers != NULL)
1661         me->free ((vfsid) VFS_SUBCLASS (me)->supers->data);
1662 
1663     if (extfs_plugins == NULL)
1664         return;
1665 
1666     for (i = 0; i < extfs_plugins->len; i++)
1667     {
1668         extfs_plugin_info_t *info;
1669 
1670         info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
1671         g_free (info->path);
1672         g_free (info->prefix);
1673     }
1674 
1675     g_array_free (extfs_plugins, TRUE);
1676 }
1677 
1678 /* --------------------------------------------------------------------------------------------- */
1679 
1680 static int
1681 extfs_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
1682 {
1683     (void) arg;
1684 
1685     if (ctlop == VFS_SETCTL_RUN)
1686     {
1687         extfs_run (vpath);
1688         return 1;
1689     }
1690     return 0;
1691 }
1692 
1693 /* --------------------------------------------------------------------------------------------- */
1694 /*** public functions ****************************************************************************/
1695 /* --------------------------------------------------------------------------------------------- */
1696 
1697 void
1698 vfs_init_extfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1699 {
1700     vfs_init_subclass (&extfs_subclass, "extfs", VFSF_UNKNOWN, NULL);
1701     vfs_extfs_ops->init = extfs_init;
1702     vfs_extfs_ops->done = extfs_done;
1703     vfs_extfs_ops->fill_names = extfs_fill_names;
1704     vfs_extfs_ops->which = extfs_which;
1705     vfs_extfs_ops->open = extfs_open;
1706     vfs_extfs_ops->close = extfs_close;
1707     vfs_extfs_ops->read = extfs_read;
1708     vfs_extfs_ops->write = extfs_write;
1709     vfs_extfs_ops->opendir = extfs_opendir;
1710     vfs_extfs_ops->readdir = extfs_readdir;
1711     vfs_extfs_ops->closedir = extfs_closedir;
1712     vfs_extfs_ops->stat = extfs_stat;
1713     vfs_extfs_ops->lstat = extfs_lstat;
1714     vfs_extfs_ops->fstat = extfs_fstat;
1715     vfs_extfs_ops->chmod = extfs_chmod;
1716     vfs_extfs_ops->chown = extfs_chown;
1717     vfs_extfs_ops->readlink = extfs_readlink;
1718     vfs_extfs_ops->unlink = extfs_unlink;
1719     vfs_extfs_ops->chdir = extfs_chdir;
1720     vfs_extfs_ops->ferrno = extfs_errno;
1721     vfs_extfs_ops->lseek = extfs_lseek;
1722     vfs_extfs_ops->getid = extfs_getid;
1723     vfs_extfs_ops->getlocalcopy = extfs_getlocalcopy;
1724     vfs_extfs_ops->ungetlocalcopy = extfs_ungetlocalcopy;
1725     vfs_extfs_ops->mkdir = extfs_mkdir;
1726     vfs_extfs_ops->rmdir = extfs_rmdir;
1727     vfs_extfs_ops->setctl = extfs_setctl;
1728     extfs_subclass.free_inode = extfs_free_inode;
1729     extfs_subclass.free_archive = extfs_free_archive;
1730     vfs_register_class (vfs_extfs_ops);
1731 }
1732 
1733 /* --------------------------------------------------------------------------------------------- */

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