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() */
38 #include <inttypes.h> /* uintmax_t */
41 #include "lib/global.h"
43 #include "lib/tty/tty.h" /* enable/disable interrupt key */
44 #include "lib/util.h" /* concat_dir_and_file */
46 #include "lib/widget.h" /* message() */
51 #include "xdirentry.h"
52 #include "gc.h" /* vfs_rmstamp */
54 /*** global variables ****************************************************************************/
56 /*** file scope macro definitions ****************************************************************/
58 #define CALL(x) if (MEDATA->x) MEDATA->x
60 /*** file scope type declarations ****************************************************************/
65 struct vfs_s_inode
*dir
;
68 /*** file scope variables ************************************************************************/
70 static volatile int total_inodes
= 0, total_entries
= 0;
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 vfs_s_entry_compare (const void *a
, const void *b
)
78 const struct vfs_s_entry
*e
= (const struct vfs_s_entry
*) a
;
79 const char *name
= (const char *) b
;
81 return strcmp (e
->name
, name
);
84 /* --------------------------------------------------------------------------------------------- */
87 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
90 vfs_die ("Don't pass NULL to me");
92 /* ==0 can happen if freshly created entry is deleted */
93 if (ino
->st
.st_nlink
> 1)
99 while (ino
->subdir
!= NULL
)
100 vfs_s_free_entry (me
, (struct vfs_s_entry
*) ino
->subdir
->data
);
102 CALL (free_inode
) (me
, ino
);
103 g_free (ino
->linkname
);
104 if (ino
->localname
!= NULL
)
106 unlink (ino
->localname
);
107 g_free (ino
->localname
);
110 ino
->super
->ino_usage
--;
114 /* --------------------------------------------------------------------------------------------- */
115 /* We were asked to create entries automagically */
117 static struct vfs_s_entry
*
118 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
120 struct vfs_s_entry
*res
;
123 sep
= strchr (path
, PATH_SEP
);
127 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
128 vfs_s_insert_entry (me
, dir
, res
);
136 /* --------------------------------------------------------------------------------------------- */
137 /* If the entry is a symlink, find the entry for its target */
139 static struct vfs_s_entry
*
140 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
, int follow
)
143 char *fullname
= NULL
;
144 struct vfs_s_entry
*target
;
146 if (follow
== LINK_NO_FOLLOW
)
149 ERRNOR (ELOOP
, NULL
);
151 ERRNOR (ENOENT
, NULL
);
152 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
155 linkname
= entry
->ino
->linkname
;
156 if (linkname
== NULL
)
157 ERRNOR (EFAULT
, NULL
);
159 /* make full path from relative */
160 if (*linkname
!= PATH_SEP
)
162 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
165 fullname
= g_strconcat (fullpath
, "/", linkname
, (char *) NULL
);
171 target
= (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
, follow
- 1, 0);
176 /* --------------------------------------------------------------------------------------------- */
178 * Follow > 0: follow links, serves as loop protect,
179 * == -1: do not follow links
182 static struct vfs_s_entry
*
183 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
184 const char *a_path
, int follow
, int flags
)
187 struct vfs_s_entry
*ent
= NULL
;
188 char *const pathref
= g_strdup (a_path
);
189 char *path
= pathref
;
191 /* canonicalize as well, but don't remove '../' from path */
192 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
198 while (*path
== PATH_SEP
) /* Strip leading '/' */
207 for (pseg
= 0; path
[pseg
] != '\0' && path
[pseg
] != PATH_SEP
; pseg
++)
210 for (iter
= root
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
212 ent
= (struct vfs_s_entry
*) iter
->data
;
213 if (strlen (ent
->name
) == pseg
&& strncmp (ent
->name
, path
, pseg
) == 0)
218 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
220 if (ent
== NULL
&& (flags
& (FL_MKFILE
| FL_MKDIR
)) != 0)
221 ent
= vfs_s_automake (me
, root
, path
, flags
);
229 /* here we must follow leading directories always;
230 only the actual file is optional */
231 ent
= vfs_s_resolve_symlink (me
, ent
,
232 strchr (path
, PATH_SEP
) != NULL
? LINK_FOLLOW
: follow
);
242 /* --------------------------------------------------------------------------------------------- */
245 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
251 s
= strrchr (path
, PATH_SEP
);
256 *dir
= path
+ strlen (path
); /* an empty string */
267 /* --------------------------------------------------------------------------------------------- */
269 static struct vfs_s_entry
*
270 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
271 const char *a_path
, int follow
, int flags
)
273 struct vfs_s_entry
*ent
= NULL
;
274 char *const path
= g_strdup (a_path
);
275 struct vfs_s_entry
*retval
= NULL
;
278 if (root
->super
->root
!= root
)
279 vfs_die ("We have to use _real_ root. Always. Sorry.");
281 /* canonicalize as well, but don't remove '../' from path */
282 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
284 if ((flags
& FL_DIR
) == 0)
286 char *dirname
, *name
, *save
;
287 struct vfs_s_inode
*ino
;
288 split_dir_name (me
, path
, &dirname
, &name
, &save
);
289 ino
= vfs_s_find_inode (me
, root
->super
, dirname
, follow
, flags
| FL_DIR
);
292 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
297 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
298 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
300 if (ent
!= NULL
&& !MEDATA
->dir_uptodate (me
, ent
->ino
))
303 vfs_print_message (_("Directory cache expired for %s"), path
);
305 vfs_s_free_entry (me
, ent
);
311 struct vfs_s_inode
*ino
;
313 ino
= vfs_s_new_inode (me
, root
->super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
314 ent
= vfs_s_new_entry (me
, path
, ino
);
315 if (MEDATA
->dir_load (me
, ino
, path
) == -1)
317 vfs_s_free_entry (me
, ent
);
322 vfs_s_insert_entry (me
, root
, ent
);
324 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
325 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
328 vfs_die ("find_linear: success but directory is not there\n");
331 if (!vfs_s_resolve_symlink (me
, ent
, follow
))
341 /* --------------------------------------------------------------------------------------------- */
342 /* Ook, these were functions around directory entries / inodes */
343 /* -------------------------------- superblock games -------------------------- */
345 static struct vfs_s_super
*
346 vfs_s_new_super (struct vfs_class
*me
)
348 struct vfs_s_super
*super
;
350 super
= g_new0 (struct vfs_s_super
, 1);
355 /* --------------------------------------------------------------------------------------------- */
358 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
360 MEDATA
->supers
= g_list_prepend (MEDATA
->supers
, super
);
363 /* --------------------------------------------------------------------------------------------- */
366 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
368 if (super
->root
!= NULL
)
370 vfs_s_free_inode (me
, super
->root
);
375 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
376 if (super
->ino_usage
)
377 message (D_ERROR
, "Direntry warning",
378 "Super ino_usage is %d, memory leak", super
->ino_usage
);
380 if (super
->want_stale
)
381 message (D_ERROR
, "Direntry warning", "%s", "Super has want_stale set");
384 MEDATA
->supers
= g_list_remove (MEDATA
->supers
, super
);
386 CALL (free_archive
) (me
, super
);
387 #ifdef ENABLE_VFS_NET
388 vfs_path_element_free (super
->path_element
);
390 g_free (super
->name
);
394 /* --------------------------------------------------------------------------------------------- */
396 * Dissect the path and create corresponding superblock.
397 * The result should be freed.
401 vfs_s_get_path (const vfs_path_t
* vpath
, struct vfs_s_super
**archive
, int flags
)
403 return g_strdup (vfs_s_get_path_mangle (vpath
, archive
, flags
));
406 /* --------------------------------------------------------------------------------------------- */
407 /* Support of archives */
408 /* ------------------------ readdir & friends ----------------------------- */
410 static struct vfs_s_inode
*
411 vfs_s_inode_from_path (const vfs_path_t
* vpath
, int flags
)
413 struct vfs_s_super
*super
;
414 struct vfs_s_inode
*ino
;
416 vfs_path_element_t
*path_element
;
418 q
= vfs_s_get_path (vpath
, &super
, 0);
422 path_element
= vfs_path_get_by_index (vpath
, -1);
425 vfs_s_find_inode (path_element
->class, super
, q
,
426 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
, flags
& ~FL_FOLLOW
);
428 /* We are asking about / directory of ftp server: assume it exists */
430 vfs_s_find_inode (path_element
->class, super
, q
,
431 flags
& FL_FOLLOW
? LINK_FOLLOW
:
432 LINK_NO_FOLLOW
, FL_DIR
| (flags
& ~FL_FOLLOW
));
437 /* --------------------------------------------------------------------------------------------- */
440 vfs_s_opendir (const vfs_path_t
* vpath
)
442 struct vfs_s_inode
*dir
;
443 struct dirhandle
*info
;
444 vfs_path_element_t
*path_element
;
446 path_element
= vfs_path_get_by_index (vpath
, -1);
448 dir
= vfs_s_inode_from_path (vpath
, FL_DIR
| FL_FOLLOW
);
451 if (!S_ISDIR (dir
->st
.st_mode
))
453 path_element
->class->verrno
= ENOTDIR
;
459 if (dir
->subdir
== NULL
) /* This can actually happen if we allow empty directories */
461 path_element
->class->verrno
= EAGAIN
;
465 info
= g_new (struct dirhandle
, 1);
466 info
->cur
= dir
->subdir
;
472 /* --------------------------------------------------------------------------------------------- */
475 vfs_s_readdir (void *data
)
477 static union vfs_dirent dir
;
478 struct dirhandle
*info
= (struct dirhandle
*) data
;
481 if (info
->cur
== NULL
|| info
->cur
->data
== NULL
)
484 name
= ((struct vfs_s_entry
*) info
->cur
->data
)->name
;
486 g_strlcpy (dir
.dent
.d_name
, name
, MC_MAXPATHLEN
);
488 vfs_die ("Null in structure-cannot happen");
490 compute_namelen (&dir
.dent
);
491 info
->cur
= g_list_next (info
->cur
);
493 return (void *) &dir
;
496 /* --------------------------------------------------------------------------------------------- */
499 vfs_s_closedir (void *data
)
501 struct dirhandle
*info
= (struct dirhandle
*) data
;
502 struct vfs_s_inode
*dir
= info
->dir
;
504 vfs_s_free_inode (dir
->super
->me
, dir
);
509 /* --------------------------------------------------------------------------------------------- */
512 vfs_s_chdir (const vfs_path_t
* vpath
)
516 data
= vfs_s_opendir (vpath
);
519 vfs_s_closedir (data
);
523 /* --------------------------------------------------------------------------------------------- */
524 /* --------------------------- stat and friends ---------------------------- */
527 vfs_s_internal_stat (const vfs_path_t
* vpath
, struct stat
*buf
, int flag
)
529 struct vfs_s_inode
*ino
;
531 ino
= vfs_s_inode_from_path (vpath
, flag
);
538 /* --------------------------------------------------------------------------------------------- */
541 vfs_s_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
543 return vfs_s_internal_stat (vpath
, buf
, FL_FOLLOW
);
546 /* --------------------------------------------------------------------------------------------- */
549 vfs_s_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
551 return vfs_s_internal_stat (vpath
, buf
, FL_NONE
);
554 /* --------------------------------------------------------------------------------------------- */
557 vfs_s_fstat (void *fh
, struct stat
*buf
)
563 /* --------------------------------------------------------------------------------------------- */
566 vfs_s_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
568 struct vfs_s_inode
*ino
;
570 vfs_path_element_t
*path_element
;
572 path_element
= vfs_path_get_by_index (vpath
, -1);
574 ino
= vfs_s_inode_from_path (vpath
, 0);
578 if (!S_ISLNK (ino
->st
.st_mode
))
580 path_element
->class->verrno
= EINVAL
;
584 if (ino
->linkname
== NULL
)
586 path_element
->class->verrno
= EFAULT
;
590 len
= strlen (ino
->linkname
);
593 /* readlink() does not append a NUL character to buf */
594 memcpy (buf
, ino
->linkname
, len
);
598 /* --------------------------------------------------------------------------------------------- */
601 vfs_s_read (void *fh
, char *buffer
, size_t count
)
604 struct vfs_class
*me
= FH_SUPER
->me
;
606 if (FH
->linear
== LS_LINEAR_PREOPEN
)
608 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
612 if (FH
->linear
== LS_LINEAR_CLOSED
)
613 vfs_die ("linear_start() did not set linear_state!");
615 if (FH
->linear
== LS_LINEAR_OPEN
)
616 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
618 if (FH
->handle
!= -1)
620 n
= read (FH
->handle
, buffer
, count
);
625 vfs_die ("vfs_s_read: This should not happen\n");
629 /* --------------------------------------------------------------------------------------------- */
632 vfs_s_write (void *fh
, const char *buffer
, size_t count
)
635 struct vfs_class
*me
= FH_SUPER
->me
;
638 vfs_die ("no writing to linear files, please");
641 if (FH
->handle
!= -1)
643 n
= write (FH
->handle
, buffer
, count
);
648 vfs_die ("vfs_s_write: This should not happen\n");
652 /* --------------------------------------------------------------------------------------------- */
655 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
657 off_t size
= FH
->ino
->st
.st_size
;
659 if (FH
->linear
== LS_LINEAR_OPEN
)
660 vfs_die ("cannot lseek() after linear_read!");
662 if (FH
->handle
!= -1)
663 { /* If we have local file opened, we want to work with it */
664 off_t retval
= lseek (FH
->handle
, offset
, whence
);
666 FH
->ino
->super
->me
->verrno
= errno
;
681 else if (offset
< size
)
688 /* --------------------------------------------------------------------------------------------- */
691 vfs_s_close (void *fh
)
694 struct vfs_class
*me
= FH_SUPER
->me
;
696 FH_SUPER
->fd_usage
--;
697 if (!FH_SUPER
->fd_usage
)
698 vfs_stamp_create (me
, FH_SUPER
);
700 if (FH
->linear
== LS_LINEAR_OPEN
)
701 MEDATA
->linear_close (me
, fh
);
702 if (MEDATA
->fh_close
)
703 res
= MEDATA
->fh_close (me
, fh
);
704 if (FH
->changed
&& MEDATA
->file_store
)
706 char *s
= vfs_s_fullpath (me
, FH
->ino
);
711 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
714 vfs_s_invalidate (me
, FH_SUPER
);
716 if (FH
->handle
!= -1)
719 vfs_s_free_inode (me
, FH
->ino
);
724 /* --------------------------------------------------------------------------------------------- */
727 vfs_s_print_stats (const char *fs_name
, const char *action
,
728 const char *file_name
, off_t have
, off_t need
)
730 static const char *i18n_percent_transf_format
= NULL
;
731 static const char *i18n_transf_format
= NULL
;
733 if (i18n_percent_transf_format
== NULL
)
735 i18n_percent_transf_format
= "%s: %s: %s %3d%% (%" PRIuMAX
" %s";
736 i18n_transf_format
= "%s: %s: %s %" PRIuMAX
" %s";
740 vfs_print_message (i18n_percent_transf_format
, fs_name
, action
,
741 file_name
, (int) ((double) have
* 100 / need
), (uintmax_t) have
,
742 _("bytes transferred"));
744 vfs_print_message (i18n_transf_format
, fs_name
, action
, file_name
, (uintmax_t) have
,
745 _("bytes transferred"));
748 /* --------------------------------------------------------------------------------------------- */
749 /* ------------------------------- mc support ---------------------------- */
752 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
756 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
758 const struct vfs_s_super
*super
= (const struct vfs_s_super
*) iter
->data
;
761 name
= g_strconcat (super
->name
, "/", me
->prefix
, VFS_PATH_URL_DELIMITER
,
762 /* super->current_dir->name, */ (char *) NULL
);
768 /* --------------------------------------------------------------------------------------------- */
771 vfs_s_ferrno (struct vfs_class
*me
)
776 /* --------------------------------------------------------------------------------------------- */
778 * Get local copy of the given file. We reuse the existing file cache
779 * for remote filesystems. Archives use standard VFS facilities.
783 vfs_s_getlocalcopy (const vfs_path_t
* vpath
)
785 vfs_file_handler_t
*fh
;
788 fh
= vfs_s_open (vpath
, O_RDONLY
, 0);
792 if ((fh
->ino
!= NULL
) && (fh
->ino
->localname
!= NULL
))
793 local
= g_strdup (fh
->ino
->localname
);
801 /* --------------------------------------------------------------------------------------------- */
803 * Return the local copy. Since we are using our cache, we do nothing -
804 * the cache will be removed when the archive is closed.
808 vfs_s_ungetlocalcopy (const vfs_path_t
* vpath
, const char *local
, int has_changed
)
816 /* --------------------------------------------------------------------------------------------- */
819 vfs_s_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
821 vfs_path_element_t
*path_element
;
823 path_element
= vfs_path_get_by_index (vpath
, -1);
826 case VFS_SETCTL_STALE_DATA
:
828 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (vpath
, 0);
833 ino
->super
->want_stale
= 1;
836 ino
->super
->want_stale
= 0;
837 vfs_s_invalidate (path_element
->class, ino
->super
);
841 case VFS_SETCTL_LOGFILE
:
842 ((struct vfs_s_subclass
*) path_element
->class->data
)->logfile
= fopen ((char *) arg
, "w");
844 case VFS_SETCTL_FLUSH
:
845 ((struct vfs_s_subclass
*) path_element
->class->data
)->flush
= 1;
851 /* --------------------------------------------------------------------------------------------- */
852 /* ----------------------------- Stamping support -------------------------- */
855 vfs_s_getid (const vfs_path_t
* vpath
)
857 struct vfs_s_super
*archive
= NULL
;
860 p
= vfs_s_get_path (vpath
, &archive
, FL_NO_OPEN
);
864 return (vfsid
) archive
;
867 /* --------------------------------------------------------------------------------------------- */
870 vfs_s_nothingisopen (vfsid id
)
873 /* Our data structures should survive free of superblock at any time */
877 /* --------------------------------------------------------------------------------------------- */
880 vfs_s_free (vfsid id
)
882 vfs_s_free_super (((struct vfs_s_super
*) id
)->me
, (struct vfs_s_super
*) id
);
885 /* --------------------------------------------------------------------------------------------- */
888 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
898 gettimeofday (&tim
, NULL
);
899 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
905 /* --------------------------------------------------------------------------------------------- */
906 /*** public functions ****************************************************************************/
907 /* --------------------------------------------------------------------------------------------- */
910 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
912 struct vfs_s_inode
*ino
;
914 ino
= g_try_new0 (struct vfs_s_inode
, 1);
921 ino
->st
.st_nlink
= 0;
922 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
923 ino
->st
.st_dev
= MEDATA
->rdev
;
928 CALL (init_inode
) (me
, ino
);
933 /* --------------------------------------------------------------------------------------------- */
936 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
938 struct vfs_s_entry
*entry
;
940 entry
= g_new0 (struct vfs_s_entry
, 1);
943 entry
->name
= g_strdup (name
);
945 entry
->ino
->ent
= entry
;
946 CALL (init_entry
) (me
, entry
);
952 /* --------------------------------------------------------------------------------------------- */
955 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
957 if (ent
->dir
!= NULL
)
958 ent
->dir
->subdir
= g_list_remove (ent
->dir
->subdir
, ent
);
961 /* ent->name = NULL; */
963 if (ent
->ino
!= NULL
)
965 ent
->ino
->ent
= NULL
;
966 vfs_s_free_inode (me
, ent
->ino
);
973 /* --------------------------------------------------------------------------------------------- */
976 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
982 ent
->ino
->st
.st_nlink
++;
983 dir
->subdir
= g_list_append (dir
->subdir
, ent
);
986 /* --------------------------------------------------------------------------------------------- */
989 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
991 static struct stat st
;
996 myumask
= umask (022);
1004 st
.st_uid
= getuid ();
1005 st
.st_gid
= getgid ();
1007 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
1012 /* --------------------------------------------------------------------------------------------- */
1014 struct vfs_s_entry
*
1015 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
,
1018 struct vfs_s_inode
*inode
;
1021 st
= vfs_s_default_stat (me
, mode
);
1022 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
1024 return vfs_s_new_entry (me
, name
, inode
);
1027 /* --------------------------------------------------------------------------------------------- */
1029 struct vfs_s_inode
*
1030 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
1031 const char *path
, int follow
, int flags
)
1033 struct vfs_s_entry
*ent
;
1035 if (((MEDATA
->flags
& VFS_S_REMOTE
) == 0) && (*path
== '\0'))
1038 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
1039 return (ent
!= NULL
) ? ent
->ino
: NULL
;
1042 /* --------------------------------------------------------------------------------------------- */
1043 /* Ook, these were functions around directory entries / inodes */
1044 /* -------------------------------- superblock games -------------------------- */
1047 * Dissect the path and create corresponding superblock. Note that inname
1048 * can be changed and the result may point inside the original string.
1051 vfs_s_get_path_mangle (const vfs_path_t
* vpath
, struct vfs_s_super
**archive
, int flags
)
1056 struct vfs_s_super
*super
;
1057 void *cookie
= NULL
;
1058 vfs_path_element_t
*path_element
;
1059 vfs_path_t
*vpath_archive
;
1060 struct vfs_s_subclass
*subclass
;
1062 path_element
= vfs_path_get_by_index (vpath
, -1);
1063 subclass
= ((struct vfs_s_subclass
*) path_element
->class->data
);
1065 vpath_archive
= vfs_path_clone (vpath
);
1066 vfs_path_remove_element_by_index (vpath_archive
, -1);
1068 retval
= (path_element
->path
!= NULL
) ? path_element
->path
: "";
1070 if (subclass
->archive_check
!= NULL
)
1072 cookie
= subclass
->archive_check (vpath_archive
);
1075 vfs_path_free (vpath_archive
);
1080 for (iter
= subclass
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
1084 super
= (struct vfs_s_super
*) iter
->data
;
1086 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1087 i
= subclass
->archive_same (path_element
, super
, vpath_archive
, cookie
);
1091 goto return_success
;
1096 if (flags
& FL_NO_OPEN
)
1098 path_element
->class->verrno
= EIO
;
1099 vfs_path_free (vpath_archive
);
1103 super
= vfs_s_new_super (path_element
->class);
1104 if (subclass
->open_archive
!= NULL
)
1105 result
= subclass
->open_archive (super
, vpath_archive
, path_element
);
1108 vfs_s_free_super (path_element
->class, super
);
1109 path_element
->class->verrno
= EIO
;
1110 vfs_path_free (vpath_archive
);
1114 vfs_die ("You have to fill name\n");
1116 vfs_die ("You have to fill root inode\n");
1118 vfs_s_insert_super (path_element
->class, super
);
1119 vfs_stamp_create (path_element
->class, super
);
1123 vfs_path_free (vpath_archive
);
1127 /* --------------------------------------------------------------------------------------------- */
1130 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
1132 if (!super
->want_stale
)
1134 vfs_s_free_inode (me
, super
->root
);
1135 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
1139 /* --------------------------------------------------------------------------------------------- */
1142 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1145 ERRNOR (EAGAIN
, NULL
);
1147 if (!(MEDATA
->flags
& VFS_S_REMOTE
))
1151 char *path
= g_strdup (ino
->ent
->name
);
1154 ino
= ino
->ent
->dir
;
1155 if (ino
== ino
->super
->root
)
1157 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
1164 /* remote systems */
1165 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
1166 return g_strdup (ino
->ent
->name
);
1168 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
, ino
->ent
->name
, (char *) NULL
);
1171 /* --------------------------------------------------------------------------------------------- */
1172 /* --------------------------- stat and friends ---------------------------- */
1175 vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
1177 int was_changed
= 0;
1178 vfs_file_handler_t
*fh
;
1179 struct vfs_s_super
*super
;
1181 struct vfs_s_inode
*ino
;
1182 vfs_path_element_t
*path_element
;
1184 path_element
= vfs_path_get_by_index (vpath
, -1);
1186 q
= vfs_s_get_path (vpath
, &super
, 0);
1189 ino
= vfs_s_find_inode (path_element
->class, super
, q
, LINK_FOLLOW
, FL_NONE
);
1190 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)))
1193 path_element
->class->verrno
= EEXIST
;
1198 char *dirname
, *name
, *save
;
1199 struct vfs_s_entry
*ent
;
1200 struct vfs_s_inode
*dir
;
1203 /* If the filesystem is read-only, disable file creation */
1204 if (!(flags
& O_CREAT
) || !(path_element
->class->write
))
1210 split_dir_name (path_element
->class, q
, &dirname
, &name
, &save
);
1211 dir
= vfs_s_find_inode (path_element
->class, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
1219 ent
= vfs_s_generate_entry (path_element
->class, name
, dir
, 0755);
1221 vfs_s_insert_entry (path_element
->class, dir
, ent
);
1222 tmp_handle
= vfs_mkstemps (&ino
->localname
, path_element
->class->name
, name
);
1223 if (tmp_handle
== -1)
1234 if (S_ISDIR (ino
->st
.st_mode
))
1236 path_element
->class->verrno
= EISDIR
;
1240 fh
= g_new (vfs_file_handler_t
, 1);
1244 fh
->changed
= was_changed
;
1248 if (IS_LINEAR (flags
))
1250 if (VFSDATA (path_element
)->linear_start
)
1252 vfs_print_message (_("Starting linear transfer..."));
1253 fh
->linear
= LS_LINEAR_PREOPEN
;
1256 else if ((VFSDATA (path_element
)->fh_open
!= NULL
)
1257 && (VFSDATA (path_element
)->fh_open (path_element
->class, fh
, flags
, mode
) != 0))
1263 if (fh
->ino
->localname
)
1265 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
1266 if (fh
->handle
== -1)
1269 path_element
->class->verrno
= errno
;
1274 /* i.e. we had no open files and now we have one */
1275 vfs_rmstamp (path_element
->class, (vfsid
) super
);
1277 fh
->ino
->st
.st_nlink
++;
1281 /* --------------------------------------------------------------------------------------------- */
1284 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1286 /* If you want reget, you'll have to open file with O_LINEAR */
1290 off_t stat_size
= ino
->st
.st_size
;
1291 vfs_file_handler_t fh
;
1293 memset (&fh
, 0, sizeof (fh
));
1298 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
1305 if (!MEDATA
->linear_start (me
, &fh
, 0))
1308 /* Clear the interrupt status */
1309 tty_got_interrupt ();
1310 tty_enable_interrupt_key ();
1312 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
))))
1319 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
, total
, stat_size
);
1321 if (tty_got_interrupt ())
1324 t
= write (handle
, buffer
, n
);
1332 MEDATA
->linear_close (me
, &fh
);
1335 tty_disable_interrupt_key ();
1340 MEDATA
->linear_close (me
, &fh
);
1342 tty_disable_interrupt_key ();
1344 unlink (ino
->localname
);
1346 g_free (ino
->localname
);
1347 ino
->localname
= NULL
;
1352 /* --------------------------------------------------------------------------------------------- */
1353 /* ----------------------------- Stamping support -------------------------- */
1355 /* Initialize one of our subclasses - fill common functions */
1357 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1360 vclass
->fill_names
= vfs_s_fill_names
;
1361 vclass
->open
= vfs_s_open
;
1362 vclass
->close
= vfs_s_close
;
1363 vclass
->read
= vfs_s_read
;
1364 if (!(sub
->flags
& VFS_S_READONLY
))
1366 vclass
->write
= vfs_s_write
;
1368 vclass
->opendir
= vfs_s_opendir
;
1369 vclass
->readdir
= vfs_s_readdir
;
1370 vclass
->closedir
= vfs_s_closedir
;
1371 vclass
->stat
= vfs_s_stat
;
1372 vclass
->lstat
= vfs_s_lstat
;
1373 vclass
->fstat
= vfs_s_fstat
;
1374 vclass
->readlink
= vfs_s_readlink
;
1375 vclass
->chdir
= vfs_s_chdir
;
1376 vclass
->ferrno
= vfs_s_ferrno
;
1377 vclass
->lseek
= vfs_s_lseek
;
1378 vclass
->getid
= vfs_s_getid
;
1379 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1380 vclass
->free
= vfs_s_free
;
1381 if (sub
->flags
& VFS_S_REMOTE
)
1383 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1384 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1385 sub
->find_entry
= vfs_s_find_entry_linear
;
1389 sub
->find_entry
= vfs_s_find_entry_tree
;
1391 vclass
->setctl
= vfs_s_setctl
;
1392 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1395 /* --------------------------------------------------------------------------------------------- */
1396 /** Find VFS id for given directory name */
1399 vfs_getid (const vfs_path_t
* vpath
)
1401 vfs_path_element_t
*path_element
;
1403 path_element
= vfs_path_get_by_index (vpath
, -1);
1404 if (path_element
== NULL
|| path_element
->class->getid
== NULL
)
1407 return (*path_element
->class->getid
) (vpath
);
1410 /* --------------------------------------------------------------------------------------------- */
1411 /* ----------- Utility functions for networked filesystems -------------- */
1413 #ifdef ENABLE_VFS_NET
1415 vfs_s_select_on_two (int fd1
, int fd2
)
1418 struct timeval time_out
;
1420 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1422 time_out
.tv_sec
= 1;
1423 time_out
.tv_usec
= 0;
1427 v
= select (maxfd
, &set
, 0, 0, &time_out
);
1430 if (FD_ISSET (fd1
, &set
))
1432 if (FD_ISSET (fd2
, &set
))
1437 /* --------------------------------------------------------------------------------------------- */
1440 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1442 FILE *logfile
= MEDATA
->logfile
;
1446 for (i
= 0; i
< buf_len
- 1; i
++, buf
++)
1448 if (read (sock
, buf
, sizeof (char)) <= 0)
1454 ret1
= fwrite (buf
, 1, 1, logfile
);
1455 ret2
= fflush (logfile
);
1464 /* Line is too long - terminate buffer and discard the rest of line */
1466 while (read (sock
, &c
, sizeof (c
)) > 0)
1472 ret1
= fwrite (&c
, 1, 1, logfile
);
1473 ret2
= fflush (logfile
);
1481 /* --------------------------------------------------------------------------------------------- */
1484 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1491 tty_enable_interrupt_key ();
1492 for (i
= 0; i
< size
- 1; i
++)
1494 n
= read (fd
, buffer
+ i
, 1);
1495 tty_disable_interrupt_key ();
1496 if (n
== -1 && errno
== EINTR
)
1506 if (buffer
[i
] == '\n')
1512 buffer
[size
- 1] = 0;
1515 #endif /* ENABLE_VFS_NET */
1517 /* --------------------------------------------------------------------------------------------- */