2 * \brief Source: directory cache support
4 * So that you do not have copy of this in each and every filesystem.
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 * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
27 * \warning Paths here do _not_ begin with '/', so root directory of
28 * archive/site is simply "".
34 #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
35 /* includes fcntl.h see IEEE Std 1003.1-2008 */
37 #include <sys/time.h> /* gettimeofday() */
39 #include "lib/global.h"
41 #include "lib/tty/tty.h" /* enable/disable interrupt key */
43 #include "src/wtools.h" /* message() */
44 #include "src/main.h" /* print_vfs_message */
49 #include "gc.h" /* vfs_rmstamp */
50 #include "xdirentry.h"
52 #define CALL(x) if (MEDATA->x) MEDATA->x
54 static volatile int total_inodes
= 0, total_entries
= 0;
57 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
59 struct vfs_s_inode
*ino
;
61 ino
= g_try_new0 (struct vfs_s_inode
, 1);
69 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
70 ino
->st
.st_dev
= MEDATA
->rdev
;
75 CALL (init_inode
) (me
, ino
);
81 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
83 struct vfs_s_entry
*entry
;
85 entry
= g_new0 (struct vfs_s_entry
, 1);
89 entry
->name
= g_strdup (name
);
92 entry
->ino
->ent
= entry
;
93 CALL (init_entry
) (me
, entry
);
99 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
102 vfs_die ("Don't pass NULL to me");
104 /* ==0 can happen if freshly created entry is deleted */
105 if (ino
->st
.st_nlink
<= 1)
109 vfs_s_free_entry (me
, ino
->subdir
);
112 CALL (free_inode
) (me
, ino
);
113 g_free (ino
->linkname
);
116 unlink (ino
->localname
);
117 g_free (ino
->localname
);
120 ino
->super
->ino_usage
--;
128 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
132 /* It is possible that we are deleting freshly created entry */
133 *ent
->prevp
= ent
->next
;
135 ent
->next
->prevp
= ent
->prevp
;
143 ent
->ino
->ent
= NULL
;
144 vfs_s_free_inode (me
, ent
->ino
);
153 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
155 struct vfs_s_entry
**ep
;
159 for (ep
= &dir
->subdir
; *ep
!= NULL
; ep
= &((*ep
)->next
))
166 ent
->ino
->st
.st_nlink
++;
170 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
172 static struct stat st
;
177 myumask
= umask (022);
185 st
.st_uid
= getuid ();
186 st
.st_gid
= getgid ();
188 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
194 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
,
197 struct vfs_s_inode
*inode
;
200 st
= vfs_s_default_stat (me
, mode
);
201 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
203 return vfs_s_new_entry (me
, name
, inode
);
206 /* We were asked to create entries automagically */
207 static struct vfs_s_entry
*
208 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
210 struct vfs_s_entry
*res
;
211 char *sep
= strchr (path
, PATH_SEP
);
215 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
216 vfs_s_insert_entry (me
, dir
, res
);
224 /* If the entry is a symlink, find the entry for its target */
225 static struct vfs_s_entry
*
226 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
, int follow
)
229 char *fullname
= NULL
;
230 struct vfs_s_entry
*target
;
232 if (follow
== LINK_NO_FOLLOW
)
235 ERRNOR (ELOOP
, NULL
);
237 ERRNOR (ENOENT
, NULL
);
238 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
241 linkname
= entry
->ino
->linkname
;
242 if (linkname
== NULL
)
243 ERRNOR (EFAULT
, NULL
);
245 /* make full path from relative */
246 if (*linkname
!= PATH_SEP
)
248 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
251 fullname
= g_strconcat (fullpath
, "/", linkname
, (char *) NULL
);
257 target
= (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
, follow
- 1, 0);
263 * Follow > 0: follow links, serves as loop protect,
264 * == -1: do not follow links
266 static struct vfs_s_entry
*
267 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
268 const char *a_path
, int follow
, int flags
)
271 struct vfs_s_entry
*ent
= NULL
;
272 char *const pathref
= g_strdup (a_path
);
273 char *path
= pathref
;
275 /* canonicalize as well, but don't remove '../' from path */
276 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
280 while (*path
== PATH_SEP
) /* Strip leading '/' */
289 for (pseg
= 0; path
[pseg
] && path
[pseg
] != PATH_SEP
; pseg
++);
291 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
292 if (strlen (ent
->name
) == pseg
&& (!strncmp (ent
->name
, path
, pseg
)))
296 if (!ent
&& (flags
& (FL_MKFILE
| FL_MKDIR
)))
297 ent
= vfs_s_automake (me
, root
, path
, flags
);
304 /* here we must follow leading directories always;
305 only the actual file is optional */
306 ent
= vfs_s_resolve_symlink (me
, ent
, strchr (path
, PATH_SEP
) ? LINK_FOLLOW
: follow
);
317 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
323 s
= strrchr (path
, PATH_SEP
);
328 *dir
= path
+ strlen (path
); /* an empty string */
339 static struct vfs_s_entry
*
340 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
341 const char *a_path
, int follow
, int flags
)
343 struct vfs_s_entry
*ent
= NULL
;
344 char *const path
= g_strdup (a_path
);
345 struct vfs_s_entry
*retval
= NULL
;
347 if (root
->super
->root
!= root
)
348 vfs_die ("We have to use _real_ root. Always. Sorry.");
350 /* canonicalize as well, but don't remove '../' from path */
351 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
353 if (!(flags
& FL_DIR
))
355 char *dirname
, *name
, *save
;
356 struct vfs_s_inode
*ino
;
357 split_dir_name (me
, path
, &dirname
, &name
, &save
);
358 ino
= vfs_s_find_inode (me
, root
->super
, dirname
, follow
, flags
| FL_DIR
);
361 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
366 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
367 if (!strcmp (ent
->name
, path
))
370 if (ent
&& (!(MEDATA
->dir_uptodate
) (me
, ent
->ino
)))
373 print_vfs_message (_("Directory cache expired for %s"), path
);
375 vfs_s_free_entry (me
, ent
);
381 struct vfs_s_inode
*ino
;
383 ino
= vfs_s_new_inode (me
, root
->super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
384 ent
= vfs_s_new_entry (me
, path
, ino
);
385 if ((MEDATA
->dir_load
) (me
, ino
, path
) == -1)
387 vfs_s_free_entry (me
, ent
);
391 vfs_s_insert_entry (me
, root
, ent
);
393 for (ent
= root
->subdir
; ent
!= NULL
; ent
= ent
->next
)
394 if (!strcmp (ent
->name
, path
))
398 vfs_die ("find_linear: success but directory is not there\n");
401 if (!vfs_s_resolve_symlink (me
, ent
, follow
))
412 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
413 const char *path
, int follow
, int flags
)
415 struct vfs_s_entry
*ent
;
417 if (((MEDATA
->flags
& VFS_S_REMOTE
) == 0) && (*path
== '\0'))
420 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
421 return (ent
!= NULL
) ? ent
->ino
: NULL
;
424 /* Ook, these were functions around directory entries / inodes */
425 /* -------------------------------- superblock games -------------------------- */
427 static struct vfs_s_super
*
428 vfs_s_new_super (struct vfs_class
*me
)
430 struct vfs_s_super
*super
;
432 super
= g_new0 (struct vfs_s_super
, 1);
438 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
440 super
->next
= MEDATA
->supers
;
441 super
->prevp
= &MEDATA
->supers
;
443 if (MEDATA
->supers
!= NULL
)
444 MEDATA
->supers
->prevp
= &super
->next
;
445 MEDATA
->supers
= super
;
449 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
453 vfs_s_free_inode (me
, super
->root
);
458 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
459 if (super
->ino_usage
)
460 message (D_ERROR
, " Direntry warning ",
461 "Super ino_usage is %d, memory leak", super
->ino_usage
);
463 if (super
->want_stale
)
464 message (D_ERROR
, " Direntry warning ", "Super has want_stale set");
469 *super
->prevp
= super
->next
;
471 super
->next
->prevp
= super
->prevp
;
474 CALL (free_archive
) (me
, super
);
475 g_free (super
->name
);
481 * Dissect the path and create corresponding superblock. Note that inname
482 * can be changed and the result may point inside the original string.
485 vfs_s_get_path_mangle (struct vfs_class
*me
, char *inname
, struct vfs_s_super
**archive
, int flags
)
489 const char *archive_name
;
491 struct vfs_s_super
*super
;
494 archive_name
= inname
;
495 vfs_split (inname
, &local
, &op
);
496 retval
= (local
) ? local
: "";
498 if (MEDATA
->archive_check
)
499 if (!(cookie
= MEDATA
->archive_check (me
, archive_name
, op
)))
502 for (super
= MEDATA
->supers
; super
!= NULL
; super
= super
->next
)
504 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
505 int i
= MEDATA
->archive_same (me
, super
, archive_name
, op
, cookie
);
515 if (flags
& FL_NO_OPEN
)
518 super
= vfs_s_new_super (me
);
519 result
= MEDATA
->open_archive (me
, super
, archive_name
, op
);
522 vfs_s_free_super (me
, super
);
526 vfs_die ("You have to fill name\n");
528 vfs_die ("You have to fill root inode\n");
530 vfs_s_insert_super (me
, super
);
531 vfs_stamp_create (me
, super
);
540 * Dissect the path and create corresponding superblock.
541 * The result should be freed.
544 vfs_s_get_path (struct vfs_class
*me
, const char *inname
, struct vfs_s_super
**archive
, int flags
)
548 buf
= g_strdup (inname
);
549 retval
= g_strdup (vfs_s_get_path_mangle (me
, buf
, archive
, flags
));
555 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
557 if (!super
->want_stale
)
559 vfs_s_free_inode (me
, super
->root
);
560 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
565 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
568 ERRNOR (EAGAIN
, NULL
);
570 if (!(MEDATA
->flags
& VFS_S_REMOTE
))
574 char *path
= g_strdup (ino
->ent
->name
);
578 if (ino
== ino
->super
->root
)
580 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
588 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
589 return g_strdup (ino
->ent
->name
);
591 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
, ino
->ent
->name
, (char *) NULL
);
594 /* Support of archives */
595 /* ------------------------ readdir & friends ----------------------------- */
597 static struct vfs_s_inode
*
598 vfs_s_inode_from_path (struct vfs_class
*me
, const char *name
, int flags
)
600 struct vfs_s_super
*super
;
601 struct vfs_s_inode
*ino
;
604 if (!(q
= vfs_s_get_path (me
, name
, &super
, 0)))
608 vfs_s_find_inode (me
, super
, q
,
609 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
, flags
& ~FL_FOLLOW
);
611 /* We are asking about / directory of ftp server: assume it exists */
613 vfs_s_find_inode (me
, super
, q
,
614 flags
& FL_FOLLOW
? LINK_FOLLOW
:
615 LINK_NO_FOLLOW
, FL_DIR
| (flags
& ~FL_FOLLOW
));
622 struct vfs_s_entry
*cur
;
623 struct vfs_s_inode
*dir
;
627 vfs_s_opendir (struct vfs_class
*me
, const char *dirname
)
629 struct vfs_s_inode
*dir
;
630 struct dirhandle
*info
;
632 dir
= vfs_s_inode_from_path (me
, dirname
, FL_DIR
| FL_FOLLOW
);
635 if (!S_ISDIR (dir
->st
.st_mode
))
636 ERRNOR (ENOTDIR
, NULL
);
640 if (!dir
->subdir
) /* This can actually happen if we allow empty directories */
641 ERRNOR (EAGAIN
, NULL
);
643 info
= g_new (struct dirhandle
, 1);
644 info
->cur
= dir
->subdir
;
651 vfs_s_readdir (void *data
)
653 static union vfs_dirent dir
;
654 struct dirhandle
*info
= (struct dirhandle
*) data
;
661 g_strlcpy (dir
.dent
.d_name
, info
->cur
->name
, MC_MAXPATHLEN
);
665 vfs_die ("Null in structure-cannot happen");
668 compute_namelen (&dir
.dent
);
669 info
->cur
= info
->cur
->next
;
671 return (void *) &dir
;
675 vfs_s_closedir (void *data
)
677 struct dirhandle
*info
= (struct dirhandle
*) data
;
678 struct vfs_s_inode
*dir
= info
->dir
;
680 vfs_s_free_inode (dir
->super
->me
, dir
);
686 vfs_s_chdir (struct vfs_class
*me
, const char *path
)
689 if (!(data
= vfs_s_opendir (me
, path
)))
691 vfs_s_closedir (data
);
695 /* --------------------------- stat and friends ---------------------------- */
698 vfs_s_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
, int flag
)
700 struct vfs_s_inode
*ino
;
702 if (!(ino
= vfs_s_inode_from_path (me
, path
, flag
)))
709 vfs_s_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
711 return vfs_s_internal_stat (me
, path
, buf
, FL_FOLLOW
);
715 vfs_s_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
717 return vfs_s_internal_stat (me
, path
, buf
, FL_NONE
);
721 vfs_s_fstat (void *fh
, struct stat
*buf
)
728 vfs_s_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
730 struct vfs_s_inode
*ino
;
733 ino
= vfs_s_inode_from_path (me
, path
, 0);
737 if (!S_ISLNK (ino
->st
.st_mode
))
740 if (ino
->linkname
== NULL
)
743 len
= strlen (ino
->linkname
);
746 /* readlink() does not append a NUL character to buf */
747 memcpy (buf
, ino
->linkname
, len
);
752 vfs_s_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
756 struct vfs_s_super
*super
;
758 struct vfs_s_inode
*ino
;
760 if ((q
= vfs_s_get_path (me
, file
, &super
, 0)) == NULL
)
762 ino
= vfs_s_find_inode (me
, super
, q
, LINK_FOLLOW
, FL_NONE
);
763 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)))
766 ERRNOR (EEXIST
, NULL
);
770 char *dirname
, *name
, *save
;
771 struct vfs_s_entry
*ent
;
772 struct vfs_s_inode
*dir
;
775 /* If the filesystem is read-only, disable file creation */
776 if (!(flags
& O_CREAT
) || !(me
->write
))
782 split_dir_name (me
, q
, &dirname
, &name
, &save
);
783 /* FIXME: check if vfs_s_find_inode returns NULL */
784 dir
= vfs_s_find_inode (me
, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
787 ent
= vfs_s_generate_entry (me
, name
, dir
, 0755);
789 vfs_s_insert_entry (me
, dir
, ent
);
790 tmp_handle
= vfs_mkstemps (&ino
->localname
, me
->name
, name
);
791 if (tmp_handle
== -1)
802 if (S_ISDIR (ino
->st
.st_mode
))
803 ERRNOR (EISDIR
, NULL
);
805 fh
= g_new (struct vfs_s_fh
, 1);
809 fh
->changed
= was_changed
;
812 if (IS_LINEAR (flags
))
814 if (MEDATA
->linear_start
)
816 print_vfs_message (_("Starting linear transfer..."));
817 fh
->linear
= LS_LINEAR_PREOPEN
;
820 else if ((MEDATA
->fh_open
) && (MEDATA
->fh_open (me
, fh
, flags
, mode
)))
826 if (fh
->ino
->localname
)
828 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
829 if (fh
->handle
== -1)
832 ERRNOR (errno
, NULL
);
836 /* i.e. we had no open files and now we have one */
837 vfs_rmstamp (me
, (vfsid
) super
);
839 fh
->ino
->st
.st_nlink
++;
844 vfs_s_read (void *fh
, char *buffer
, int count
)
847 struct vfs_class
*me
= FH_SUPER
->me
;
849 if (FH
->linear
== LS_LINEAR_PREOPEN
)
851 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
855 if (FH
->linear
== LS_LINEAR_CLOSED
)
856 vfs_die ("linear_start() did not set linear_state!");
858 if (FH
->linear
== LS_LINEAR_OPEN
)
859 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
861 if (FH
->handle
!= -1)
863 n
= read (FH
->handle
, buffer
, count
);
868 vfs_die ("vfs_s_read: This should not happen\n");
873 vfs_s_write (void *fh
, const char *buffer
, int count
)
876 struct vfs_class
*me
= FH_SUPER
->me
;
879 vfs_die ("no writing to linear files, please");
882 if (FH
->handle
!= -1)
884 n
= write (FH
->handle
, buffer
, count
);
889 vfs_die ("vfs_s_write: This should not happen\n");
894 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
896 off_t size
= FH
->ino
->st
.st_size
;
898 if (FH
->linear
== LS_LINEAR_OPEN
)
899 vfs_die ("cannot lseek() after linear_read!");
901 if (FH
->handle
!= -1)
902 { /* If we have local file opened, we want to work with it */
903 int retval
= lseek (FH
->handle
, offset
, whence
);
905 FH
->ino
->super
->me
->verrno
= errno
;
920 else if (offset
< size
)
928 vfs_s_close (void *fh
)
931 struct vfs_class
*me
= FH_SUPER
->me
;
933 FH_SUPER
->fd_usage
--;
934 if (!FH_SUPER
->fd_usage
)
935 vfs_stamp_create (me
, FH_SUPER
);
937 if (FH
->linear
== LS_LINEAR_OPEN
)
938 MEDATA
->linear_close (me
, fh
);
939 if (MEDATA
->fh_close
)
940 res
= MEDATA
->fh_close (me
, fh
);
941 if (FH
->changed
&& MEDATA
->file_store
)
943 char *s
= vfs_s_fullpath (me
, FH
->ino
);
948 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
951 vfs_s_invalidate (me
, FH_SUPER
);
953 if (FH
->handle
!= -1)
956 vfs_s_free_inode (me
, FH
->ino
);
962 vfs_s_print_stats (const char *fs_name
, const char *action
,
963 const char *file_name
, off_t have
, off_t need
)
965 static const char *i18n_percent_transf_format
= NULL
;
966 static const char *i18n_transf_format
= NULL
;
968 if (i18n_percent_transf_format
== NULL
)
970 i18n_percent_transf_format
= _("%s: %s: %s %3d%% (%lu bytes transferred)");
971 i18n_transf_format
= _("%s: %s: %s %lu bytes transferred");
975 print_vfs_message (i18n_percent_transf_format
, fs_name
, action
,
976 file_name
, (int) ((double) have
* 100 / need
), (unsigned long) have
);
978 print_vfs_message (i18n_transf_format
, fs_name
, action
, file_name
, (unsigned long) have
);
982 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
984 /* If you want reget, you'll have to open file with O_LINEAR */
988 off_t stat_size
= ino
->st
.st_size
;
991 memset (&fh
, 0, sizeof (fh
));
996 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
1003 if (!MEDATA
->linear_start (me
, &fh
, 0))
1006 /* Clear the interrupt status */
1007 tty_got_interrupt ();
1008 tty_enable_interrupt_key ();
1010 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
))))
1017 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
, total
, stat_size
);
1019 if (tty_got_interrupt ())
1022 t
= write (handle
, buffer
, n
);
1030 MEDATA
->linear_close (me
, &fh
);
1033 tty_disable_interrupt_key ();
1037 MEDATA
->linear_close (me
, &fh
);
1039 tty_disable_interrupt_key ();
1041 unlink (ino
->localname
);
1043 g_free (ino
->localname
);
1044 ino
->localname
= NULL
;
1048 /* ------------------------------- mc support ---------------------------- */
1051 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
1053 struct vfs_s_super
*a
= MEDATA
->supers
;
1058 name
= g_strconcat (a
->name
, "#", me
->prefix
, "/",
1059 /* a->current_dir->name, */ (char *) NULL
);
1067 vfs_s_ferrno (struct vfs_class
*me
)
1073 * Get local copy of the given file. We reuse the existing file cache
1074 * for remote filesystems. Archives use standard VFS facilities.
1077 vfs_s_getlocalcopy (struct vfs_class
*me
, const char *path
)
1079 struct vfs_s_fh
*fh
;
1082 fh
= vfs_s_open (me
, path
, O_RDONLY
, 0);
1086 if ((fh
->ino
!= NULL
) && (fh
->ino
->localname
!= NULL
))
1087 local
= g_strdup (fh
->ino
->localname
);
1096 * Return the local copy. Since we are using our cache, we do nothing -
1097 * the cache will be removed when the archive is closed.
1100 vfs_s_ungetlocalcopy (struct vfs_class
*me
, const char *path
, const char *local
, int has_changed
)
1110 vfs_s_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
1114 case VFS_SETCTL_STALE_DATA
:
1116 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (me
, path
, 0);
1121 ino
->super
->want_stale
= 1;
1124 ino
->super
->want_stale
= 0;
1125 vfs_s_invalidate (me
, ino
->super
);
1129 case VFS_SETCTL_LOGFILE
:
1130 MEDATA
->logfile
= fopen ((char *) arg
, "w");
1132 case VFS_SETCTL_FLUSH
:
1140 /* ----------------------------- Stamping support -------------------------- */
1143 vfs_s_getid (struct vfs_class
*me
, const char *path
)
1145 struct vfs_s_super
*archive
;
1148 if (!(p
= vfs_s_get_path (me
, path
, &archive
, FL_NO_OPEN
)))
1151 return (vfsid
) archive
;
1155 vfs_s_nothingisopen (vfsid id
)
1158 /* Our data structures should survive free of superblock at any time */
1163 vfs_s_free (vfsid id
)
1165 vfs_s_free_super (((struct vfs_s_super
*) id
)->me
, (struct vfs_s_super
*) id
);
1169 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1179 gettimeofday (&tim
, NULL
);
1180 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
1185 /* Initialize one of our subclasses - fill common functions */
1187 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1190 vclass
->fill_names
= vfs_s_fill_names
;
1191 vclass
->open
= vfs_s_open
;
1192 vclass
->close
= vfs_s_close
;
1193 vclass
->read
= vfs_s_read
;
1194 if (!(sub
->flags
& VFS_S_READONLY
))
1196 vclass
->write
= vfs_s_write
;
1198 vclass
->opendir
= vfs_s_opendir
;
1199 vclass
->readdir
= vfs_s_readdir
;
1200 vclass
->closedir
= vfs_s_closedir
;
1201 vclass
->stat
= vfs_s_stat
;
1202 vclass
->lstat
= vfs_s_lstat
;
1203 vclass
->fstat
= vfs_s_fstat
;
1204 vclass
->readlink
= vfs_s_readlink
;
1205 vclass
->chdir
= vfs_s_chdir
;
1206 vclass
->ferrno
= vfs_s_ferrno
;
1207 vclass
->lseek
= vfs_s_lseek
;
1208 vclass
->getid
= vfs_s_getid
;
1209 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1210 vclass
->free
= vfs_s_free
;
1211 if (sub
->flags
& VFS_S_REMOTE
)
1213 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1214 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1215 sub
->find_entry
= vfs_s_find_entry_linear
;
1219 sub
->find_entry
= vfs_s_find_entry_tree
;
1221 vclass
->setctl
= vfs_s_setctl
;
1222 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1225 /* ----------- Utility functions for networked filesystems -------------- */
1229 vfs_s_select_on_two (int fd1
, int fd2
)
1232 struct timeval time_out
;
1234 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1236 time_out
.tv_sec
= 1;
1237 time_out
.tv_usec
= 0;
1241 v
= select (maxfd
, &set
, 0, 0, &time_out
);
1244 if (FD_ISSET (fd1
, &set
))
1246 if (FD_ISSET (fd2
, &set
))
1252 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1254 FILE *logfile
= MEDATA
->logfile
;
1258 for (i
= 0; i
< buf_len
- 1; i
++, buf
++)
1260 if (read (sock
, buf
, sizeof (char)) <= 0)
1264 fwrite (buf
, 1, 1, logfile
);
1274 /* Line is too long - terminate buffer and discard the rest of line */
1276 while (read (sock
, &c
, sizeof (c
)) > 0)
1280 fwrite (&c
, 1, 1, logfile
);
1290 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1297 tty_enable_interrupt_key ();
1298 for (i
= 0; i
< size
- 1; i
++)
1300 n
= read (fd
, buffer
+ i
, 1);
1301 tty_disable_interrupt_key ();
1302 if (n
== -1 && errno
== EINTR
)
1312 if (buffer
[i
] == '\n')
1318 buffer
[size
- 1] = 0;
1321 #endif /* USE_NETCODE */