3 * \brief Source: directory cache support
5 * So that you do not have copy of this in each and every filesystem.
7 * Very loosely based on tar.c from midnight and archives.[ch] from
8 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
10 * Unfortunately, I was unable to keep all filesystems
11 * uniform. tar-like filesystems use tree structure where each
12 * directory has pointers to its subdirectories. We can do this
13 * because we have full information about our archive.
15 * At ftp-like filesystems, situation is a little bit different. When
16 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
17 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
18 * listed. That means that we do not have complete information, and if
19 * /usr is symlink to /4, we will not know. Also we have to time out
20 * entries and things would get messy with tree-like approach. So we
21 * do different trick: root directory is completely special and
22 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
23 * and we'll try to use custom find_entry function.
25 * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
28 * \warning Paths here do _not_ begin with '/', so root directory of
29 * archive/site is simply "".
35 #include <sys/fcntl.h>
37 #include <sys/time.h> /* gettimeofday() */
39 #include "../src/global.h"
41 #include "../src/tty/tty.h" /* enable/disable interrupt key */
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
48 #include "gc.h" /* vfs_rmstamp */
49 #include "xdirentry.h"
51 #define CALL(x) if (MEDATA->x) MEDATA->x
53 static volatile int total_inodes
= 0, total_entries
= 0;
56 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
58 struct vfs_s_inode
*ino
;
60 ino
= g_new0 (struct vfs_s_inode
, 1);
68 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
69 ino
->st
.st_dev
= MEDATA
->rdev
;
74 CALL (init_inode
) (me
, ino
);
80 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
82 struct vfs_s_entry
*entry
;
84 entry
= g_new0 (struct vfs_s_entry
, 1);
88 entry
->name
= g_strdup (name
);
91 entry
->ino
->ent
= entry
;
92 CALL (init_entry
) (me
, entry
);
98 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
101 vfs_die ("Don't pass NULL to me");
103 /* ==0 can happen if freshly created entry is deleted */
104 if (ino
->st
.st_nlink
<= 1){
106 vfs_s_free_entry (me
, ino
->subdir
);
109 CALL (free_inode
) (me
, ino
);
110 g_free (ino
->linkname
);
112 unlink (ino
->localname
);
113 g_free(ino
->localname
);
116 ino
->super
->ino_usage
--;
118 } else ino
->st
.st_nlink
--;
122 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
124 if (ent
->prevp
){ /* It is possible that we are deleting freshly created entry */
125 *ent
->prevp
= ent
->next
;
127 ent
->next
->prevp
= ent
->prevp
;
134 ent
->ino
->ent
= NULL
;
135 vfs_s_free_inode (me
, ent
->ino
);
144 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
146 struct vfs_s_entry
**ep
;
150 for (ep
= &dir
->subdir
; *ep
!= NULL
; ep
= &((*ep
)->next
))
157 ent
->ino
->st
.st_nlink
++;
161 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
163 static struct stat st
;
168 myumask
= umask (022);
176 st
.st_uid
= getuid ();
177 st
.st_gid
= getgid ();
179 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
185 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
, mode_t mode
)
187 struct vfs_s_inode
*inode
;
190 st
= vfs_s_default_stat (me
, mode
);
191 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
193 return vfs_s_new_entry (me
, name
, inode
);
196 /* We were asked to create entries automagically */
197 static struct vfs_s_entry
*
198 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
200 struct vfs_s_entry
*res
;
201 char *sep
= strchr (path
, PATH_SEP
);
205 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
206 vfs_s_insert_entry (me
, dir
, res
);
214 /* If the entry is a symlink, find the entry for its target */
215 static struct vfs_s_entry
*
216 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
,
220 char *fullname
= NULL
;
221 struct vfs_s_entry
*target
;
223 if (follow
== LINK_NO_FOLLOW
)
226 ERRNOR (ELOOP
, NULL
);
228 ERRNOR (ENOENT
, NULL
);
229 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
232 linkname
= entry
->ino
->linkname
;
233 if (linkname
== NULL
)
234 ERRNOR (EFAULT
, NULL
);
236 /* make full path from relative */
237 if (*linkname
!= PATH_SEP
) {
238 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
240 fullname
= g_strconcat (fullpath
, "/", linkname
, NULL
);
247 (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
,
254 * Follow > 0: follow links, serves as loop protect,
255 * == -1: do not follow links
257 static struct vfs_s_entry
*
258 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
259 const char *a_path
, int follow
, int flags
)
262 struct vfs_s_entry
*ent
= NULL
;
263 char * const pathref
= g_strdup (a_path
);
264 char *path
= pathref
;
266 canonicalize_pathname (path
);
269 while (*path
== PATH_SEP
) /* Strip leading '/' */
277 for (pseg
= 0; path
[pseg
] && path
[pseg
] != PATH_SEP
; pseg
++);
279 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
280 if (strlen (ent
->name
) == pseg
281 && (!strncmp (ent
->name
, path
, pseg
)))
285 if (!ent
&& (flags
& (FL_MKFILE
| FL_MKDIR
)))
286 ent
= vfs_s_automake (me
, root
, path
, flags
);
292 /* here we must follow leading directories always;
293 only the actual file is optional */
295 vfs_s_resolve_symlink (me
, ent
,
297 PATH_SEP
) ? LINK_FOLLOW
:
309 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
315 s
= strrchr (path
, PATH_SEP
);
319 *dir
= path
+ strlen(path
); /* an empty string */
328 static struct vfs_s_entry
*
329 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
330 const char *a_path
, int follow
, int flags
)
332 struct vfs_s_entry
*ent
= NULL
;
333 char * const path
= g_strdup (a_path
);
334 struct vfs_s_entry
*retval
= NULL
;
336 if (root
->super
->root
!= root
)
337 vfs_die ("We have to use _real_ root. Always. Sorry.");
339 canonicalize_pathname (path
);
341 if (!(flags
& FL_DIR
)) {
342 char *dirname
, *name
, *save
;
343 struct vfs_s_inode
*ino
;
344 split_dir_name (me
, path
, &dirname
, &name
, &save
);
346 vfs_s_find_inode (me
, root
->super
, dirname
, follow
,
350 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
355 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
356 if (!strcmp (ent
->name
, path
))
359 if (ent
&& (!(MEDATA
->dir_uptodate
) (me
, ent
->ino
))) {
361 print_vfs_message (_("Directory cache expired for %s"), path
);
363 vfs_s_free_entry (me
, ent
);
368 struct vfs_s_inode
*ino
;
371 vfs_s_new_inode (me
, root
->super
,
372 vfs_s_default_stat (me
, S_IFDIR
| 0755));
373 ent
= vfs_s_new_entry (me
, path
, ino
);
374 if ((MEDATA
->dir_load
) (me
, ino
, path
) == -1) {
375 vfs_s_free_entry (me
, ent
);
379 vfs_s_insert_entry (me
, root
, ent
);
381 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
382 if (!strcmp (ent
->name
, path
))
386 vfs_die ("find_linear: success but directory is not there\n");
389 if (!vfs_s_resolve_symlink (me
, ent
, follow
)) {
399 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
400 const char *path
, int follow
, int flags
)
402 struct vfs_s_entry
*ent
;
403 if (!(MEDATA
->flags
& VFS_S_REMOTE
) && (!*path
))
405 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
411 /* Ook, these were functions around directory entries / inodes */
412 /* -------------------------------- superblock games -------------------------- */
414 static struct vfs_s_super
*
415 vfs_s_new_super (struct vfs_class
*me
)
417 struct vfs_s_super
*super
;
419 super
= g_new0 (struct vfs_s_super
, 1);
425 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
427 super
->next
= MEDATA
->supers
;
428 super
->prevp
= &MEDATA
->supers
;
430 if (MEDATA
->supers
!= NULL
)
431 MEDATA
->supers
->prevp
= &super
->next
;
432 MEDATA
->supers
= super
;
436 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
439 vfs_s_free_inode (me
, super
->root
);
444 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
445 if (super
->ino_usage
)
446 message (D_ERROR
, " Direntry warning ",
447 "Super ino_usage is %d, memory leak",
450 if (super
->want_stale
)
451 message (D_ERROR
, " Direntry warning ", "Super has want_stale set");
455 *super
->prevp
= super
->next
;
457 super
->next
->prevp
= super
->prevp
;
460 CALL (free_archive
) (me
, super
);
461 g_free (super
->name
);
467 * Dissect the path and create corresponding superblock. Note that inname
468 * can be changed and the result may point inside the original string.
471 vfs_s_get_path_mangle (struct vfs_class
*me
, char *inname
,
472 struct vfs_s_super
**archive
, int flags
)
476 const char *archive_name
;
478 struct vfs_s_super
*super
;
481 archive_name
= inname
;
482 vfs_split (inname
, &local
, &op
);
483 retval
= (local
) ? local
: "";
485 if (MEDATA
->archive_check
)
486 if (!(cookie
= MEDATA
->archive_check (me
, archive_name
, op
)))
489 for (super
= MEDATA
->supers
; super
!= NULL
; super
= super
->next
) {
490 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
493 MEDATA
->archive_same (me
, super
, archive_name
, op
, cookie
))) {
501 if (flags
& FL_NO_OPEN
)
504 super
= vfs_s_new_super (me
);
505 result
= MEDATA
->open_archive (me
, super
, archive_name
, op
);
507 vfs_s_free_super (me
, super
);
511 vfs_die ("You have to fill name\n");
513 vfs_die ("You have to fill root inode\n");
515 vfs_s_insert_super (me
, super
);
516 vfs_stamp_create (me
, super
);
525 * Dissect the path and create corresponding superblock.
526 * The result should be freed.
529 vfs_s_get_path (struct vfs_class
*me
, const char *inname
,
530 struct vfs_s_super
**archive
, int flags
)
534 buf
= g_strdup (inname
);
535 retval
= g_strdup (vfs_s_get_path_mangle (me
, buf
, archive
, flags
));
541 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
543 if (!super
->want_stale
){
544 vfs_s_free_inode (me
, super
->root
);
545 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
550 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
553 ERRNOR (EAGAIN
, NULL
);
555 if (!(MEDATA
->flags
& VFS_S_REMOTE
)) {
558 char *path
= g_strdup (ino
->ent
->name
);
561 if (ino
== ino
->super
->root
)
563 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
571 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
572 return g_strdup (ino
->ent
->name
);
574 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
,
575 ino
->ent
->name
, (char *) NULL
);
578 /* Support of archives */
579 /* ------------------------ readdir & friends ----------------------------- */
581 static struct vfs_s_inode
*
582 vfs_s_inode_from_path (struct vfs_class
*me
, const char *name
, int flags
)
584 struct vfs_s_super
*super
;
585 struct vfs_s_inode
*ino
;
588 if (!(q
= vfs_s_get_path (me
, name
, &super
, 0)))
592 vfs_s_find_inode (me
, super
, q
,
593 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
,
596 /* We are asking about / directory of ftp server: assume it exists */
598 vfs_s_find_inode (me
, super
, q
,
599 flags
& FL_FOLLOW
? LINK_FOLLOW
:
601 FL_DIR
| (flags
& ~FL_FOLLOW
));
607 struct vfs_s_entry
*cur
;
608 struct vfs_s_inode
*dir
;
612 vfs_s_opendir (struct vfs_class
*me
, const char *dirname
)
614 struct vfs_s_inode
*dir
;
615 struct dirhandle
*info
;
617 dir
= vfs_s_inode_from_path (me
, dirname
, FL_DIR
| FL_FOLLOW
);
620 if (!S_ISDIR (dir
->st
.st_mode
))
621 ERRNOR (ENOTDIR
, NULL
);
625 if (!dir
->subdir
) /* This can actually happen if we allow empty directories */
626 ERRNOR (EAGAIN
, NULL
);
628 info
= g_new (struct dirhandle
, 1);
629 info
->cur
= dir
->subdir
;
636 vfs_s_readdir(void *data
)
638 static union vfs_dirent dir
;
639 struct dirhandle
*info
= (struct dirhandle
*) data
;
644 if (info
->cur
->name
) {
645 g_strlcpy (dir
.dent
.d_name
, info
->cur
->name
, MC_MAXPATHLEN
);
647 vfs_die("Null in structure-cannot happen");
650 compute_namelen(&dir
.dent
);
651 info
->cur
= info
->cur
->next
;
653 return (void *) &dir
;
657 vfs_s_closedir (void *data
)
659 struct dirhandle
*info
= (struct dirhandle
*) data
;
660 struct vfs_s_inode
*dir
= info
->dir
;
662 vfs_s_free_inode (dir
->super
->me
, dir
);
668 vfs_s_chdir (struct vfs_class
*me
, const char *path
)
671 if (!(data
= vfs_s_opendir (me
, path
)))
673 vfs_s_closedir (data
);
677 /* --------------------------- stat and friends ---------------------------- */
680 vfs_s_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
, int flag
)
682 struct vfs_s_inode
*ino
;
684 if (!(ino
= vfs_s_inode_from_path (me
, path
, flag
)))
691 vfs_s_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
693 return vfs_s_internal_stat (me
, path
, buf
, FL_FOLLOW
);
697 vfs_s_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
699 return vfs_s_internal_stat (me
, path
, buf
, FL_NONE
);
703 vfs_s_fstat (void *fh
, struct stat
*buf
)
710 vfs_s_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
712 struct vfs_s_inode
*ino
;
715 ino
= vfs_s_inode_from_path (me
, path
, 0);
719 if (!S_ISLNK (ino
->st
.st_mode
))
722 if (ino
->linkname
== NULL
)
725 len
= strlen (ino
->linkname
);
728 /* readlink() does not append a NUL character to buf */
729 memcpy (buf
, ino
->linkname
, len
);
734 vfs_s_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
738 struct vfs_s_super
*super
;
740 struct vfs_s_inode
*ino
;
742 if ((q
= vfs_s_get_path (me
, file
, &super
, 0)) == NULL
)
744 ino
= vfs_s_find_inode (me
, super
, q
, LINK_FOLLOW
, FL_NONE
);
745 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))) {
747 ERRNOR (EEXIST
, NULL
);
750 char *dirname
, *name
, *save
;
751 struct vfs_s_entry
*ent
;
752 struct vfs_s_inode
*dir
;
755 /* If the filesystem is read-only, disable file creation */
756 if (!(flags
& O_CREAT
) || !(me
->write
)) {
761 split_dir_name (me
, q
, &dirname
, &name
, &save
);
762 /* FIXME: check if vfs_s_find_inode returns NULL */
763 dir
= vfs_s_find_inode (me
, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
766 ent
= vfs_s_generate_entry (me
, name
, dir
, 0755);
768 vfs_s_insert_entry (me
, dir
, ent
);
769 tmp_handle
= vfs_mkstemps (&ino
->localname
, me
->name
, name
);
770 if (tmp_handle
== -1) {
780 if (S_ISDIR (ino
->st
.st_mode
))
781 ERRNOR (EISDIR
, NULL
);
783 fh
= g_new (struct vfs_s_fh
, 1);
787 fh
->changed
= was_changed
;
790 if (IS_LINEAR (flags
)) {
791 if (MEDATA
->linear_start
) {
792 print_vfs_message (_("Starting linear transfer..."));
793 fh
->linear
= LS_LINEAR_PREOPEN
;
795 } else if ((MEDATA
->fh_open
)
796 && (MEDATA
->fh_open (me
, fh
, flags
, mode
))) {
801 if (fh
->ino
->localname
) {
802 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
803 if (fh
->handle
== -1) {
805 ERRNOR (errno
, NULL
);
809 /* i.e. we had no open files and now we have one */
810 vfs_rmstamp (me
, (vfsid
) super
);
812 fh
->ino
->st
.st_nlink
++;
817 vfs_s_read (void *fh
, char *buffer
, int count
)
820 struct vfs_class
*me
= FH_SUPER
->me
;
822 if (FH
->linear
== LS_LINEAR_PREOPEN
) {
823 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
827 if (FH
->linear
== LS_LINEAR_CLOSED
)
828 vfs_die ("linear_start() did not set linear_state!");
830 if (FH
->linear
== LS_LINEAR_OPEN
)
831 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
833 if (FH
->handle
!= -1){
834 n
= read (FH
->handle
, buffer
, count
);
839 vfs_die ("vfs_s_read: This should not happen\n");
844 vfs_s_write (void *fh
, const char *buffer
, int count
)
847 struct vfs_class
*me
= FH_SUPER
->me
;
850 vfs_die ("no writing to linear files, please");
853 if (FH
->handle
!= -1){
854 n
= write (FH
->handle
, buffer
, count
);
859 vfs_die ("vfs_s_write: This should not happen\n");
864 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
866 off_t size
= FH
->ino
->st
.st_size
;
868 if (FH
->linear
== LS_LINEAR_OPEN
)
869 vfs_die ("cannot lseek() after linear_read!");
871 if (FH
->handle
!= -1){ /* If we have local file opened, we want to work with it */
872 int retval
= lseek (FH
->handle
, offset
, whence
);
874 FH
->ino
->super
->me
->verrno
= errno
;
880 offset
+= FH
->pos
; break;
882 offset
+= size
; break;
886 else if (offset
< size
)
894 vfs_s_close (void *fh
)
897 struct vfs_class
*me
= FH_SUPER
->me
;
899 FH_SUPER
->fd_usage
--;
900 if (!FH_SUPER
->fd_usage
)
901 vfs_stamp_create (me
, FH_SUPER
);
903 if (FH
->linear
== LS_LINEAR_OPEN
)
904 MEDATA
->linear_close (me
, fh
);
905 if (MEDATA
->fh_close
)
906 res
= MEDATA
->fh_close (me
, fh
);
907 if (FH
->changed
&& MEDATA
->file_store
){
908 char *s
= vfs_s_fullpath (me
, FH
->ino
);
912 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
915 vfs_s_invalidate (me
, FH_SUPER
);
917 if (FH
->handle
!= -1)
920 vfs_s_free_inode (me
, FH
->ino
);
926 vfs_s_print_stats (const char *fs_name
, const char *action
,
927 const char *file_name
, off_t have
, off_t need
)
929 static const char *i18n_percent_transf_format
= NULL
;
930 static const char *i18n_transf_format
= NULL
;
932 if (i18n_percent_transf_format
== NULL
) {
933 i18n_percent_transf_format
=
934 _("%s: %s: %s %3d%% (%lu bytes transferred)");
935 i18n_transf_format
= _("%s: %s: %s %lu bytes transferred");
939 print_vfs_message (i18n_percent_transf_format
, fs_name
, action
,
940 file_name
, (int) ((double) have
* 100 / need
),
941 (unsigned long) have
);
943 print_vfs_message (i18n_transf_format
, fs_name
, action
, file_name
,
944 (unsigned long) have
);
948 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
950 /* If you want reget, you'll have to open file with O_LINEAR */
954 off_t stat_size
= ino
->st
.st_size
;
957 memset (&fh
, 0, sizeof (fh
));
962 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
968 if (!MEDATA
->linear_start (me
, &fh
, 0))
971 /* Clear the interrupt status */
972 tty_got_interrupt ();
973 tty_enable_interrupt_key ();
975 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
)))) {
981 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
,
984 if (tty_got_interrupt ())
987 t
= write (handle
, buffer
, n
);
994 MEDATA
->linear_close (me
, &fh
);
997 tty_disable_interrupt_key ();
1001 MEDATA
->linear_close (me
, &fh
);
1003 tty_disable_interrupt_key ();
1005 unlink (ino
->localname
);
1007 g_free (ino
->localname
);
1008 ino
->localname
= NULL
;
1012 /* ------------------------------- mc support ---------------------------- */
1015 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
1017 struct vfs_s_super
*a
= MEDATA
->supers
;
1021 name
= g_strconcat ( a
->name
, "#", me
->prefix
, "/", /* a->current_dir->name, */ NULL
);
1029 vfs_s_ferrno (struct vfs_class
*me
)
1035 * Get local copy of the given file. We reuse the existing file cache
1036 * for remote filesystems. Archives use standard VFS facilities.
1039 vfs_s_getlocalcopy (struct vfs_class
*me
, const char *path
)
1041 struct vfs_s_fh
*fh
;
1044 fh
= vfs_s_open (me
, path
, O_RDONLY
, 0);
1045 if (!fh
|| !fh
->ino
|| !fh
->ino
->localname
)
1048 local
= g_strdup (fh
->ino
->localname
);
1054 * Return the local copy. Since we are using our cache, we do nothing -
1055 * the cache will be removed when the archive is closed.
1058 vfs_s_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1059 const char *local
, int has_changed
)
1069 vfs_s_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1072 case VFS_SETCTL_STALE_DATA
:
1074 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (me
, path
, 0);
1079 ino
->super
->want_stale
= 1;
1081 ino
->super
->want_stale
= 0;
1082 vfs_s_invalidate (me
, ino
->super
);
1086 case VFS_SETCTL_LOGFILE
:
1087 MEDATA
->logfile
= fopen ((char *) arg
, "w");
1089 case VFS_SETCTL_FLUSH
:
1097 /* ----------------------------- Stamping support -------------------------- */
1100 vfs_s_getid (struct vfs_class
*me
, const char *path
)
1102 struct vfs_s_super
*archive
;
1105 if (!(p
= vfs_s_get_path (me
, path
, &archive
, FL_NO_OPEN
)))
1108 return (vfsid
) archive
;
1112 vfs_s_nothingisopen (vfsid id
)
1115 /* Our data structures should survive free of superblock at any time */
1120 vfs_s_free (vfsid id
)
1122 vfs_s_free_super (((struct vfs_s_super
*)id
)->me
, (struct vfs_s_super
*)id
);
1126 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1130 if (MEDATA
->flush
) {
1135 gettimeofday(&tim
, NULL
);
1136 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
1141 /* Initialize one of our subclasses - fill common functions */
1143 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1146 vclass
->fill_names
= vfs_s_fill_names
;
1147 vclass
->open
= vfs_s_open
;
1148 vclass
->close
= vfs_s_close
;
1149 vclass
->read
= vfs_s_read
;
1150 if (!(sub
->flags
& VFS_S_READONLY
)) {
1151 vclass
->write
= vfs_s_write
;
1153 vclass
->opendir
= vfs_s_opendir
;
1154 vclass
->readdir
= vfs_s_readdir
;
1155 vclass
->closedir
= vfs_s_closedir
;
1156 vclass
->stat
= vfs_s_stat
;
1157 vclass
->lstat
= vfs_s_lstat
;
1158 vclass
->fstat
= vfs_s_fstat
;
1159 vclass
->readlink
= vfs_s_readlink
;
1160 vclass
->chdir
= vfs_s_chdir
;
1161 vclass
->ferrno
= vfs_s_ferrno
;
1162 vclass
->lseek
= vfs_s_lseek
;
1163 vclass
->getid
= vfs_s_getid
;
1164 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1165 vclass
->free
= vfs_s_free
;
1166 if (sub
->flags
& VFS_S_REMOTE
) {
1167 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1168 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1169 sub
->find_entry
= vfs_s_find_entry_linear
;
1171 sub
->find_entry
= vfs_s_find_entry_tree
;
1173 vclass
->setctl
= vfs_s_setctl
;
1174 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1177 /* ----------- Utility functions for networked filesystems -------------- */
1181 vfs_s_select_on_two (int fd1
, int fd2
)
1184 struct timeval timeout
;
1186 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1189 timeout
.tv_usec
= 0;
1193 v
= select (maxfd
, &set
, 0, 0, &timeout
);
1196 if (FD_ISSET (fd1
, &set
))
1198 if (FD_ISSET (fd2
, &set
))
1204 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1206 FILE *logfile
= MEDATA
->logfile
;
1210 for (i
= 0; i
< buf_len
- 1; i
++, buf
++){
1211 if (read (sock
, buf
, sizeof(char)) <= 0)
1214 fwrite (buf
, 1, 1, logfile
);
1223 /* Line is too long - terminate buffer and discard the rest of line */
1225 while (read (sock
, &c
, sizeof (c
)) > 0) {
1227 fwrite (&c
, 1, 1, logfile
);
1237 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1244 tty_enable_interrupt_key ();
1245 for (i
= 0; i
< size
-1; i
++){
1246 n
= read (fd
, buffer
+i
, 1);
1247 tty_disable_interrupt_key ();
1248 if (n
== -1 && errno
== EINTR
){
1256 if (buffer
[i
] == '\n'){
1261 buffer
[size
-1] = 0;
1264 #endif /* USE_NETCODE */