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 "../src/global.h"
40 #include "../src/tty.h" /* enable/disable interrupt key */
41 #include "../src/wtools.h" /* message() */
42 #include "../src/main.h" /* print_vfs_message */
44 #include "../src/execute.h" /* For shell_execute */
47 #include "gc.h" /* vfs_rmstamp */
50 #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
54 struct entry
*first_in_subdir
; /* only used if this is a directory */
55 struct entry
*last_in_subdir
;
56 ino_t inode
; /* This is inode # */
57 dev_t dev
; /* This is an internal identification of the extfs archive */
58 struct archive
*archive
; /* And this is an archive structure */
72 struct entry
*next_in_dir
;
79 struct archive
*archive
;
80 unsigned int has_changed
:1;
89 struct stat local_stat
;
93 struct entry
*root_entry
;
97 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
,
98 int make_dirs
, int make_file
);
99 static int extfs_which (struct vfs_class
*me
, const char *path
);
100 static void extfs_remove_entry (struct entry
*e
);
101 static void extfs_free (vfsid id
);
102 static void extfs_free_entry (struct entry
*e
);
104 static struct vfs_class vfs_extfs_ops
;
105 static struct archive
*first_archive
= NULL
;
106 static int my_errno
= 0;
109 static char *extfs_prefixes
[MAXEXTFS
];
110 static char extfs_need_archive
[MAXEXTFS
];
111 static int extfs_no
= 0;
114 extfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
116 struct archive
*a
= first_archive
;
123 g_strconcat (a
->name
? a
->name
: "", "#",
124 extfs_prefixes
[a
->fstype
], (char *) NULL
);
131 static void extfs_make_dots (struct entry
*ent
)
133 struct entry
*entry
= g_new (struct entry
, 1);
134 struct entry
*parentry
= ent
->dir
;
135 struct inode
*inode
= ent
->inode
, *parent
;
137 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
138 entry
->name
= g_strdup (".");
139 entry
->inode
= inode
;
141 inode
->local_filename
= NULL
;
142 inode
->first_in_subdir
= entry
;
144 entry
->next_in_dir
= g_new (struct entry
, 1);
145 entry
= entry
->next_in_dir
;
146 entry
->name
= g_strdup ("..");
147 inode
->last_in_subdir
= entry
;
148 entry
->next_in_dir
= NULL
;
149 if (parent
!= NULL
) {
150 entry
->inode
= parent
;
151 entry
->dir
= parentry
;
154 entry
->inode
= inode
;
160 static struct entry
*extfs_generate_entry (struct archive
*archive
,
161 const char *name
, struct entry
*parentry
, mode_t mode
)
164 struct inode
*inode
, *parent
;
167 parent
= (parentry
!= NULL
) ? parentry
->inode
: NULL
;
168 entry
= g_new (struct entry
, 1);
170 entry
->name
= g_strdup (name
);
171 entry
->next_in_dir
= NULL
;
172 entry
->dir
= parentry
;
173 if (parent
!= NULL
) {
174 parent
->last_in_subdir
->next_in_dir
= entry
;
175 parent
->last_in_subdir
= entry
;
177 inode
= g_new (struct inode
, 1);
178 entry
->inode
= inode
;
179 inode
->local_filename
= NULL
;
180 inode
->linkname
= NULL
;
181 inode
->last_in_subdir
= NULL
;
182 inode
->inode
= (archive
->inode_counter
)++;
183 inode
->dev
= archive
->rdev
;
184 inode
->archive
= archive
;
185 myumask
= umask (022);
187 inode
->mode
= mode
& ~myumask
;
190 inode
->uid
= getuid ();
191 inode
->gid
= getgid ();
193 inode
->mtime
= time (NULL
);
194 inode
->atime
= inode
->mtime
;
195 inode
->ctime
= inode
->mtime
;
198 extfs_make_dots (entry
);
203 static void extfs_free_entries (struct entry
*entry
)
210 static void extfs_free_archive (struct archive
*archive
)
212 extfs_free_entry (archive
->root_entry
);
213 if (archive
->local_name
!= NULL
) {
216 mc_stat (archive
->local_name
, &my
);
217 mc_ungetlocalcopy (archive
->name
, archive
->local_name
,
218 archive
->local_stat
.st_mtime
!= my
.st_mtime
);
219 g_free(archive
->local_name
);
221 g_free (archive
->name
);
226 extfs_open_archive (int fstype
, const char *name
, struct archive
**pparc
)
228 static dev_t archive_counter
= 0;
234 struct archive
*current_archive
;
235 struct entry
*root_entry
;
236 char *local_name
= NULL
, *tmp
= 0;
237 int uses_archive
= extfs_need_archive
[fstype
];
240 if (mc_stat (name
, &mystat
) == -1)
242 if (!vfs_file_is_local (name
)) {
243 local_name
= mc_getlocalcopy (name
);
244 if (local_name
== NULL
)
247 tmp
= name_quote (name
, 0);
250 mc_extfsdir
= concat_dir_and_file (mc_home
, "extfs" PATH_SEP_STR
);
252 g_strconcat (mc_extfsdir
, extfs_prefixes
[fstype
], " list ",
253 local_name
? local_name
: tmp
, (char *) NULL
);
255 g_free (mc_extfsdir
);
257 result
= popen (cmd
, "r");
259 if (result
== NULL
) {
260 close_error_pipe (D_ERROR
, NULL
);
262 mc_ungetlocalcopy (name
, local_name
, 0);
268 setvbuf (result
, NULL
, _IONBF
, 0);
271 current_archive
= g_new (struct archive
, 1);
272 current_archive
->fstype
= fstype
;
273 current_archive
->name
= name
? g_strdup (name
) : NULL
;
274 current_archive
->local_name
= local_name
;
276 if (local_name
!= NULL
)
277 mc_stat (local_name
, ¤t_archive
->local_stat
);
278 current_archive
->inode_counter
= 0;
279 current_archive
->fd_usage
= 0;
280 current_archive
->rdev
= archive_counter
++;
281 current_archive
->next
= first_archive
;
282 first_archive
= current_archive
;
283 mode
= mystat
.st_mode
& 07777;
291 root_entry
= extfs_generate_entry (current_archive
, "/", NULL
, mode
);
292 root_entry
->inode
->uid
= mystat
.st_uid
;
293 root_entry
->inode
->gid
= mystat
.st_gid
;
294 root_entry
->inode
->atime
= mystat
.st_atime
;
295 root_entry
->inode
->ctime
= mystat
.st_ctime
;
296 root_entry
->inode
->mtime
= mystat
.st_mtime
;
297 current_archive
->root_entry
= root_entry
;
299 *pparc
= current_archive
;
305 * Main loop for reading an archive.
306 * Return 0 on success, -1 on error.
309 extfs_read_archive (int fstype
, const char *name
, struct archive
**pparc
)
313 struct archive
*current_archive
;
314 char *current_file_name
, *current_link_name
;
317 extfs_open_archive (fstype
, name
, ¤t_archive
)) == NULL
) {
318 message (D_ERROR
, MSG_ERROR
, _("Cannot open %s archive\n%s"),
319 extfs_prefixes
[fstype
], name
);
323 buffer
= g_malloc (4096);
324 while (fgets (buffer
, 4096, extfsd
) != NULL
) {
327 current_link_name
= NULL
;
329 (buffer
, &hstat
, ¤t_file_name
, ¤t_link_name
)) {
330 struct entry
*entry
, *pent
;
332 char *p
, *q
, *cfn
= current_file_name
;
338 if (p
!= cfn
&& *(p
- 1) == '/')
340 p
= strrchr (cfn
, '/');
348 if (S_ISDIR (hstat
.st_mode
)
349 && (!strcmp (p
, ".") || !strcmp (p
, "..")))
350 goto read_extfs_continue
;
352 extfs_find_entry (current_archive
->root_entry
, q
, 1,
355 /* FIXME: Should clean everything one day */
358 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
361 entry
= g_new (struct entry
, 1);
362 entry
->name
= g_strdup (p
);
363 entry
->next_in_dir
= NULL
;
365 if (pent
->inode
->last_in_subdir
) {
366 pent
->inode
->last_in_subdir
->next_in_dir
= entry
;
367 pent
->inode
->last_in_subdir
= entry
;
369 if (!S_ISLNK (hstat
.st_mode
) && current_link_name
!= NULL
) {
371 extfs_find_entry (current_archive
->root_entry
,
372 current_link_name
, 0, 0);
374 /* FIXME: Should clean everything one day */
377 close_error_pipe (D_ERROR
,
378 _("Inconsistent extfs archive"));
381 entry
->inode
= pent
->inode
;
382 pent
->inode
->nlink
++;
385 inode
= g_new (struct inode
, 1);
386 entry
->inode
= inode
;
387 inode
->local_filename
= NULL
;
388 inode
->inode
= (current_archive
->inode_counter
)++;
390 inode
->dev
= current_archive
->rdev
;
391 inode
->archive
= current_archive
;
392 inode
->mode
= hstat
.st_mode
;
393 #ifdef HAVE_STRUCT_STAT_ST_RDEV
394 inode
->rdev
= hstat
.st_rdev
;
398 inode
->uid
= hstat
.st_uid
;
399 inode
->gid
= hstat
.st_gid
;
400 inode
->size
= hstat
.st_size
;
401 inode
->mtime
= hstat
.st_mtime
;
402 inode
->atime
= hstat
.st_atime
;
403 inode
->ctime
= hstat
.st_ctime
;
404 inode
->first_in_subdir
= NULL
;
405 inode
->last_in_subdir
= NULL
;
406 if (current_link_name
!= NULL
407 && S_ISLNK (hstat
.st_mode
)) {
408 inode
->linkname
= current_link_name
;
409 current_link_name
= NULL
;
411 if (S_ISLNK (hstat
.st_mode
))
412 inode
->mode
&= ~S_IFLNK
; /* You *DON'T* want to do this always */
413 inode
->linkname
= NULL
;
415 if (S_ISDIR (hstat
.st_mode
))
416 extfs_make_dots (entry
);
420 g_free (current_file_name
);
421 g_free (current_link_name
);
426 /* Check if extfs 'list' returned 0 */
427 if (pclose (extfsd
) != 0) {
428 extfs_free (current_archive
);
429 close_error_pipe (D_ERROR
, _("Inconsistent extfs archive"));
433 close_error_pipe (D_ERROR
, NULL
);
434 *pparc
= current_archive
;
439 * Dissect the path and create corresponding superblock. Note that inname
440 * can be changed and the result may point inside the original string.
443 extfs_get_path_mangle (struct vfs_class
*me
, char *inname
, struct archive
**archive
,
447 const char *archive_name
;
449 struct archive
*parc
;
452 archive_name
= inname
;
453 vfs_split (inname
, &local
, &op
);
455 fstype
= extfs_which (me
, op
);
460 local
= inname
+ strlen (inname
);
463 * All filesystems should have some local archive, at least
466 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
468 if (!strcmp (parc
->name
, archive_name
)) {
469 vfs_stamp (&vfs_extfs_ops
, (vfsid
) parc
);
475 do_not_open
? -1 : extfs_read_archive (fstype
, archive_name
,
487 * Dissect the path and create corresponding superblock.
488 * The result should be freed.
491 extfs_get_path (struct vfs_class
*me
, const char *inname
, struct archive
**archive
,
494 char *buf
= g_strdup (inname
);
495 char *res
= extfs_get_path_mangle (me
, buf
, archive
, do_not_open
);
498 res2
= g_strdup (res
);
504 /* Return allocated path (without leading slash) inside the archive */
505 static char *extfs_get_path_from_entry (struct entry
*entry
)
514 for (len
= 0, head
= 0; entry
->dir
; entry
= entry
->dir
) {
515 p
= g_new (struct list
, 1);
517 p
->name
= entry
->name
;
519 len
+= strlen (entry
->name
) + 1;
523 return g_strdup ("");
525 localpath
= g_malloc (len
);
528 strcat (localpath
, head
->name
);
530 strcat (localpath
, "/");
539 struct loop_protect
{
541 struct loop_protect
*next
;
546 static struct entry
*
547 extfs_find_entry_int (struct entry
*dir
, char *name
,
548 struct loop_protect
*list
, int make_dirs
, int make_file
);
550 static struct entry
*
551 extfs_resolve_symlinks_int (struct entry
*entry
,
552 struct loop_protect
*list
)
555 struct loop_protect
*looping
;
557 if (!S_ISLNK (entry
->inode
->mode
))
559 for (looping
= list
; looping
!= NULL
; looping
= looping
->next
)
560 if (entry
== looping
->entry
) { /* Here we protect us against symlink looping */
564 looping
= g_new (struct loop_protect
, 1);
565 looping
->entry
= entry
;
566 looping
->next
= list
;
567 pent
= extfs_find_entry_int (entry
->dir
, entry
->inode
->linkname
, looping
, 0, 0);
574 static struct entry
*extfs_resolve_symlinks (struct entry
*entry
)
580 res
= extfs_resolve_symlinks_int (entry
, NULL
);
591 extfs_get_archive_name (struct archive
*archive
)
593 const char *archive_name
;
595 if (archive
->local_name
)
596 archive_name
= archive
->local_name
;
598 archive_name
= archive
->name
;
600 if (!archive_name
|| !*archive_name
)
601 return "no_archive_name";
606 /* Don't pass localname as NULL */
608 extfs_cmd (const char *extfs_cmd
, struct archive
*archive
,
609 struct entry
*entry
, const char *localname
)
613 char *quoted_localname
;
619 file
= extfs_get_path_from_entry (entry
);
620 quoted_file
= name_quote (file
, 0);
622 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
623 quoted_localname
= name_quote (localname
, 0);
625 mc_extfsdir
= concat_dir_and_file (mc_home
, "extfs" PATH_SEP_STR
);
626 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
627 extfs_cmd
, archive_name
, " ", quoted_file
, " ",
628 quoted_localname
, (char *) NULL
);
629 g_free (quoted_file
);
630 g_free (quoted_localname
);
631 g_free (mc_extfsdir
);
632 g_free (archive_name
);
635 retval
= my_system (EXECUTE_AS_SHELL
, shell
, cmd
);
637 close_error_pipe (D_ERROR
, NULL
);
642 extfs_run (struct vfs_class
*me
, const char *file
)
644 struct archive
*archive
= NULL
;
645 char *p
, *q
, *archive_name
, *mc_extfsdir
;
648 if ((p
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
650 q
= name_quote (p
, 0);
653 archive_name
= name_quote (extfs_get_archive_name (archive
), 0);
654 mc_extfsdir
= concat_dir_and_file (mc_home
, "extfs" PATH_SEP_STR
);
655 cmd
= g_strconcat (mc_extfsdir
, extfs_prefixes
[archive
->fstype
],
656 " run ", archive_name
, " ", q
, (char *) NULL
);
657 g_free (mc_extfsdir
);
658 g_free (archive_name
);
660 shell_execute (cmd
, 0);
665 extfs_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
667 struct pseudofile
*extfs_info
;
668 struct archive
*archive
= NULL
;
674 if ((q
= extfs_get_path (me
, file
, &archive
, 0)) == NULL
)
676 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
677 if (entry
== NULL
&& (flags
& O_CREAT
)) {
678 /* Create new entry */
679 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 1);
680 created
= (entry
!= NULL
);
686 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
689 if (S_ISDIR (entry
->inode
->mode
))
690 ERRNOR (EISDIR
, NULL
);
692 if (entry
->inode
->local_filename
== NULL
) {
693 char *local_filename
;
695 local_handle
= vfs_mkstemps (&local_filename
, "extfs", entry
->name
);
697 if (local_handle
== -1)
699 close (local_handle
);
701 if (!created
&& !(flags
& O_TRUNC
)
702 && extfs_cmd (" copyout ", archive
, entry
, local_filename
)) {
703 unlink (local_filename
);
704 free (local_filename
);
708 entry
->inode
->local_filename
= local_filename
;
712 open (entry
->inode
->local_filename
, NO_LINEAR (flags
), mode
);
713 if (local_handle
== -1)
716 extfs_info
= g_new (struct pseudofile
, 1);
717 extfs_info
->archive
= archive
;
718 extfs_info
->entry
= entry
;
719 extfs_info
->has_changed
= created
;
720 extfs_info
->local_handle
= local_handle
;
722 /* i.e. we had no open files and now we have one */
723 vfs_rmstamp (&vfs_extfs_ops
, (vfsid
) archive
);
728 static ssize_t
extfs_read (void *data
, char *buffer
, int count
)
730 struct pseudofile
*file
= (struct pseudofile
*)data
;
732 return read (file
->local_handle
, buffer
, count
);
736 extfs_close (void *data
)
738 struct pseudofile
*file
;
740 file
= (struct pseudofile
*) data
;
742 close (file
->local_handle
);
744 /* Commit the file if it has changed */
745 if (file
->has_changed
) {
747 (" copyin ", file
->archive
, file
->entry
,
748 file
->entry
->inode
->local_filename
))
751 struct stat file_status
;
752 if (stat (file
->entry
->inode
->local_filename
, &file_status
) !=
756 file
->entry
->inode
->size
= file_status
.st_size
;
759 file
->entry
->inode
->mtime
= time (NULL
);
762 file
->archive
->fd_usage
--;
763 if (!file
->archive
->fd_usage
)
764 vfs_stamp_create (&vfs_extfs_ops
, file
->archive
);
772 #define RECORDSIZE 512
775 extfs_find_entry_int (struct entry
*dir
, char *name
,
776 struct loop_protect
*list
, int make_dirs
, int make_file
)
778 struct entry
*pent
, *pdir
;
779 char *p
, *q
, *name_end
;
782 if (*name
== '/') { /* Handle absolute paths */
784 dir
= dir
->inode
->archive
->root_entry
;
789 name_end
= name
+ strlen (name
);
795 for (; pent
!= NULL
&& c
&& *p
; ){
799 if (strcmp (p
, ".")){
800 if (!strcmp (p
, ".."))
803 if ((pent
= extfs_resolve_symlinks_int (pent
, list
))==NULL
){
807 if (!S_ISDIR (pent
->inode
->mode
)){
813 for (pent
= pent
->inode
->first_in_subdir
; pent
; pent
= pent
->next_in_dir
)
814 /* Hack: I keep the original semanthic unless
815 q+1 would break in the strchr */
816 if (!strcmp (pent
->name
, p
)){
817 if (q
+ 1 > name_end
){
819 notadir
= !S_ISDIR (pent
->inode
->mode
);
825 /* When we load archive, we create automagically
826 * non-existant directories
828 if (pent
== NULL
&& make_dirs
) {
829 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFDIR
| 0777);
831 if (pent
== NULL
&& make_file
) {
832 pent
= extfs_generate_entry (dir
->inode
->archive
, p
, pdir
, S_IFREG
| 0666);
848 static struct entry
*extfs_find_entry (struct entry
*dir
, char *name
, int make_dirs
, int make_file
)
854 res
= extfs_find_entry_int (dir
, name
, NULL
, make_dirs
, make_file
);
865 static int extfs_errno (struct vfs_class
*me
)
872 static void * extfs_opendir (struct vfs_class
*me
, const char *dirname
)
874 struct archive
*archive
= NULL
;
879 if ((q
= extfs_get_path (me
, dirname
, &archive
, 0)) == NULL
)
881 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
885 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
887 if (!S_ISDIR (entry
->inode
->mode
)) ERRNOR (ENOTDIR
, NULL
);
889 info
= g_new (struct entry
*, 2);
890 info
[0] = entry
->inode
->first_in_subdir
;
891 info
[1] = entry
->inode
->first_in_subdir
;
896 static void * extfs_readdir(void *data
)
898 static union vfs_dirent dir
;
899 struct entry
**info
= (struct entry
**) data
;
904 g_strlcpy(dir
.dent
.d_name
, (*info
)->name
, MC_MAXPATHLEN
);
906 compute_namelen(&dir
.dent
);
907 *info
= (*info
)->next_in_dir
;
909 return (void *) &dir
;
912 static int extfs_closedir (void *data
)
918 static void extfs_stat_move (struct stat
*buf
, const struct inode
*inode
)
920 buf
->st_dev
= inode
->dev
;
921 buf
->st_ino
= inode
->inode
;
922 buf
->st_mode
= inode
->mode
;
923 buf
->st_nlink
= inode
->nlink
;
924 buf
->st_uid
= inode
->uid
;
925 buf
->st_gid
= inode
->gid
;
926 #ifdef HAVE_STRUCT_STAT_ST_RDEV
927 buf
->st_rdev
= inode
->rdev
;
929 buf
->st_size
= inode
->size
;
930 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
931 buf
->st_blksize
= RECORDSIZE
;
933 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
934 buf
->st_blocks
= (inode
->size
+ RECORDSIZE
- 1) / RECORDSIZE
;
936 buf
->st_atime
= inode
->atime
;
937 buf
->st_mtime
= inode
->mtime
;
938 buf
->st_ctime
= inode
->ctime
;
942 extfs_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
,
945 struct archive
*archive
;
948 char *path2
= g_strdup (path
);
951 if ((q
= extfs_get_path_mangle (me
, path2
, &archive
, 0)) == NULL
)
953 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
956 if (resolve
&& (entry
= extfs_resolve_symlinks (entry
)) == NULL
)
958 extfs_stat_move (buf
, entry
->inode
);
965 static int extfs_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
967 return extfs_internal_stat (me
, path
, buf
, 1);
970 static int extfs_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
972 return extfs_internal_stat (me
, path
, buf
, 0);
975 static int extfs_fstat (void *data
, struct stat
*buf
)
977 struct pseudofile
*file
= (struct pseudofile
*)data
;
979 extfs_stat_move (buf
, file
->entry
->inode
);
984 extfs_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
986 struct archive
*archive
;
990 char *mpath
= g_strdup (path
);
993 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
995 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
998 if (!S_ISLNK (entry
->inode
->mode
)) {
1002 len
= strlen (entry
->inode
->linkname
);
1005 /* readlink() does not append a NUL character to buf */
1006 memcpy (buf
, entry
->inode
->linkname
, result
= len
);
1012 static int extfs_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1020 static ssize_t
extfs_write (void *data
, const char *buf
, int nbyte
)
1022 struct pseudofile
*file
= (struct pseudofile
*)data
;
1024 file
->has_changed
= 1;
1025 return write (file
->local_handle
, buf
, nbyte
);
1028 static int extfs_unlink (struct vfs_class
*me
, const char *file
)
1030 struct archive
*archive
;
1031 char *q
, *mpath
= g_strdup (file
);
1032 struct entry
*entry
;
1035 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1037 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1040 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1042 if (S_ISDIR (entry
->inode
->mode
)) {
1043 me
->verrno
= EISDIR
;
1046 if (extfs_cmd (" rm ", archive
, entry
, "")){
1050 extfs_remove_entry (entry
);
1057 static int extfs_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1059 struct archive
*archive
;
1060 char *q
, *mpath
= g_strdup(path
);
1061 struct entry
*entry
;
1066 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1068 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1069 if (entry
!= NULL
) {
1070 me
->verrno
= EEXIST
;
1073 entry
= extfs_find_entry (archive
->root_entry
, q
, 1, 0);
1076 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1078 if (!S_ISDIR (entry
->inode
->mode
)) {
1079 me
->verrno
= ENOTDIR
;
1083 if (extfs_cmd (" mkdir ", archive
, entry
, "")){
1085 extfs_remove_entry (entry
);
1094 static int extfs_rmdir (struct vfs_class
*me
, const char *path
)
1096 struct archive
*archive
;
1097 char *q
, *mpath
= g_strdup(path
);
1098 struct entry
*entry
;
1101 if ((q
= extfs_get_path_mangle (me
, mpath
, &archive
, 0)) == NULL
)
1103 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1106 if ((entry
= extfs_resolve_symlinks (entry
)) == NULL
)
1108 if (!S_ISDIR (entry
->inode
->mode
)) {
1109 me
->verrno
= ENOTDIR
;
1113 if (extfs_cmd (" rmdir ", archive
, entry
, "")){
1117 extfs_remove_entry (entry
);
1125 extfs_chdir (struct vfs_class
*me
, const char *path
)
1127 struct archive
*archive
= NULL
;
1129 struct entry
*entry
;
1132 if ((q
= extfs_get_path (me
, path
, &archive
, 0)) == NULL
)
1134 entry
= extfs_find_entry (archive
->root_entry
, q
, 0, 0);
1138 entry
= extfs_resolve_symlinks (entry
);
1139 if ((!entry
) || (!S_ISDIR (entry
->inode
->mode
)))
1145 static off_t
extfs_lseek (void *data
, off_t offset
, int whence
)
1147 struct pseudofile
*file
= (struct pseudofile
*) data
;
1149 return lseek (file
->local_handle
, offset
, whence
);
1153 extfs_getid (struct vfs_class
*me
, const char *path
)
1155 struct archive
*archive
= NULL
;
1158 if (!(p
= extfs_get_path (me
, path
, &archive
, 1)))
1161 return (vfsid
) archive
;
1164 static int extfs_nothingisopen (vfsid id
)
1166 if (((struct archive
*)id
)->fd_usage
<= 0)
1171 static void extfs_remove_entry (struct entry
*e
)
1173 int i
= --(e
->inode
->nlink
);
1174 struct entry
*pe
, *ent
, *prev
;
1176 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1177 struct entry
*f
= e
->inode
->first_in_subdir
;
1178 e
->inode
->first_in_subdir
= NULL
;
1179 extfs_remove_entry (f
);
1182 if (e
== pe
->inode
->first_in_subdir
)
1183 pe
->inode
->first_in_subdir
= e
->next_in_dir
;
1186 for (ent
= pe
->inode
->first_in_subdir
; ent
&& ent
->next_in_dir
;
1187 ent
= ent
->next_in_dir
)
1188 if (e
== ent
->next_in_dir
) {
1193 prev
->next_in_dir
= e
->next_in_dir
;
1194 if (e
== pe
->inode
->last_in_subdir
)
1195 pe
->inode
->last_in_subdir
= prev
;
1198 if (e
->inode
->local_filename
!= NULL
) {
1199 unlink (e
->inode
->local_filename
);
1200 free (e
->inode
->local_filename
);
1202 g_free (e
->inode
->linkname
);
1210 static void extfs_free_entry (struct entry
*e
)
1212 int i
= --(e
->inode
->nlink
);
1213 if (S_ISDIR (e
->inode
->mode
) && e
->inode
->first_in_subdir
!= NULL
) {
1214 struct entry
*f
= e
->inode
->first_in_subdir
;
1216 e
->inode
->first_in_subdir
= NULL
;
1217 extfs_free_entry (f
);
1220 if (e
->inode
->local_filename
!= NULL
) {
1221 unlink (e
->inode
->local_filename
);
1222 free (e
->inode
->local_filename
);
1224 g_free (e
->inode
->linkname
);
1227 if (e
->next_in_dir
!= NULL
)
1228 extfs_free_entry (e
->next_in_dir
);
1233 static void extfs_free (vfsid id
)
1235 struct archive
*parc
;
1236 struct archive
*archive
= (struct archive
*)id
;
1238 if (archive
== first_archive
) {
1239 first_archive
= archive
->next
;
1241 for (parc
= first_archive
; parc
!= NULL
; parc
= parc
->next
)
1242 if (parc
->next
== archive
) {
1243 parc
->next
= archive
->next
;
1247 extfs_free_archive (archive
);
1251 extfs_getlocalcopy (struct vfs_class
*me
, const char *path
)
1253 struct pseudofile
*fp
=
1254 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1259 if (fp
->entry
->inode
->local_filename
== NULL
) {
1260 extfs_close ((void *) fp
);
1263 p
= g_strdup (fp
->entry
->inode
->local_filename
);
1264 fp
->archive
->fd_usage
++;
1265 extfs_close ((void *) fp
);
1270 extfs_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1271 const char *local
, int has_changed
)
1273 struct pseudofile
*fp
=
1274 (struct pseudofile
*) extfs_open (me
, path
, O_RDONLY
, 0);
1278 if (!strcmp (fp
->entry
->inode
->local_filename
, local
)) {
1279 fp
->archive
->fd_usage
--;
1280 fp
->has_changed
|= has_changed
;
1281 extfs_close ((void *) fp
);
1284 /* Should not happen */
1285 extfs_close ((void *) fp
);
1291 static int extfs_init (struct vfs_class
*me
)
1299 mc_extfsini
= concat_dir_and_file (mc_home
, "extfs" PATH_SEP_STR
"extfs.ini");
1300 cfg
= fopen (mc_extfsini
, "r");
1302 /* We may not use vfs_die() message or message or similar,
1303 * UI is not initialized at this time and message would not
1304 * appear on screen. */
1306 fprintf (stderr
, _("Warning: file %s not found\n"), mc_extfsini
);
1307 g_free (mc_extfsini
);
1312 while (extfs_no
< MAXEXTFS
&& fgets (key
, sizeof (key
), cfg
)) {
1315 /* Handle those with a trailing ':', those flag that the
1316 * file system does not require an archive to work
1320 fprintf(stderr
, "Warning: You need to update your %s file.\n",
1323 g_free (mc_extfsini
);
1326 if (*key
== '#' || *key
== '\n')
1329 if ((c
= strchr (key
, '\n'))){
1331 } else { /* Last line without newline or strlen (key) > 255 */
1332 c
= &key
[strlen (key
) - 1];
1334 extfs_need_archive
[extfs_no
] = !(*c
== ':');
1340 extfs_prefixes
[extfs_no
++] = g_strdup (key
);
1343 g_free (mc_extfsini
);
1347 static int extfs_which (struct vfs_class
*me
, const char *path
)
1353 for (i
= 0; i
< extfs_no
; i
++)
1354 if (!strcmp (path
, extfs_prefixes
[i
]))
1359 static void extfs_done (struct vfs_class
*me
)
1366 for (ar
= first_archive
; ar
!= NULL
;) {
1367 extfs_free ((vfsid
) ar
);
1371 for (i
= 0; i
< extfs_no
; i
++ )
1372 g_free (extfs_prefixes
[i
]);
1377 extfs_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1381 if (ctlop
== VFS_SETCTL_RUN
) {
1382 extfs_run (me
, path
);
1391 vfs_extfs_ops
.name
= "extfs";
1392 vfs_extfs_ops
.init
= extfs_init
;
1393 vfs_extfs_ops
.done
= extfs_done
;
1394 vfs_extfs_ops
.fill_names
= extfs_fill_names
;
1395 vfs_extfs_ops
.which
= extfs_which
;
1396 vfs_extfs_ops
.open
= extfs_open
;
1397 vfs_extfs_ops
.close
= extfs_close
;
1398 vfs_extfs_ops
.read
= extfs_read
;
1399 vfs_extfs_ops
.write
= extfs_write
;
1400 vfs_extfs_ops
.opendir
= extfs_opendir
;
1401 vfs_extfs_ops
.readdir
= extfs_readdir
;
1402 vfs_extfs_ops
.closedir
= extfs_closedir
;
1403 vfs_extfs_ops
.stat
= extfs_stat
;
1404 vfs_extfs_ops
.lstat
= extfs_lstat
;
1405 vfs_extfs_ops
.fstat
= extfs_fstat
;
1406 vfs_extfs_ops
.chmod
= extfs_chmod
;
1407 vfs_extfs_ops
.readlink
= extfs_readlink
;
1408 vfs_extfs_ops
.unlink
= extfs_unlink
;
1409 vfs_extfs_ops
.chdir
= extfs_chdir
;
1410 vfs_extfs_ops
.ferrno
= extfs_errno
;
1411 vfs_extfs_ops
.lseek
= extfs_lseek
;
1412 vfs_extfs_ops
.getid
= extfs_getid
;
1413 vfs_extfs_ops
.nothingisopen
= extfs_nothingisopen
;
1414 vfs_extfs_ops
.free
= extfs_free
;
1415 vfs_extfs_ops
.getlocalcopy
= extfs_getlocalcopy
;
1416 vfs_extfs_ops
.ungetlocalcopy
= extfs_ungetlocalcopy
;
1417 vfs_extfs_ops
.mkdir
= extfs_mkdir
;
1418 vfs_extfs_ops
.rmdir
= extfs_rmdir
;
1419 vfs_extfs_ops
.setctl
= extfs_setctl
;
1420 vfs_register_class (&vfs_extfs_ops
);