1 /* Virtual File System: External file system.
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009 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. */
25 * \brief Source: Virtual File System: External file system
26 * \author Jakub Jelinek
27 * \author Pavel Machek
28 * \author Andrew T. Veliath
29 * \date 1995, 1998, 1999
32 /* Namespace: init_extfs */
40 #include <sys/types.h>
48 #include "../src/global.h"
49 #include "../src/tty.h" /* enable/disable interrupt key */
50 #include "../src/wtools.h" /* message() */
51 #include "../src/main.h" /* print_vfs_message */
53 #include "../src/execute.h" /* For shell_execute */
56 #include "gc.h" /* vfs_rmstamp */
59 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
63 struct entry
*first_in_subdir
; /* only used if this is a directory */
64 struct entry
*last_in_subdir
;
65 ino_t inode
; /* This is inode # */
66 dev_t dev
; /* This is an internal identification of the extfs archive */
67 struct archive
*archive
; /* And this is an archive structure */
81 struct entry
*next_in_dir
;
88 struct archive
*archive
;
89 unsigned int has_changed
:1;
98 struct stat local_stat
;
102 struct entry
*root_entry
;
103 struct archive
*next
;
106 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
,
107 int make_dirs
, int make_file
);
108 static int extfs_which (struct vfs_class
*me
, const char *path
);
109 static void extfs_remove_entry (struct entry
*e
);
110 static void extfs_free (vfsid id
);
111 static void extfs_free_entry (struct entry
*e
);
113 static struct vfs_class vfs_extfs_ops
;
114 static struct archive
*first_archive
= NULL
;
115 static int my_errno
= 0;
118 static char *extfs_prefixes
[MAXEXTFS
];
119 static char extfs_need_archive
[MAXEXTFS
];
120 static int extfs_no
= 0;
123 extfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
125 struct archive
*a
= first_archive
;
132 g_strconcat (a
->name
? a
->name
: "", "#",
133 extfs_prefixes
[a
->fstype
], (char *) NULL
);
140 static void extfs_make_dots (struct entry
*ent
)
142 struct entry
*entry
= g_new (struct entry
, 1);
143 struct entry
*parentry
= ent
->dir
;
144 struct inode
*inode
= ent
->inode
, *parent
;
146 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
147 entry
->name
= g_strdup (".");
148 entry
->inode
= inode
;
150 inode
->local_filename
= NULL
;
151 inode
->first_in_subdir
= entry
;
153 entry
->next_in_dir
= g_new (struct entry
, 1);
154 entry
= entry
->next_in_dir
;
155 entry
->name
= g_strdup ("..");
156 inode
->last_in_subdir
= entry
;
157 entry
->next_in_dir
= NULL
;
158 if (parent
!= NULL
) {
159 entry
->inode
= parent
;
160 entry
->dir
= parentry
;
163 entry
->inode
= inode
;
169 static struct entry
*extfs_generate_entry (struct archive
*archive
,
170 const char *name
, struct entry
*parentry
, mode_t mode
)
173 struct inode
*inode
, *parent
;
176 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
177 entry
= g_new (struct entry
, 1);
179 entry
->name
= g_strdup (name
);
180 entry
->next_in_dir
= NULL
;
181 entry
->dir
= parentry
;
182 if (parent
!= NULL
) {
183 parent
->last_in_subdir
->next_in_dir
= entry
;
184 parent
->last_in_subdir
= entry
;
186 inode
= g_new (struct inode
, 1);
187 entry
->inode
= inode
;
188 inode
->local_filename
= NULL
;
189 inode
->linkname
= NULL
;
190 inode
->last_in_subdir
= NULL
;
191 inode
->inode
= (archive
->inode_counter
)++;
192 inode
->dev
= archive
->rdev
;
193 inode
->archive
= archive
;
194 myumask
= umask (022);
196 inode
->mode
= mode
& ~myumask
;
199 inode
->uid
= getuid ();
200 inode
->gid
= getgid ();
202 inode
->mtime
= time (NULL
);
203 inode
->atime
= inode
->mtime
;
204 inode
->ctime
= inode
->mtime
;
207 extfs_make_dots (entry
);
212 static void extfs_free_entries (struct entry
*entry
)
219 static void extfs_free_archive (struct archive
*archive
)
221 extfs_free_entry (archive
->root_entry
);
222 if (archive
->local_name
!= NULL
) {
225 mc_stat (archive
->local_name
, &my
);
226 mc_ungetlocalcopy (archive
->name
, archive
->local_name
,
227 archive
->local_stat
.st_mtime
!= my
.st_mtime
);
228 g_free(archive
->local_name
);
230 g_free (archive
->name
);
235 extfs_open_archive (int fstype
, const char *name
, struct archive
**pparc
)
237 static dev_t archive_counter
= 0;
243 struct archive
*current_archive
;
244 struct entry
*root_entry
;
245 char *local_name
= NULL
, *tmp
= 0;
246 int uses_archive
= extfs_need_archive
[fstype
];
249 if (mc_stat (name
, &mystat
) == -1)
251 if (!vfs_file_is_local (name
)) {
252 local_name
= mc_getlocalcopy (name
);
253 if (local_name
== NULL
)
256 tmp
= name_quote (name
, 0);
259 mc_extfsdir
= concat_dir_and_file (mc_home_alt
, "extfs" PATH_SEP_STR
);
261 g_strconcat (mc_extfsdir
, extfs_prefixes
[fstype
], " list ",
262 local_name
? local_name
: tmp
, (char *) NULL
);
264 g_free (mc_extfsdir
);
266 result
= popen (cmd
, "r");
268 if (result
== NULL
) {
269 close_error_pipe (D_ERROR
, NULL
);
271 mc_ungetlocalcopy (name
, local_name
, 0);
277 setvbuf (result
, NULL
, _IONBF
, 0);
280 current_archive
= g_new (struct archive
, 1);
281 current_archive
->fstype
= fstype
;
282 current_archive
->name
= name
? g_strdup (name
) : NULL
;
283 current_archive
->local_name
= local_name
;
285 if (local_name
!= NULL
)
286 mc_stat (local_name
, ¤t_archive
->local_stat
);
287 current_archive
->inode_counter
= 0;
288 current_archive
->fd_usage
= 0;
289 current_archive
->rdev
= archive_counter
++;
290 current_archive
->next
= first_archive
;
291 first_archive
= current_archive
;
292 mode
= mystat
.st_mode
& 07777;
300 root_entry
= extfs_generate_entry (current_archive
, "/", NULL
, mode
);
301 root_entry
->inode
->uid
= mystat
.st_uid
;
302 root_entry
->inode
->gid
= mystat
.st_gid
;
303 root_entry
->inode
->atime
= mystat
.st_atime
;
304 root_entry
->inode
->ctime
= mystat
.st_ctime
;
305 root_entry
->inode
->mtime
= mystat
.st_mtime
;
306 current_archive
->root_entry
= root_entry
;
308 *pparc
= current_archive
;
314 * Main loop for reading an archive.
315 * Return 0 on success, -1 on error.
318 extfs_read_archive (int fstype
, const char *name
, struct archive
**pparc
)
322 struct archive
*current_archive
;
323 char *current_file_name
, *current_link_name
;
326 extfs_open_archive (fstype
, name
, ¤t_archive
)) == NULL
) {
327 message (D_ERROR
, MSG_ERROR
, _("Cannot open %s archive\n%s"),
328 extfs_prefixes
[fstype
], name
);
332 buffer
= g_malloc (4096);
333 while (fgets (buffer
, 4096, extfsd
) != NULL
) {
336 current_link_name
= NULL
;
338 (buffer
, &hstat
, ¤t_file_name
, ¤t_link_name
)) {
339 struct entry
*entry
, *pent
;
341 char *p
, *q
, *cfn
= current_file_name
;
347 if (p
!= cfn
&& *(p
- 1) == '/')
349 p
= strrchr (cfn
, '/');
357 if (S_ISDIR (hstat
.st_mode
)
358 && (!strcmp (p
, ".") || !strcmp (p
, "..")))
359 goto read_extfs_continue
;
361 extfs_find_entry (current_archive
->root_entry
, q
, 1,
364 /* FIXME: Should clean everything one day */
367 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
370 entry
= g_new (struct entry
, 1);
371 entry
->name
= g_strdup (p
);
372 entry
->next_in_dir
= NULL
;
374 if (pent
->inode
->last_in_subdir
) {
375 pent
->inode
->last_in_subdir
->next_in_dir
= entry
;
376 pent
->inode
->last_in_subdir
= entry
;
378 if (!S_ISLNK (hstat
.st_mode
) && current_link_name
!= NULL
) {
380 extfs_find_entry (current_archive
->root_entry
,
381 current_link_name
, 0, 0);
383 /* FIXME: Should clean everything one day */
386 close_error_pipe (D_ERROR
,
387 _("Inconsistent extfs archive"));
390 entry
->inode
= pent
->inode
;
391 pent
->inode
->nlink
++;
394 inode
= g_new (struct inode
, 1);
395 entry
->inode
= inode
;
396 inode
->local_filename
= NULL
;
397 inode
->inode
= (current_archive
->inode_counter
)++;
399 inode
->dev
= current_archive
->rdev
;
400 inode
->archive
= current_archive
;
401 inode
->mode
= hstat
.st_mode
;
402 #ifdef HAVE_STRUCT_STAT_ST_RDEV
403 inode
->rdev
= hstat
.st_rdev
;
407 inode
->uid
= hstat
.st_uid
;
408 inode
->gid
= hstat
.st_gid
;
409 inode
->size
= hstat
.st_size
;
410 inode
->mtime
= hstat
.st_mtime
;
411 inode
->atime
= hstat
.st_atime
;
412 inode
->ctime
= hstat
.st_ctime
;
413 inode
->first_in_subdir
= NULL
;
414 inode
->last_in_subdir
= NULL
;
415 if (current_link_name
!= NULL
416 && S_ISLNK (hstat
.st_mode
)) {
417 inode
->linkname
= current_link_name
;
418 current_link_name
= NULL
;
420 if (S_ISLNK (hstat
.st_mode
))
421 inode
->mode
&= ~S_IFLNK
; /* You *DON'T* want to do this always */
422 inode
->linkname
= NULL
;
424 if (S_ISDIR (hstat
.st_mode
))
425 extfs_make_dots (entry
);
429 g_free (current_file_name
);
430 g_free (current_link_name
);
435 /* Check if extfs 'list' returned 0 */
436 if (pclose (extfsd
) != 0) {
437 extfs_free (current_archive
);
438 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
442 close_error_pipe (D_ERROR
, NULL
);
443 *pparc
= current_archive
;
448 * Dissect the path and create corresponding superblock. Note that inname
449 * can be changed and the result may point inside the original string.
452 extfs_get_path_mangle (struct vfs_class
*me
, char *inname
, struct archive
**archive
,
456 const char *archive_name
;
458 struct archive
*parc
;
461 archive_name
= inname
;
462 vfs_split (inname
, &local
, &op
);
464 fstype
= extfs_which (me
, op
);
469 local
= inname
+ strlen (inname
);
472 * All filesystems should have some local archive, at least
475 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
477 if (!strcmp (parc
->name
, archive_name
)) {
478 vfs_stamp (&vfs_extfs_ops
, (vfsid
) parc
);
484 do_not_open
? -1 : extfs_read_archive (fstype
, archive_name
,
496 * Dissect the path and create corresponding superblock.
497 * The result should be freed.
500 extfs_get_path (struct vfs_class
*me
, const char *inname
, struct archive
**archive
,
503 char *buf
= g_strdup (inname
);
504 char *res
= extfs_get_path_mangle (me
, buf
, archive
, do_not_open
);
507 res2
= g_strdup (res
);
513 /* Return allocated path (without leading slash) inside the archive */
514 static char *extfs_get_path_from_entry (struct entry
*entry
)
523 for (len
= 0, head
= 0; entry
->dir
; entry
= entry
->dir
) {
524 p
= g_new (struct list
, 1);
526 p
->name
= entry
->name
;
528 len
+= strlen (entry
->name
) + 1;
532 return g_strdup ("");
534 localpath
= g_malloc (len
);
537 strcat (localpath
, head
->name
);
539 strcat (localpath
, "/");
548 struct loop_protect
{
550 struct loop_protect
*next
;
555 static struct entry
*
556 extfs_find_entry_int (struct entry
*dir
, char *name
,
557 struct loop_protect
*list
, int make_dirs
, int make_file
);
559 static struct entry
*
560 extfs_resolve_symlinks_int (struct entry
*entry
,
561 struct loop_protect
*list
)
564 struct loop_protect
*looping
;
566 if (!S_ISLNK (entry
->inode
->mode
))
568 for (looping
= list
; looping
!= NULL
; looping
= looping
->next
)
569 if (entry
== looping
->entry
) { /* Here we protect us against symlink looping */
573 looping
= g_new (struct loop_protect
, 1);
574 looping
->entry
= entry
;
575 looping
->next
= list
;
576 pent
= extfs_find_entry_int (entry
->dir
, entry
->inode
->linkname
, looping
, 0, 0);
583 static struct entry
*extfs_resolve_symlinks (struct entry
*entry
)
589 res
= extfs_resolve_symlinks_int (entry
, NULL
);
600 extfs_get_archive_name (struct archive
*archive
)
602 const char *archive_name
;
604 if (archive
->local_name
)
605 archive_name
= archive
->local_name
;
607 archive_name
= archive
->name
;
609 if (!archive_name
|| !*archive_name
)
610 return "no_archive_name";
615 /* Don't pass localname as NULL */
617 extfs_cmd (const char *extfs_cmd
, struct archive
*archive
,
618 struct entry
*entry
, const char *localname
)
622 char *quoted_localname
;
628 file
= extfs_get_path_from_entry (entry
);
629 quoted_file
= name_quote (file
, 0);
631 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
632 quoted_localname
= name_quote (localname
, 0);
634 mc_extfsdir
= concat_dir_and_file (mc_home_alt
, "extfs" PATH_SEP_STR
);
635 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
636 extfs_cmd
, archive_name
, " ", quoted_file
, " ",
637 quoted_localname
, (char *) NULL
);
638 g_free (quoted_file
);
639 g_free (quoted_localname
);
640 g_free (mc_extfsdir
);
641 g_free (archive_name
);
644 retval
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
646 close_error_pipe (D_ERROR
, NULL
);
651 extfs_run (struct vfs_class
*me
, const char *file
)
653 struct archive
*archive
= NULL
;
654 char *p
, *q
, *archive_name
, *mc_extfsdir
;
657 if ((p
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
659 q
= name_quote (p
, 0);
662 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
663 mc_extfsdir
= concat_dir_and_file (mc_home_alt
, "extfs" PATH_SEP_STR
);
664 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
665 " run ", archive_name
, " ", q
, (char *) NULL
);
666 g_free (mc_extfsdir
);
667 g_free (archive_name
);
669 shell_execute (cmd
, 0);
674 extfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
676 struct pseudofile
*extfs_info
;
677 struct archive
*archive
= NULL
;
683 if ((q
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
685 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
686 if (entry
== NULL
&& (flags
& O_CREAT
)) {
687 /* Create new entry */
688 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 1);
689 created
= (entry
!= NULL
);
695 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
698 if (S_ISDIR (entry
->inode
->mode
))
699 ERRNOR (EISDIR
, NULL
);
701 if (entry
->inode
->local_filename
== NULL
) {
702 char *local_filename
;
704 local_handle
= vfs_mkstemps (&local_filename
, "extfs", entry
->name
);
706 if (local_handle
== -1)
708 close (local_handle
);
710 if (!created
&& !(flags
& O_TRUNC
)
711 && extfs_cmd (" copyout ", archive
, entry
, local_filename
)) {
712 unlink (local_filename
);
713 free (local_filename
);
717 entry
->inode
->local_filename
= local_filename
;
721 open (entry
->inode
->local_filename
, NO_LINEAR (flags
), mode
);
722 if (local_handle
== -1)
725 extfs_info
= g_new (struct pseudofile
, 1);
726 extfs_info
->archive
= archive
;
727 extfs_info
->entry
= entry
;
728 extfs_info
->has_changed
= created
;
729 extfs_info
->local_handle
= local_handle
;
731 /* i.e. we had no open files and now we have one */
732 vfs_rmstamp (&vfs_extfs_ops
, (vfsid
) archive
);
737 static ssize_t
extfs_read (void *data
, char *buffer
, int count
)
739 struct pseudofile
*file
= (struct pseudofile
*)data
;
741 return read (file
->local_handle
, buffer
, count
);
745 extfs_close (void *data
)
747 struct pseudofile
*file
;
749 file
= (struct pseudofile
*) data
;
751 close (file
->local_handle
);
753 /* Commit the file if it has changed */
754 if (file
->has_changed
) {
756 (" copyin ", file
->archive
, file
->entry
,
757 file
->entry
->inode
->local_filename
))
760 struct stat file_status
;
761 if (stat (file
->entry
->inode
->local_filename
, &file_status
) !=
765 file
->entry
->inode
->size
= file_status
.st_size
;
768 file
->entry
->inode
->mtime
= time (NULL
);
771 file
->archive
->fd_usage
--;
772 if (!file
->archive
->fd_usage
)
773 vfs_stamp_create (&vfs_extfs_ops
, file
->archive
);
781 #define RECORDSIZE 512
784 extfs_find_entry_int (struct entry
*dir
, char *name
,
785 struct loop_protect
*list
, int make_dirs
, int make_file
)
787 struct entry
*pent
, *pdir
;
788 char *p
, *q
, *name_end
;
791 if (*name
== '/') { /* Handle absolute paths */
793 dir
= dir
->inode
->archive
->root_entry
;
798 name_end
= name
+ strlen (name
);
804 for (; pent
!= NULL
&& c
&& *p
; ){
808 if (strcmp (p
, ".")){
809 if (!strcmp (p
, ".."))
812 if ((pent
= extfs_resolve_symlinks_int (pent
, list
))==NULL
){
816 if (!S_ISDIR (pent
->inode
->mode
)){
822 for (pent
= pent
->inode
->first_in_subdir
; pent
; pent
= pent
->next_in_dir
)
823 /* Hack: I keep the original semanthic unless
824 q+1 would break in the strchr */
825 if (!strcmp (pent
->name
, p
)){
826 if (q
+ 1 > name_end
){
828 notadir
= !S_ISDIR (pent
->inode
->mode
);
834 /* When we load archive, we create automagically
835 * non-existant directories
837 if (pent
== NULL
&& make_dirs
) {
838 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFDIR
| 0777);
840 if (pent
== NULL
&& make_file
) {
841 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFREG
| 0666);
857 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
, int make_dirs
, int make_file
)
863 res
= extfs_find_entry_int (dir
, name
, NULL
, make_dirs
, make_file
);
874 static int extfs_errno (struct vfs_class
*me
)
881 static void * extfs_opendir (struct vfs_class
*me
, const char *dirname
)
883 struct archive
*archive
= NULL
;
888 if ((q
= extfs_get_path (me
, dirname
, &archive
, 0)) == NULL
)
890 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
894 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
896 if (!S_ISDIR (entry
->inode
->mode
)) ERRNOR (ENOTDIR
, NULL
);
898 info
= g_new (struct entry
*, 2);
899 info
[0] = entry
->inode
->first_in_subdir
;
900 info
[1] = entry
->inode
->first_in_subdir
;
905 static void * extfs_readdir(void *data
)
907 static union vfs_dirent dir
;
908 struct entry
**info
= (struct entry
**) data
;
913 g_strlcpy(dir
.dent
.d_name
, (*info
)->name
, MC_MAXPATHLEN
);
915 compute_namelen(&dir
.dent
);
916 *info
= (*info
)->next_in_dir
;
918 return (void *) &dir
;
921 static int extfs_closedir (void *data
)
927 static void extfs_stat_move (struct stat
*buf
, const struct inode
*inode
)
929 buf
->st_dev
= inode
->dev
;
930 buf
->st_ino
= inode
->inode
;
931 buf
->st_mode
= inode
->mode
;
932 buf
->st_nlink
= inode
->nlink
;
933 buf
->st_uid
= inode
->uid
;
934 buf
->st_gid
= inode
->gid
;
935 #ifdef HAVE_STRUCT_STAT_ST_RDEV
936 buf
->st_rdev
= inode
->rdev
;
938 buf
->st_size
= inode
->size
;
939 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
940 buf
->st_blksize
= RECORDSIZE
;
942 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
943 buf
->st_blocks
= (inode
->size
+ RECORDSIZE
- 1) / RECORDSIZE
;
945 buf
->st_atime
= inode
->atime
;
946 buf
->st_mtime
= inode
->mtime
;
947 buf
->st_ctime
= inode
->ctime
;
951 extfs_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
,
954 struct archive
*archive
;
957 char *path2
= g_strdup (path
);
960 if ((q
= extfs_get_path_mangle (me
, path2
, &archive
, 0)) == NULL
)
962 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
965 if (resolve
&& (entry
= extfs_resolve_symlinks (entry
)) == NULL
)
967 extfs_stat_move (buf
, entry
->inode
);
974 static int extfs_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
976 return extfs_internal_stat (me
, path
, buf
, 1);
979 static int extfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
981 return extfs_internal_stat (me
, path
, buf
, 0);
984 static int extfs_fstat (void *data
, struct stat
*buf
)
986 struct pseudofile
*file
= (struct pseudofile
*)data
;
988 extfs_stat_move (buf
, file
->entry
->inode
);
993 extfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
995 struct archive
*archive
;
999 char *mpath
= g_strdup (path
);
1002 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1004 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1007 if (!S_ISLNK (entry
->inode
->mode
)) {
1008 me
->verrno
= EINVAL
;
1011 len
= strlen (entry
->inode
->linkname
);
1014 /* readlink() does not append a NUL character to buf */
1015 memcpy (buf
, entry
->inode
->linkname
, result
= len
);
1021 static int extfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1029 static ssize_t
extfs_write (void *data
, const char *buf
, int nbyte
)
1031 struct pseudofile
*file
= (struct pseudofile
*)data
;
1033 file
->has_changed
= 1;
1034 return write (file
->local_handle
, buf
, nbyte
);
1037 static int extfs_unlink (struct vfs_class
*me
, const char *file
)
1039 struct archive
*archive
;
1040 char *q
, *mpath
= g_strdup (file
);
1041 struct entry
*entry
;
1044 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1046 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1049 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1051 if (S_ISDIR (entry
->inode
->mode
)) {
1052 me
->verrno
= EISDIR
;
1055 if (extfs_cmd (" rm ", archive
, entry
, "")){
1059 extfs_remove_entry (entry
);
1066 static int extfs_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1068 struct archive
*archive
;
1069 char *q
, *mpath
= g_strdup(path
);
1070 struct entry
*entry
;
1075 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1077 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1078 if (entry
!= NULL
) {
1079 me
->verrno
= EEXIST
;
1082 entry
= extfs_find_entry (archive
->root_entry
, q
, 1, 0);
1085 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1087 if (!S_ISDIR (entry
->inode
->mode
)) {
1088 me
->verrno
= ENOTDIR
;
1092 if (extfs_cmd (" mkdir ", archive
, entry
, "")){
1094 extfs_remove_entry (entry
);
1103 static int extfs_rmdir (struct vfs_class
*me
, const char *path
)
1105 struct archive
*archive
;
1106 char *q
, *mpath
= g_strdup(path
);
1107 struct entry
*entry
;
1110 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1112 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1115 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1117 if (!S_ISDIR (entry
->inode
->mode
)) {
1118 me
->verrno
= ENOTDIR
;
1122 if (extfs_cmd (" rmdir ", archive
, entry
, "")){
1126 extfs_remove_entry (entry
);
1134 extfs_chdir (struct vfs_class
*me
, const char *path
)
1136 struct archive
*archive
= NULL
;
1138 struct entry
*entry
;
1141 if ((q
= extfs_get_path (me
, path
, &archive
, 0)) == NULL
)
1143 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1147 entry
= extfs_resolve_symlinks (entry
);
1148 if ((!entry
) || (!S_ISDIR (entry
->inode
->mode
)))
1154 static off_t
extfs_lseek (void *data
, off_t offset
, int whence
)
1156 struct pseudofile
*file
= (struct pseudofile
*) data
;
1158 return lseek (file
->local_handle
, offset
, whence
);
1162 extfs_getid (struct vfs_class
*me
, const char *path
)
1164 struct archive
*archive
= NULL
;
1167 if (!(p
= extfs_get_path (me
, path
, &archive
, 1)))
1170 return (vfsid
) archive
;
1173 static int extfs_nothingisopen (vfsid id
)
1175 if (((struct archive
*)id
)->fd_usage
<= 0)
1180 static void extfs_remove_entry (struct entry
*e
)
1182 int i
= --(e
->inode
->nlink
);
1183 struct entry
*pe
, *ent
, *prev
;
1185 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1186 struct entry
*f
= e
->inode
->first_in_subdir
;
1187 e
->inode
->first_in_subdir
= NULL
;
1188 extfs_remove_entry (f
);
1191 if (e
== pe
->inode
->first_in_subdir
)
1192 pe
->inode
->first_in_subdir
= e
->next_in_dir
;
1195 for (ent
= pe
->inode
->first_in_subdir
; ent
&& ent
->next_in_dir
;
1196 ent
= ent
->next_in_dir
)
1197 if (e
== ent
->next_in_dir
) {
1202 prev
->next_in_dir
= e
->next_in_dir
;
1203 if (e
== pe
->inode
->last_in_subdir
)
1204 pe
->inode
->last_in_subdir
= prev
;
1207 if (e
->inode
->local_filename
!= NULL
) {
1208 unlink (e
->inode
->local_filename
);
1209 free (e
->inode
->local_filename
);
1211 g_free (e
->inode
->linkname
);
1219 static void extfs_free_entry (struct entry
*e
)
1221 int i
= --(e
->inode
->nlink
);
1222 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1223 struct entry
*f
= e
->inode
->first_in_subdir
;
1225 e
->inode
->first_in_subdir
= NULL
;
1226 extfs_free_entry (f
);
1229 if (e
->inode
->local_filename
!= NULL
) {
1230 unlink (e
->inode
->local_filename
);
1231 free (e
->inode
->local_filename
);
1233 g_free (e
->inode
->linkname
);
1236 if (e
->next_in_dir
!= NULL
)
1237 extfs_free_entry (e
->next_in_dir
);
1242 static void extfs_free (vfsid id
)
1244 struct archive
*parc
;
1245 struct archive
*archive
= (struct archive
*)id
;
1247 if (archive
== first_archive
) {
1248 first_archive
= archive
->next
;
1250 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
1251 if (parc
->next
== archive
) {
1252 parc
->next
= archive
->next
;
1256 extfs_free_archive (archive
);
1260 extfs_getlocalcopy (struct vfs_class
*me
, const char *path
)
1262 struct pseudofile
*fp
=
1263 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1268 if (fp
->entry
->inode
->local_filename
== NULL
) {
1269 extfs_close ((void *) fp
);
1272 p
= g_strdup (fp
->entry
->inode
->local_filename
);
1273 fp
->archive
->fd_usage
++;
1274 extfs_close ((void *) fp
);
1279 extfs_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1280 const char *local
, int has_changed
)
1282 struct pseudofile
*fp
=
1283 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1287 if (!strcmp (fp
->entry
->inode
->local_filename
, local
)) {
1288 fp
->archive
->fd_usage
--;
1289 fp
->has_changed
|= has_changed
;
1290 extfs_close ((void *) fp
);
1293 /* Should not happen */
1294 extfs_close ((void *) fp
);
1300 static int extfs_init (struct vfs_class
*me
)
1308 mc_extfsini
= concat_dir_and_file (mc_home
, "extfs" PATH_SEP_STR
"extfs.ini");
1309 cfg
= fopen (mc_extfsini
, "r");
1311 /* We may not use vfs_die() message or message or similar,
1312 * UI is not initialized at this time and message would not
1313 * appear on screen. */
1315 fprintf (stderr
, _("Warning: file %s not found\n"), mc_extfsini
);
1316 g_free (mc_extfsini
);
1321 while (extfs_no
< MAXEXTFS
&& fgets (key
, sizeof (key
), cfg
)) {
1324 /* Handle those with a trailing ':', those flag that the
1325 * file system does not require an archive to work
1329 fprintf(stderr
, "Warning: You need to update your %s file.\n",
1332 g_free (mc_extfsini
);
1335 if (*key
== '#' || *key
== '\n')
1338 if ((c
= strchr (key
, '\n'))){
1340 } else { /* Last line without newline or strlen (key) > 255 */
1341 c
= &key
[strlen (key
) - 1];
1343 extfs_need_archive
[extfs_no
] = !(*c
== ':');
1349 extfs_prefixes
[extfs_no
++] = g_strdup (key
);
1352 g_free (mc_extfsini
);
1356 static int extfs_which (struct vfs_class
*me
, const char *path
)
1362 for (i
= 0; i
< extfs_no
; i
++)
1363 if (!strcmp (path
, extfs_prefixes
[i
]))
1368 static void extfs_done (struct vfs_class
*me
)
1375 for (ar
= first_archive
; ar
!= NULL
;) {
1376 extfs_free ((vfsid
) ar
);
1380 for (i
= 0; i
< extfs_no
; i
++ )
1381 g_free (extfs_prefixes
[i
]);
1386 extfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1390 if (ctlop
== VFS_SETCTL_RUN
) {
1391 extfs_run (me
, path
);
1400 vfs_extfs_ops
.name
= "extfs";
1401 vfs_extfs_ops
.init
= extfs_init
;
1402 vfs_extfs_ops
.done
= extfs_done
;
1403 vfs_extfs_ops
.fill_names
= extfs_fill_names
;
1404 vfs_extfs_ops
.which
= extfs_which
;
1405 vfs_extfs_ops
.open
= extfs_open
;
1406 vfs_extfs_ops
.close
= extfs_close
;
1407 vfs_extfs_ops
.read
= extfs_read
;
1408 vfs_extfs_ops
.write
= extfs_write
;
1409 vfs_extfs_ops
.opendir
= extfs_opendir
;
1410 vfs_extfs_ops
.readdir
= extfs_readdir
;
1411 vfs_extfs_ops
.closedir
= extfs_closedir
;
1412 vfs_extfs_ops
.stat
= extfs_stat
;
1413 vfs_extfs_ops
.lstat
= extfs_lstat
;
1414 vfs_extfs_ops
.fstat
= extfs_fstat
;
1415 vfs_extfs_ops
.chmod
= extfs_chmod
;
1416 vfs_extfs_ops
.readlink
= extfs_readlink
;
1417 vfs_extfs_ops
.unlink
= extfs_unlink
;
1418 vfs_extfs_ops
.chdir
= extfs_chdir
;
1419 vfs_extfs_ops
.ferrno
= extfs_errno
;
1420 vfs_extfs_ops
.lseek
= extfs_lseek
;
1421 vfs_extfs_ops
.getid
= extfs_getid
;
1422 vfs_extfs_ops
.nothingisopen
= extfs_nothingisopen
;
1423 vfs_extfs_ops
.free
= extfs_free
;
1424 vfs_extfs_ops
.getlocalcopy
= extfs_getlocalcopy
;
1425 vfs_extfs_ops
.ungetlocalcopy
= extfs_ungetlocalcopy
;
1426 vfs_extfs_ops
.mkdir
= extfs_mkdir
;
1427 vfs_extfs_ops
.rmdir
= extfs_rmdir
;
1428 vfs_extfs_ops
.setctl
= extfs_setctl
;
1429 vfs_register_class (&vfs_extfs_ops
);