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. */
30 #include "../src/global.h"
31 #include "../src/tty.h" /* enable/disable interrupt key */
32 #include "../src/wtools.h" /* message() */
33 #include "../src/main.h" /* print_vfs_message */
36 #include "gc.h" /* vfs_rmstamp */
37 #include "xdirentry.h"
39 #define CALL(x) if (MEDATA->x) MEDATA->x
41 static volatile int total_inodes
= 0, total_entries
= 0;
44 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
46 struct vfs_s_inode
*ino
;
48 ino
= g_new0 (struct vfs_s_inode
, 1);
56 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
57 ino
->st
.st_dev
= MEDATA
->rdev
;
62 CALL (init_inode
) (me
, ino
);
68 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
70 struct vfs_s_entry
*entry
;
72 entry
= g_new0 (struct vfs_s_entry
, 1);
76 entry
->name
= g_strdup (name
);
79 entry
->ino
->ent
= entry
;
80 CALL (init_entry
) (me
, entry
);
86 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
89 vfs_die ("Don't pass NULL to me");
91 /* ==0 can happen if freshly created entry is deleted */
92 if (ino
->st
.st_nlink
<= 1){
94 vfs_s_free_entry (me
, ino
->subdir
);
97 CALL (free_inode
) (me
, ino
);
98 g_free (ino
->linkname
);
100 unlink (ino
->localname
);
101 g_free(ino
->localname
);
104 ino
->super
->ino_usage
--;
106 } else ino
->st
.st_nlink
--;
110 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
112 if (ent
->prevp
){ /* It is possible that we are deleting freshly created entry */
113 *ent
->prevp
= ent
->next
;
115 ent
->next
->prevp
= ent
->prevp
;
122 ent
->ino
->ent
= NULL
;
123 vfs_s_free_inode (me
, ent
->ino
);
132 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
134 struct vfs_s_entry
**ep
;
138 for (ep
= &dir
->subdir
; *ep
!= NULL
; ep
= &((*ep
)->next
))
145 ent
->ino
->st
.st_nlink
++;
149 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
151 static struct stat st
;
156 myumask
= umask (022);
164 st
.st_uid
= getuid ();
165 st
.st_gid
= getgid ();
167 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
173 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
, mode_t mode
)
175 struct vfs_s_inode
*inode
;
178 st
= vfs_s_default_stat (me
, mode
);
179 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
181 return vfs_s_new_entry (me
, name
, inode
);
184 /* We were asked to create entries automagically */
185 static struct vfs_s_entry
*
186 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
188 struct vfs_s_entry
*res
;
189 char *sep
= strchr (path
, PATH_SEP
);
193 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
194 vfs_s_insert_entry (me
, dir
, res
);
202 /* If the entry is a symlink, find the entry for its target */
203 static struct vfs_s_entry
*
204 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
,
208 char *fullname
= NULL
;
209 struct vfs_s_entry
*target
;
211 if (follow
== LINK_NO_FOLLOW
)
214 ERRNOR (ELOOP
, NULL
);
216 ERRNOR (ENOENT
, NULL
);
217 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
220 linkname
= entry
->ino
->linkname
;
221 if (linkname
== NULL
)
222 ERRNOR (EFAULT
, NULL
);
224 /* make full path from relative */
225 if (*linkname
!= PATH_SEP
) {
226 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
228 fullname
= g_strconcat (fullpath
, "/", linkname
, NULL
);
235 (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
,
242 * Follow > 0: follow links, serves as loop protect,
243 * == -1: do not follow links
245 static struct vfs_s_entry
*
246 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
247 const char *a_path
, int follow
, int flags
)
250 struct vfs_s_entry
*ent
= NULL
;
251 char * const pathref
= g_strdup (a_path
);
252 char *path
= pathref
;
254 canonicalize_pathname (path
);
257 while (*path
== PATH_SEP
) /* Strip leading '/' */
265 for (pseg
= 0; path
[pseg
] && path
[pseg
] != PATH_SEP
; pseg
++);
267 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
268 if (strlen (ent
->name
) == pseg
269 && (!strncmp (ent
->name
, path
, pseg
)))
273 if (!ent
&& (flags
& (FL_MKFILE
| FL_MKDIR
)))
274 ent
= vfs_s_automake (me
, root
, path
, flags
);
280 /* here we must follow leading directories always;
281 only the actual file is optional */
283 vfs_s_resolve_symlink (me
, ent
,
285 PATH_SEP
) ? LINK_FOLLOW
:
297 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
303 s
= strrchr (path
, PATH_SEP
);
307 *dir
= path
+ strlen(path
); /* an empty string */
316 static struct vfs_s_entry
*
317 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
318 const char *a_path
, int follow
, int flags
)
320 struct vfs_s_entry
*ent
= NULL
;
321 char * const path
= g_strdup (a_path
);
322 struct vfs_s_entry
*retval
= NULL
;
324 if (root
->super
->root
!= root
)
325 vfs_die ("We have to use _real_ root. Always. Sorry.");
327 canonicalize_pathname (path
);
329 if (!(flags
& FL_DIR
)) {
330 char *dirname
, *name
, *save
;
331 struct vfs_s_inode
*ino
;
332 split_dir_name (me
, path
, &dirname
, &name
, &save
);
334 vfs_s_find_inode (me
, root
->super
, dirname
, follow
,
338 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
343 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
344 if (!strcmp (ent
->name
, path
))
347 if (ent
&& (!(MEDATA
->dir_uptodate
) (me
, ent
->ino
))) {
349 print_vfs_message (_("Directory cache expired for %s"), path
);
351 vfs_s_free_entry (me
, ent
);
356 struct vfs_s_inode
*ino
;
359 vfs_s_new_inode (me
, root
->super
,
360 vfs_s_default_stat (me
, S_IFDIR
| 0755));
361 ent
= vfs_s_new_entry (me
, path
, ino
);
362 if ((MEDATA
->dir_load
) (me
, ino
, path
) == -1) {
363 vfs_s_free_entry (me
, ent
);
367 vfs_s_insert_entry (me
, root
, ent
);
369 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
370 if (!strcmp (ent
->name
, path
))
374 vfs_die ("find_linear: success but directory is not there\n");
377 if (!vfs_s_resolve_symlink (me
, ent
, follow
)) {
387 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
388 const char *path
, int follow
, int flags
)
390 struct vfs_s_entry
*ent
;
391 if (!(MEDATA
->flags
& VFS_S_REMOTE
) && (!*path
))
393 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
399 /* Ook, these were functions around directory entries / inodes */
400 /* -------------------------------- superblock games -------------------------- */
402 static struct vfs_s_super
*
403 vfs_s_new_super (struct vfs_class
*me
)
405 struct vfs_s_super
*super
;
407 super
= g_new0 (struct vfs_s_super
, 1);
413 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
415 super
->next
= MEDATA
->supers
;
416 super
->prevp
= &MEDATA
->supers
;
418 if (MEDATA
->supers
!= NULL
)
419 MEDATA
->supers
->prevp
= &super
->next
;
420 MEDATA
->supers
= super
;
424 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
427 vfs_s_free_inode (me
, super
->root
);
432 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
433 if (super
->ino_usage
)
434 message (D_ERROR
, " Direntry warning ",
435 "Super ino_usage is %d, memory leak",
438 if (super
->want_stale
)
439 message (D_ERROR
, " Direntry warning ", "Super has want_stale set");
443 *super
->prevp
= super
->next
;
445 super
->next
->prevp
= super
->prevp
;
448 CALL (free_archive
) (me
, super
);
449 g_free (super
->name
);
455 * Dissect the path and create corresponding superblock. Note that inname
456 * can be changed and the result may point inside the original string.
459 vfs_s_get_path_mangle (struct vfs_class
*me
, char *inname
,
460 struct vfs_s_super
**archive
, int flags
)
464 const char *archive_name
;
466 struct vfs_s_super
*super
;
469 archive_name
= inname
;
470 vfs_split (inname
, &local
, &op
);
471 retval
= (local
) ? local
: "";
473 if (MEDATA
->archive_check
)
474 if (!(cookie
= MEDATA
->archive_check (me
, archive_name
, op
)))
477 for (super
= MEDATA
->supers
; super
!= NULL
; super
= super
->next
) {
478 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
481 MEDATA
->archive_same (me
, super
, archive_name
, op
, cookie
))) {
489 if (flags
& FL_NO_OPEN
)
492 super
= vfs_s_new_super (me
);
493 result
= MEDATA
->open_archive (me
, super
, archive_name
, op
);
495 vfs_s_free_super (me
, super
);
499 vfs_die ("You have to fill name\n");
501 vfs_die ("You have to fill root inode\n");
503 vfs_s_insert_super (me
, super
);
504 vfs_stamp_create (me
, super
);
513 * Dissect the path and create corresponding superblock.
514 * The result should be freed.
517 vfs_s_get_path (struct vfs_class
*me
, const char *inname
,
518 struct vfs_s_super
**archive
, int flags
)
522 buf
= g_strdup (inname
);
523 retval
= g_strdup (vfs_s_get_path_mangle (me
, buf
, archive
, flags
));
529 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
531 if (!super
->want_stale
){
532 vfs_s_free_inode (me
, super
->root
);
533 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
538 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
541 ERRNOR (EAGAIN
, NULL
);
543 if (!(MEDATA
->flags
& VFS_S_REMOTE
)) {
546 char *path
= g_strdup (ino
->ent
->name
);
549 if (ino
== ino
->super
->root
)
551 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
559 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
560 return g_strdup (ino
->ent
->name
);
562 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
,
563 ino
->ent
->name
, (char *) NULL
);
566 /* Support of archives */
567 /* ------------------------ readdir & friends ----------------------------- */
569 static struct vfs_s_inode
*
570 vfs_s_inode_from_path (struct vfs_class
*me
, const char *name
, int flags
)
572 struct vfs_s_super
*super
;
573 struct vfs_s_inode
*ino
;
576 if (!(q
= vfs_s_get_path (me
, name
, &super
, 0)))
580 vfs_s_find_inode (me
, super
, q
,
581 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
,
584 /* We are asking about / directory of ftp server: assume it exists */
586 vfs_s_find_inode (me
, super
, q
,
587 flags
& FL_FOLLOW
? LINK_FOLLOW
:
589 FL_DIR
| (flags
& ~FL_FOLLOW
));
595 struct vfs_s_entry
*cur
;
596 struct vfs_s_inode
*dir
;
600 vfs_s_opendir (struct vfs_class
*me
, const char *dirname
)
602 struct vfs_s_inode
*dir
;
603 struct dirhandle
*info
;
605 dir
= vfs_s_inode_from_path (me
, dirname
, FL_DIR
| FL_FOLLOW
);
608 if (!S_ISDIR (dir
->st
.st_mode
))
609 ERRNOR (ENOTDIR
, NULL
);
613 if (!dir
->subdir
) /* This can actually happen if we allow empty directories */
614 ERRNOR (EAGAIN
, NULL
);
616 info
= g_new (struct dirhandle
, 1);
617 info
->cur
= dir
->subdir
;
624 vfs_s_readdir(void *data
)
626 static union vfs_dirent dir
;
627 struct dirhandle
*info
= (struct dirhandle
*) data
;
632 if (info
->cur
->name
) {
633 g_strlcpy (dir
.dent
.d_name
, info
->cur
->name
, MC_MAXPATHLEN
);
635 vfs_die("Null in structure-cannot happen");
638 compute_namelen(&dir
.dent
);
639 info
->cur
= info
->cur
->next
;
641 return (void *) &dir
;
645 vfs_s_closedir (void *data
)
647 struct dirhandle
*info
= (struct dirhandle
*) data
;
648 struct vfs_s_inode
*dir
= info
->dir
;
650 vfs_s_free_inode (dir
->super
->me
, dir
);
656 vfs_s_chdir (struct vfs_class
*me
, const char *path
)
659 if (!(data
= vfs_s_opendir (me
, path
)))
661 vfs_s_closedir (data
);
665 /* --------------------------- stat and friends ---------------------------- */
668 vfs_s_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
, int flag
)
670 struct vfs_s_inode
*ino
;
672 if (!(ino
= vfs_s_inode_from_path (me
, path
, flag
)))
679 vfs_s_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
681 return vfs_s_internal_stat (me
, path
, buf
, FL_FOLLOW
);
685 vfs_s_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
687 return vfs_s_internal_stat (me
, path
, buf
, FL_NONE
);
691 vfs_s_fstat (void *fh
, struct stat
*buf
)
698 vfs_s_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
700 struct vfs_s_inode
*ino
;
703 ino
= vfs_s_inode_from_path (me
, path
, 0);
707 if (!S_ISLNK (ino
->st
.st_mode
))
710 if (ino
->linkname
== NULL
)
713 len
= strlen (ino
->linkname
);
716 /* readlink() does not append a NUL character to buf */
717 memcpy (buf
, ino
->linkname
, len
);
722 vfs_s_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
726 struct vfs_s_super
*super
;
728 struct vfs_s_inode
*ino
;
730 if ((q
= vfs_s_get_path (me
, file
, &super
, 0)) == NULL
)
732 ino
= vfs_s_find_inode (me
, super
, q
, LINK_FOLLOW
, FL_NONE
);
733 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))) {
735 ERRNOR (EEXIST
, NULL
);
738 char *dirname
, *name
, *save
;
739 struct vfs_s_entry
*ent
;
740 struct vfs_s_inode
*dir
;
743 /* If the filesystem is read-only, disable file creation */
744 if (!(flags
& O_CREAT
) || !(me
->write
)) {
749 split_dir_name (me
, q
, &dirname
, &name
, &save
);
750 /* FIXME: check if vfs_s_find_inode returns NULL */
751 dir
= vfs_s_find_inode (me
, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
754 ent
= vfs_s_generate_entry (me
, name
, dir
, 0755);
756 vfs_s_insert_entry (me
, dir
, ent
);
757 tmp_handle
= vfs_mkstemps (&ino
->localname
, me
->name
, name
);
758 if (tmp_handle
== -1) {
768 if (S_ISDIR (ino
->st
.st_mode
))
769 ERRNOR (EISDIR
, NULL
);
771 fh
= g_new (struct vfs_s_fh
, 1);
775 fh
->changed
= was_changed
;
778 if (IS_LINEAR (flags
)) {
779 if (MEDATA
->linear_start
) {
780 print_vfs_message (_("Starting linear transfer..."));
781 fh
->linear
= LS_LINEAR_PREOPEN
;
783 } else if ((MEDATA
->fh_open
)
784 && (MEDATA
->fh_open (me
, fh
, flags
, mode
))) {
789 if (fh
->ino
->localname
) {
790 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
791 if (fh
->handle
== -1) {
793 ERRNOR (errno
, NULL
);
797 /* i.e. we had no open files and now we have one */
798 vfs_rmstamp (me
, (vfsid
) super
);
800 fh
->ino
->st
.st_nlink
++;
805 vfs_s_read (void *fh
, char *buffer
, int count
)
808 struct vfs_class
*me
= FH_SUPER
->me
;
810 if (FH
->linear
== LS_LINEAR_PREOPEN
) {
811 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
815 if (FH
->linear
== LS_LINEAR_CLOSED
)
816 vfs_die ("linear_start() did not set linear_state!");
818 if (FH
->linear
== LS_LINEAR_OPEN
)
819 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
821 if (FH
->handle
!= -1){
822 n
= read (FH
->handle
, buffer
, count
);
827 vfs_die ("vfs_s_read: This should not happen\n");
832 vfs_s_write (void *fh
, const char *buffer
, int count
)
835 struct vfs_class
*me
= FH_SUPER
->me
;
838 vfs_die ("no writing to linear files, please");
841 if (FH
->handle
!= -1){
842 n
= write (FH
->handle
, buffer
, count
);
847 vfs_die ("vfs_s_write: This should not happen\n");
852 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
854 off_t size
= FH
->ino
->st
.st_size
;
856 if (FH
->linear
== LS_LINEAR_OPEN
)
857 vfs_die ("cannot lseek() after linear_read!");
859 if (FH
->handle
!= -1){ /* If we have local file opened, we want to work with it */
860 int retval
= lseek (FH
->handle
, offset
, whence
);
862 FH
->ino
->super
->me
->verrno
= errno
;
868 offset
+= FH
->pos
; break;
870 offset
+= size
; break;
874 else if (offset
< size
)
882 vfs_s_close (void *fh
)
885 struct vfs_class
*me
= FH_SUPER
->me
;
887 FH_SUPER
->fd_usage
--;
888 if (!FH_SUPER
->fd_usage
)
889 vfs_stamp_create (me
, FH_SUPER
);
891 if (FH
->linear
== LS_LINEAR_OPEN
)
892 MEDATA
->linear_close (me
, fh
);
893 if (MEDATA
->fh_close
)
894 res
= MEDATA
->fh_close (me
, fh
);
895 if (FH
->changed
&& MEDATA
->file_store
){
896 char *s
= vfs_s_fullpath (me
, FH
->ino
);
900 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
903 vfs_s_invalidate (me
, FH_SUPER
);
905 if (FH
->handle
!= -1)
908 vfs_s_free_inode (me
, FH
->ino
);
914 vfs_s_print_stats (const char *fs_name
, const char *action
,
915 const char *file_name
, off_t have
, off_t need
)
917 static const char *i18n_percent_transf_format
= NULL
;
918 static const char *i18n_transf_format
= NULL
;
920 if (i18n_percent_transf_format
== NULL
) {
921 i18n_percent_transf_format
=
922 _("%s: %s: %s %3d%% (%lu bytes transferred)");
923 i18n_transf_format
= _("%s: %s: %s %lu bytes transferred");
927 print_vfs_message (i18n_percent_transf_format
, fs_name
, action
,
928 file_name
, (int) ((double) have
* 100 / need
),
929 (unsigned long) have
);
931 print_vfs_message (i18n_transf_format
, fs_name
, action
, file_name
,
932 (unsigned long) have
);
936 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
938 /* If you want reget, you'll have to open file with O_LINEAR */
942 off_t stat_size
= ino
->st
.st_size
;
945 memset (&fh
, 0, sizeof (fh
));
950 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
956 if (!MEDATA
->linear_start (me
, &fh
, 0))
959 /* Clear the interrupt status */
961 enable_interrupt_key ();
963 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
)))) {
969 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
,
972 if (got_interrupt ())
975 t
= write (handle
, buffer
, n
);
982 MEDATA
->linear_close (me
, &fh
);
985 disable_interrupt_key ();
989 MEDATA
->linear_close (me
, &fh
);
991 disable_interrupt_key ();
993 unlink (ino
->localname
);
995 g_free (ino
->localname
);
996 ino
->localname
= NULL
;
1000 /* ------------------------------- mc support ---------------------------- */
1003 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
1005 struct vfs_s_super
*a
= MEDATA
->supers
;
1009 name
= g_strconcat ( a
->name
, "#", me
->prefix
, "/", /* a->current_dir->name, */ NULL
);
1017 vfs_s_ferrno (struct vfs_class
*me
)
1023 * Get local copy of the given file. We reuse the existing file cache
1024 * for remote filesystems. Archives use standard VFS facilities.
1027 vfs_s_getlocalcopy (struct vfs_class
*me
, const char *path
)
1029 struct vfs_s_fh
*fh
;
1032 fh
= vfs_s_open (me
, path
, O_RDONLY
, 0);
1033 if (!fh
|| !fh
->ino
|| !fh
->ino
->localname
)
1036 local
= g_strdup (fh
->ino
->localname
);
1042 * Return the local copy. Since we are using our cache, we do nothing -
1043 * the cache will be removed when the archive is closed.
1046 vfs_s_ungetlocalcopy (struct vfs_class
*me
, const char *path
,
1047 const char *local
, int has_changed
)
1057 vfs_s_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1060 case VFS_SETCTL_STALE_DATA
:
1062 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (me
, path
, 0);
1067 ino
->super
->want_stale
= 1;
1069 ino
->super
->want_stale
= 0;
1070 vfs_s_invalidate (me
, ino
->super
);
1074 case VFS_SETCTL_LOGFILE
:
1075 MEDATA
->logfile
= fopen ((char *) arg
, "w");
1077 case VFS_SETCTL_FLUSH
:
1085 /* ----------------------------- Stamping support -------------------------- */
1088 vfs_s_getid (struct vfs_class
*me
, const char *path
)
1090 struct vfs_s_super
*archive
;
1093 if (!(p
= vfs_s_get_path (me
, path
, &archive
, FL_NO_OPEN
)))
1096 return (vfsid
) archive
;
1100 vfs_s_nothingisopen (vfsid id
)
1103 /* Our data structures should survive free of superblock at any time */
1108 vfs_s_free (vfsid id
)
1110 vfs_s_free_super (((struct vfs_s_super
*)id
)->me
, (struct vfs_s_super
*)id
);
1114 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1118 if (MEDATA
->flush
) {
1123 gettimeofday(&tim
, NULL
);
1124 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
1129 /* Initialize one of our subclasses - fill common functions */
1131 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1134 vclass
->fill_names
= vfs_s_fill_names
;
1135 vclass
->open
= vfs_s_open
;
1136 vclass
->close
= vfs_s_close
;
1137 vclass
->read
= vfs_s_read
;
1138 if (!(sub
->flags
& VFS_S_READONLY
)) {
1139 vclass
->write
= vfs_s_write
;
1141 vclass
->opendir
= vfs_s_opendir
;
1142 vclass
->readdir
= vfs_s_readdir
;
1143 vclass
->closedir
= vfs_s_closedir
;
1144 vclass
->stat
= vfs_s_stat
;
1145 vclass
->lstat
= vfs_s_lstat
;
1146 vclass
->fstat
= vfs_s_fstat
;
1147 vclass
->readlink
= vfs_s_readlink
;
1148 vclass
->chdir
= vfs_s_chdir
;
1149 vclass
->ferrno
= vfs_s_ferrno
;
1150 vclass
->lseek
= vfs_s_lseek
;
1151 vclass
->getid
= vfs_s_getid
;
1152 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1153 vclass
->free
= vfs_s_free
;
1154 if (sub
->flags
& VFS_S_REMOTE
) {
1155 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1156 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1157 sub
->find_entry
= vfs_s_find_entry_linear
;
1159 sub
->find_entry
= vfs_s_find_entry_tree
;
1161 vclass
->setctl
= vfs_s_setctl
;
1162 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1165 /* ----------- Utility functions for networked filesystems -------------- */
1169 vfs_s_select_on_two (int fd1
, int fd2
)
1172 struct timeval timeout
;
1174 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1177 timeout
.tv_usec
= 0;
1181 v
= select (maxfd
, &set
, 0, 0, &timeout
);
1184 if (FD_ISSET (fd1
, &set
))
1186 if (FD_ISSET (fd2
, &set
))
1192 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1194 FILE *logfile
= MEDATA
->logfile
;
1198 for (i
= 0; i
< buf_len
- 1; i
++, buf
++){
1199 if (read (sock
, buf
, sizeof(char)) <= 0)
1202 fwrite (buf
, 1, 1, logfile
);
1211 /* Line is too long - terminate buffer and discard the rest of line */
1213 while (read (sock
, &c
, sizeof (c
)) > 0) {
1215 fwrite (&c
, 1, 1, logfile
);
1225 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1232 enable_interrupt_key ();
1233 for (i
= 0; i
< size
-1; i
++){
1234 n
= read (fd
, buffer
+i
, 1);
1235 disable_interrupt_key ();
1236 if (n
== -1 && errno
== EINTR
){
1244 if (buffer
[i
] == '\n'){
1249 buffer
[size
-1] = 0;
1252 #endif /* USE_NETCODE */