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/string.h>
41 #include "../src/global.h"
42 #include "../src/tty.h" /* enable/disable interrupt key */
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
46 #include "../src/execute.h" /* For shell_execute */
49 #include "gc.h" /* vfs_rmstamp */
52 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
56 struct entry
*first_in_subdir
; /* only used if this is a directory */
57 struct entry
*last_in_subdir
;
58 ino_t inode
; /* This is inode # */
59 dev_t dev
; /* This is an internal identification of the extfs archive */
60 struct archive
*archive
; /* And this is an archive structure */
74 struct entry
*next_in_dir
;
81 struct archive
*archive
;
82 unsigned int has_changed
:1;
91 struct stat local_stat
;
95 struct entry
*root_entry
;
99 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
,
100 int make_dirs
, int make_file
);
101 static int extfs_which (struct vfs_class
*me
, const char *path
);
102 static void extfs_remove_entry (struct entry
*e
);
103 static void extfs_free (vfsid id
);
104 static void extfs_free_entry (struct entry
*e
);
106 static struct vfs_class vfs_extfs_ops
;
107 static struct archive
*first_archive
= NULL
;
108 static int my_errno
= 0;
111 static char *extfs_prefixes
[MAXEXTFS
];
112 static char extfs_need_archive
[MAXEXTFS
];
113 static int extfs_no
= 0;
116 extfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
118 struct archive
*a
= first_archive
;
125 g_strconcat (a
->name
? a
->name
: "", "#",
126 extfs_prefixes
[a
->fstype
], (char *) NULL
);
133 static void extfs_make_dots (struct entry
*ent
)
135 struct entry
*entry
= g_new (struct entry
, 1);
136 struct entry
*parentry
= ent
->dir
;
137 struct inode
*inode
= ent
->inode
, *parent
;
139 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
140 entry
->name
= g_strdup (".");
141 entry
->inode
= inode
;
143 inode
->local_filename
= NULL
;
144 inode
->first_in_subdir
= entry
;
146 entry
->next_in_dir
= g_new (struct entry
, 1);
147 entry
= entry
->next_in_dir
;
148 entry
->name
= g_strdup ("..");
149 inode
->last_in_subdir
= entry
;
150 entry
->next_in_dir
= NULL
;
151 if (parent
!= NULL
) {
152 entry
->inode
= parent
;
153 entry
->dir
= parentry
;
156 entry
->inode
= inode
;
162 static struct entry
*extfs_generate_entry (struct archive
*archive
,
163 const char *name
, struct entry
*parentry
, mode_t mode
)
166 struct inode
*inode
, *parent
;
169 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
170 entry
= g_new (struct entry
, 1);
172 entry
->name
= g_strdup (name
);
173 entry
->next_in_dir
= NULL
;
174 entry
->dir
= parentry
;
175 if (parent
!= NULL
) {
176 parent
->last_in_subdir
->next_in_dir
= entry
;
177 parent
->last_in_subdir
= entry
;
179 inode
= g_new (struct inode
, 1);
180 entry
->inode
= inode
;
181 inode
->local_filename
= NULL
;
182 inode
->linkname
= NULL
;
183 inode
->last_in_subdir
= NULL
;
184 inode
->inode
= (archive
->inode_counter
)++;
185 inode
->dev
= archive
->rdev
;
186 inode
->archive
= archive
;
187 myumask
= umask (022);
189 inode
->mode
= mode
& ~myumask
;
192 inode
->uid
= getuid ();
193 inode
->gid
= getgid ();
195 inode
->mtime
= time (NULL
);
196 inode
->atime
= inode
->mtime
;
197 inode
->ctime
= inode
->mtime
;
200 extfs_make_dots (entry
);
205 static void extfs_free_entries (struct entry
*entry
)
212 static void extfs_free_archive (struct archive
*archive
)
214 extfs_free_entry (archive
->root_entry
);
215 if (archive
->local_name
!= NULL
) {
218 mc_stat (archive
->local_name
, &my
);
219 mc_ungetlocalcopy (archive
->name
, archive
->local_name
,
220 archive
->local_stat
.st_mtime
!= my
.st_mtime
);
221 g_free(archive
->local_name
);
223 g_free (archive
->name
);
228 extfs_open_archive (int fstype
, const char *name
, struct archive
**pparc
)
230 static dev_t archive_counter
= 0;
236 struct archive
*current_archive
;
237 struct entry
*root_entry
;
238 char *local_name
= NULL
, *tmp
= 0;
239 int uses_archive
= extfs_need_archive
[fstype
];
242 if (mc_stat (name
, &mystat
) == -1)
244 if (!vfs_file_is_local (name
)) {
245 local_name
= mc_getlocalcopy (name
);
246 if (local_name
== NULL
)
249 tmp
= name_quote (name
, 0);
252 mc_extfsdir
= mhl_str_dir_plus_file (mc_home
, "extfs" PATH_SEP_STR
);
254 g_strconcat (mc_extfsdir
, extfs_prefixes
[fstype
], " list ",
255 local_name
? local_name
: tmp
, (char *) NULL
);
257 g_free (mc_extfsdir
);
259 result
= popen (cmd
, "r");
261 if (result
== NULL
) {
262 close_error_pipe (D_ERROR
, NULL
);
264 mc_ungetlocalcopy (name
, local_name
, 0);
270 setvbuf (result
, NULL
, _IONBF
, 0);
273 current_archive
= g_new (struct archive
, 1);
274 current_archive
->fstype
= fstype
;
275 current_archive
->name
= name
? g_strdup (name
) : NULL
;
276 current_archive
->local_name
= local_name
;
278 if (local_name
!= NULL
)
279 mc_stat (local_name
, ¤t_archive
->local_stat
);
280 current_archive
->inode_counter
= 0;
281 current_archive
->fd_usage
= 0;
282 current_archive
->rdev
= archive_counter
++;
283 current_archive
->next
= first_archive
;
284 first_archive
= current_archive
;
285 mode
= mystat
.st_mode
& 07777;
293 root_entry
= extfs_generate_entry (current_archive
, "/", NULL
, mode
);
294 root_entry
->inode
->uid
= mystat
.st_uid
;
295 root_entry
->inode
->gid
= mystat
.st_gid
;
296 root_entry
->inode
->atime
= mystat
.st_atime
;
297 root_entry
->inode
->ctime
= mystat
.st_ctime
;
298 root_entry
->inode
->mtime
= mystat
.st_mtime
;
299 current_archive
->root_entry
= root_entry
;
301 *pparc
= current_archive
;
307 * Main loop for reading an archive.
308 * Return 0 on success, -1 on error.
311 extfs_read_archive (int fstype
, const char *name
, struct archive
**pparc
)
315 struct archive
*current_archive
;
316 char *current_file_name
, *current_link_name
;
319 extfs_open_archive (fstype
, name
, ¤t_archive
)) == NULL
) {
320 message (D_ERROR
, MSG_ERROR
, _("Cannot open %s archive\n%s"),
321 extfs_prefixes
[fstype
], name
);
325 buffer
= g_malloc (4096);
326 while (fgets (buffer
, 4096, extfsd
) != NULL
) {
329 current_link_name
= NULL
;
331 (buffer
, &hstat
, ¤t_file_name
, ¤t_link_name
)) {
332 struct entry
*entry
, *pent
;
334 char *p
, *q
, *cfn
= current_file_name
;
340 if (p
!= cfn
&& *(p
- 1) == '/')
342 p
= strrchr (cfn
, '/');
350 if (S_ISDIR (hstat
.st_mode
)
351 && (!strcmp (p
, ".") || !strcmp (p
, "..")))
352 goto read_extfs_continue
;
354 extfs_find_entry (current_archive
->root_entry
, q
, 1,
357 /* FIXME: Should clean everything one day */
360 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
363 entry
= g_new (struct entry
, 1);
364 entry
->name
= g_strdup (p
);
365 entry
->next_in_dir
= NULL
;
367 if (pent
->inode
->last_in_subdir
) {
368 pent
->inode
->last_in_subdir
->next_in_dir
= entry
;
369 pent
->inode
->last_in_subdir
= entry
;
371 if (!S_ISLNK (hstat
.st_mode
) && current_link_name
!= NULL
) {
373 extfs_find_entry (current_archive
->root_entry
,
374 current_link_name
, 0, 0);
376 /* FIXME: Should clean everything one day */
379 close_error_pipe (D_ERROR
,
380 _("Inconsistent extfs archive"));
383 entry
->inode
= pent
->inode
;
384 pent
->inode
->nlink
++;
387 inode
= g_new (struct inode
, 1);
388 entry
->inode
= inode
;
389 inode
->local_filename
= NULL
;
390 inode
->inode
= (current_archive
->inode_counter
)++;
392 inode
->dev
= current_archive
->rdev
;
393 inode
->archive
= current_archive
;
394 inode
->mode
= hstat
.st_mode
;
395 #ifdef HAVE_STRUCT_STAT_ST_RDEV
396 inode
->rdev
= hstat
.st_rdev
;
400 inode
->uid
= hstat
.st_uid
;
401 inode
->gid
= hstat
.st_gid
;
402 inode
->size
= hstat
.st_size
;
403 inode
->mtime
= hstat
.st_mtime
;
404 inode
->atime
= hstat
.st_atime
;
405 inode
->ctime
= hstat
.st_ctime
;
406 inode
->first_in_subdir
= NULL
;
407 inode
->last_in_subdir
= NULL
;
408 if (current_link_name
!= NULL
409 && S_ISLNK (hstat
.st_mode
)) {
410 inode
->linkname
= current_link_name
;
411 current_link_name
= NULL
;
413 if (S_ISLNK (hstat
.st_mode
))
414 inode
->mode
&= ~S_IFLNK
; /* You *DON'T* want to do this always */
415 inode
->linkname
= NULL
;
417 if (S_ISDIR (hstat
.st_mode
))
418 extfs_make_dots (entry
);
422 g_free (current_file_name
);
423 g_free (current_link_name
);
428 /* Check if extfs 'list' returned 0 */
429 if (pclose (extfsd
) != 0) {
430 extfs_free (current_archive
);
431 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
435 close_error_pipe (D_ERROR
, NULL
);
436 *pparc
= current_archive
;
441 * Dissect the path and create corresponding superblock. Note that inname
442 * can be changed and the result may point inside the original string.
445 extfs_get_path_mangle (struct vfs_class
*me
, char *inname
, struct archive
**archive
,
449 const char *archive_name
;
451 struct archive
*parc
;
454 archive_name
= inname
;
455 vfs_split (inname
, &local
, &op
);
457 fstype
= extfs_which (me
, op
);
462 local
= inname
+ strlen (inname
);
465 * All filesystems should have some local archive, at least
468 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
470 if (!strcmp (parc
->name
, archive_name
)) {
471 vfs_stamp (&vfs_extfs_ops
, (vfsid
) parc
);
477 do_not_open
? -1 : extfs_read_archive (fstype
, archive_name
,
489 * Dissect the path and create corresponding superblock.
490 * The result should be freed.
493 extfs_get_path (struct vfs_class
*me
, const char *inname
, struct archive
**archive
,
496 char *buf
= g_strdup (inname
);
497 char *res
= extfs_get_path_mangle (me
, buf
, archive
, do_not_open
);
500 res2
= g_strdup (res
);
506 /* Return allocated path (without leading slash) inside the archive */
507 static char *extfs_get_path_from_entry (struct entry
*entry
)
516 for (len
= 0, head
= 0; entry
->dir
; entry
= entry
->dir
) {
517 p
= g_new (struct list
, 1);
519 p
->name
= entry
->name
;
521 len
+= strlen (entry
->name
) + 1;
525 return g_strdup ("");
527 localpath
= g_malloc (len
);
530 strcat (localpath
, head
->name
);
532 strcat (localpath
, "/");
541 struct loop_protect
{
543 struct loop_protect
*next
;
548 static struct entry
*
549 extfs_find_entry_int (struct entry
*dir
, char *name
,
550 struct loop_protect
*list
, int make_dirs
, int make_file
);
552 static struct entry
*
553 extfs_resolve_symlinks_int (struct entry
*entry
,
554 struct loop_protect
*list
)
557 struct loop_protect
*looping
;
559 if (!S_ISLNK (entry
->inode
->mode
))
561 for (looping
= list
; looping
!= NULL
; looping
= looping
->next
)
562 if (entry
== looping
->entry
) { /* Here we protect us against symlink looping */
566 looping
= g_new (struct loop_protect
, 1);
567 looping
->entry
= entry
;
568 looping
->next
= list
;
569 pent
= extfs_find_entry_int (entry
->dir
, entry
->inode
->linkname
, looping
, 0, 0);
576 static struct entry
*extfs_resolve_symlinks (struct entry
*entry
)
582 res
= extfs_resolve_symlinks_int (entry
, NULL
);
593 extfs_get_archive_name (struct archive
*archive
)
595 const char *archive_name
;
597 if (archive
->local_name
)
598 archive_name
= archive
->local_name
;
600 archive_name
= archive
->name
;
602 if (!archive_name
|| !*archive_name
)
603 return "no_archive_name";
608 /* Don't pass localname as NULL */
610 extfs_cmd (const char *extfs_cmd
, struct archive
*archive
,
611 struct entry
*entry
, const char *localname
)
615 char *quoted_localname
;
621 file
= extfs_get_path_from_entry (entry
);
622 quoted_file
= name_quote (file
, 0);
624 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
625 quoted_localname
= name_quote (localname
, 0);
627 mc_extfsdir
= mhl_str_dir_plus_file (mc_home
, "extfs" PATH_SEP_STR
);
628 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
629 extfs_cmd
, archive_name
, " ", quoted_file
, " ",
630 quoted_localname
, (char *) NULL
);
631 g_free (quoted_file
);
632 g_free (quoted_localname
);
633 g_free (mc_extfsdir
);
634 g_free (archive_name
);
637 retval
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
639 close_error_pipe (D_ERROR
, NULL
);
644 extfs_run (struct vfs_class
*me
, const char *file
)
646 struct archive
*archive
= NULL
;
647 char *p
, *q
, *archive_name
, *mc_extfsdir
;
650 if ((p
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
652 q
= name_quote (p
, 0);
655 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
656 mc_extfsdir
= mhl_str_dir_plus_file (mc_home
, "extfs" PATH_SEP_STR
);
657 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
658 " run ", archive_name
, " ", q
, (char *) NULL
);
659 g_free (mc_extfsdir
);
660 g_free (archive_name
);
662 shell_execute (cmd
, 0);
667 extfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
669 struct pseudofile
*extfs_info
;
670 struct archive
*archive
= NULL
;
676 if ((q
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
678 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
679 if (entry
== NULL
&& (flags
& O_CREAT
)) {
680 /* Create new entry */
681 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 1);
682 created
= (entry
!= NULL
);
688 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
691 if (S_ISDIR (entry
->inode
->mode
))
692 ERRNOR (EISDIR
, NULL
);
694 if (entry
->inode
->local_filename
== NULL
) {
695 char *local_filename
;
697 local_handle
= vfs_mkstemps (&local_filename
, "extfs", entry
->name
);
699 if (local_handle
== -1)
701 close (local_handle
);
703 if (!created
&& !(flags
& O_TRUNC
)
704 && extfs_cmd (" copyout ", archive
, entry
, local_filename
)) {
705 unlink (local_filename
);
706 free (local_filename
);
710 entry
->inode
->local_filename
= local_filename
;
714 open (entry
->inode
->local_filename
, NO_LINEAR (flags
), mode
);
715 if (local_handle
== -1)
718 extfs_info
= g_new (struct pseudofile
, 1);
719 extfs_info
->archive
= archive
;
720 extfs_info
->entry
= entry
;
721 extfs_info
->has_changed
= created
;
722 extfs_info
->local_handle
= local_handle
;
724 /* i.e. we had no open files and now we have one */
725 vfs_rmstamp (&vfs_extfs_ops
, (vfsid
) archive
);
730 static ssize_t
extfs_read (void *data
, char *buffer
, int count
)
732 struct pseudofile
*file
= (struct pseudofile
*)data
;
734 return read (file
->local_handle
, buffer
, count
);
738 extfs_close (void *data
)
740 struct pseudofile
*file
;
742 file
= (struct pseudofile
*) data
;
744 close (file
->local_handle
);
746 /* Commit the file if it has changed */
747 if (file
->has_changed
) {
749 (" copyin ", file
->archive
, file
->entry
,
750 file
->entry
->inode
->local_filename
))
753 struct stat file_status
;
754 if (stat (file
->entry
->inode
->local_filename
, &file_status
) !=
758 file
->entry
->inode
->size
= file_status
.st_size
;
761 file
->entry
->inode
->mtime
= time (NULL
);
764 file
->archive
->fd_usage
--;
765 if (!file
->archive
->fd_usage
)
766 vfs_stamp_create (&vfs_extfs_ops
, file
->archive
);
774 #define RECORDSIZE 512
777 extfs_find_entry_int (struct entry
*dir
, char *name
,
778 struct loop_protect
*list
, int make_dirs
, int make_file
)
780 struct entry
*pent
, *pdir
;
781 char *p
, *q
, *name_end
;
784 if (*name
== '/') { /* Handle absolute paths */
786 dir
= dir
->inode
->archive
->root_entry
;
791 name_end
= name
+ strlen (name
);
797 for (; pent
!= NULL
&& c
&& *p
; ){
801 if (strcmp (p
, ".")){
802 if (!strcmp (p
, ".."))
805 if ((pent
= extfs_resolve_symlinks_int (pent
, list
))==NULL
){
809 if (!S_ISDIR (pent
->inode
->mode
)){
815 for (pent
= pent
->inode
->first_in_subdir
; pent
; pent
= pent
->next_in_dir
)
816 /* Hack: I keep the original semanthic unless
817 q+1 would break in the strchr */
818 if (!strcmp (pent
->name
, p
)){
819 if (q
+ 1 > name_end
){
821 notadir
= !S_ISDIR (pent
->inode
->mode
);
827 /* When we load archive, we create automagically
828 * non-existant directories
830 if (pent
== NULL
&& make_dirs
) {
831 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFDIR
| 0777);
833 if (pent
== NULL
&& make_file
) {
834 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFREG
| 0666);
850 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
, int make_dirs
, int make_file
)
856 res
= extfs_find_entry_int (dir
, name
, NULL
, make_dirs
, make_file
);
867 static int extfs_errno (struct vfs_class
*me
)
874 static void * extfs_opendir (struct vfs_class
*me
, const char *dirname
)
876 struct archive
*archive
= NULL
;
881 if ((q
= extfs_get_path (me
, dirname
, &archive
, 0)) == NULL
)
883 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
887 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
889 if (!S_ISDIR (entry
->inode
->mode
)) ERRNOR (ENOTDIR
, NULL
);
891 info
= g_new (struct entry
*, 2);
892 info
[0] = entry
->inode
->first_in_subdir
;
893 info
[1] = entry
->inode
->first_in_subdir
;
898 static void * extfs_readdir(void *data
)
900 static union vfs_dirent dir
;
901 struct entry
**info
= (struct entry
**) data
;
906 g_strlcpy(dir
.dent
.d_name
, (*info
)->name
, MC_MAXPATHLEN
);
908 compute_namelen(&dir
.dent
);
909 *info
= (*info
)->next_in_dir
;
911 return (void *) &dir
;
914 static int extfs_closedir (void *data
)
920 static void extfs_stat_move (struct stat
*buf
, const struct inode
*inode
)
922 buf
->st_dev
= inode
->dev
;
923 buf
->st_ino
= inode
->inode
;
924 buf
->st_mode
= inode
->mode
;
925 buf
->st_nlink
= inode
->nlink
;
926 buf
->st_uid
= inode
->uid
;
927 buf
->st_gid
= inode
->gid
;
928 #ifdef HAVE_STRUCT_STAT_ST_RDEV
929 buf
->st_rdev
= inode
->rdev
;
931 buf
->st_size
= inode
->size
;
932 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
933 buf
->st_blksize
= RECORDSIZE
;
935 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
936 buf
->st_blocks
= (inode
->size
+ RECORDSIZE
- 1) / RECORDSIZE
;
938 buf
->st_atime
= inode
->atime
;
939 buf
->st_mtime
= inode
->mtime
;
940 buf
->st_ctime
= inode
->ctime
;
944 extfs_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
,
947 struct archive
*archive
;
950 char *path2
= g_strdup (path
);
953 if ((q
= extfs_get_path_mangle (me
, path2
, &archive
, 0)) == NULL
)
955 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
958 if (resolve
&& (entry
= extfs_resolve_symlinks (entry
)) == NULL
)
960 extfs_stat_move (buf
, entry
->inode
);
967 static int extfs_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
969 return extfs_internal_stat (me
, path
, buf
, 1);
972 static int extfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
974 return extfs_internal_stat (me
, path
, buf
, 0);
977 static int extfs_fstat (void *data
, struct stat
*buf
)
979 struct pseudofile
*file
= (struct pseudofile
*)data
;
981 extfs_stat_move (buf
, file
->entry
->inode
);
986 extfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
988 struct archive
*archive
;
992 char *mpath
= g_strdup (path
);
995 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
997 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1000 if (!S_ISLNK (entry
->inode
->mode
)) {
1001 me
->verrno
= EINVAL
;
1004 len
= strlen (entry
->inode
->linkname
);
1007 /* readlink() does not append a NUL character to buf */
1008 memcpy (buf
, entry
->inode
->linkname
, result
= len
);
1014 static int extfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1022 static ssize_t
extfs_write (void *data
, const char *buf
, int nbyte
)
1024 struct pseudofile
*file
= (struct pseudofile
*)data
;
1026 file
->has_changed
= 1;
1027 return write (file
->local_handle
, buf
, nbyte
);
1030 static int extfs_unlink (struct vfs_class
*me
, const char *file
)
1032 struct archive
*archive
;
1033 char *q
, *mpath
= g_strdup (file
);
1034 struct entry
*entry
;
1037 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1039 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1042 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1044 if (S_ISDIR (entry
->inode
->mode
)) {
1045 me
->verrno
= EISDIR
;
1048 if (extfs_cmd (" rm ", archive
, entry
, "")){
1052 extfs_remove_entry (entry
);
1059 static int extfs_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1061 struct archive
*archive
;
1062 char *q
, *mpath
= g_strdup(path
);
1063 struct entry
*entry
;
1068 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1070 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1071 if (entry
!= NULL
) {
1072 me
->verrno
= EEXIST
;
1075 entry
= extfs_find_entry (archive
->root_entry
, q
, 1, 0);
1078 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1080 if (!S_ISDIR (entry
->inode
->mode
)) {
1081 me
->verrno
= ENOTDIR
;
1085 if (extfs_cmd (" mkdir ", archive
, entry
, "")){
1087 extfs_remove_entry (entry
);
1096 static int extfs_rmdir (struct vfs_class
*me
, const char *path
)
1098 struct archive
*archive
;
1099 char *q
, *mpath
= g_strdup(path
);
1100 struct entry
*entry
;
1103 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1105 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1108 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1110 if (!S_ISDIR (entry
->inode
->mode
)) {
1111 me
->verrno
= ENOTDIR
;
1115 if (extfs_cmd (" rmdir ", archive
, entry
, "")){
1119 extfs_remove_entry (entry
);
1127 extfs_chdir (struct vfs_class
*me
, const char *path
)
1129 struct archive
*archive
= NULL
;
1131 struct entry
*entry
;
1134 if ((q
= extfs_get_path (me
, path
, &archive
, 0)) == NULL
)
1136 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1140 entry
= extfs_resolve_symlinks (entry
);
1141 if ((!entry
) || (!S_ISDIR (entry
->inode
->mode
)))
1147 static off_t
extfs_lseek (void *data
, off_t offset
, int whence
)
1149 struct pseudofile
*file
= (struct pseudofile
*) data
;
1151 return lseek (file
->local_handle
, offset
, whence
);
1155 extfs_getid (struct vfs_class
*me
, const char *path
)
1157 struct archive
*archive
= NULL
;
1160 if (!(p
= extfs_get_path (me
, path
, &archive
, 1)))
1163 return (vfsid
) archive
;
1166 static int extfs_nothingisopen (vfsid id
)
1168 if (((struct archive
*)id
)->fd_usage
<= 0)
1173 static void extfs_remove_entry (struct entry
*e
)
1175 int i
= --(e
->inode
->nlink
);
1176 struct entry
*pe
, *ent
, *prev
;
1178 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1179 struct entry
*f
= e
->inode
->first_in_subdir
;
1180 e
->inode
->first_in_subdir
= NULL
;
1181 extfs_remove_entry (f
);
1184 if (e
== pe
->inode
->first_in_subdir
)
1185 pe
->inode
->first_in_subdir
= e
->next_in_dir
;
1188 for (ent
= pe
->inode
->first_in_subdir
; ent
&& ent
->next_in_dir
;
1189 ent
= ent
->next_in_dir
)
1190 if (e
== ent
->next_in_dir
) {
1195 prev
->next_in_dir
= e
->next_in_dir
;
1196 if (e
== pe
->inode
->last_in_subdir
)
1197 pe
->inode
->last_in_subdir
= prev
;
1200 if (e
->inode
->local_filename
!= NULL
) {
1201 unlink (e
->inode
->local_filename
);
1202 free (e
->inode
->local_filename
);
1204 g_free (e
->inode
->linkname
);
1212 static void extfs_free_entry (struct entry
*e
)
1214 int i
= --(e
->inode
->nlink
);
1215 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1216 struct entry
*f
= e
->inode
->first_in_subdir
;
1218 e
->inode
->first_in_subdir
= NULL
;
1219 extfs_free_entry (f
);
1222 if (e
->inode
->local_filename
!= NULL
) {
1223 unlink (e
->inode
->local_filename
);
1224 free (e
->inode
->local_filename
);
1226 g_free (e
->inode
->linkname
);
1229 if (e
->next_in_dir
!= NULL
)
1230 extfs_free_entry (e
->next_in_dir
);
1235 static void extfs_free (vfsid id
)
1237 struct archive
*parc
;
1238 struct archive
*archive
= (struct archive
*)id
;
1240 if (archive
== first_archive
) {
1241 first_archive
= archive
->next
;
1243 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
1244 if (parc
->next
== archive
) {
1245 parc
->next
= archive
->next
;
1249 extfs_free_archive (archive
);
1253 extfs_getlocalcopy (struct vfs_class
*me
, const char *path
)
1255 struct pseudofile
*fp
=
1256 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1261 if (fp
->entry
->inode
->local_filename
== NULL
) {
1262 extfs_close ((void *) fp
);
1265 p
= g_strdup (fp
->entry
->inode
->local_filename
);
1266 fp
->archive
->fd_usage
++;
1267 extfs_close ((void *) fp
);
1272 extfs_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1273 const char *local
, int has_changed
)
1275 struct pseudofile
*fp
=
1276 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1280 if (!strcmp (fp
->entry
->inode
->local_filename
, local
)) {
1281 fp
->archive
->fd_usage
--;
1282 fp
->has_changed
|= has_changed
;
1283 extfs_close ((void *) fp
);
1286 /* Should not happen */
1287 extfs_close ((void *) fp
);
1293 static int extfs_init (struct vfs_class
*me
)
1301 mc_extfsini
= mhl_str_dir_plus_file (mc_home
, "extfs" PATH_SEP_STR
"extfs.ini");
1302 cfg
= fopen (mc_extfsini
, "r");
1304 /* We may not use vfs_die() message or message or similar,
1305 * UI is not initialized at this time and message would not
1306 * appear on screen. */
1308 fprintf (stderr
, _("Warning: file %s not found\n"), mc_extfsini
);
1309 g_free (mc_extfsini
);
1314 while (extfs_no
< MAXEXTFS
&& fgets (key
, sizeof (key
), cfg
)) {
1317 /* Handle those with a trailing ':', those flag that the
1318 * file system does not require an archive to work
1322 fprintf(stderr
, "Warning: You need to update your %s file.\n",
1325 g_free (mc_extfsini
);
1328 if (*key
== '#' || *key
== '\n')
1331 if ((c
= strchr (key
, '\n'))){
1333 } else { /* Last line without newline or strlen (key) > 255 */
1334 c
= &key
[strlen (key
) - 1];
1336 extfs_need_archive
[extfs_no
] = !(*c
== ':');
1342 extfs_prefixes
[extfs_no
++] = g_strdup (key
);
1345 g_free (mc_extfsini
);
1349 static int extfs_which (struct vfs_class
*me
, const char *path
)
1355 for (i
= 0; i
< extfs_no
; i
++)
1356 if (!strcmp (path
, extfs_prefixes
[i
]))
1361 static void extfs_done (struct vfs_class
*me
)
1368 for (ar
= first_archive
; ar
!= NULL
;) {
1369 extfs_free ((vfsid
) ar
);
1373 for (i
= 0; i
< extfs_no
; i
++ )
1374 g_free (extfs_prefixes
[i
]);
1379 extfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1383 if (ctlop
== VFS_SETCTL_RUN
) {
1384 extfs_run (me
, path
);
1393 vfs_extfs_ops
.name
= "extfs";
1394 vfs_extfs_ops
.init
= extfs_init
;
1395 vfs_extfs_ops
.done
= extfs_done
;
1396 vfs_extfs_ops
.fill_names
= extfs_fill_names
;
1397 vfs_extfs_ops
.which
= extfs_which
;
1398 vfs_extfs_ops
.open
= extfs_open
;
1399 vfs_extfs_ops
.close
= extfs_close
;
1400 vfs_extfs_ops
.read
= extfs_read
;
1401 vfs_extfs_ops
.write
= extfs_write
;
1402 vfs_extfs_ops
.opendir
= extfs_opendir
;
1403 vfs_extfs_ops
.readdir
= extfs_readdir
;
1404 vfs_extfs_ops
.closedir
= extfs_closedir
;
1405 vfs_extfs_ops
.stat
= extfs_stat
;
1406 vfs_extfs_ops
.lstat
= extfs_lstat
;
1407 vfs_extfs_ops
.fstat
= extfs_fstat
;
1408 vfs_extfs_ops
.chmod
= extfs_chmod
;
1409 vfs_extfs_ops
.readlink
= extfs_readlink
;
1410 vfs_extfs_ops
.unlink
= extfs_unlink
;
1411 vfs_extfs_ops
.chdir
= extfs_chdir
;
1412 vfs_extfs_ops
.ferrno
= extfs_errno
;
1413 vfs_extfs_ops
.lseek
= extfs_lseek
;
1414 vfs_extfs_ops
.getid
= extfs_getid
;
1415 vfs_extfs_ops
.nothingisopen
= extfs_nothingisopen
;
1416 vfs_extfs_ops
.free
= extfs_free
;
1417 vfs_extfs_ops
.getlocalcopy
= extfs_getlocalcopy
;
1418 vfs_extfs_ops
.ungetlocalcopy
= extfs_ungetlocalcopy
;
1419 vfs_extfs_ops
.mkdir
= extfs_mkdir
;
1420 vfs_extfs_ops
.rmdir
= extfs_rmdir
;
1421 vfs_extfs_ops
.setctl
= extfs_setctl
;
1422 vfs_register_class (&vfs_extfs_ops
);