Traslated some chmod mgs related (for win32 support? not shure)
[midnight-commander.git] / vfs / extfs.c
blobcbf67881a759c5cba1705d2b23ddc1c947dafef8
1 /* Virtual File System: External file system.
2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
5 Rewritten by: 1998 Pavel Machek
6 Additional changes by: 1999 Andrew T. Veliath
8 $Id$
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License
12 as published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Namespace: exports only vfs_extfs_ops */
26 #include <config.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>
38 #endif
39 #include <errno.h>
40 #include "utilvfs.h"
41 #include "../src/dialog.h"
42 #include "../src/main.h" /* For shell_execute */
43 #include "xdirentry.h"
44 #include "vfs.h"
45 #include "extfs.h"
47 #undef ERRNOR
48 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
50 struct inode {
51 nlink_t nlink;
52 struct entry *first_in_subdir; /* only used if this is a directory */
53 struct entry *last_in_subdir;
54 ino_t inode; /* This is inode # */
55 dev_t dev; /* This is an internal identification of the extfs archive */
56 struct archive *archive; /* And this is an archive structure */
57 dev_t rdev;
58 mode_t mode;
59 uid_t uid;
60 gid_t gid;
61 int size;
62 time_t mtime;
63 char linkflag;
64 char *linkname;
65 time_t atime;
66 time_t ctime;
67 char *local_filename;
70 struct entry {
71 struct entry *next_in_dir;
72 struct entry *dir;
73 char *name;
74 struct inode *inode;
77 struct pseudofile {
78 struct archive *archive;
79 unsigned int has_changed:1;
80 int local_handle;
81 struct entry *entry;
84 static struct entry *
85 find_entry (struct entry *dir, char *name, int make_dirs, int make_file);
86 static int extfs_which (vfs *me, char *path);
87 static void remove_entry (struct entry *e);
89 static struct archive *first_archive = NULL;
90 static int my_errno = 0;
91 static struct stat hstat; /* Stat struct corresponding */
93 #define MAXEXTFS 32
94 static char *extfs_prefixes [MAXEXTFS];
95 static char extfs_need_archive [MAXEXTFS];
96 static int extfs_no = 0;
98 static void extfs_fill_names (vfs *me, void (*func)(char *))
100 struct archive *a = first_archive;
101 char *name;
103 while (a){
104 name = g_strconcat (extfs_prefixes [a->fstype], "#",
105 (a->name ? a->name : ""), "/",
106 a->current_dir->name, NULL);
107 (*func)(name);
108 g_free (name);
109 a = a->next;
113 static void make_dot_doubledot (struct entry *ent)
115 struct entry *entry = g_new (struct entry, 1);
116 struct entry *parentry = ent->dir;
117 struct inode *inode = ent->inode, *parent;
119 parent = (parentry != NULL) ? parentry->inode : NULL;
120 entry->name = g_strdup (".");
121 entry->inode = inode;
122 entry->dir = ent;
123 inode->local_filename = NULL;
124 inode->first_in_subdir = entry;
125 inode->last_in_subdir = entry;
126 inode->nlink++;
127 entry->next_in_dir = g_new (struct entry, 1);
128 entry=entry->next_in_dir;
129 entry->name = g_strdup ("..");
130 inode->last_in_subdir = entry;
131 entry->next_in_dir = NULL;
132 if (parent != NULL) {
133 entry->inode = parent;
134 entry->dir = parentry;
135 parent->nlink++;
136 } else {
137 entry->inode = inode;
138 entry->dir = ent;
139 inode->nlink++;
143 static struct entry *generate_entry (struct archive *archive,
144 char *name, struct entry *parentry, mode_t mode)
146 mode_t myumask;
147 struct inode *inode, *parent;
148 struct entry *entry;
150 parent = (parentry != NULL) ? parentry->inode : NULL;
151 entry = g_new (struct entry, 1);
153 entry->name = g_strdup (name);
154 entry->next_in_dir = NULL;
155 entry->dir = parentry;
156 if (parent != NULL) {
157 parent->last_in_subdir->next_in_dir = entry;
158 parent->last_in_subdir = entry;
160 inode = g_new (struct inode, 1);
161 entry->inode = inode;
162 inode->local_filename = NULL;
163 inode->linkname = 0;
164 inode->inode = (archive->__inode_counter)++;
165 inode->dev = archive->rdev;
166 inode->archive = archive;
167 myumask = umask (022);
168 umask (myumask);
169 inode->mode = mode & ~myumask;
170 mode = inode->mode;
171 inode->rdev = 0;
172 inode->uid = getuid ();
173 inode->gid = getgid ();
174 inode->size = 0;
175 inode->mtime = time (NULL);
176 inode->atime = inode->mtime;
177 inode->ctime = inode->mtime;
178 inode->nlink = 1;
179 if (S_ISDIR (mode))
180 make_dot_doubledot (entry);
181 return entry;
184 static void free_entries (struct entry *entry)
186 return;
189 static void free_archive (struct archive *archive)
191 free_entries (archive->root_entry);
192 if (archive->local_name != NULL) {
193 struct stat my;
195 mc_stat (archive->local_name, &my);
196 mc_ungetlocalcopy (archive->name, archive->local_name,
197 archive->local_stat.st_mtime != my.st_mtime);
198 /* ungetlocalcopy frees local_name for us */
200 if (archive->name)
201 g_free (archive->name);
202 g_free (archive);
205 static FILE *open_archive (int fstype, char *name, struct archive **pparc)
207 static dev_t __extfs_no = 0;
208 FILE *result;
209 mode_t mode;
210 char *cmd;
211 char *mc_extfsdir;
212 struct stat mystat;
213 struct archive *current_archive;
214 struct entry *root_entry;
215 char *local_name = NULL, *tmp = 0;
216 int uses_archive = extfs_need_archive [fstype];
218 if (uses_archive){
219 if (mc_stat (name, &mystat) == -1)
220 return NULL;
221 if (!vfs_file_is_local (name)) {
222 local_name = mc_getlocalcopy (name);
223 if (local_name == NULL)
224 return NULL;
226 tmp = name_quote (name, 0);
229 #if 0
230 /* Sorry, what is this good for? */
231 if (uses_archive == EFS_NEED_ARG){
232 tmp = name_quote (name, 0);
234 #endif
236 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
237 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [fstype],
238 " list ", local_name ? local_name : tmp, NULL);
239 if (tmp)
240 g_free (tmp);
241 g_free (mc_extfsdir);
242 result = popen (cmd, "r");
243 g_free (cmd);
244 if (result == NULL) {
245 if (local_name != NULL && uses_archive)
246 mc_ungetlocalcopy (name, local_name, 0);
247 return NULL;
250 current_archive = g_new (struct archive, 1);
251 current_archive->fstype = fstype;
252 current_archive->name = name ? g_strdup (name): name;
253 current_archive->local_name = local_name;
255 if (local_name != NULL)
256 mc_stat (local_name, &current_archive->local_stat);
257 current_archive->__inode_counter = 0;
258 current_archive->fd_usage = 0;
259 current_archive->extfsstat = mystat;
260 current_archive->rdev = __extfs_no++;
261 current_archive->next = first_archive;
262 first_archive = current_archive;
263 mode = current_archive->extfsstat.st_mode & 07777;
264 if (mode & 0400)
265 mode |= 0100;
266 if (mode & 0040)
267 mode |= 0010;
268 if (mode & 0004)
269 mode |= 0001;
270 mode |= S_IFDIR;
271 root_entry = generate_entry (current_archive, "/", NULL, mode);
272 root_entry->inode->uid = current_archive->extfsstat.st_uid;
273 root_entry->inode->gid = current_archive->extfsstat.st_gid;
274 root_entry->inode->atime = current_archive->extfsstat.st_atime;
275 root_entry->inode->ctime = current_archive->extfsstat.st_ctime;
276 root_entry->inode->mtime = current_archive->extfsstat.st_mtime;
277 current_archive->root_entry = root_entry;
278 current_archive->current_dir = root_entry;
280 *pparc = current_archive;
282 return result;
286 * Main loop for reading an archive.
287 * Returns 0 on success, -1 on error.
289 static int read_archive (int fstype, char *name, struct archive **pparc)
291 FILE *extfsd;
292 char *buffer;
293 struct archive *current_archive;
294 char *current_file_name, *current_link_name;
297 if ((extfsd = open_archive (fstype, name, &current_archive)) == NULL) {
298 message_3s (1, MSG_ERROR, _("Couldn't open %s archive\n%s"),
299 extfs_prefixes [fstype], name);
300 return -1;
303 buffer = g_malloc (4096);
304 while (fgets (buffer, 4096, extfsd) != NULL) {
305 current_link_name = NULL;
306 if (vfs_parse_ls_lga (buffer, &hstat, &current_file_name, &current_link_name)) {
307 struct entry *entry, *pent;
308 struct inode *inode;
309 char *p, *q, *cfn = current_file_name;
311 if (*cfn) {
312 if (*cfn == '/')
313 cfn++;
314 p = strchr (cfn, 0);
315 if (p != cfn && *(p - 1) == '/')
316 *(p - 1) = 0;
317 p = strrchr (cfn, '/');
318 if (p == NULL) {
319 p = cfn;
320 q = strchr (cfn, 0);
321 } else {
322 *(p++) = 0;
323 q = cfn;
325 if (S_ISDIR (hstat.st_mode) &&
326 (!strcmp (p, ".") || !strcmp (p, "..")))
327 goto read_extfs_continue;
328 pent = find_entry (current_archive->root_entry, q, 1, 0) ;
329 if (pent == NULL) {
330 message_1s (1, MSG_ERROR, _("Inconsistent extfs archive"));
331 /* FIXME: Should clean everything one day */
332 g_free (buffer);
333 pclose (extfsd);
334 return -1;
336 entry = g_new (struct entry, 1);
337 entry->name = g_strdup (p);
338 entry->next_in_dir = NULL;
339 entry->dir = pent;
340 if (pent != NULL) {
341 if (pent->inode->last_in_subdir){
342 pent->inode->last_in_subdir->next_in_dir = entry;
343 pent->inode->last_in_subdir = entry;
346 if (!S_ISLNK (hstat.st_mode) && current_link_name != NULL) {
347 pent = find_entry (current_archive->root_entry, current_link_name, 0, 0);
348 if (pent == NULL) {
349 message_1s (1, MSG_ERROR, _("Inconsistent extfs archive"));
350 /* FIXME: Should clean everything one day */
351 g_free (buffer);
352 pclose (extfsd);
353 return -1;
354 } else {
355 entry->inode = pent->inode;
356 pent->inode->nlink++;
358 } else {
359 inode = g_new (struct inode, 1);
360 entry->inode = inode;
361 inode->local_filename = NULL;
362 inode->inode = (current_archive->__inode_counter)++;
363 inode->nlink = 1;
364 inode->dev = current_archive->rdev;
365 inode->archive = current_archive;
366 inode->mode = hstat.st_mode;
367 #ifdef HAVE_ST_RDEV
368 inode->rdev = hstat.st_rdev;
369 #else
370 inode->rdev = 0;
371 #endif
372 inode->uid = hstat.st_uid;
373 inode->gid = hstat.st_gid;
374 inode->size = hstat.st_size;
375 inode->mtime = hstat.st_mtime;
376 inode->atime = hstat.st_atime;
377 inode->ctime = hstat.st_ctime;
378 if (current_link_name != NULL && S_ISLNK (hstat.st_mode)) {
379 inode->linkname = current_link_name;
380 current_link_name = NULL;
381 } else {
382 if (S_ISLNK( hstat.st_mode))
383 inode->mode &= ~S_IFLNK; /* You *DON'T* want to do this always */
384 inode->linkname = NULL;
386 if (S_ISDIR (hstat.st_mode))
387 make_dot_doubledot (entry);
390 read_extfs_continue:
391 g_free (current_file_name);
392 if (current_link_name != NULL)
393 g_free (current_link_name);
396 pclose (extfsd);
397 #ifdef SCO_FLAVOR
398 waitpid(-1,NULL,WNOHANG);
399 #endif /* SCO_FLAVOR */
400 *pparc = current_archive;
401 g_free (buffer);
402 return 0;
405 static char *get_path (char *inname, struct archive **archive, int is_dir,
406 int do_not_open);
408 /* Returns path inside argument. Returned char* is inside inname, which is mangled
409 * by this operation (so you must not free it's return value)
411 static char *get_path_mangle (char *inname, struct archive **archive, int is_dir,
412 int do_not_open)
414 char *local, *archive_name, *op;
415 int result = -1;
416 struct archive *parc;
417 struct vfs_stamping *parent;
418 vfs *v;
419 int fstype;
421 archive_name = inname;
422 vfs_split( inname, &local, &op );
423 fstype = extfs_which( NULL, op ); /* FIXME: we really should pass
424 self pointer. But as we know that extfs_which does not touch vfs
425 *me, it does not matter for now */
426 if (fstype == -1)
427 return NULL;
428 if (!local)
429 local = "";
431 /* All filesystems should have some local archive, at least
432 * it can be '/'.
434 * Actually, we should implement an alias mechanism that would
435 * translate: "a:" to "dos:a.
438 for (parc = first_archive; parc != NULL; parc = parc->next)
439 if (parc->name) {
440 if (!strcmp (parc->name, archive_name)) {
441 struct stat *s=&(parc->extfsstat);
442 if (vfs_uid && (!(s->st_mode & 0004)))
443 if ((s->st_gid != vfs_gid) || !(s->st_mode & 0040))
444 if ((s->st_uid != vfs_uid) || !(s->st_mode & 0400))
445 return NULL;
446 /* This is not too secure - in some cases (/#mtools) files created
447 under user a are probably visible to everyone else since / usually
448 has permissions 755 */
449 vfs_stamp (&vfs_extfs_ops, (vfsid) parc);
450 goto return_success;
454 result = do_not_open ? -1 : read_archive (fstype, archive_name, &parc);
455 if (result == -1) ERRNOR (EIO, NULL);
457 if (archive_name){
458 v = vfs_type (archive_name);
459 if (v == &vfs_local_ops) {
460 parent = NULL;
461 } else {
462 parent = g_new (struct vfs_stamping, 1);
463 parent->v = v;
464 parent->next = 0;
465 parent->id = (*v->getid) (v, archive_name, &(parent->parent));
467 vfs_add_noncurrent_stamps (&vfs_extfs_ops, (vfsid) parc, parent);
468 vfs_rm_parents (parent);
470 return_success:
471 *archive = parc;
472 return local;
475 /* Returns allocated path (without leading slash) inside the archive */
476 static char *get_path_from_entry (struct entry *entry)
478 struct list {
479 struct list *next;
480 char *name;
481 } *head, *p;
482 char *localpath;
483 size_t len;
485 for (len = 0, head = 0; entry->dir; entry = entry->dir) {
486 p = g_new (struct list, 1);
487 p->next = head;
488 p->name = entry->name;
489 head = p;
490 len += strlen (entry->name) + 1;
493 if (len == 0)
494 return g_strdup ("");
496 localpath = g_malloc (len);
497 *localpath = '\0';
498 while (head) {
499 strcat (localpath, head->name);
500 if (head->next)
501 strcat (localpath, "/");
502 p = head;
503 head = head->next;
504 g_free (p);
506 return (localpath);
510 struct loop_protect {
511 struct entry *entry;
512 struct loop_protect *next;
514 static int errloop;
515 static int notadir;
517 static struct entry *
518 __find_entry (struct entry *dir, char *name,
519 struct loop_protect *list, int make_dirs, int make_file);
521 static struct entry *
522 __resolve_symlinks (struct entry *entry,
523 struct loop_protect *list)
525 struct entry *pent;
526 struct loop_protect *looping;
528 if (!S_ISLNK (entry->inode->mode))
529 return entry;
530 for (looping = list; looping != NULL; looping = looping->next)
531 if (entry == looping->entry) { /* Here we protect us against symlink looping */
532 errloop = 1;
533 return NULL;
535 looping = g_new (struct loop_protect, 1);
536 looping->entry = entry;
537 looping->next = list;
538 pent = __find_entry (entry->dir, entry->inode->linkname, looping, 0, 0);
539 g_free (looping);
540 if (pent == NULL)
541 my_errno = ENOENT;
542 return pent;
545 static struct entry *my_resolve_symlinks (struct entry *entry)
547 struct entry *res;
549 errloop = 0;
550 notadir = 0;
551 res = __resolve_symlinks (entry, NULL);
552 if (res == NULL) {
553 if (errloop)
554 my_errno = ELOOP;
555 else if (notadir)
556 my_errno = ENOTDIR;
558 return res;
561 static char *get_archive_name (struct archive *archive)
563 char *archive_name;
565 if (archive->local_name)
566 archive_name = archive->local_name;
567 else
568 archive_name = archive->name;
570 if (!archive_name || !*archive_name)
571 return "no_archive_name";
572 else
573 return archive_name;
576 static void extfs_run (char *file)
578 struct archive *archive;
579 char *p, *q, *archive_name, *mc_extfsdir;
580 char *cmd;
582 if ((p = get_path (file, &archive, 0, 0)) == NULL)
583 return;
584 q = name_quote (p, 0);
585 g_free (p);
587 archive_name = name_quote (get_archive_name(archive), 0);
588 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
589 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
590 " run ", archive_name, " ", q, NULL);
591 g_free (mc_extfsdir);
592 g_free (archive_name);
593 g_free (q);
594 #ifndef VFS_STANDALONE
595 shell_execute(cmd, 0);
596 #else
597 vfs_die( "shell_execute: implement me!" );
598 #endif
599 g_free(cmd);
602 static void *extfs_open (vfs *me, char *file, int flags, int mode)
604 struct pseudofile *extfs_info;
605 struct archive *archive;
606 char *q;
607 char *mc_extfsdir;
608 struct entry *entry;
609 int local_handle;
610 const int do_create = (flags & O_ACCMODE) != O_RDONLY;
612 if ((q = get_path_mangle (file, &archive, 0, 0)) == NULL)
613 return NULL;
614 entry = find_entry (archive->root_entry, q, 0, do_create);
615 if (entry == NULL)
616 return NULL;
617 if ((entry = my_resolve_symlinks (entry)) == NULL)
618 return NULL;
619 if (S_ISDIR (entry->inode->mode)) ERRNOR (EISDIR, NULL);
620 if (entry->inode->local_filename == NULL) {
621 char *cmd;
622 char *archive_name, *p;
625 int handle;
626 handle = mc_mkstemps (&entry->inode->local_filename, "extfs", NULL);
628 if (handle == -1)
629 return NULL;
630 close(handle);
632 p = get_path_from_entry (entry);
633 q = name_quote (p, 0);
634 g_free (p);
635 archive_name = name_quote (get_archive_name (archive), 0);
637 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
638 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
639 " copyout ",
640 archive_name,
641 " ", q, " ", entry->inode->local_filename, NULL);
642 g_free (q);
643 g_free (mc_extfsdir);
644 g_free (archive_name);
645 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, shell, cmd) && !do_create){
646 free (entry->inode->local_filename);
647 entry->inode->local_filename = NULL;
648 g_free (cmd);
649 my_errno = EIO;
650 return NULL;
652 g_free (cmd);
655 local_handle = open (entry->inode->local_filename, flags, mode);
656 if (local_handle == -1) ERRNOR (EIO, NULL);
658 extfs_info = g_new (struct pseudofile, 1);
659 extfs_info->archive = archive;
660 extfs_info->entry = entry;
661 extfs_info->has_changed = do_create;
662 extfs_info->local_handle = local_handle;
664 /* i.e. we had no open files and now we have one */
665 vfs_rmstamp (&vfs_extfs_ops, (vfsid) archive, 1);
666 archive->fd_usage++;
667 return extfs_info;
670 static int extfs_read (void *data, char *buffer, int count)
672 struct pseudofile *file = (struct pseudofile *)data;
674 return read (file->local_handle, buffer, count);
677 static int extfs_close (void *data)
679 struct pseudofile *file;
680 int errno_code = 0;
681 file = (struct pseudofile *)data;
683 close (file->local_handle);
685 /* Commit the file if it has changed */
686 if (file->has_changed){
687 struct archive *archive;
688 char *archive_name, *file_name;
689 char *cmd;
690 char *mc_extfsdir;
691 char *p;
693 archive = file->archive;
694 archive_name = name_quote (get_archive_name (archive), 0);
695 p = get_path_from_entry (file->entry);
696 file_name = name_quote (p, 0);
697 g_free (p);
699 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
700 cmd = g_strconcat (mc_extfsdir,
701 extfs_prefixes [archive->fstype],
702 " copyin ", archive_name, " ",
703 file_name, " ",
704 file->entry->inode->local_filename, NULL);
705 g_free (archive_name);
706 g_free (file_name);
707 g_free (mc_extfsdir);
708 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, shell, cmd))
709 errno_code = EIO;
710 g_free (cmd);
712 struct stat file_status;
713 if (stat(file->entry->inode->local_filename,&file_status) != 0)
714 errno_code = EIO;
715 else
716 file->entry->inode->size = file_status.st_size;
719 file->entry->inode->mtime = time (NULL);
722 file->archive->fd_usage--;
723 if (!file->archive->fd_usage) {
724 struct vfs_stamping *parent;
725 vfs *v;
727 if (!file->archive->name || !*file->archive->name || (v = vfs_type (file->archive->name)) == &vfs_local_ops) {
728 parent = NULL;
729 } else {
730 parent = g_new (struct vfs_stamping, 1);
731 parent->v = v;
732 parent->next = 0;
733 parent->id = (*v->getid) (v, file->archive->name, &(parent->parent));
735 vfs_add_noncurrent_stamps (&vfs_extfs_ops, (vfsid) (file->archive), parent);
736 vfs_rm_parents (parent);
739 g_free (data);
740 if (errno_code) ERRNOR (EIO, -1);
741 return 0;
744 #define RECORDSIZE 512
746 static char *get_path (char *inname, struct archive **archive, int is_dir,
747 int do_not_open)
749 char *buf = g_strdup (inname);
750 char *res = get_path_mangle( buf, archive, is_dir, do_not_open );
751 char *res2 = NULL;
752 if (res)
753 res2 = g_strdup(res);
754 g_free(buf);
755 return res2;
758 static struct entry*
759 __find_entry (struct entry *dir, char *name,
760 struct loop_protect *list, int make_dirs, int make_file)
762 struct entry *pent, *pdir;
763 char *p, *q, *name_end;
764 char c;
766 if (*name == '/') { /* Handle absolute paths */
767 name++;
768 dir = dir->inode->archive->root_entry;
771 pent = dir;
772 p = name;
773 name_end = name + strlen (name);
774 q = strchr (p, '/');
775 c = '/';
776 if (!q)
777 q = strchr (p, 0);
779 for (; pent != NULL && c && *p; ){
780 c = *q;
781 *q = 0;
783 if (strcmp (p, ".")){
784 if (!strcmp (p, ".."))
785 pent = pent->dir;
786 else {
787 if ((pent = __resolve_symlinks (pent, list))==NULL){
788 *q = c;
789 return NULL;
791 if (!S_ISDIR (pent->inode->mode)){
792 *q = c;
793 notadir = 1;
794 return NULL;
796 pdir = pent;
797 for (pent = pent->inode->first_in_subdir; pent; pent = pent->next_in_dir)
798 /* Hack: I keep the original semanthic unless
799 q+1 would break in the strchr */
800 if (!strcmp (pent->name, p)){
801 if (q + 1 > name_end){
802 *q = c;
803 notadir = !S_ISDIR (pent->inode->mode);
804 return pent;
806 break;
809 /* When we load archive, we create automagically
810 * non-existant directories
812 if (pent == NULL && make_dirs) {
813 pent = generate_entry (dir->inode->archive, p, pdir, S_IFDIR | 0777);
815 if (pent == NULL && make_file) {
816 pent = generate_entry (dir->inode->archive, p, pdir, 0777);
820 /* Next iteration */
821 *q = c;
822 p = q + 1;
823 q = strchr (p, '/');
824 if (!q)
825 q = strchr (p, 0);
827 if (pent == NULL)
828 my_errno = ENOENT;
829 return pent;
832 static struct entry *find_entry (struct entry *dir, char *name, int make_dirs, int make_file)
834 struct entry *res;
836 errloop = 0;
837 notadir = 0;
838 res = __find_entry (dir, name, NULL, make_dirs, make_file);
839 if (res == NULL) {
840 if (errloop)
841 my_errno = ELOOP;
842 else if (notadir)
843 my_errno = ENOTDIR;
845 return res;
849 static int s_errno (vfs *me)
851 return my_errno;
854 static void * s_opendir (vfs *me, char *dirname)
856 struct archive *archive;
857 char *q;
858 struct entry *entry;
859 struct entry **info;
861 if ((q = get_path_mangle (dirname, &archive, 1, 0)) == NULL)
862 return NULL;
863 entry = find_entry (archive->root_entry, q, 0, 0);
864 if (entry == NULL)
865 return NULL;
866 if ((entry = my_resolve_symlinks (entry)) == NULL)
867 return NULL;
868 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, NULL);
870 info = g_new (struct entry *, 2);
871 info[0] = entry->inode->first_in_subdir;
872 info[1] = entry->inode->first_in_subdir;
874 return info;
877 static void * s_readdir (void *data)
879 static struct {
880 struct dirent dir;
881 #ifdef NEED_EXTRA_DIRENT_BUFFER
882 char extra_buffer [MC_MAXPATHLEN];
883 #endif
884 } dir;
886 struct entry **info = (struct entry **) data;
888 if (!*info)
889 return NULL;
891 strcpy (&(dir.dir.d_name [0]), (*info)->name);
893 #ifndef DIRENT_LENGTH_COMPUTED
894 dir.d_namlen = strlen (dir.dir.d_name);
895 #endif
896 *info = (*info)->next_in_dir;
898 return (void *)&dir;
901 static int s_telldir (void *data)
903 struct entry **info = (struct entry **) data;
904 struct entry *cur;
905 int num = 0;
907 cur = info[1];
908 while (cur!=NULL) {
909 if (cur == info[0]) return num;
910 num++;
911 cur = cur->next_in_dir;
913 return -1;
916 static void s_seekdir (void *data, int offset)
918 struct entry **info = (struct entry **) data;
919 int i;
920 info[0] = info[1];
921 for (i=0; i<offset; i++)
922 s_readdir( data );
925 static int s_closedir (void *data)
927 g_free (data);
928 return 0;
931 static void stat_move( struct stat *buf, struct inode *inode )
933 buf->st_dev = inode->dev;
934 buf->st_ino = inode->inode;
935 buf->st_mode = inode->mode;
936 buf->st_nlink = inode->nlink;
937 buf->st_uid = inode->uid;
938 buf->st_gid = inode->gid;
939 #ifdef HAVE_ST_RDEV
940 buf->st_rdev = inode->rdev;
941 #endif
942 buf->st_size = inode->size;
943 #ifdef HAVE_ST_BLKSIZE
944 buf->st_blksize = RECORDSIZE;
945 #endif
946 #ifdef HAVE_ST_BLOCKS
947 buf->st_blocks = (inode->size + RECORDSIZE - 1) / RECORDSIZE;
948 #endif
949 buf->st_atime = inode->atime;
950 buf->st_mtime = inode->mtime;
951 buf->st_ctime = inode->ctime;
954 static int s_internal_stat (char *path, struct stat *buf, int resolve)
956 struct archive *archive;
957 char *q;
958 struct entry *entry;
959 struct inode *inode;
961 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
962 return -1;
963 entry = find_entry (archive->root_entry, q, 0, 0);
964 if (entry == NULL)
965 return -1;
966 if (resolve && (entry = my_resolve_symlinks (entry)) == NULL)
967 return -1;
968 inode = entry->inode;
969 stat_move( buf, inode );
970 return 0;
973 static int s_stat (vfs *me, char *path, struct stat *buf)
975 return s_internal_stat (path, buf, 1);
978 static int s_lstat (vfs *me, char *path, struct stat *buf)
980 return s_internal_stat (path, buf, 0);
983 static int s_fstat (void *data, struct stat *buf)
985 struct pseudofile *file = (struct pseudofile *)data;
986 struct inode *inode;
988 inode = file->entry->inode;
989 stat_move( buf, inode );
990 return 0;
993 static int s_readlink (vfs *me, char *path, char *buf, int size)
995 struct archive *archive;
996 char *q;
997 int i;
998 struct entry *entry;
1000 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
1001 return -1;
1002 entry = find_entry (archive->root_entry, q, 0, 0);
1003 if (entry == NULL)
1004 return -1;
1005 if (!S_ISLNK (entry->inode->mode)) ERRNOR (EINVAL, -1);
1006 if (size > (i = strlen (entry->inode->linkname))) {
1007 size = i;
1009 strncpy (buf, entry->inode->linkname, i);
1010 return i;
1013 static int extfs_chmod (vfs *me, char *path, int mode)
1015 return 0;
1018 static int extfs_write (void *data, char *buf, int nbyte)
1020 struct pseudofile *file = (struct pseudofile *)data;
1022 file->has_changed = 1;
1023 return write (file->local_handle, buf, nbyte);
1026 static int extfs_unlink (vfs *me, char *file)
1028 struct archive *archive;
1029 char *q;
1030 char *mc_extfsdir;
1031 struct entry *entry;
1032 char *cmd;
1033 char *archive_name, *p;
1035 if ((q = get_path_mangle (file, &archive, 0, 0)) == NULL)
1036 return -1;
1037 entry = find_entry (archive->root_entry, q, 0, 0);
1038 if (entry == NULL)
1039 return -1;
1040 if ((entry = my_resolve_symlinks (entry)) == NULL)
1041 return -1;
1042 if (S_ISDIR (entry->inode->mode)) ERRNOR (EISDIR, -1);
1044 p = get_path_from_entry (entry);
1045 q = name_quote (p, 0);
1046 g_free (p);
1047 archive_name = name_quote (get_archive_name (archive), 0);
1049 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1050 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1051 " rm ", archive_name, " ", q, NULL);
1052 g_free (q);
1053 g_free (mc_extfsdir);
1054 g_free (archive_name);
1055 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, shell, cmd)){
1056 g_free (cmd);
1057 my_errno = EIO;
1058 return -1;
1060 g_free (cmd);
1061 remove_entry (entry);
1063 return 0;
1066 static int extfs_mkdir (vfs *me, char *path, mode_t mode)
1068 struct archive *archive;
1069 char *q;
1070 char *mc_extfsdir;
1071 struct entry *entry;
1072 char *cmd;
1073 char *archive_name, *p;
1075 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
1076 return -1;
1077 entry = find_entry (archive->root_entry, q, 0, 0);
1078 if (entry != NULL) ERRNOR (EEXIST, -1);
1079 entry = find_entry (archive->root_entry, q, 1, 0);
1080 if (entry == NULL)
1081 return -1;
1082 if ((entry = my_resolve_symlinks (entry)) == NULL)
1083 return -1;
1084 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, -1);
1086 p = get_path_from_entry (entry);
1087 q = name_quote (p, 0);
1088 g_free (p);
1089 archive_name = name_quote (get_archive_name (archive), 0);
1091 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1092 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1093 " mkdir ", archive_name, " ", q, NULL);
1094 g_free (q);
1095 g_free (mc_extfsdir);
1096 g_free (archive_name);
1097 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, shell, cmd)){
1098 g_free (cmd);
1099 my_errno = EIO;
1100 remove_entry (entry);
1101 return -1;
1103 g_free (cmd);
1105 return 0;
1108 static int extfs_rmdir (vfs *me, char *path)
1110 struct archive *archive;
1111 char *q;
1112 char *mc_extfsdir;
1113 struct entry *entry;
1114 char *cmd;
1115 char *archive_name, *p;
1117 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
1118 return -1;
1119 entry = find_entry (archive->root_entry, q, 0, 0);
1120 if (entry == NULL)
1121 return -1;
1122 if ((entry = my_resolve_symlinks (entry)) == NULL)
1123 return -1;
1124 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, -1);
1126 p = get_path_from_entry (entry);
1127 q = name_quote (p, 0);
1128 g_free (p);
1129 archive_name = name_quote (get_archive_name (archive), 0);
1131 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1132 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1133 " rmdir ", archive_name, " ", q, NULL);
1134 g_free (q);
1135 g_free (mc_extfsdir);
1136 g_free (archive_name);
1137 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, shell, cmd)){
1138 g_free (cmd);
1139 my_errno = EIO;
1140 return -1;
1142 g_free (cmd);
1143 remove_entry (entry);
1145 return 0;
1148 static int extfs_chdir (vfs *me, char *path)
1150 struct archive *archive;
1151 char *q;
1152 struct entry *entry;
1154 my_errno = ENOTDIR;
1155 if ((q = get_path_mangle (path, &archive, 1, 0)) == NULL)
1156 return -1;
1157 entry = find_entry (archive->root_entry, q, 0, 0);
1158 if (!entry)
1159 return -1;
1160 entry = my_resolve_symlinks (entry);
1161 if ((!entry) || (!S_ISDIR (entry->inode->mode)))
1162 return -1;
1163 entry->inode->archive->current_dir = entry;
1164 my_errno = 0;
1165 return 0;
1168 static int extfs_lseek (void *data, off_t offset, int whence)
1170 struct pseudofile *file = (struct pseudofile *) data;
1172 return lseek (file->local_handle, offset, whence);
1175 static vfsid extfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
1177 struct archive *archive;
1178 vfs *v;
1179 vfsid id;
1180 struct vfs_stamping *par;
1181 char *p;
1183 *parent = NULL;
1184 if (!(p = get_path (path, &archive, 1, 1)))
1185 return (vfsid) -1;
1186 g_free(p);
1187 if (archive->name){
1188 v = vfs_type (archive->name);
1189 id = (*v->getid) (v, archive->name, &par);
1190 if (id != (vfsid)-1) {
1191 *parent = g_new (struct vfs_stamping, 1);
1192 (*parent)->v = v;
1193 (*parent)->id = id;
1194 (*parent)->parent = par;
1195 (*parent)->next = NULL;
1198 return (vfsid) archive;
1201 static int extfs_nothingisopen (vfsid id)
1203 if (((struct archive *)id)->fd_usage <= 0)
1204 return 1;
1205 return 0;
1208 static void remove_entry (struct entry *e)
1210 int i = --(e->inode->nlink);
1211 struct entry *pe, *ent, *prev;
1213 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1214 struct entry *f = e->inode->first_in_subdir;
1215 e->inode->first_in_subdir = NULL;
1216 remove_entry (f);
1218 pe = e->dir;
1219 if (e == pe->inode->first_in_subdir)
1220 pe->inode->first_in_subdir = e->next_in_dir;
1222 prev = NULL;
1223 for (ent = pe->inode->first_in_subdir; ent && ent->next_in_dir;
1224 ent = ent->next_in_dir)
1225 if (e == ent->next_in_dir) {
1226 prev = ent;
1227 break;
1229 if (prev)
1230 prev->next_in_dir = e->next_in_dir;
1231 if (e == pe->inode->last_in_subdir)
1232 pe->inode->last_in_subdir = prev;
1234 if (i <= 0) {
1235 if (e->inode->local_filename != NULL) {
1236 unlink (e->inode->local_filename);
1237 free (e->inode->local_filename);
1239 if (e->inode->linkname != NULL)
1240 g_free (e->inode->linkname);
1241 g_free (e->inode);
1244 g_free (e->name);
1245 g_free (e);
1248 static void free_entry (struct entry *e)
1250 int i = --(e->inode->nlink);
1251 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1252 struct entry *f = e->inode->first_in_subdir;
1254 e->inode->first_in_subdir = NULL;
1255 free_entry (f);
1257 if (i <= 0) {
1258 if (e->inode->local_filename != NULL) {
1259 unlink (e->inode->local_filename);
1260 free (e->inode->local_filename);
1262 if (e->inode->linkname != NULL)
1263 g_free (e->inode->linkname);
1264 g_free (e->inode);
1266 if (e->next_in_dir != NULL)
1267 free_entry (e->next_in_dir);
1268 g_free (e->name);
1269 g_free (e);
1272 static void extfs_free (vfsid id)
1274 struct archive *parc;
1275 struct archive *archive = (struct archive *)id;
1277 free_entry (archive->root_entry);
1278 if (archive == first_archive) {
1279 first_archive = archive->next;
1280 } else {
1281 for (parc = first_archive; parc != NULL; parc = parc->next)
1282 if (parc->next == archive)
1283 break;
1284 if (parc != NULL)
1285 parc->next = archive->next;
1287 free_archive (archive);
1290 static char *extfs_getlocalcopy (vfs *me, char *path)
1292 struct pseudofile *fp =
1293 (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
1294 char *p;
1296 if (fp == NULL)
1297 return NULL;
1298 if (fp->entry->inode->local_filename == NULL) {
1299 extfs_close ((void *) fp);
1300 return NULL;
1302 p = g_strdup (fp->entry->inode->local_filename);
1303 fp->archive->fd_usage++;
1304 extfs_close ((void *) fp);
1305 return p;
1308 static int extfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
1310 struct pseudofile *fp =
1311 (struct pseudofile *) extfs_open (me, path, O_WRONLY, 0);
1313 if (fp == NULL)
1314 return 0;
1315 if (!strcmp (fp->entry->inode->local_filename, local)) {
1316 fp->archive->fd_usage--;
1317 extfs_close ((void *) fp);
1318 return 0;
1319 } else {
1320 /* Should not happen */
1321 extfs_close ((void *) fp);
1322 return mc_def_ungetlocalcopy (me, path, local, has_changed);
1327 #include "../src/profile.h"
1328 static int extfs_init (vfs *me)
1330 FILE *cfg;
1331 char *mc_extfsini;
1333 mc_extfsini = concat_dir_and_file (mc_home, "extfs/extfs.ini");
1334 cfg = fopen (mc_extfsini, "r");
1336 if (!cfg) {
1337 fprintf(stderr, _("Warning: file %s not found\n"), mc_extfsini);
1338 g_free (mc_extfsini);
1339 return 0;
1342 extfs_no = 0;
1343 while ( extfs_no < MAXEXTFS ) {
1344 char key[256];
1345 char *c;
1347 if (!fgets( key, sizeof (key)-1, cfg ))
1348 break;
1350 /* Handle those with a trailing ':', those flag that the
1351 * file system does not require an archive to work
1354 if (*key == '[') {
1355 /* We may not use vfs_die() message or message_1s or similar,
1356 * UI is not initialized at this time and message would not
1357 * appear on screen. */
1358 fprintf(stderr, "Warning: You need to update your %s file.\n",
1359 mc_extfsini);
1360 fclose(cfg);
1361 g_free (mc_extfsini);
1362 return 0;
1364 if (*key == '#')
1365 continue;
1367 if ((c = strchr (key, '\n'))){
1368 *c = 0;
1369 c = &key [strlen (key) - 1];
1370 } else {
1371 c = key;
1373 extfs_need_archive [extfs_no] = !(*c == ':');
1374 if (*c == ':')
1375 *c = 0;
1376 if (!(*key))
1377 continue;
1379 extfs_prefixes [extfs_no] = g_strdup (key);
1380 extfs_no++;
1382 fclose(cfg);
1383 g_free (mc_extfsini);
1384 return 1;
1387 /* Do NOT use me argument in this function */
1388 static int extfs_which (vfs *me, char *path)
1390 int i;
1392 for (i = 0; i < extfs_no; i++)
1393 if (!strcmp (path, extfs_prefixes [i]))
1394 return i;
1395 return -1;
1398 static void extfs_done (vfs *me)
1400 int i;
1402 for (i = 0; i < extfs_no; i++ )
1403 g_free (extfs_prefixes [i]);
1404 extfs_no = 0;
1407 static int extfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1409 if (ctlop == MCCTL_EXTFS_RUN) {
1410 extfs_run (path);
1411 return 1;
1413 return 0;
1416 vfs vfs_extfs_ops = {
1417 NULL, /* This is place of next pointer */
1418 "extfs",
1419 F_EXEC, /* flags */
1420 NULL, /* prefix */
1421 NULL, /* data */
1422 0, /* errno */
1423 extfs_init,
1424 extfs_done,
1425 extfs_fill_names,
1426 extfs_which,
1428 extfs_open,
1429 extfs_close,
1430 extfs_read,
1431 extfs_write,
1433 s_opendir,
1434 s_readdir,
1435 s_closedir,
1436 s_telldir,
1437 s_seekdir,
1439 s_stat,
1440 s_lstat,
1441 s_fstat,
1443 extfs_chmod, /* chmod ... strange, returns success? */
1444 NULL,
1445 NULL,
1447 s_readlink,
1449 NULL, /* symlink */
1450 NULL,
1451 extfs_unlink,
1453 NULL,
1454 extfs_chdir,
1455 s_errno,
1456 extfs_lseek,
1457 NULL,
1459 extfs_getid,
1460 extfs_nothingisopen,
1461 extfs_free,
1463 extfs_getlocalcopy,
1464 extfs_ungetlocalcopy,
1466 extfs_mkdir, /* mkdir */
1467 extfs_rmdir,
1468 NULL,
1469 extfs_setctl
1471 MMAPNULL