1 /* Directory cache support -- so that you do not have copy of this in
2 * each and every filesystem.
4 * Written at 1998 by Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
6 * Very loosely based on tar.c from midnight and archives.[ch] from
7 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
9 * Unfortunately, I was unable to keep all filesystems
10 * uniform. tar-like filesystems use tree structure where each
11 * directory has pointers to its subdirectories. We can do this
12 * because we have full information about our archive.
14 * At ftp-like filesystems, situation is a little bit different. When
15 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
16 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
17 * listed. That means that we do not have complete information, and if
18 * /usr is symlink to /4, we will not know. Also we have to time out
19 * entries and things would get messy with tree-like approach. So we
20 * do different trick: root directory is completely special and
21 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
22 * and we'll try to use custom find_entry function.
24 * Paths here do _not_ begin with '/', so root directory of
25 * archive/site is simply "". Beware. */
31 #include <mhl/string.h>
33 #include "../src/global.h"
34 #include "../src/tty.h" /* enable/disable interrupt key */
35 #include "../src/wtools.h" /* message() */
36 #include "../src/main.h" /* print_vfs_message */
39 #include "gc.h" /* vfs_rmstamp */
40 #include "xdirentry.h"
42 #define CALL(x) if (MEDATA->x) MEDATA->x
44 static volatile int total_inodes
= 0, total_entries
= 0;
47 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
49 struct vfs_s_inode
*ino
;
51 ino
= g_new0 (struct vfs_s_inode
, 1);
59 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
60 ino
->st
.st_dev
= MEDATA
->rdev
;
65 CALL (init_inode
) (me
, ino
);
71 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
73 struct vfs_s_entry
*entry
;
75 entry
= g_new0 (struct vfs_s_entry
, 1);
79 entry
->name
= mhl_str_dup (name
);
82 entry
->ino
->ent
= entry
;
83 CALL (init_entry
) (me
, entry
);
89 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
92 vfs_die ("Don't pass NULL to me");
94 /* ==0 can happen if freshly created entry is deleted */
95 if (ino
->st
.st_nlink
<= 1){
97 vfs_s_free_entry (me
, ino
->subdir
);
100 CALL (free_inode
) (me
, ino
);
101 g_free (ino
->linkname
);
103 unlink (ino
->localname
);
104 g_free(ino
->localname
);
107 ino
->super
->ino_usage
--;
109 } else ino
->st
.st_nlink
--;
113 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
115 if (ent
->prevp
){ /* It is possible that we are deleting freshly created entry */
116 *ent
->prevp
= ent
->next
;
118 ent
->next
->prevp
= ent
->prevp
;
125 ent
->ino
->ent
= NULL
;
126 vfs_s_free_inode (me
, ent
->ino
);
135 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
137 struct vfs_s_entry
**ep
;
141 for (ep
= &dir
->subdir
; *ep
!= NULL
; ep
= &((*ep
)->next
))
148 ent
->ino
->st
.st_nlink
++;
152 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
154 static struct stat st
;
159 myumask
= umask (022);
167 st
.st_uid
= getuid ();
168 st
.st_gid
= getgid ();
170 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
176 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
, mode_t mode
)
178 struct vfs_s_inode
*inode
;
181 st
= vfs_s_default_stat (me
, mode
);
182 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
184 return vfs_s_new_entry (me
, name
, inode
);
187 /* We were asked to create entries automagically */
188 static struct vfs_s_entry
*
189 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
191 struct vfs_s_entry
*res
;
192 char *sep
= strchr (path
, PATH_SEP
);
196 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
197 vfs_s_insert_entry (me
, dir
, res
);
205 /* If the entry is a symlink, find the entry for its target */
206 static struct vfs_s_entry
*
207 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
,
211 char *fullname
= NULL
;
212 struct vfs_s_entry
*target
;
214 if (follow
== LINK_NO_FOLLOW
)
217 ERRNOR (ELOOP
, NULL
);
219 ERRNOR (ENOENT
, NULL
);
220 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
223 linkname
= entry
->ino
->linkname
;
224 if (linkname
== NULL
)
225 ERRNOR (EFAULT
, NULL
);
227 /* make full path from relative */
228 if (*linkname
!= PATH_SEP
) {
229 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
231 fullname
= g_strconcat (fullpath
, "/", linkname
, NULL
);
238 (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
,
245 * Follow > 0: follow links, serves as loop protect,
246 * == -1: do not follow links
248 static struct vfs_s_entry
*
249 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
250 const char *a_path
, int follow
, int flags
)
253 struct vfs_s_entry
*ent
= NULL
;
254 char * const pathref
= mhl_str_dup (a_path
);
255 char *path
= pathref
;
257 canonicalize_pathname (path
);
260 while (*path
== PATH_SEP
) /* Strip leading '/' */
268 for (pseg
= 0; path
[pseg
] && path
[pseg
] != PATH_SEP
; pseg
++);
270 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
271 if (strlen (ent
->name
) == pseg
272 && (!strncmp (ent
->name
, path
, pseg
)))
276 if (!ent
&& (flags
& (FL_MKFILE
| FL_MKDIR
)))
277 ent
= vfs_s_automake (me
, root
, path
, flags
);
283 /* here we must follow leading directories always;
284 only the actual file is optional */
286 vfs_s_resolve_symlink (me
, ent
,
288 PATH_SEP
) ? LINK_FOLLOW
:
300 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
306 s
= strrchr (path
, PATH_SEP
);
310 *dir
= path
+ strlen(path
); /* an empty string */
319 static struct vfs_s_entry
*
320 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
321 const char *a_path
, int follow
, int flags
)
323 struct vfs_s_entry
*ent
= NULL
;
324 char * const path
= mhl_str_dup (a_path
);
325 struct vfs_s_entry
*retval
= NULL
;
327 if (root
->super
->root
!= root
)
328 vfs_die ("We have to use _real_ root. Always. Sorry.");
330 canonicalize_pathname (path
);
332 if (!(flags
& FL_DIR
)) {
333 char *dirname
, *name
, *save
;
334 struct vfs_s_inode
*ino
;
335 split_dir_name (me
, path
, &dirname
, &name
, &save
);
337 vfs_s_find_inode (me
, root
->super
, dirname
, follow
,
341 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
346 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
347 if (!strcmp (ent
->name
, path
))
350 if (ent
&& (!(MEDATA
->dir_uptodate
) (me
, ent
->ino
))) {
352 print_vfs_message (_("Directory cache expired for %s"), path
);
354 vfs_s_free_entry (me
, ent
);
359 struct vfs_s_inode
*ino
;
362 vfs_s_new_inode (me
, root
->super
,
363 vfs_s_default_stat (me
, S_IFDIR
| 0755));
364 ent
= vfs_s_new_entry (me
, path
, ino
);
365 if ((MEDATA
->dir_load
) (me
, ino
, path
) == -1) {
366 vfs_s_free_entry (me
, ent
);
370 vfs_s_insert_entry (me
, root
, ent
);
372 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
373 if (!strcmp (ent
->name
, path
))
377 vfs_die ("find_linear: success but directory is not there\n");
380 if (!vfs_s_resolve_symlink (me
, ent
, follow
)) {
390 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
391 const char *path
, int follow
, int flags
)
393 struct vfs_s_entry
*ent
;
394 if (!(MEDATA
->flags
& VFS_S_REMOTE
) && (!*path
))
396 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
402 /* Ook, these were functions around directory entries / inodes */
403 /* -------------------------------- superblock games -------------------------- */
405 static struct vfs_s_super
*
406 vfs_s_new_super (struct vfs_class
*me
)
408 struct vfs_s_super
*super
;
410 super
= g_new0 (struct vfs_s_super
, 1);
416 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
418 super
->next
= MEDATA
->supers
;
419 super
->prevp
= &MEDATA
->supers
;
421 if (MEDATA
->supers
!= NULL
)
422 MEDATA
->supers
->prevp
= &super
->next
;
423 MEDATA
->supers
= super
;
427 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
430 vfs_s_free_inode (me
, super
->root
);
435 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
436 if (super
->ino_usage
)
437 message (D_ERROR
, " Direntry warning ",
438 "Super ino_usage is %d, memory leak",
441 if (super
->want_stale
)
442 message (D_ERROR
, " Direntry warning ", "Super has want_stale set");
446 *super
->prevp
= super
->next
;
448 super
->next
->prevp
= super
->prevp
;
451 CALL (free_archive
) (me
, super
);
452 g_free (super
->name
);
458 * Dissect the path and create corresponding superblock. Note that inname
459 * can be changed and the result may point inside the original string.
462 vfs_s_get_path_mangle (struct vfs_class
*me
, char *inname
,
463 struct vfs_s_super
**archive
, int flags
)
467 const char *archive_name
;
469 struct vfs_s_super
*super
;
472 archive_name
= inname
;
473 vfs_split (inname
, &local
, &op
);
474 retval
= (local
) ? local
: "";
476 if (MEDATA
->archive_check
)
477 if (!(cookie
= MEDATA
->archive_check (me
, archive_name
, op
)))
480 for (super
= MEDATA
->supers
; super
!= NULL
; super
= super
->next
) {
481 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
484 MEDATA
->archive_same (me
, super
, archive_name
, op
, cookie
))) {
492 if (flags
& FL_NO_OPEN
)
495 super
= vfs_s_new_super (me
);
496 result
= MEDATA
->open_archive (me
, super
, archive_name
, op
);
498 vfs_s_free_super (me
, super
);
502 vfs_die ("You have to fill name\n");
504 vfs_die ("You have to fill root inode\n");
506 vfs_s_insert_super (me
, super
);
507 vfs_stamp_create (me
, super
);
516 * Dissect the path and create corresponding superblock.
517 * The result should be freed.
520 vfs_s_get_path (struct vfs_class
*me
, const char *inname
,
521 struct vfs_s_super
**archive
, int flags
)
525 buf
= mhl_str_dup (inname
);
526 retval
= mhl_str_dup (vfs_s_get_path_mangle (me
, buf
, archive
, flags
));
532 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
534 if (!super
->want_stale
){
535 vfs_s_free_inode (me
, super
->root
);
536 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
541 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
544 ERRNOR (EAGAIN
, NULL
);
546 if (!(MEDATA
->flags
& VFS_S_REMOTE
)) {
549 char *path
= mhl_str_dup (ino
->ent
->name
);
552 if (ino
== ino
->super
->root
)
554 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
562 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
563 return mhl_str_dup (ino
->ent
->name
);
565 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
,
566 ino
->ent
->name
, (char *) NULL
);
569 /* Support of archives */
570 /* ------------------------ readdir & friends ----------------------------- */
572 static struct vfs_s_inode
*
573 vfs_s_inode_from_path (struct vfs_class
*me
, const char *name
, int flags
)
575 struct vfs_s_super
*super
;
576 struct vfs_s_inode
*ino
;
579 if (!(q
= vfs_s_get_path (me
, name
, &super
, 0)))
583 vfs_s_find_inode (me
, super
, q
,
584 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
,
587 /* We are asking about / directory of ftp server: assume it exists */
589 vfs_s_find_inode (me
, super
, q
,
590 flags
& FL_FOLLOW
? LINK_FOLLOW
:
592 FL_DIR
| (flags
& ~FL_FOLLOW
));
598 struct vfs_s_entry
*cur
;
599 struct vfs_s_inode
*dir
;
603 vfs_s_opendir (struct vfs_class
*me
, const char *dirname
)
605 struct vfs_s_inode
*dir
;
606 struct dirhandle
*info
;
608 dir
= vfs_s_inode_from_path (me
, dirname
, FL_DIR
| FL_FOLLOW
);
611 if (!S_ISDIR (dir
->st
.st_mode
))
612 ERRNOR (ENOTDIR
, NULL
);
616 if (!dir
->subdir
) /* This can actually happen if we allow empty directories */
617 ERRNOR (EAGAIN
, NULL
);
619 info
= g_new (struct dirhandle
, 1);
620 info
->cur
= dir
->subdir
;
627 vfs_s_readdir(void *data
)
629 static union vfs_dirent dir
;
630 struct dirhandle
*info
= (struct dirhandle
*) data
;
635 if (info
->cur
->name
) {
636 g_strlcpy (dir
.dent
.d_name
, info
->cur
->name
, MC_MAXPATHLEN
);
638 vfs_die("Null in structure-cannot happen");
641 compute_namelen(&dir
.dent
);
642 info
->cur
= info
->cur
->next
;
644 return (void *) &dir
;
648 vfs_s_closedir (void *data
)
650 struct dirhandle
*info
= (struct dirhandle
*) data
;
651 struct vfs_s_inode
*dir
= info
->dir
;
653 vfs_s_free_inode (dir
->super
->me
, dir
);
659 vfs_s_chdir (struct vfs_class
*me
, const char *path
)
662 if (!(data
= vfs_s_opendir (me
, path
)))
664 vfs_s_closedir (data
);
668 /* --------------------------- stat and friends ---------------------------- */
671 vfs_s_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
, int flag
)
673 struct vfs_s_inode
*ino
;
675 if (!(ino
= vfs_s_inode_from_path (me
, path
, flag
)))
682 vfs_s_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
684 return vfs_s_internal_stat (me
, path
, buf
, FL_FOLLOW
);
688 vfs_s_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
690 return vfs_s_internal_stat (me
, path
, buf
, FL_NONE
);
694 vfs_s_fstat (void *fh
, struct stat
*buf
)
701 vfs_s_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
703 struct vfs_s_inode
*ino
;
706 ino
= vfs_s_inode_from_path (me
, path
, 0);
710 if (!S_ISLNK (ino
->st
.st_mode
))
713 if (ino
->linkname
== NULL
)
716 len
= strlen (ino
->linkname
);
719 /* readlink() does not append a NUL character to buf */
720 memcpy (buf
, ino
->linkname
, len
);
725 vfs_s_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
729 struct vfs_s_super
*super
;
731 struct vfs_s_inode
*ino
;
733 if ((q
= vfs_s_get_path (me
, file
, &super
, 0)) == NULL
)
735 ino
= vfs_s_find_inode (me
, super
, q
, LINK_FOLLOW
, FL_NONE
);
736 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))) {
738 ERRNOR (EEXIST
, NULL
);
741 char *dirname
, *name
, *save
;
742 struct vfs_s_entry
*ent
;
743 struct vfs_s_inode
*dir
;
746 /* If the filesystem is read-only, disable file creation */
747 if (!(flags
& O_CREAT
) || !(me
->write
)) {
752 split_dir_name (me
, q
, &dirname
, &name
, &save
);
753 /* FIXME: check if vfs_s_find_inode returns NULL */
754 dir
= vfs_s_find_inode (me
, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
757 ent
= vfs_s_generate_entry (me
, name
, dir
, 0755);
759 vfs_s_insert_entry (me
, dir
, ent
);
760 tmp_handle
= vfs_mkstemps (&ino
->localname
, me
->name
, name
);
761 if (tmp_handle
== -1) {
771 if (S_ISDIR (ino
->st
.st_mode
))
772 ERRNOR (EISDIR
, NULL
);
774 fh
= g_new (struct vfs_s_fh
, 1);
778 fh
->changed
= was_changed
;
781 if (IS_LINEAR (flags
)) {
782 if (MEDATA
->linear_start
) {
783 print_vfs_message (_("Starting linear transfer..."));
784 fh
->linear
= LS_LINEAR_PREOPEN
;
786 } else if ((MEDATA
->fh_open
)
787 && (MEDATA
->fh_open (me
, fh
, flags
, mode
))) {
792 if (fh
->ino
->localname
) {
793 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
794 if (fh
->handle
== -1) {
796 ERRNOR (errno
, NULL
);
800 /* i.e. we had no open files and now we have one */
801 vfs_rmstamp (me
, (vfsid
) super
);
803 fh
->ino
->st
.st_nlink
++;
808 vfs_s_read (void *fh
, char *buffer
, int count
)
811 struct vfs_class
*me
= FH_SUPER
->me
;
813 if (FH
->linear
== LS_LINEAR_PREOPEN
) {
814 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
818 if (FH
->linear
== LS_LINEAR_CLOSED
)
819 vfs_die ("linear_start() did not set linear_state!");
821 if (FH
->linear
== LS_LINEAR_OPEN
)
822 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
824 if (FH
->handle
!= -1){
825 n
= read (FH
->handle
, buffer
, count
);
830 vfs_die ("vfs_s_read: This should not happen\n");
835 vfs_s_write (void *fh
, const char *buffer
, int count
)
838 struct vfs_class
*me
= FH_SUPER
->me
;
841 vfs_die ("no writing to linear files, please");
844 if (FH
->handle
!= -1){
845 n
= write (FH
->handle
, buffer
, count
);
850 vfs_die ("vfs_s_write: This should not happen\n");
855 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
857 off_t size
= FH
->ino
->st
.st_size
;
859 if (FH
->linear
== LS_LINEAR_OPEN
)
860 vfs_die ("cannot lseek() after linear_read!");
862 if (FH
->handle
!= -1){ /* If we have local file opened, we want to work with it */
863 int retval
= lseek (FH
->handle
, offset
, whence
);
865 FH
->ino
->super
->me
->verrno
= errno
;
871 offset
+= FH
->pos
; break;
873 offset
+= size
; break;
877 else if (offset
< size
)
885 vfs_s_close (void *fh
)
888 struct vfs_class
*me
= FH_SUPER
->me
;
890 FH_SUPER
->fd_usage
--;
891 if (!FH_SUPER
->fd_usage
)
892 vfs_stamp_create (me
, FH_SUPER
);
894 if (FH
->linear
== LS_LINEAR_OPEN
)
895 MEDATA
->linear_close (me
, fh
);
896 if (MEDATA
->fh_close
)
897 res
= MEDATA
->fh_close (me
, fh
);
898 if (FH
->changed
&& MEDATA
->file_store
){
899 char *s
= vfs_s_fullpath (me
, FH
->ino
);
903 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
906 vfs_s_invalidate (me
, FH_SUPER
);
908 if (FH
->handle
!= -1)
911 vfs_s_free_inode (me
, FH
->ino
);
917 vfs_s_print_stats (const char *fs_name
, const char *action
,
918 const char *file_name
, off_t have
, off_t need
)
920 static const char *i18n_percent_transf_format
= NULL
;
921 static const char *i18n_transf_format
= NULL
;
923 if (i18n_percent_transf_format
== NULL
) {
924 i18n_percent_transf_format
=
925 _("%s: %s: %s %3d%% (%lu bytes transferred)");
926 i18n_transf_format
= _("%s: %s: %s %lu bytes transferred");
930 print_vfs_message (i18n_percent_transf_format
, fs_name
, action
,
931 file_name
, (int) ((double) have
* 100 / need
),
932 (unsigned long) have
);
934 print_vfs_message (i18n_transf_format
, fs_name
, action
, file_name
,
935 (unsigned long) have
);
939 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
941 /* If you want reget, you'll have to open file with O_LINEAR */
945 off_t stat_size
= ino
->st
.st_size
;
948 memset (&fh
, 0, sizeof (fh
));
953 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
959 if (!MEDATA
->linear_start (me
, &fh
, 0))
962 /* Clear the interrupt status */
964 enable_interrupt_key ();
966 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
)))) {
972 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
,
975 if (got_interrupt ())
978 t
= write (handle
, buffer
, n
);
985 MEDATA
->linear_close (me
, &fh
);
988 disable_interrupt_key ();
992 MEDATA
->linear_close (me
, &fh
);
994 disable_interrupt_key ();
996 unlink (ino
->localname
);
998 g_free (ino
->localname
);
999 ino
->localname
= NULL
;
1003 /* ------------------------------- mc support ---------------------------- */
1006 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
1008 struct vfs_s_super
*a
= MEDATA
->supers
;
1012 name
= g_strconcat ( a
->name
, "#", me
->prefix
, "/", /* a->current_dir->name, */ NULL
);
1020 vfs_s_ferrno (struct vfs_class
*me
)
1026 * Get local copy of the given file. We reuse the existing file cache
1027 * for remote filesystems. Archives use standard VFS facilities.
1030 vfs_s_getlocalcopy (struct vfs_class
*me
, const char *path
)
1032 struct vfs_s_fh
*fh
;
1035 fh
= vfs_s_open (me
, path
, O_RDONLY
, 0);
1036 if (!fh
|| !fh
->ino
|| !fh
->ino
->localname
)
1039 local
= mhl_str_dup (fh
->ino
->localname
);
1045 * Return the local copy. Since we are using our cache, we do nothing -
1046 * the cache will be removed when the archive is closed.
1049 vfs_s_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1050 const char *local
, int has_changed
)
1060 vfs_s_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1063 case VFS_SETCTL_STALE_DATA
:
1065 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (me
, path
, 0);
1070 ino
->super
->want_stale
= 1;
1072 ino
->super
->want_stale
= 0;
1073 vfs_s_invalidate (me
, ino
->super
);
1077 case VFS_SETCTL_LOGFILE
:
1078 MEDATA
->logfile
= fopen ((char *) arg
, "w");
1080 case VFS_SETCTL_FLUSH
:
1088 /* ----------------------------- Stamping support -------------------------- */
1091 vfs_s_getid (struct vfs_class
*me
, const char *path
)
1093 struct vfs_s_super
*archive
;
1096 if (!(p
= vfs_s_get_path (me
, path
, &archive
, FL_NO_OPEN
)))
1099 return (vfsid
) archive
;
1103 vfs_s_nothingisopen (vfsid id
)
1106 /* Our data structures should survive free of superblock at any time */
1111 vfs_s_free (vfsid id
)
1113 vfs_s_free_super (((struct vfs_s_super
*)id
)->me
, (struct vfs_s_super
*)id
);
1117 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1121 if (MEDATA
->flush
) {
1126 gettimeofday(&tim
, NULL
);
1127 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
1132 /* Initialize one of our subclasses - fill common functions */
1134 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1137 vclass
->fill_names
= vfs_s_fill_names
;
1138 vclass
->open
= vfs_s_open
;
1139 vclass
->close
= vfs_s_close
;
1140 vclass
->read
= vfs_s_read
;
1141 if (!(sub
->flags
& VFS_S_READONLY
)) {
1142 vclass
->write
= vfs_s_write
;
1144 vclass
->opendir
= vfs_s_opendir
;
1145 vclass
->readdir
= vfs_s_readdir
;
1146 vclass
->closedir
= vfs_s_closedir
;
1147 vclass
->stat
= vfs_s_stat
;
1148 vclass
->lstat
= vfs_s_lstat
;
1149 vclass
->fstat
= vfs_s_fstat
;
1150 vclass
->readlink
= vfs_s_readlink
;
1151 vclass
->chdir
= vfs_s_chdir
;
1152 vclass
->ferrno
= vfs_s_ferrno
;
1153 vclass
->lseek
= vfs_s_lseek
;
1154 vclass
->getid
= vfs_s_getid
;
1155 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1156 vclass
->free
= vfs_s_free
;
1157 if (sub
->flags
& VFS_S_REMOTE
) {
1158 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1159 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1160 sub
->find_entry
= vfs_s_find_entry_linear
;
1162 sub
->find_entry
= vfs_s_find_entry_tree
;
1164 vclass
->setctl
= vfs_s_setctl
;
1165 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1168 /* ----------- Utility functions for networked filesystems -------------- */
1172 vfs_s_select_on_two (int fd1
, int fd2
)
1175 struct timeval timeout
;
1177 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1180 timeout
.tv_usec
= 0;
1184 v
= select (maxfd
, &set
, 0, 0, &timeout
);
1187 if (FD_ISSET (fd1
, &set
))
1189 if (FD_ISSET (fd2
, &set
))
1195 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1197 FILE *logfile
= MEDATA
->logfile
;
1201 for (i
= 0; i
< buf_len
- 1; i
++, buf
++){
1202 if (read (sock
, buf
, sizeof(char)) <= 0)
1205 fwrite (buf
, 1, 1, logfile
);
1214 /* Line is too long - terminate buffer and discard the rest of line */
1216 while (read (sock
, &c
, sizeof (c
)) > 0) {
1218 fwrite (&c
, 1, 1, logfile
);
1228 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1235 enable_interrupt_key ();
1236 for (i
= 0; i
< size
-1; i
++){
1237 n
= read (fd
, buffer
+i
, 1);
1238 disable_interrupt_key ();
1239 if (n
== -1 && errno
== EINTR
){
1247 if (buffer
[i
] == '\n'){
1252 buffer
[size
-1] = 0;
1255 #endif /* USE_NETCODE */