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 */
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_WAIT_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 */
47 #include "../src/execute.h" /* For shell_execute */
50 #include "gc.h" /* vfs_rmstamp */
53 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
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 */
75 struct entry
*next_in_dir
;
82 struct archive
*archive
;
83 unsigned int has_changed
:1;
92 struct stat local_stat
;
96 struct entry
*root_entry
;
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;
112 static char *extfs_prefixes
[MAXEXTFS
];
113 static char extfs_need_archive
[MAXEXTFS
];
114 static int extfs_no
= 0;
117 extfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
119 struct archive
*a
= first_archive
;
126 g_strconcat (a
->name
? a
->name
: "", "#",
127 extfs_prefixes
[a
->fstype
], (char *) NULL
);
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
= g_strdup (".");
142 entry
->inode
= inode
;
144 inode
->local_filename
= NULL
;
145 inode
->first_in_subdir
= entry
;
147 entry
->next_in_dir
= g_new (struct entry
, 1);
148 entry
= entry
->next_in_dir
;
149 entry
->name
= g_strdup ("..");
150 inode
->last_in_subdir
= entry
;
151 entry
->next_in_dir
= NULL
;
152 if (parent
!= NULL
) {
153 entry
->inode
= parent
;
154 entry
->dir
= parentry
;
157 entry
->inode
= inode
;
163 static struct entry
*extfs_generate_entry (struct archive
*archive
,
164 const char *name
, struct entry
*parentry
, mode_t mode
)
167 struct inode
*inode
, *parent
;
170 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
171 entry
= g_new (struct entry
, 1);
173 entry
->name
= g_strdup (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);
190 inode
->mode
= mode
& ~myumask
;
193 inode
->uid
= getuid ();
194 inode
->gid
= getgid ();
196 inode
->mtime
= time (NULL
);
197 inode
->atime
= inode
->mtime
;
198 inode
->ctime
= inode
->mtime
;
201 extfs_make_dots (entry
);
206 static void extfs_free_entries (struct entry
*entry
)
213 static void extfs_free_archive (struct archive
*archive
)
215 extfs_free_entry (archive
->root_entry
);
216 if (archive
->local_name
!= NULL
) {
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
);
229 extfs_open_archive (int fstype
, const char *name
, struct archive
**pparc
)
231 static dev_t archive_counter
= 0;
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
];
243 if (mc_stat (name
, &mystat
) == -1)
245 if (!vfs_file_is_local (name
)) {
246 local_name
= mc_getlocalcopy (name
);
247 if (local_name
== NULL
)
250 tmp
= name_quote (name
, 0);
253 mc_extfsdir
= mhl_str_dir_plus_file (mc_home
, "extfs" PATH_SEP_STR
);
255 g_strconcat (mc_extfsdir
, extfs_prefixes
[fstype
], " list ",
256 local_name
? local_name
: tmp
, (char *) NULL
);
258 g_free (mc_extfsdir
);
260 result
= popen (cmd
, "r");
262 if (result
== NULL
) {
263 close_error_pipe (D_ERROR
, NULL
);
265 mc_ungetlocalcopy (name
, local_name
, 0);
271 setvbuf (result
, NULL
, _IONBF
, 0);
274 current_archive
= g_new (struct archive
, 1);
275 current_archive
->fstype
= fstype
;
276 current_archive
->name
= name
? g_strdup (name
) : NULL
;
277 current_archive
->local_name
= local_name
;
279 if (local_name
!= NULL
)
280 mc_stat (local_name
, ¤t_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;
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
;
308 * Main loop for reading an archive.
309 * Return 0 on success, -1 on error.
312 extfs_read_archive (int fstype
, const char *name
, struct archive
**pparc
)
316 struct archive
*current_archive
;
317 char *current_file_name
, *current_link_name
;
320 extfs_open_archive (fstype
, name
, ¤t_archive
)) == NULL
) {
321 message (D_ERROR
, MSG_ERROR
, _("Cannot open %s archive\n%s"),
322 extfs_prefixes
[fstype
], name
);
326 buffer
= g_malloc (4096);
327 while (fgets (buffer
, 4096, extfsd
) != NULL
) {
330 current_link_name
= NULL
;
332 (buffer
, &hstat
, ¤t_file_name
, ¤t_link_name
)) {
333 struct entry
*entry
, *pent
;
335 char *p
, *q
, *cfn
= current_file_name
;
341 if (p
!= cfn
&& *(p
- 1) == '/')
343 p
= strrchr (cfn
, '/');
351 if (S_ISDIR (hstat
.st_mode
)
352 && (!strcmp (p
, ".") || !strcmp (p
, "..")))
353 goto read_extfs_continue
;
355 extfs_find_entry (current_archive
->root_entry
, q
, 1,
358 /* FIXME: Should clean everything one day */
361 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
364 entry
= g_new (struct entry
, 1);
365 entry
->name
= g_strdup (p
);
366 entry
->next_in_dir
= NULL
;
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
) {
374 extfs_find_entry (current_archive
->root_entry
,
375 current_link_name
, 0, 0);
377 /* FIXME: Should clean everything one day */
380 close_error_pipe (D_ERROR
,
381 _("Inconsistent extfs archive"));
384 entry
->inode
= pent
->inode
;
385 pent
->inode
->nlink
++;
388 inode
= g_new (struct inode
, 1);
389 entry
->inode
= inode
;
390 inode
->local_filename
= NULL
;
391 inode
->inode
= (current_archive
->inode_counter
)++;
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
;
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
;
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
);
423 g_free (current_file_name
);
424 g_free (current_link_name
);
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"));
436 close_error_pipe (D_ERROR
, NULL
);
437 *pparc
= current_archive
;
442 * Dissect the path and create corresponding superblock. Note that inname
443 * can be changed and the result may point inside the original string.
446 extfs_get_path_mangle (struct vfs_class
*me
, char *inname
, struct archive
**archive
,
450 const char *archive_name
;
452 struct archive
*parc
;
455 archive_name
= inname
;
456 vfs_split (inname
, &local
, &op
);
458 fstype
= extfs_which (me
, op
);
463 local
= inname
+ strlen (inname
);
466 * All filesystems should have some local archive, at least
469 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
471 if (!strcmp (parc
->name
, archive_name
)) {
472 vfs_stamp (&vfs_extfs_ops
, (vfsid
) parc
);
478 do_not_open
? -1 : extfs_read_archive (fstype
, archive_name
,
490 * Dissect the path and create corresponding superblock.
491 * The result should be freed.
494 extfs_get_path (struct vfs_class
*me
, const char *inname
, struct archive
**archive
,
497 char *buf
= g_strdup (inname
);
498 char *res
= extfs_get_path_mangle (me
, buf
, archive
, do_not_open
);
501 res2
= g_strdup (res
);
507 /* Return allocated path (without leading slash) inside the archive */
508 static char *extfs_get_path_from_entry (struct entry
*entry
)
517 for (len
= 0, head
= 0; entry
->dir
; entry
= entry
->dir
) {
518 p
= g_new (struct list
, 1);
520 p
->name
= entry
->name
;
522 len
+= strlen (entry
->name
) + 1;
526 return g_strdup ("");
528 localpath
= g_malloc (len
);
531 strcat (localpath
, head
->name
);
533 strcat (localpath
, "/");
542 struct loop_protect
{
544 struct loop_protect
*next
;
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
)
558 struct loop_protect
*looping
;
560 if (!S_ISLNK (entry
->inode
->mode
))
562 for (looping
= list
; looping
!= NULL
; looping
= looping
->next
)
563 if (entry
== looping
->entry
) { /* Here we protect us against symlink looping */
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);
577 static struct entry
*extfs_resolve_symlinks (struct entry
*entry
)
583 res
= extfs_resolve_symlinks_int (entry
, NULL
);
594 extfs_get_archive_name (struct archive
*archive
)
596 const char *archive_name
;
598 if (archive
->local_name
)
599 archive_name
= archive
->local_name
;
601 archive_name
= archive
->name
;
603 if (!archive_name
|| !*archive_name
)
604 return "no_archive_name";
609 /* Don't pass localname as NULL */
611 extfs_cmd (const char *extfs_cmd
, struct archive
*archive
,
612 struct entry
*entry
, const char *localname
)
616 char *quoted_localname
;
622 file
= extfs_get_path_from_entry (entry
);
623 quoted_file
= name_quote (file
, 0);
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
);
638 retval
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
640 close_error_pipe (D_ERROR
, NULL
);
645 extfs_run (struct vfs_class
*me
, const char *file
)
647 struct archive
*archive
= NULL
;
648 char *p
, *q
, *archive_name
, *mc_extfsdir
;
651 if ((p
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
653 q
= name_quote (p
, 0);
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
);
663 shell_execute (cmd
, 0);
668 extfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
670 struct pseudofile
*extfs_info
;
671 struct archive
*archive
= NULL
;
677 if ((q
= extfs_get_path (me
, file
, &archive
, 0)) == 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
);
689 if ((entry
= extfs_resolve_symlinks (entry
)) == 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)
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
);
711 entry
->inode
->local_filename
= local_filename
;
715 open (entry
->inode
->local_filename
, NO_LINEAR (flags
), mode
);
716 if (local_handle
== -1)
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
);
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
);
739 extfs_close (void *data
)
741 struct pseudofile
*file
;
743 file
= (struct pseudofile
*) data
;
745 close (file
->local_handle
);
747 /* Commit the file if it has changed */
748 if (file
->has_changed
) {
750 (" copyin ", file
->archive
, file
->entry
,
751 file
->entry
->inode
->local_filename
))
754 struct stat file_status
;
755 if (stat (file
->entry
->inode
->local_filename
, &file_status
) !=
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
);
775 #define RECORDSIZE 512
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
;
785 if (*name
== '/') { /* Handle absolute paths */
787 dir
= dir
->inode
->archive
->root_entry
;
792 name_end
= name
+ strlen (name
);
798 for (; pent
!= NULL
&& c
&& *p
; ){
802 if (strcmp (p
, ".")){
803 if (!strcmp (p
, ".."))
806 if ((pent
= extfs_resolve_symlinks_int (pent
, list
))==NULL
){
810 if (!S_ISDIR (pent
->inode
->mode
)){
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
){
822 notadir
= !S_ISDIR (pent
->inode
->mode
);
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);
851 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
, int make_dirs
, int make_file
)
857 res
= extfs_find_entry_int (dir
, name
, NULL
, make_dirs
, make_file
);
868 static int extfs_errno (struct vfs_class
*me
)
875 static void * extfs_opendir (struct vfs_class
*me
, const char *dirname
)
877 struct archive
*archive
= NULL
;
882 if ((q
= extfs_get_path (me
, dirname
, &archive
, 0)) == NULL
)
884 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
888 if ((entry
= extfs_resolve_symlinks (entry
)) == 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
;
899 static void * extfs_readdir(void *data
)
901 static union vfs_dirent dir
;
902 struct entry
**info
= (struct entry
**) data
;
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
)
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
;
932 buf
->st_size
= inode
->size
;
933 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
934 buf
->st_blksize
= RECORDSIZE
;
936 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
937 buf
->st_blocks
= (inode
->size
+ RECORDSIZE
- 1) / RECORDSIZE
;
939 buf
->st_atime
= inode
->atime
;
940 buf
->st_mtime
= inode
->mtime
;
941 buf
->st_ctime
= inode
->ctime
;
945 extfs_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
,
948 struct archive
*archive
;
951 char *path2
= g_strdup (path
);
954 if ((q
= extfs_get_path_mangle (me
, path2
, &archive
, 0)) == NULL
)
956 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
959 if (resolve
&& (entry
= extfs_resolve_symlinks (entry
)) == NULL
)
961 extfs_stat_move (buf
, entry
->inode
);
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
);
987 extfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
989 struct archive
*archive
;
993 char *mpath
= g_strdup (path
);
996 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
998 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1001 if (!S_ISLNK (entry
->inode
->mode
)) {
1002 me
->verrno
= EINVAL
;
1005 len
= strlen (entry
->inode
->linkname
);
1008 /* readlink() does not append a NUL character to buf */
1009 memcpy (buf
, entry
->inode
->linkname
, result
= len
);
1015 static int extfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
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
= g_strdup (file
);
1035 struct entry
*entry
;
1038 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1040 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1043 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1045 if (S_ISDIR (entry
->inode
->mode
)) {
1046 me
->verrno
= EISDIR
;
1049 if (extfs_cmd (" rm ", archive
, entry
, "")){
1053 extfs_remove_entry (entry
);
1060 static int extfs_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1062 struct archive
*archive
;
1063 char *q
, *mpath
= g_strdup(path
);
1064 struct entry
*entry
;
1069 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1071 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1072 if (entry
!= NULL
) {
1073 me
->verrno
= EEXIST
;
1076 entry
= extfs_find_entry (archive
->root_entry
, q
, 1, 0);
1079 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1081 if (!S_ISDIR (entry
->inode
->mode
)) {
1082 me
->verrno
= ENOTDIR
;
1086 if (extfs_cmd (" mkdir ", archive
, entry
, "")){
1088 extfs_remove_entry (entry
);
1097 static int extfs_rmdir (struct vfs_class
*me
, const char *path
)
1099 struct archive
*archive
;
1100 char *q
, *mpath
= g_strdup(path
);
1101 struct entry
*entry
;
1104 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1106 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1109 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1111 if (!S_ISDIR (entry
->inode
->mode
)) {
1112 me
->verrno
= ENOTDIR
;
1116 if (extfs_cmd (" rmdir ", archive
, entry
, "")){
1120 extfs_remove_entry (entry
);
1128 extfs_chdir (struct vfs_class
*me
, const char *path
)
1130 struct archive
*archive
= NULL
;
1132 struct entry
*entry
;
1135 if ((q
= extfs_get_path (me
, path
, &archive
, 0)) == NULL
)
1137 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1141 entry
= extfs_resolve_symlinks (entry
);
1142 if ((!entry
) || (!S_ISDIR (entry
->inode
->mode
)))
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
);
1156 extfs_getid (struct vfs_class
*me
, const char *path
)
1158 struct archive
*archive
= NULL
;
1161 if (!(p
= extfs_get_path (me
, path
, &archive
, 1)))
1164 return (vfsid
) archive
;
1167 static int extfs_nothingisopen (vfsid id
)
1169 if (((struct archive
*)id
)->fd_usage
<= 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
);
1185 if (e
== pe
->inode
->first_in_subdir
)
1186 pe
->inode
->first_in_subdir
= e
->next_in_dir
;
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
) {
1196 prev
->next_in_dir
= e
->next_in_dir
;
1197 if (e
== pe
->inode
->last_in_subdir
)
1198 pe
->inode
->last_in_subdir
= prev
;
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
);
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
);
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
);
1230 if (e
->next_in_dir
!= NULL
)
1231 extfs_free_entry (e
->next_in_dir
);
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
;
1244 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
1245 if (parc
->next
== archive
) {
1246 parc
->next
= archive
->next
;
1250 extfs_free_archive (archive
);
1254 extfs_getlocalcopy (struct vfs_class
*me
, const char *path
)
1256 struct pseudofile
*fp
=
1257 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1262 if (fp
->entry
->inode
->local_filename
== NULL
) {
1263 extfs_close ((void *) fp
);
1266 p
= g_strdup (fp
->entry
->inode
->local_filename
);
1267 fp
->archive
->fd_usage
++;
1268 extfs_close ((void *) fp
);
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);
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
);
1287 /* Should not happen */
1288 extfs_close ((void *) fp
);
1294 static int extfs_init (struct vfs_class
*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. */
1309 fprintf (stderr
, _("Warning: file %s not found\n"), mc_extfsini
);
1310 g_free (mc_extfsini
);
1315 while (extfs_no
< MAXEXTFS
&& fgets (key
, sizeof (key
), cfg
)) {
1318 /* Handle those with a trailing ':', those flag that the
1319 * file system does not require an archive to work
1323 fprintf(stderr
, "Warning: You need to update your %s file.\n",
1326 g_free (mc_extfsini
);
1329 if (*key
== '#' || *key
== '\n')
1332 if ((c
= strchr (key
, '\n'))){
1334 } else { /* Last line without newline or strlen (key) > 255 */
1335 c
= &key
[strlen (key
) - 1];
1337 extfs_need_archive
[extfs_no
] = !(*c
== ':');
1343 extfs_prefixes
[extfs_no
++] = g_strdup (key
);
1346 g_free (mc_extfsini
);
1350 static int extfs_which (struct vfs_class
*me
, const char *path
)
1356 for (i
= 0; i
< extfs_no
; i
++)
1357 if (!strcmp (path
, extfs_prefixes
[i
]))
1362 static void extfs_done (struct vfs_class
*me
)
1369 for (ar
= first_archive
; ar
!= NULL
;) {
1370 extfs_free ((vfsid
) ar
);
1374 for (i
= 0; i
< extfs_no
; i
++ )
1375 g_free (extfs_prefixes
[i
]);
1380 extfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1384 if (ctlop
== VFS_SETCTL_RUN
) {
1385 extfs_run (me
, path
);
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
);