First bunch of mhl_mem_free removal patches
[midnight-commander.git] / vfs / extfs.c
blobf966b82807f463965a4f9d8e9526a9368fe930cb
1 /* Virtual File System: External file system.
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 Written by: 1995 Jakub Jelinek
6 Rewritten by: 1998 Pavel Machek
7 Additional changes by: 1999 Andrew T. Veliath
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Namespace: init_extfs */
25 #include <config.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h>
36 #endif
37 #include <errno.h>
39 #include <mhl/memory.h>
40 #include <mhl/string.h>
42 #include "../src/global.h"
43 #include "../src/tty.h" /* enable/disable interrupt key */
44 #include "../src/wtools.h" /* message() */
45 #include "../src/main.h" /* print_vfs_message */
46 #include "utilvfs.h"
47 #include "../src/execute.h" /* For shell_execute */
48 #include "vfs.h"
49 #include "vfs-impl.h"
50 #include "gc.h" /* vfs_rmstamp */
52 #undef ERRNOR
53 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
55 struct inode {
56 nlink_t nlink;
57 struct entry *first_in_subdir; /* only used if this is a directory */
58 struct entry *last_in_subdir;
59 ino_t inode; /* This is inode # */
60 dev_t dev; /* This is an internal identification of the extfs archive */
61 struct archive *archive; /* And this is an archive structure */
62 dev_t rdev;
63 mode_t mode;
64 uid_t uid;
65 gid_t gid;
66 off_t size;
67 time_t mtime;
68 char *linkname;
69 time_t atime;
70 time_t ctime;
71 char *local_filename;
74 struct entry {
75 struct entry *next_in_dir;
76 struct entry *dir;
77 char *name;
78 struct inode *inode;
81 struct pseudofile {
82 struct archive *archive;
83 unsigned int has_changed:1;
84 int local_handle;
85 struct entry *entry;
88 struct archive {
89 int fstype;
90 char *name;
91 char *local_name;
92 struct stat local_stat;
93 dev_t rdev;
94 int fd_usage;
95 ino_t inode_counter;
96 struct entry *root_entry;
97 struct archive *next;
100 static struct entry *extfs_find_entry (struct entry *dir, char *name,
101 int make_dirs, int make_file);
102 static int extfs_which (struct vfs_class *me, const char *path);
103 static void extfs_remove_entry (struct entry *e);
104 static void extfs_free (vfsid id);
105 static void extfs_free_entry (struct entry *e);
107 static struct vfs_class vfs_extfs_ops;
108 static struct archive *first_archive = NULL;
109 static int my_errno = 0;
111 #define MAXEXTFS 32
112 static char *extfs_prefixes [MAXEXTFS];
113 static char extfs_need_archive [MAXEXTFS];
114 static int extfs_no = 0;
116 static void
117 extfs_fill_names (struct vfs_class *me, fill_names_f func)
119 struct archive *a = first_archive;
120 char *name;
122 (void) me;
124 while (a) {
125 name =
126 g_strconcat (a->name ? a->name : "", "#",
127 extfs_prefixes[a->fstype], (char *) NULL);
128 (*func) (name);
129 g_free (name);
130 a = a->next;
134 static void extfs_make_dots (struct entry *ent)
136 struct entry *entry = g_new (struct entry, 1);
137 struct entry *parentry = ent->dir;
138 struct inode *inode = ent->inode, *parent;
140 parent = (parentry != NULL) ? parentry->inode : NULL;
141 entry->name = mhl_str_dup (".");
142 entry->inode = inode;
143 entry->dir = ent;
144 inode->local_filename = NULL;
145 inode->first_in_subdir = entry;
146 inode->nlink++;
147 entry->next_in_dir = g_new (struct entry, 1);
148 entry = entry->next_in_dir;
149 entry->name = mhl_str_dup ("..");
150 inode->last_in_subdir = entry;
151 entry->next_in_dir = NULL;
152 if (parent != NULL) {
153 entry->inode = parent;
154 entry->dir = parentry;
155 parent->nlink++;
156 } else {
157 entry->inode = inode;
158 entry->dir = ent;
159 inode->nlink++;
163 static struct entry *extfs_generate_entry (struct archive *archive,
164 const char *name, struct entry *parentry, mode_t mode)
166 mode_t myumask;
167 struct inode *inode, *parent;
168 struct entry *entry;
170 parent = (parentry != NULL) ? parentry->inode : NULL;
171 entry = g_new (struct entry, 1);
173 entry->name = mhl_str_dup (name);
174 entry->next_in_dir = NULL;
175 entry->dir = parentry;
176 if (parent != NULL) {
177 parent->last_in_subdir->next_in_dir = entry;
178 parent->last_in_subdir = entry;
180 inode = g_new (struct inode, 1);
181 entry->inode = inode;
182 inode->local_filename = NULL;
183 inode->linkname = NULL;
184 inode->last_in_subdir = NULL;
185 inode->inode = (archive->inode_counter)++;
186 inode->dev = archive->rdev;
187 inode->archive = archive;
188 myumask = umask (022);
189 umask (myumask);
190 inode->mode = mode & ~myumask;
191 mode = inode->mode;
192 inode->rdev = 0;
193 inode->uid = getuid ();
194 inode->gid = getgid ();
195 inode->size = 0;
196 inode->mtime = time (NULL);
197 inode->atime = inode->mtime;
198 inode->ctime = inode->mtime;
199 inode->nlink = 1;
200 if (S_ISDIR (mode))
201 extfs_make_dots (entry);
202 return entry;
205 #if 0
206 static void extfs_free_entries (struct entry *entry)
208 (void) entry;
209 return;
211 #endif
213 static void extfs_free_archive (struct archive *archive)
215 extfs_free_entry (archive->root_entry);
216 if (archive->local_name != NULL) {
217 struct stat my;
219 mc_stat (archive->local_name, &my);
220 mc_ungetlocalcopy (archive->name, archive->local_name,
221 archive->local_stat.st_mtime != my.st_mtime);
222 g_free(archive->local_name);
224 g_free (archive->name);
225 g_free (archive);
228 static FILE *
229 extfs_open_archive (int fstype, const char *name, struct archive **pparc)
231 static dev_t archive_counter = 0;
232 FILE *result;
233 mode_t mode;
234 char *cmd;
235 char *mc_extfsdir;
236 struct stat mystat;
237 struct archive *current_archive;
238 struct entry *root_entry;
239 char *local_name = NULL, *tmp = 0;
240 int uses_archive = extfs_need_archive[fstype];
242 if (uses_archive) {
243 if (mc_stat (name, &mystat) == -1)
244 return NULL;
245 if (!vfs_file_is_local (name)) {
246 local_name = mc_getlocalcopy (name);
247 if (local_name == NULL)
248 return NULL;
250 tmp = name_quote (name, 0);
253 mc_extfsdir = mhl_str_dir_plus_file (mc_home, "extfs" PATH_SEP_STR);
254 cmd =
255 g_strconcat (mc_extfsdir, extfs_prefixes[fstype], " list ",
256 local_name ? local_name : tmp, (char *) NULL);
257 g_free (tmp);
258 g_free (mc_extfsdir);
259 open_error_pipe ();
260 result = popen (cmd, "r");
261 g_free (cmd);
262 if (result == NULL) {
263 close_error_pipe (D_ERROR, NULL);
264 if (local_name) {
265 mc_ungetlocalcopy (name, local_name, 0);
266 g_free(local_name);
268 return NULL;
270 #ifdef ___QNXNTO__
271 setvbuf (result, NULL, _IONBF, 0);
272 #endif
274 current_archive = g_new (struct archive, 1);
275 current_archive->fstype = fstype;
276 current_archive->name = name ? mhl_str_dup (name) : NULL;
277 current_archive->local_name = local_name;
279 if (local_name != NULL)
280 mc_stat (local_name, &current_archive->local_stat);
281 current_archive->inode_counter = 0;
282 current_archive->fd_usage = 0;
283 current_archive->rdev = archive_counter++;
284 current_archive->next = first_archive;
285 first_archive = current_archive;
286 mode = mystat.st_mode & 07777;
287 if (mode & 0400)
288 mode |= 0100;
289 if (mode & 0040)
290 mode |= 0010;
291 if (mode & 0004)
292 mode |= 0001;
293 mode |= S_IFDIR;
294 root_entry = extfs_generate_entry (current_archive, "/", NULL, mode);
295 root_entry->inode->uid = mystat.st_uid;
296 root_entry->inode->gid = mystat.st_gid;
297 root_entry->inode->atime = mystat.st_atime;
298 root_entry->inode->ctime = mystat.st_ctime;
299 root_entry->inode->mtime = mystat.st_mtime;
300 current_archive->root_entry = root_entry;
302 *pparc = current_archive;
304 return result;
308 * Main loop for reading an archive.
309 * Return 0 on success, -1 on error.
311 static int
312 extfs_read_archive (int fstype, const char *name, struct archive **pparc)
314 FILE *extfsd;
315 char *buffer;
316 struct archive *current_archive;
317 char *current_file_name, *current_link_name;
319 if ((extfsd =
320 extfs_open_archive (fstype, name, &current_archive)) == NULL) {
321 message (D_ERROR, MSG_ERROR, _("Cannot open %s archive\n%s"),
322 extfs_prefixes[fstype], name);
323 return -1;
326 buffer = g_malloc (4096);
327 while (fgets (buffer, 4096, extfsd) != NULL) {
328 struct stat hstat;
330 current_link_name = NULL;
331 if (vfs_parse_ls_lga
332 (buffer, &hstat, &current_file_name, &current_link_name)) {
333 struct entry *entry, *pent;
334 struct inode *inode;
335 char *p, *q, *cfn = current_file_name;
337 if (*cfn) {
338 if (*cfn == '/')
339 cfn++;
340 p = strchr (cfn, 0);
341 if (p != cfn && *(p - 1) == '/')
342 *(p - 1) = 0;
343 p = strrchr (cfn, '/');
344 if (p == NULL) {
345 p = cfn;
346 q = strchr (cfn, 0);
347 } else {
348 *(p++) = 0;
349 q = cfn;
351 if (S_ISDIR (hstat.st_mode)
352 && (!strcmp (p, ".") || !strcmp (p, "..")))
353 goto read_extfs_continue;
354 pent =
355 extfs_find_entry (current_archive->root_entry, q, 1,
357 if (pent == NULL) {
358 /* FIXME: Should clean everything one day */
359 g_free (buffer);
360 pclose (extfsd);
361 close_error_pipe (D_ERROR, _("Inconsistent extfs archive"));
362 return -1;
364 entry = g_new (struct entry, 1);
365 entry->name = mhl_str_dup (p);
366 entry->next_in_dir = NULL;
367 entry->dir = pent;
368 if (pent->inode->last_in_subdir) {
369 pent->inode->last_in_subdir->next_in_dir = entry;
370 pent->inode->last_in_subdir = entry;
372 if (!S_ISLNK (hstat.st_mode) && current_link_name != NULL) {
373 pent =
374 extfs_find_entry (current_archive->root_entry,
375 current_link_name, 0, 0);
376 if (pent == NULL) {
377 /* FIXME: Should clean everything one day */
378 g_free (buffer);
379 pclose (extfsd);
380 close_error_pipe (D_ERROR,
381 _("Inconsistent extfs archive"));
382 return -1;
383 } else {
384 entry->inode = pent->inode;
385 pent->inode->nlink++;
387 } else {
388 inode = g_new (struct inode, 1);
389 entry->inode = inode;
390 inode->local_filename = NULL;
391 inode->inode = (current_archive->inode_counter)++;
392 inode->nlink = 1;
393 inode->dev = current_archive->rdev;
394 inode->archive = current_archive;
395 inode->mode = hstat.st_mode;
396 #ifdef HAVE_STRUCT_STAT_ST_RDEV
397 inode->rdev = hstat.st_rdev;
398 #else
399 inode->rdev = 0;
400 #endif
401 inode->uid = hstat.st_uid;
402 inode->gid = hstat.st_gid;
403 inode->size = hstat.st_size;
404 inode->mtime = hstat.st_mtime;
405 inode->atime = hstat.st_atime;
406 inode->ctime = hstat.st_ctime;
407 inode->first_in_subdir = NULL;
408 inode->last_in_subdir = NULL;
409 if (current_link_name != NULL
410 && S_ISLNK (hstat.st_mode)) {
411 inode->linkname = current_link_name;
412 current_link_name = NULL;
413 } else {
414 if (S_ISLNK (hstat.st_mode))
415 inode->mode &= ~S_IFLNK; /* You *DON'T* want to do this always */
416 inode->linkname = NULL;
418 if (S_ISDIR (hstat.st_mode))
419 extfs_make_dots (entry);
422 read_extfs_continue:
423 g_free (current_file_name);
424 g_free (current_link_name);
427 g_free (buffer);
429 /* Check if extfs 'list' returned 0 */
430 if (pclose (extfsd) != 0) {
431 extfs_free (current_archive);
432 close_error_pipe (D_ERROR, _("Inconsistent extfs archive"));
433 return -1;
436 close_error_pipe (D_ERROR, NULL);
437 *pparc = current_archive;
438 return 0;
442 * Dissect the path and create corresponding superblock. Note that inname
443 * can be changed and the result may point inside the original string.
445 static char *
446 extfs_get_path_mangle (struct vfs_class *me, char *inname, struct archive **archive,
447 int do_not_open)
449 char *local, *op;
450 const char *archive_name;
451 int result = -1;
452 struct archive *parc;
453 int fstype;
455 archive_name = inname;
456 vfs_split (inname, &local, &op);
458 fstype = extfs_which (me, op);
460 if (fstype == -1)
461 return NULL;
462 if (!local)
463 local = inname + strlen (inname);
466 * All filesystems should have some local archive, at least
467 * it can be '/'.
469 for (parc = first_archive; parc != NULL; parc = parc->next)
470 if (parc->name) {
471 if (!strcmp (parc->name, archive_name)) {
472 vfs_stamp (&vfs_extfs_ops, (vfsid) parc);
473 goto return_success;
477 result =
478 do_not_open ? -1 : extfs_read_archive (fstype, archive_name,
479 &parc);
480 if (result == -1)
481 ERRNOR (EIO, NULL);
483 return_success:
484 *archive = parc;
485 return local;
490 * Dissect the path and create corresponding superblock.
491 * The result should be freed.
493 static char *
494 extfs_get_path (struct vfs_class *me, const char *inname, struct archive **archive,
495 int do_not_open)
497 char *buf = mhl_str_dup (inname);
498 char *res = extfs_get_path_mangle (me, buf, archive, do_not_open);
499 char *res2 = NULL;
500 if (res)
501 res2 = mhl_str_dup (res);
502 g_free (buf);
503 return res2;
507 /* Return allocated path (without leading slash) inside the archive */
508 static char *extfs_get_path_from_entry (struct entry *entry)
510 struct list {
511 struct list *next;
512 char *name;
513 } *head, *p;
514 char *localpath;
515 size_t len;
517 for (len = 0, head = 0; entry->dir; entry = entry->dir) {
518 p = g_new (struct list, 1);
519 p->next = head;
520 p->name = entry->name;
521 head = p;
522 len += strlen (entry->name) + 1;
525 if (len == 0)
526 return mhl_str_dup ("");
528 localpath = g_malloc (len);
529 *localpath = '\0';
530 while (head) {
531 strcat (localpath, head->name);
532 if (head->next)
533 strcat (localpath, "/");
534 p = head;
535 head = head->next;
536 g_free (p);
538 return (localpath);
542 struct loop_protect {
543 struct entry *entry;
544 struct loop_protect *next;
546 static int errloop;
547 static int notadir;
549 static struct entry *
550 extfs_find_entry_int (struct entry *dir, char *name,
551 struct loop_protect *list, int make_dirs, int make_file);
553 static struct entry *
554 extfs_resolve_symlinks_int (struct entry *entry,
555 struct loop_protect *list)
557 struct entry *pent;
558 struct loop_protect *looping;
560 if (!S_ISLNK (entry->inode->mode))
561 return entry;
562 for (looping = list; looping != NULL; looping = looping->next)
563 if (entry == looping->entry) { /* Here we protect us against symlink looping */
564 errloop = 1;
565 return NULL;
567 looping = g_new (struct loop_protect, 1);
568 looping->entry = entry;
569 looping->next = list;
570 pent = extfs_find_entry_int (entry->dir, entry->inode->linkname, looping, 0, 0);
571 g_free (looping);
572 if (pent == NULL)
573 my_errno = ENOENT;
574 return pent;
577 static struct entry *extfs_resolve_symlinks (struct entry *entry)
579 struct entry *res;
581 errloop = 0;
582 notadir = 0;
583 res = extfs_resolve_symlinks_int (entry, NULL);
584 if (res == NULL) {
585 if (errloop)
586 my_errno = ELOOP;
587 else if (notadir)
588 my_errno = ENOTDIR;
590 return res;
593 static const char *
594 extfs_get_archive_name (struct archive *archive)
596 const char *archive_name;
598 if (archive->local_name)
599 archive_name = archive->local_name;
600 else
601 archive_name = archive->name;
603 if (!archive_name || !*archive_name)
604 return "no_archive_name";
605 else
606 return archive_name;
609 /* Don't pass localname as NULL */
610 static int
611 extfs_cmd (const char *extfs_cmd, struct archive *archive,
612 struct entry *entry, const char *localname)
614 char *file;
615 char *quoted_file;
616 char *quoted_localname;
617 char *archive_name;
618 char *mc_extfsdir;
619 char *cmd;
620 int retval;
622 file = extfs_get_path_from_entry (entry);
623 quoted_file = name_quote (file, 0);
624 g_free (file);
625 archive_name = name_quote (extfs_get_archive_name (archive), 0);
626 quoted_localname = name_quote (localname, 0);
628 mc_extfsdir = mhl_str_dir_plus_file (mc_home, "extfs" PATH_SEP_STR);
629 cmd = g_strconcat (mc_extfsdir, extfs_prefixes[archive->fstype],
630 extfs_cmd, archive_name, " ", quoted_file, " ",
631 quoted_localname, (char *) NULL);
632 g_free (quoted_file);
633 g_free (quoted_localname);
634 g_free (mc_extfsdir);
635 g_free (archive_name);
637 open_error_pipe ();
638 retval = my_system (EXECUTE_AS_SHELL, shell, cmd);
639 g_free (cmd);
640 close_error_pipe (D_ERROR, NULL);
641 return retval;
644 static void
645 extfs_run (struct vfs_class *me, const char *file)
647 struct archive *archive = NULL;
648 char *p, *q, *archive_name, *mc_extfsdir;
649 char *cmd;
651 if ((p = extfs_get_path (me, file, &archive, 0)) == NULL)
652 return;
653 q = name_quote (p, 0);
654 g_free (p);
656 archive_name = name_quote (extfs_get_archive_name (archive), 0);
657 mc_extfsdir = mhl_str_dir_plus_file (mc_home, "extfs" PATH_SEP_STR);
658 cmd = g_strconcat (mc_extfsdir, extfs_prefixes[archive->fstype],
659 " run ", archive_name, " ", q, (char *) NULL);
660 g_free (mc_extfsdir);
661 g_free (archive_name);
662 g_free (q);
663 shell_execute (cmd, 0);
664 g_free (cmd);
667 static void *
668 extfs_open (struct vfs_class *me, const char *file, int flags, int mode)
670 struct pseudofile *extfs_info;
671 struct archive *archive = NULL;
672 char *q;
673 struct entry *entry;
674 int local_handle;
675 int created = 0;
677 if ((q = extfs_get_path (me, file, &archive, 0)) == NULL)
678 return NULL;
679 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
680 if (entry == NULL && (flags & O_CREAT)) {
681 /* Create new entry */
682 entry = extfs_find_entry (archive->root_entry, q, 0, 1);
683 created = (entry != NULL);
686 g_free (q);
687 if (entry == NULL)
688 return NULL;
689 if ((entry = extfs_resolve_symlinks (entry)) == NULL)
690 return NULL;
692 if (S_ISDIR (entry->inode->mode))
693 ERRNOR (EISDIR, NULL);
695 if (entry->inode->local_filename == NULL) {
696 char *local_filename;
698 local_handle = vfs_mkstemps (&local_filename, "extfs", entry->name);
700 if (local_handle == -1)
701 return NULL;
702 close (local_handle);
704 if (!created && !(flags & O_TRUNC)
705 && extfs_cmd (" copyout ", archive, entry, local_filename)) {
706 unlink (local_filename);
707 free (local_filename);
708 my_errno = EIO;
709 return NULL;
711 entry->inode->local_filename = local_filename;
714 local_handle =
715 open (entry->inode->local_filename, NO_LINEAR (flags), mode);
716 if (local_handle == -1)
717 ERRNOR (EIO, NULL);
719 extfs_info = g_new (struct pseudofile, 1);
720 extfs_info->archive = archive;
721 extfs_info->entry = entry;
722 extfs_info->has_changed = created;
723 extfs_info->local_handle = local_handle;
725 /* i.e. we had no open files and now we have one */
726 vfs_rmstamp (&vfs_extfs_ops, (vfsid) archive);
727 archive->fd_usage++;
728 return extfs_info;
731 static ssize_t extfs_read (void *data, char *buffer, int count)
733 struct pseudofile *file = (struct pseudofile *)data;
735 return read (file->local_handle, buffer, count);
738 static int
739 extfs_close (void *data)
741 struct pseudofile *file;
742 int errno_code = 0;
743 file = (struct pseudofile *) data;
745 close (file->local_handle);
747 /* Commit the file if it has changed */
748 if (file->has_changed) {
749 if (extfs_cmd
750 (" copyin ", file->archive, file->entry,
751 file->entry->inode->local_filename))
752 errno_code = EIO;
754 struct stat file_status;
755 if (stat (file->entry->inode->local_filename, &file_status) !=
757 errno_code = EIO;
758 else
759 file->entry->inode->size = file_status.st_size;
762 file->entry->inode->mtime = time (NULL);
765 file->archive->fd_usage--;
766 if (!file->archive->fd_usage)
767 vfs_stamp_create (&vfs_extfs_ops, file->archive);
769 g_free (data);
770 if (errno_code)
771 ERRNOR (EIO, -1);
772 return 0;
775 #define RECORDSIZE 512
777 static struct entry*
778 extfs_find_entry_int (struct entry *dir, char *name,
779 struct loop_protect *list, int make_dirs, int make_file)
781 struct entry *pent, *pdir;
782 char *p, *q, *name_end;
783 char c;
785 if (*name == '/') { /* Handle absolute paths */
786 name++;
787 dir = dir->inode->archive->root_entry;
790 pent = dir;
791 p = name;
792 name_end = name + strlen (name);
793 q = strchr (p, '/');
794 c = '/';
795 if (!q)
796 q = strchr (p, 0);
798 for (; pent != NULL && c && *p; ){
799 c = *q;
800 *q = 0;
802 if (strcmp (p, ".")){
803 if (!strcmp (p, ".."))
804 pent = pent->dir;
805 else {
806 if ((pent = extfs_resolve_symlinks_int (pent, list))==NULL){
807 *q = c;
808 return NULL;
810 if (!S_ISDIR (pent->inode->mode)){
811 *q = c;
812 notadir = 1;
813 return NULL;
815 pdir = pent;
816 for (pent = pent->inode->first_in_subdir; pent; pent = pent->next_in_dir)
817 /* Hack: I keep the original semanthic unless
818 q+1 would break in the strchr */
819 if (!strcmp (pent->name, p)){
820 if (q + 1 > name_end){
821 *q = c;
822 notadir = !S_ISDIR (pent->inode->mode);
823 return pent;
825 break;
828 /* When we load archive, we create automagically
829 * non-existant directories
831 if (pent == NULL && make_dirs) {
832 pent = extfs_generate_entry (dir->inode->archive, p, pdir, S_IFDIR | 0777);
834 if (pent == NULL && make_file) {
835 pent = extfs_generate_entry (dir->inode->archive, p, pdir, S_IFREG | 0666);
839 /* Next iteration */
840 *q = c;
841 p = q + 1;
842 q = strchr (p, '/');
843 if (!q)
844 q = strchr (p, 0);
846 if (pent == NULL)
847 my_errno = ENOENT;
848 return pent;
851 static struct entry *extfs_find_entry (struct entry *dir, char *name, int make_dirs, int make_file)
853 struct entry *res;
855 errloop = 0;
856 notadir = 0;
857 res = extfs_find_entry_int (dir, name, NULL, make_dirs, make_file);
858 if (res == NULL) {
859 if (errloop)
860 my_errno = ELOOP;
861 else if (notadir)
862 my_errno = ENOTDIR;
864 return res;
868 static int extfs_errno (struct vfs_class *me)
870 (void) me;
872 return my_errno;
875 static void * extfs_opendir (struct vfs_class *me, const char *dirname)
877 struct archive *archive = NULL;
878 char *q;
879 struct entry *entry;
880 struct entry **info;
882 if ((q = extfs_get_path (me, dirname, &archive, 0)) == NULL)
883 return NULL;
884 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
885 g_free (q);
886 if (entry == NULL)
887 return NULL;
888 if ((entry = extfs_resolve_symlinks (entry)) == NULL)
889 return NULL;
890 if (!S_ISDIR (entry->inode->mode)) ERRNOR (ENOTDIR, NULL);
892 info = g_new (struct entry *, 2);
893 info[0] = entry->inode->first_in_subdir;
894 info[1] = entry->inode->first_in_subdir;
896 return info;
899 static void * extfs_readdir(void *data)
901 static union vfs_dirent dir;
902 struct entry **info = (struct entry **) data;
904 if (!*info)
905 return NULL;
907 g_strlcpy(dir.dent.d_name, (*info)->name, MC_MAXPATHLEN);
909 compute_namelen(&dir.dent);
910 *info = (*info)->next_in_dir;
912 return (void *) &dir;
915 static int extfs_closedir (void *data)
917 g_free (data);
918 return 0;
921 static void extfs_stat_move (struct stat *buf, const struct inode *inode)
923 buf->st_dev = inode->dev;
924 buf->st_ino = inode->inode;
925 buf->st_mode = inode->mode;
926 buf->st_nlink = inode->nlink;
927 buf->st_uid = inode->uid;
928 buf->st_gid = inode->gid;
929 #ifdef HAVE_STRUCT_STAT_ST_RDEV
930 buf->st_rdev = inode->rdev;
931 #endif
932 buf->st_size = inode->size;
933 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
934 buf->st_blksize = RECORDSIZE;
935 #endif
936 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
937 buf->st_blocks = (inode->size + RECORDSIZE - 1) / RECORDSIZE;
938 #endif
939 buf->st_atime = inode->atime;
940 buf->st_mtime = inode->mtime;
941 buf->st_ctime = inode->ctime;
944 static int
945 extfs_internal_stat (struct vfs_class *me, const char *path, struct stat *buf,
946 int resolve)
948 struct archive *archive;
949 char *q;
950 struct entry *entry;
951 char *path2 = mhl_str_dup (path);
952 int result = -1;
954 if ((q = extfs_get_path_mangle (me, path2, &archive, 0)) == NULL)
955 goto cleanup;
956 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
957 if (entry == NULL)
958 goto cleanup;
959 if (resolve && (entry = extfs_resolve_symlinks (entry)) == NULL)
960 goto cleanup;
961 extfs_stat_move (buf, entry->inode);
962 result = 0;
963 cleanup:
964 g_free (path2);
965 return result;
968 static int extfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
970 return extfs_internal_stat (me, path, buf, 1);
973 static int extfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
975 return extfs_internal_stat (me, path, buf, 0);
978 static int extfs_fstat (void *data, struct stat *buf)
980 struct pseudofile *file = (struct pseudofile *)data;
982 extfs_stat_move (buf, file->entry->inode);
983 return 0;
986 static int
987 extfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
989 struct archive *archive;
990 char *q;
991 size_t len;
992 struct entry *entry;
993 char *mpath = mhl_str_dup (path);
994 int result = -1;
996 if ((q = extfs_get_path_mangle (me, mpath, &archive, 0)) == NULL)
997 goto cleanup;
998 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
999 if (entry == NULL)
1000 goto cleanup;
1001 if (!S_ISLNK (entry->inode->mode)) {
1002 me->verrno = EINVAL;
1003 goto cleanup;
1005 len = strlen (entry->inode->linkname);
1006 if (size < len)
1007 len = size;
1008 /* readlink() does not append a NUL character to buf */
1009 memcpy (buf, entry->inode->linkname, result = len);
1010 cleanup:
1011 g_free (mpath);
1012 return result;
1015 static int extfs_chmod (struct vfs_class *me, const char *path, int mode)
1017 (void) me;
1018 (void) path;
1019 (void) mode;
1020 return 0;
1023 static ssize_t extfs_write (void *data, const char *buf, int nbyte)
1025 struct pseudofile *file = (struct pseudofile *)data;
1027 file->has_changed = 1;
1028 return write (file->local_handle, buf, nbyte);
1031 static int extfs_unlink (struct vfs_class *me, const char *file)
1033 struct archive *archive;
1034 char *q, *mpath = mhl_str_dup (file);
1035 struct entry *entry;
1036 int result = -1;
1038 if ((q = extfs_get_path_mangle (me, mpath, &archive, 0)) == NULL)
1039 goto cleanup;
1040 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
1041 if (entry == NULL)
1042 goto cleanup;
1043 if ((entry = extfs_resolve_symlinks (entry)) == NULL)
1044 goto cleanup;
1045 if (S_ISDIR (entry->inode->mode)) {
1046 me->verrno = EISDIR;
1047 goto cleanup;
1049 if (extfs_cmd (" rm ", archive, entry, "")){
1050 my_errno = EIO;
1051 goto cleanup;
1053 extfs_remove_entry (entry);
1054 result = 0;
1055 cleanup:
1056 g_free (mpath);
1057 return result;
1060 static int extfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1062 struct archive *archive;
1063 char *q, *mpath = mhl_str_dup(path);
1064 struct entry *entry;
1065 int result = -1;
1067 (void) mode;
1069 if ((q = extfs_get_path_mangle (me, mpath, &archive, 0)) == NULL)
1070 goto cleanup;
1071 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
1072 if (entry != NULL) {
1073 me->verrno = EEXIST;
1074 goto cleanup;
1076 entry = extfs_find_entry (archive->root_entry, q, 1, 0);
1077 if (entry == NULL)
1078 goto cleanup;
1079 if ((entry = extfs_resolve_symlinks (entry)) == NULL)
1080 goto cleanup;
1081 if (!S_ISDIR (entry->inode->mode)) {
1082 me->verrno = ENOTDIR;
1083 goto cleanup;
1086 if (extfs_cmd (" mkdir ", archive, entry, "")){
1087 my_errno = EIO;
1088 extfs_remove_entry (entry);
1089 goto cleanup;
1091 result = 0;
1092 cleanup:
1093 g_free (mpath);
1094 return result;
1097 static int extfs_rmdir (struct vfs_class *me, const char *path)
1099 struct archive *archive;
1100 char *q, *mpath = mhl_str_dup(path);
1101 struct entry *entry;
1102 int result = -1;
1104 if ((q = extfs_get_path_mangle (me, mpath, &archive, 0)) == NULL)
1105 goto cleanup;
1106 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
1107 if (entry == NULL)
1108 goto cleanup;
1109 if ((entry = extfs_resolve_symlinks (entry)) == NULL)
1110 goto cleanup;
1111 if (!S_ISDIR (entry->inode->mode)) {
1112 me->verrno = ENOTDIR;
1113 goto cleanup;
1116 if (extfs_cmd (" rmdir ", archive, entry, "")){
1117 my_errno = EIO;
1118 goto cleanup;
1120 extfs_remove_entry (entry);
1121 result = 0;
1122 cleanup:
1123 g_free (mpath);
1124 return result;
1127 static int
1128 extfs_chdir (struct vfs_class *me, const char *path)
1130 struct archive *archive = NULL;
1131 char *q;
1132 struct entry *entry;
1134 my_errno = ENOTDIR;
1135 if ((q = extfs_get_path (me, path, &archive, 0)) == NULL)
1136 return -1;
1137 entry = extfs_find_entry (archive->root_entry, q, 0, 0);
1138 g_free (q);
1139 if (!entry)
1140 return -1;
1141 entry = extfs_resolve_symlinks (entry);
1142 if ((!entry) || (!S_ISDIR (entry->inode->mode)))
1143 return -1;
1144 my_errno = 0;
1145 return 0;
1148 static off_t extfs_lseek (void *data, off_t offset, int whence)
1150 struct pseudofile *file = (struct pseudofile *) data;
1152 return lseek (file->local_handle, offset, whence);
1155 static vfsid
1156 extfs_getid (struct vfs_class *me, const char *path)
1158 struct archive *archive = NULL;
1159 char *p;
1161 if (!(p = extfs_get_path (me, path, &archive, 1)))
1162 return NULL;
1163 g_free (p);
1164 return (vfsid) archive;
1167 static int extfs_nothingisopen (vfsid id)
1169 if (((struct archive *)id)->fd_usage <= 0)
1170 return 1;
1171 return 0;
1174 static void extfs_remove_entry (struct entry *e)
1176 int i = --(e->inode->nlink);
1177 struct entry *pe, *ent, *prev;
1179 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1180 struct entry *f = e->inode->first_in_subdir;
1181 e->inode->first_in_subdir = NULL;
1182 extfs_remove_entry (f);
1184 pe = e->dir;
1185 if (e == pe->inode->first_in_subdir)
1186 pe->inode->first_in_subdir = e->next_in_dir;
1188 prev = NULL;
1189 for (ent = pe->inode->first_in_subdir; ent && ent->next_in_dir;
1190 ent = ent->next_in_dir)
1191 if (e == ent->next_in_dir) {
1192 prev = ent;
1193 break;
1195 if (prev)
1196 prev->next_in_dir = e->next_in_dir;
1197 if (e == pe->inode->last_in_subdir)
1198 pe->inode->last_in_subdir = prev;
1200 if (i <= 0) {
1201 if (e->inode->local_filename != NULL) {
1202 unlink (e->inode->local_filename);
1203 free (e->inode->local_filename);
1205 g_free (e->inode->linkname);
1206 g_free (e->inode);
1209 g_free (e->name);
1210 g_free (e);
1213 static void extfs_free_entry (struct entry *e)
1215 int i = --(e->inode->nlink);
1216 if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
1217 struct entry *f = e->inode->first_in_subdir;
1219 e->inode->first_in_subdir = NULL;
1220 extfs_free_entry (f);
1222 if (i <= 0) {
1223 if (e->inode->local_filename != NULL) {
1224 unlink (e->inode->local_filename);
1225 free (e->inode->local_filename);
1227 g_free (e->inode->linkname);
1228 g_free (e->inode);
1230 if (e->next_in_dir != NULL)
1231 extfs_free_entry (e->next_in_dir);
1232 g_free (e->name);
1233 g_free (e);
1236 static void extfs_free (vfsid id)
1238 struct archive *parc;
1239 struct archive *archive = (struct archive *)id;
1241 if (archive == first_archive) {
1242 first_archive = archive->next;
1243 } else {
1244 for (parc = first_archive; parc != NULL; parc = parc->next)
1245 if (parc->next == archive) {
1246 parc->next = archive->next;
1247 break;
1250 extfs_free_archive (archive);
1253 static char *
1254 extfs_getlocalcopy (struct vfs_class *me, const char *path)
1256 struct pseudofile *fp =
1257 (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
1258 char *p;
1260 if (fp == NULL)
1261 return NULL;
1262 if (fp->entry->inode->local_filename == NULL) {
1263 extfs_close ((void *) fp);
1264 return NULL;
1266 p = mhl_str_dup (fp->entry->inode->local_filename);
1267 fp->archive->fd_usage++;
1268 extfs_close ((void *) fp);
1269 return p;
1272 static int
1273 extfs_ungetlocalcopy (struct vfs_class *me, const char *path,
1274 const char *local, int has_changed)
1276 struct pseudofile *fp =
1277 (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
1279 if (fp == NULL)
1280 return 0;
1281 if (!strcmp (fp->entry->inode->local_filename, local)) {
1282 fp->archive->fd_usage--;
1283 fp->has_changed |= has_changed;
1284 extfs_close ((void *) fp);
1285 return 0;
1286 } else {
1287 /* Should not happen */
1288 extfs_close ((void *) fp);
1289 return 0;
1294 static int extfs_init (struct vfs_class *me)
1296 FILE *cfg;
1297 char *mc_extfsini;
1298 char key[256];
1300 (void) me;
1302 mc_extfsini = mhl_str_dir_plus_file (mc_home, "extfs" PATH_SEP_STR "extfs.ini");
1303 cfg = fopen (mc_extfsini, "r");
1305 /* We may not use vfs_die() message or message or similar,
1306 * UI is not initialized at this time and message would not
1307 * appear on screen. */
1308 if (!cfg) {
1309 fprintf (stderr, _("Warning: file %s not found\n"), mc_extfsini);
1310 g_free (mc_extfsini);
1311 return 0;
1314 extfs_no = 0;
1315 while (extfs_no < MAXEXTFS && fgets (key, sizeof (key), cfg)) {
1316 char *c;
1318 /* Handle those with a trailing ':', those flag that the
1319 * file system does not require an archive to work
1322 if (*key == '[') {
1323 fprintf(stderr, "Warning: You need to update your %s file.\n",
1324 mc_extfsini);
1325 fclose(cfg);
1326 g_free (mc_extfsini);
1327 return 0;
1329 if (*key == '#' || *key == '\n')
1330 continue;
1332 if ((c = strchr (key, '\n'))){
1333 *c-- = 0;
1334 } else { /* Last line without newline or strlen (key) > 255 */
1335 c = &key [strlen (key) - 1];
1337 extfs_need_archive [extfs_no] = !(*c == ':');
1338 if (*c == ':')
1339 *c = 0;
1340 if (!(*key))
1341 continue;
1343 extfs_prefixes [extfs_no++] = mhl_str_dup (key);
1345 fclose(cfg);
1346 g_free (mc_extfsini);
1347 return 1;
1350 static int extfs_which (struct vfs_class *me, const char *path)
1352 int i;
1354 (void) me;
1356 for (i = 0; i < extfs_no; i++)
1357 if (!strcmp (path, extfs_prefixes [i]))
1358 return i;
1359 return -1;
1362 static void extfs_done (struct vfs_class *me)
1364 int i;
1365 struct archive *ar;
1367 (void) me;
1369 for (ar = first_archive; ar != NULL;) {
1370 extfs_free ((vfsid) ar);
1371 ar = first_archive;
1374 for (i = 0; i < extfs_no; i++ )
1375 g_free (extfs_prefixes [i]);
1376 extfs_no = 0;
1379 static int
1380 extfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1382 (void) arg;
1384 if (ctlop == VFS_SETCTL_RUN) {
1385 extfs_run (me, path);
1386 return 1;
1388 return 0;
1391 void
1392 init_extfs (void)
1394 vfs_extfs_ops.name = "extfs";
1395 vfs_extfs_ops.init = extfs_init;
1396 vfs_extfs_ops.done = extfs_done;
1397 vfs_extfs_ops.fill_names = extfs_fill_names;
1398 vfs_extfs_ops.which = extfs_which;
1399 vfs_extfs_ops.open = extfs_open;
1400 vfs_extfs_ops.close = extfs_close;
1401 vfs_extfs_ops.read = extfs_read;
1402 vfs_extfs_ops.write = extfs_write;
1403 vfs_extfs_ops.opendir = extfs_opendir;
1404 vfs_extfs_ops.readdir = extfs_readdir;
1405 vfs_extfs_ops.closedir = extfs_closedir;
1406 vfs_extfs_ops.stat = extfs_stat;
1407 vfs_extfs_ops.lstat = extfs_lstat;
1408 vfs_extfs_ops.fstat = extfs_fstat;
1409 vfs_extfs_ops.chmod = extfs_chmod;
1410 vfs_extfs_ops.readlink = extfs_readlink;
1411 vfs_extfs_ops.unlink = extfs_unlink;
1412 vfs_extfs_ops.chdir = extfs_chdir;
1413 vfs_extfs_ops.ferrno = extfs_errno;
1414 vfs_extfs_ops.lseek = extfs_lseek;
1415 vfs_extfs_ops.getid = extfs_getid;
1416 vfs_extfs_ops.nothingisopen = extfs_nothingisopen;
1417 vfs_extfs_ops.free = extfs_free;
1418 vfs_extfs_ops.getlocalcopy = extfs_getlocalcopy;
1419 vfs_extfs_ops.ungetlocalcopy = extfs_ungetlocalcopy;
1420 vfs_extfs_ops.mkdir = extfs_mkdir;
1421 vfs_extfs_ops.rmdir = extfs_rmdir;
1422 vfs_extfs_ops.setctl = extfs_setctl;
1423 vfs_register_class (&vfs_extfs_ops);