translated tartially 'til file operations...
[midnight-commander.git] / vfs / extfs.c
blob3435c2cebe3a6eb2b38c0281507c073c37456b47
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 (a->name ? a->name : "",
105 "#", extfs_prefixes [a->fstype],
106 PATH_SEP_STR, 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 shell_execute(cmd, 0);
595 g_free(cmd);
598 static void *extfs_open (vfs *me, char *file, int flags, int mode)
600 struct pseudofile *extfs_info;
601 struct archive *archive;
602 char *q;
603 char *mc_extfsdir;
604 struct entry *entry;
605 int local_handle;
606 int created = 0;
608 if ((q = get_path_mangle (file, &archive, 0, 0)) == NULL)
609 return NULL;
610 entry = find_entry (archive->root_entry, q, 0, 0);
611 if (entry == NULL && (flags & O_CREAT)) {
612 /* Create new entry */
613 entry = find_entry (archive->root_entry, q, 0, 1);
614 created = (entry != NULL);
616 if (entry == NULL)
617 return NULL;
618 if ((entry = my_resolve_symlinks (entry)) == NULL)
619 return NULL;
620 if (S_ISDIR (entry->inode->mode)) ERRNOR (EISDIR, NULL);
621 if (entry->inode->local_filename == NULL) {
622 char *cmd;
623 char *archive_name, *p;
626 int handle;
627 handle = mc_mkstemps (&entry->inode->local_filename, "extfs", NULL);
629 if (handle == -1)
630 return NULL;
631 close(handle);
633 p = get_path_from_entry (entry);
634 q = name_quote (p, 0);
635 g_free (p);
636 archive_name = name_quote (get_archive_name (archive), 0);
638 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
639 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
640 " copyout ",
641 archive_name,
642 " ", q, " ", entry->inode->local_filename, NULL);
643 g_free (q);
644 g_free (mc_extfsdir);
645 g_free (archive_name);
646 if (my_system (EXECUTE_AS_SHELL, shell, cmd) && !created){
647 free (entry->inode->local_filename);
648 entry->inode->local_filename = NULL;
649 g_free (cmd);
650 my_errno = EIO;
651 return NULL;
653 g_free (cmd);
656 local_handle = open (entry->inode->local_filename, NO_LINEAR(flags),
657 mode);
658 if (local_handle == -1) ERRNOR (EIO, NULL);
660 extfs_info = g_new (struct pseudofile, 1);
661 extfs_info->archive = archive;
662 extfs_info->entry = entry;
663 extfs_info->has_changed = created;
664 extfs_info->local_handle = local_handle;
666 /* i.e. we had no open files and now we have one */
667 vfs_rmstamp (&vfs_extfs_ops, (vfsid) archive, 1);
668 archive->fd_usage++;
669 return extfs_info;
672 static int extfs_read (void *data, char *buffer, int count)
674 struct pseudofile *file = (struct pseudofile *)data;
676 return read (file->local_handle, buffer, count);
679 static int extfs_close (void *data)
681 struct pseudofile *file;
682 int errno_code = 0;
683 file = (struct pseudofile *)data;
685 close (file->local_handle);
687 /* Commit the file if it has changed */
688 if (file->has_changed){
689 struct archive *archive;
690 char *archive_name, *file_name;
691 char *cmd;
692 char *mc_extfsdir;
693 char *p;
695 archive = file->archive;
696 archive_name = name_quote (get_archive_name (archive), 0);
697 p = get_path_from_entry (file->entry);
698 file_name = name_quote (p, 0);
699 g_free (p);
701 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
702 cmd = g_strconcat (mc_extfsdir,
703 extfs_prefixes [archive->fstype],
704 " copyin ", archive_name, " ",
705 file_name, " ",
706 file->entry->inode->local_filename, NULL);
707 g_free (archive_name);
708 g_free (file_name);
709 g_free (mc_extfsdir);
710 if (my_system (EXECUTE_AS_SHELL, shell, cmd))
711 errno_code = EIO;
712 g_free (cmd);
714 struct stat file_status;
715 if (stat(file->entry->inode->local_filename,&file_status) != 0)
716 errno_code = EIO;
717 else
718 file->entry->inode->size = file_status.st_size;
721 file->entry->inode->mtime = time (NULL);
724 file->archive->fd_usage--;
725 if (!file->archive->fd_usage) {
726 struct vfs_stamping *parent;
727 vfs *v;
729 if (!file->archive->name || !*file->archive->name || (v = vfs_type (file->archive->name)) == &vfs_local_ops) {
730 parent = NULL;
731 } else {
732 parent = g_new (struct vfs_stamping, 1);
733 parent->v = v;
734 parent->next = 0;
735 parent->id = (*v->getid) (v, file->archive->name, &(parent->parent));
737 vfs_add_noncurrent_stamps (&vfs_extfs_ops, (vfsid) (file->archive), parent);
738 vfs_rm_parents (parent);
741 g_free (data);
742 if (errno_code) ERRNOR (EIO, -1);
743 return 0;
746 #define RECORDSIZE 512
748 static char *get_path (char *inname, struct archive **archive, int is_dir,
749 int do_not_open)
751 char *buf = g_strdup (inname);
752 char *res = get_path_mangle( buf, archive, is_dir, do_not_open );
753 char *res2 = NULL;
754 if (res)
755 res2 = g_strdup(res);
756 g_free(buf);
757 return res2;
760 static struct entry*
761 __find_entry (struct entry *dir, char *name,
762 struct loop_protect *list, int make_dirs, int make_file)
764 struct entry *pent, *pdir;
765 char *p, *q, *name_end;
766 char c;
768 if (*name == '/') { /* Handle absolute paths */
769 name++;
770 dir = dir->inode->archive->root_entry;
773 pent = dir;
774 p = name;
775 name_end = name + strlen (name);
776 q = strchr (p, '/');
777 c = '/';
778 if (!q)
779 q = strchr (p, 0);
781 for (; pent != NULL && c && *p; ){
782 c = *q;
783 *q = 0;
785 if (strcmp (p, ".")){
786 if (!strcmp (p, ".."))
787 pent = pent->dir;
788 else {
789 if ((pent = __resolve_symlinks (pent, list))==NULL){
790 *q = c;
791 return NULL;
793 if (!S_ISDIR (pent->inode->mode)){
794 *q = c;
795 notadir = 1;
796 return NULL;
798 pdir = pent;
799 for (pent = pent->inode->first_in_subdir; pent; pent = pent->next_in_dir)
800 /* Hack: I keep the original semanthic unless
801 q+1 would break in the strchr */
802 if (!strcmp (pent->name, p)){
803 if (q + 1 > name_end){
804 *q = c;
805 notadir = !S_ISDIR (pent->inode->mode);
806 return pent;
808 break;
811 /* When we load archive, we create automagically
812 * non-existant directories
814 if (pent == NULL && make_dirs) {
815 pent = generate_entry (dir->inode->archive, p, pdir, S_IFDIR | 0777);
817 if (pent == NULL && make_file) {
818 pent = generate_entry (dir->inode->archive, p, pdir, 0777);
822 /* Next iteration */
823 *q = c;
824 p = q + 1;
825 q = strchr (p, '/');
826 if (!q)
827 q = strchr (p, 0);
829 if (pent == NULL)
830 my_errno = ENOENT;
831 return pent;
834 static struct entry *find_entry (struct entry *dir, char *name, int make_dirs, int make_file)
836 struct entry *res;
838 errloop = 0;
839 notadir = 0;
840 res = __find_entry (dir, name, NULL, make_dirs, make_file);
841 if (res == NULL) {
842 if (errloop)
843 my_errno = ELOOP;
844 else if (notadir)
845 my_errno = ENOTDIR;
847 return res;
851 static int s_errno (vfs *me)
853 return my_errno;
856 static void * s_opendir (vfs *me, char *dirname)
858 struct archive *archive;
859 char *q;
860 struct entry *entry;
861 struct entry **info;
863 if ((q = get_path_mangle (dirname, &archive, 1, 0)) == NULL)
864 return NULL;
865 entry = find_entry (archive->root_entry, q, 0, 0);
866 if (entry == NULL)
867 return NULL;
868 if ((entry = my_resolve_symlinks (entry)) == NULL)
869 return NULL;
870 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, NULL);
872 info = g_new (struct entry *, 2);
873 info[0] = entry->inode->first_in_subdir;
874 info[1] = entry->inode->first_in_subdir;
876 return info;
879 static void * s_readdir(void *data)
881 static union vfs_dirent dir;
882 struct entry **info = (struct entry **) data;
884 if (!*info)
885 return NULL;
887 strncpy(dir.dent.d_name, (*info)->name, MC_MAXPATHLEN);
888 dir.dent.d_name[MC_MAXPATHLEN] = 0;
890 compute_namelen(&dir.dent);
891 *info = (*info)->next_in_dir;
893 return (void *) &dir;
896 static int s_telldir (void *data)
898 struct entry **info = (struct entry **) data;
899 struct entry *cur;
900 int num = 0;
902 cur = info[1];
903 while (cur!=NULL) {
904 if (cur == info[0]) return num;
905 num++;
906 cur = cur->next_in_dir;
908 return -1;
911 static void s_seekdir (void *data, int offset)
913 struct entry **info = (struct entry **) data;
914 int i;
915 info[0] = info[1];
916 for (i=0; i<offset; i++)
917 s_readdir( data );
920 static int s_closedir (void *data)
922 g_free (data);
923 return 0;
926 static void stat_move( struct stat *buf, struct inode *inode )
928 buf->st_dev = inode->dev;
929 buf->st_ino = inode->inode;
930 buf->st_mode = inode->mode;
931 buf->st_nlink = inode->nlink;
932 buf->st_uid = inode->uid;
933 buf->st_gid = inode->gid;
934 #ifdef HAVE_ST_RDEV
935 buf->st_rdev = inode->rdev;
936 #endif
937 buf->st_size = inode->size;
938 #ifdef HAVE_ST_BLKSIZE
939 buf->st_blksize = RECORDSIZE;
940 #endif
941 #ifdef HAVE_ST_BLOCKS
942 buf->st_blocks = (inode->size + RECORDSIZE - 1) / RECORDSIZE;
943 #endif
944 buf->st_atime = inode->atime;
945 buf->st_mtime = inode->mtime;
946 buf->st_ctime = inode->ctime;
949 static int s_internal_stat (char *path, struct stat *buf, int resolve)
951 struct archive *archive;
952 char *q;
953 struct entry *entry;
954 struct inode *inode;
956 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
957 return -1;
958 entry = find_entry (archive->root_entry, q, 0, 0);
959 if (entry == NULL)
960 return -1;
961 if (resolve && (entry = my_resolve_symlinks (entry)) == NULL)
962 return -1;
963 inode = entry->inode;
964 stat_move( buf, inode );
965 return 0;
968 static int s_stat (vfs *me, char *path, struct stat *buf)
970 return s_internal_stat (path, buf, 1);
973 static int s_lstat (vfs *me, char *path, struct stat *buf)
975 return s_internal_stat (path, buf, 0);
978 static int s_fstat (void *data, struct stat *buf)
980 struct pseudofile *file = (struct pseudofile *)data;
981 struct inode *inode;
983 inode = file->entry->inode;
984 stat_move( buf, inode );
985 return 0;
988 static int s_readlink (vfs *me, char *path, char *buf, int size)
990 struct archive *archive;
991 char *q;
992 int i;
993 struct entry *entry;
995 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
996 return -1;
997 entry = find_entry (archive->root_entry, q, 0, 0);
998 if (entry == NULL)
999 return -1;
1000 if (!S_ISLNK (entry->inode->mode)) ERRNOR (EINVAL, -1);
1001 if (size > (i = strlen (entry->inode->linkname))) {
1002 size = i;
1004 strncpy (buf, entry->inode->linkname, i);
1005 return i;
1008 static int extfs_chmod (vfs *me, char *path, int mode)
1010 return 0;
1013 static int extfs_write (void *data, char *buf, int nbyte)
1015 struct pseudofile *file = (struct pseudofile *)data;
1017 file->has_changed = 1;
1018 return write (file->local_handle, buf, nbyte);
1021 static int extfs_unlink (vfs *me, char *file)
1023 struct archive *archive;
1024 char *q;
1025 char *mc_extfsdir;
1026 struct entry *entry;
1027 char *cmd;
1028 char *archive_name, *p;
1030 if ((q = get_path_mangle (file, &archive, 0, 0)) == NULL)
1031 return -1;
1032 entry = find_entry (archive->root_entry, q, 0, 0);
1033 if (entry == NULL)
1034 return -1;
1035 if ((entry = my_resolve_symlinks (entry)) == NULL)
1036 return -1;
1037 if (S_ISDIR (entry->inode->mode)) ERRNOR (EISDIR, -1);
1039 p = get_path_from_entry (entry);
1040 q = name_quote (p, 0);
1041 g_free (p);
1042 archive_name = name_quote (get_archive_name (archive), 0);
1044 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1045 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1046 " rm ", archive_name, " ", q, NULL);
1047 g_free (q);
1048 g_free (mc_extfsdir);
1049 g_free (archive_name);
1050 if (my_system (EXECUTE_AS_SHELL, shell, cmd)){
1051 g_free (cmd);
1052 my_errno = EIO;
1053 return -1;
1055 g_free (cmd);
1056 remove_entry (entry);
1058 return 0;
1061 static int extfs_mkdir (vfs *me, char *path, mode_t mode)
1063 struct archive *archive;
1064 char *q;
1065 char *mc_extfsdir;
1066 struct entry *entry;
1067 char *cmd;
1068 char *archive_name, *p;
1070 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
1071 return -1;
1072 entry = find_entry (archive->root_entry, q, 0, 0);
1073 if (entry != NULL) ERRNOR (EEXIST, -1);
1074 entry = find_entry (archive->root_entry, q, 1, 0);
1075 if (entry == NULL)
1076 return -1;
1077 if ((entry = my_resolve_symlinks (entry)) == NULL)
1078 return -1;
1079 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, -1);
1081 p = get_path_from_entry (entry);
1082 q = name_quote (p, 0);
1083 g_free (p);
1084 archive_name = name_quote (get_archive_name (archive), 0);
1086 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1087 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1088 " mkdir ", archive_name, " ", q, NULL);
1089 g_free (q);
1090 g_free (mc_extfsdir);
1091 g_free (archive_name);
1092 if (my_system (EXECUTE_AS_SHELL, shell, cmd)){
1093 g_free (cmd);
1094 my_errno = EIO;
1095 remove_entry (entry);
1096 return -1;
1098 g_free (cmd);
1100 return 0;
1103 static int extfs_rmdir (vfs *me, char *path)
1105 struct archive *archive;
1106 char *q;
1107 char *mc_extfsdir;
1108 struct entry *entry;
1109 char *cmd;
1110 char *archive_name, *p;
1112 if ((q = get_path_mangle (path, &archive, 0, 0)) == NULL)
1113 return -1;
1114 entry = find_entry (archive->root_entry, q, 0, 0);
1115 if (entry == NULL)
1116 return -1;
1117 if ((entry = my_resolve_symlinks (entry)) == NULL)
1118 return -1;
1119 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, -1);
1121 p = get_path_from_entry (entry);
1122 q = name_quote (p, 0);
1123 g_free (p);
1124 archive_name = name_quote (get_archive_name (archive), 0);
1126 mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
1127 cmd = g_strconcat (mc_extfsdir, extfs_prefixes [archive->fstype],
1128 " rmdir ", archive_name, " ", q, NULL);
1129 g_free (q);
1130 g_free (mc_extfsdir);
1131 g_free (archive_name);
1132 if (my_system (EXECUTE_AS_SHELL, shell, cmd)){
1133 g_free (cmd);
1134 my_errno = EIO;
1135 return -1;
1137 g_free (cmd);
1138 remove_entry (entry);
1140 return 0;
1143 static int extfs_chdir (vfs *me, char *path)
1145 struct archive *archive;
1146 char *q;
1147 struct entry *entry;
1149 my_errno = ENOTDIR;
1150 if ((q = get_path_mangle (path, &archive, 1, 0)) == NULL)
1151 return -1;
1152 entry = find_entry (archive->root_entry, q, 0, 0);
1153 if (!entry)
1154 return -1;
1155 entry = my_resolve_symlinks (entry);
1156 if ((!entry) || (!S_ISDIR (entry->inode->mode)))
1157 return -1;
1158 entry->inode->archive->current_dir = entry;
1159 my_errno = 0;
1160 return 0;
1163 static int extfs_lseek (void *data, off_t offset, int whence)
1165 struct pseudofile *file = (struct pseudofile *) data;
1167 return lseek (file->local_handle, offset, whence);
1170 static vfsid extfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
1172 struct archive *archive;
1173 vfs *v;
1174 vfsid id;
1175 struct vfs_stamping *par;
1176 char *p;
1178 *parent = NULL;
1179 if (!(p = get_path (path, &archive, 1, 1)))
1180 return (vfsid) -1;
1181 g_free(p);
1182 if (archive->name){
1183 v = vfs_type (archive->name);
1184 id = (*v->getid) (v, archive->name, &par);
1185 if (id != (vfsid)-1) {
1186 *parent = g_new (struct vfs_stamping, 1);
1187 (*parent)->v = v;
1188 (*parent)->id = id;
1189 (*parent)->parent = par;
1190 (*parent)->next = NULL;
1193 return (vfsid) archive;
1196 static int extfs_nothingisopen (vfsid id)
1198 if (((struct archive *)id)->fd_usage <= 0)
1199 return 1;
1200 return 0;
1203 static void remove_entry (struct entry *e)
1205 int i = --(e->inode->nlink);
1206 struct entry *pe, *ent, *prev;
1208 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1209 struct entry *f = e->inode->first_in_subdir;
1210 e->inode->first_in_subdir = NULL;
1211 remove_entry (f);
1213 pe = e->dir;
1214 if (e == pe->inode->first_in_subdir)
1215 pe->inode->first_in_subdir = e->next_in_dir;
1217 prev = NULL;
1218 for (ent = pe->inode->first_in_subdir; ent && ent->next_in_dir;
1219 ent = ent->next_in_dir)
1220 if (e == ent->next_in_dir) {
1221 prev = ent;
1222 break;
1224 if (prev)
1225 prev->next_in_dir = e->next_in_dir;
1226 if (e == pe->inode->last_in_subdir)
1227 pe->inode->last_in_subdir = prev;
1229 if (i <= 0) {
1230 if (e->inode->local_filename != NULL) {
1231 unlink (e->inode->local_filename);
1232 free (e->inode->local_filename);
1234 if (e->inode->linkname != NULL)
1235 g_free (e->inode->linkname);
1236 g_free (e->inode);
1239 g_free (e->name);
1240 g_free (e);
1243 static void free_entry (struct entry *e)
1245 int i = --(e->inode->nlink);
1246 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1247 struct entry *f = e->inode->first_in_subdir;
1249 e->inode->first_in_subdir = NULL;
1250 free_entry (f);
1252 if (i <= 0) {
1253 if (e->inode->local_filename != NULL) {
1254 unlink (e->inode->local_filename);
1255 free (e->inode->local_filename);
1257 if (e->inode->linkname != NULL)
1258 g_free (e->inode->linkname);
1259 g_free (e->inode);
1261 if (e->next_in_dir != NULL)
1262 free_entry (e->next_in_dir);
1263 g_free (e->name);
1264 g_free (e);
1267 static void extfs_free (vfsid id)
1269 struct archive *parc;
1270 struct archive *archive = (struct archive *)id;
1272 free_entry (archive->root_entry);
1273 if (archive == first_archive) {
1274 first_archive = archive->next;
1275 } else {
1276 for (parc = first_archive; parc != NULL; parc = parc->next)
1277 if (parc->next == archive)
1278 break;
1279 if (parc != NULL)
1280 parc->next = archive->next;
1282 free_archive (archive);
1285 static char *extfs_getlocalcopy (vfs *me, char *path)
1287 struct pseudofile *fp =
1288 (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
1289 char *p;
1291 if (fp == NULL)
1292 return NULL;
1293 if (fp->entry->inode->local_filename == NULL) {
1294 extfs_close ((void *) fp);
1295 return NULL;
1297 p = g_strdup (fp->entry->inode->local_filename);
1298 fp->archive->fd_usage++;
1299 extfs_close ((void *) fp);
1300 return p;
1303 static int extfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
1305 struct pseudofile *fp =
1306 (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
1308 if (fp == NULL)
1309 return 0;
1310 if (!strcmp (fp->entry->inode->local_filename, local)) {
1311 fp->archive->fd_usage--;
1312 fp->has_changed |= has_changed;
1313 extfs_close ((void *) fp);
1314 return 0;
1315 } else {
1316 /* Should not happen */
1317 extfs_close ((void *) fp);
1318 return mc_def_ungetlocalcopy (me, path, local, has_changed);
1323 #include "../src/profile.h"
1324 static int extfs_init (vfs *me)
1326 FILE *cfg;
1327 char *mc_extfsini;
1329 mc_extfsini = concat_dir_and_file (mc_home, "extfs/extfs.ini");
1330 cfg = fopen (mc_extfsini, "r");
1332 if (!cfg) {
1333 fprintf(stderr, _("Warning: file %s not found\n"), mc_extfsini);
1334 g_free (mc_extfsini);
1335 return 0;
1338 extfs_no = 0;
1339 while ( extfs_no < MAXEXTFS ) {
1340 char key[256];
1341 char *c;
1343 if (!fgets( key, sizeof (key)-1, cfg ))
1344 break;
1346 /* Handle those with a trailing ':', those flag that the
1347 * file system does not require an archive to work
1350 if (*key == '[') {
1351 /* We may not use vfs_die() message or message_1s or similar,
1352 * UI is not initialized at this time and message would not
1353 * appear on screen. */
1354 fprintf(stderr, "Warning: You need to update your %s file.\n",
1355 mc_extfsini);
1356 fclose(cfg);
1357 g_free (mc_extfsini);
1358 return 0;
1360 if (*key == '#')
1361 continue;
1363 if ((c = strchr (key, '\n'))){
1364 *c = 0;
1365 c = &key [strlen (key) - 1];
1366 } else {
1367 c = key;
1369 extfs_need_archive [extfs_no] = !(*c == ':');
1370 if (*c == ':')
1371 *c = 0;
1372 if (!(*key))
1373 continue;
1375 extfs_prefixes [extfs_no] = g_strdup (key);
1376 extfs_no++;
1378 fclose(cfg);
1379 g_free (mc_extfsini);
1380 return 1;
1383 /* Do NOT use me argument in this function */
1384 static int extfs_which (vfs *me, char *path)
1386 int i;
1388 for (i = 0; i < extfs_no; i++)
1389 if (!strcmp (path, extfs_prefixes [i]))
1390 return i;
1391 return -1;
1394 static void extfs_done (vfs *me)
1396 int i;
1398 for (i = 0; i < extfs_no; i++ )
1399 g_free (extfs_prefixes [i]);
1400 extfs_no = 0;
1403 static int extfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1405 if (ctlop == MCCTL_EXTFS_RUN) {
1406 extfs_run (path);
1407 return 1;
1409 return 0;
1412 vfs vfs_extfs_ops = {
1413 NULL, /* This is place of next pointer */
1414 "extfs",
1415 F_EXEC, /* flags */
1416 NULL, /* prefix */
1417 NULL, /* data */
1418 0, /* errno */
1419 extfs_init,
1420 extfs_done,
1421 extfs_fill_names,
1422 extfs_which,
1424 extfs_open,
1425 extfs_close,
1426 extfs_read,
1427 extfs_write,
1429 s_opendir,
1430 s_readdir,
1431 s_closedir,
1432 s_telldir,
1433 s_seekdir,
1435 s_stat,
1436 s_lstat,
1437 s_fstat,
1439 extfs_chmod, /* chmod ... strange, returns success? */
1440 NULL,
1441 NULL,
1443 s_readlink,
1445 NULL, /* symlink */
1446 NULL,
1447 extfs_unlink,
1449 NULL,
1450 extfs_chdir,
1451 s_errno,
1452 extfs_lseek,
1453 NULL,
1455 extfs_getid,
1456 extfs_nothingisopen,
1457 extfs_free,
1459 extfs_getlocalcopy,
1460 extfs_ungetlocalcopy,
1462 extfs_mkdir, /* mkdir */
1463 extfs_rmdir,
1464 NULL,
1465 extfs_setctl
1467 MMAPNULL