2 Directory cache support
4 Copyright (C) 1998-2017
5 Free Software Foundation, Inc.
8 Pavel Machek <pavel@ucw.cz>, 1998
9 Slava Zanko <slavazanko@gmail.com>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 \warning Paths here do _not_ begin with '/', so root directory of
27 archive/site is simply "".
31 * \brief Source: directory cache support
33 * So that you do not have copy of this in each and every filesystem.
35 * Very loosely based on tar.c from midnight and archives.[ch] from
36 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
38 * Unfortunately, I was unable to keep all filesystems
39 * uniform. tar-like filesystems use tree structure where each
40 * directory has pointers to its subdirectories. We can do this
41 * because we have full information about our archive.
43 * At ftp-like filesystems, situation is a little bit different. When
44 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
45 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
46 * listed. That means that we do not have complete information, and if
47 * /usr is symlink to /4, we will not know. Also we have to time out
48 * entries and things would get messy with tree-like approach. So we
49 * do different trick: root directory is completely special and
50 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
51 * and we'll try to use custom find_entry function.
53 * \author Pavel Machek <pavel@ucw.cz>
62 #include <sys/time.h> /* gettimeofday() */
63 #include <inttypes.h> /* uintmax_t */
66 #include "lib/global.h"
68 #include "lib/tty/tty.h" /* enable/disable interrupt key */
69 #include "lib/util.h" /* custom_canonicalize_pathname() */
71 #include "lib/widget.h" /* message() */
76 #include "xdirentry.h"
77 #include "gc.h" /* vfs_rmstamp */
79 /*** global variables ****************************************************************************/
81 /*** file scope macro definitions ****************************************************************/
83 #define CALL(x) if (MEDATA->x) MEDATA->x
85 /*** file scope type declarations ****************************************************************/
90 struct vfs_s_inode
*dir
;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
96 /* --------------------------------------------------------------------------------------------- */
99 vfs_s_entry_compare (const void *a
, const void *b
)
101 const struct vfs_s_entry
*e
= (const struct vfs_s_entry
*) a
;
102 const char *name
= (const char *) b
;
104 return strcmp (e
->name
, name
);
107 /* --------------------------------------------------------------------------------------------- */
109 /* We were asked to create entries automagically */
111 static struct vfs_s_entry
*
112 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
114 struct vfs_s_entry
*res
;
117 sep
= strchr (path
, PATH_SEP
);
121 res
= vfs_s_generate_entry (me
, path
, dir
, (flags
& FL_MKDIR
) != 0 ? (0777 | S_IFDIR
) : 0777);
122 vfs_s_insert_entry (me
, dir
, res
);
130 /* --------------------------------------------------------------------------------------------- */
131 /* If the entry is a symlink, find the entry for its target */
133 static struct vfs_s_entry
*
134 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
, int follow
)
137 char *fullname
= NULL
;
138 struct vfs_s_entry
*target
;
140 if (follow
== LINK_NO_FOLLOW
)
143 ERRNOR (ELOOP
, NULL
);
145 ERRNOR (ENOENT
, NULL
);
146 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
149 linkname
= entry
->ino
->linkname
;
150 if (linkname
== NULL
)
151 ERRNOR (EFAULT
, NULL
);
153 /* make full path from relative */
154 if (!IS_PATH_SEP (*linkname
))
158 fullpath
= vfs_s_fullpath (me
, entry
->dir
);
159 if (fullpath
!= NULL
)
161 fullname
= g_strconcat (fullpath
, PATH_SEP_STR
, linkname
, (char *) NULL
);
167 target
= (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
, follow
- 1, 0);
172 /* --------------------------------------------------------------------------------------------- */
174 * Follow > 0: follow links, serves as loop protect,
175 * == -1: do not follow links
178 static struct vfs_s_entry
*
179 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
180 const char *a_path
, int follow
, int flags
)
183 struct vfs_s_entry
*ent
= NULL
;
184 char *const pathref
= g_strdup (a_path
);
185 char *path
= pathref
;
187 /* canonicalize as well, but don't remove '../' from path */
188 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
194 while (IS_PATH_SEP (*path
)) /* Strip leading '/' */
203 for (pseg
= 0; path
[pseg
] != '\0' && !IS_PATH_SEP (path
[pseg
]); pseg
++)
206 for (iter
= root
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
208 ent
= (struct vfs_s_entry
*) iter
->data
;
209 if (strlen (ent
->name
) == pseg
&& strncmp (ent
->name
, path
, pseg
) == 0)
214 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
216 if (ent
== NULL
&& (flags
& (FL_MKFILE
| FL_MKDIR
)) != 0)
217 ent
= vfs_s_automake (me
, root
, path
, flags
);
225 /* here we must follow leading directories always;
226 only the actual file is optional */
227 ent
= vfs_s_resolve_symlink (me
, ent
,
228 strchr (path
, PATH_SEP
) != NULL
? LINK_FOLLOW
: follow
);
238 /* --------------------------------------------------------------------------------------------- */
240 static struct vfs_s_entry
*
241 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
242 const char *a_path
, int follow
, int flags
)
244 struct vfs_s_entry
*ent
= NULL
;
245 char *const path
= g_strdup (a_path
);
248 if (root
->super
->root
!= root
)
249 vfs_die ("We have to use _real_ root. Always. Sorry.");
251 /* canonicalize as well, but don't remove '../' from path */
252 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
254 if ((flags
& FL_DIR
) == 0)
256 char *dirname
, *name
;
257 struct vfs_s_inode
*ino
;
259 dirname
= g_path_get_dirname (path
);
260 name
= g_path_get_basename (path
);
261 ino
= vfs_s_find_inode (me
, root
->super
, dirname
, follow
, flags
| FL_DIR
);
262 ent
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
269 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
270 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
272 if (ent
!= NULL
&& !MEDATA
->dir_uptodate (me
, ent
->ino
))
275 vfs_print_message (_("Directory cache expired for %s"), path
);
277 vfs_s_free_entry (me
, ent
);
283 struct vfs_s_inode
*ino
;
285 ino
= vfs_s_new_inode (me
, root
->super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
286 ent
= vfs_s_new_entry (me
, path
, ino
);
287 if (MEDATA
->dir_load (me
, ino
, path
) == -1)
289 vfs_s_free_entry (me
, ent
);
294 vfs_s_insert_entry (me
, root
, ent
);
296 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
297 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
300 vfs_die ("find_linear: success but directory is not there\n");
303 if (!vfs_s_resolve_symlink (me
, ent
, follow
))
313 /* --------------------------------------------------------------------------------------------- */
314 /* Ook, these were functions around directory entries / inodes */
315 /* -------------------------------- superblock games -------------------------- */
317 static struct vfs_s_super
*
318 vfs_s_new_super (struct vfs_class
*me
)
320 struct vfs_s_super
*super
;
322 super
= g_new0 (struct vfs_s_super
, 1);
327 /* --------------------------------------------------------------------------------------------- */
330 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
332 MEDATA
->supers
= g_list_prepend (MEDATA
->supers
, super
);
335 /* --------------------------------------------------------------------------------------------- */
338 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
340 if (super
->root
!= NULL
)
342 vfs_s_free_inode (me
, super
->root
);
347 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
348 if (super
->ino_usage
)
349 message (D_ERROR
, "Direntry warning",
350 "Super ino_usage is %d, memory leak", super
->ino_usage
);
352 if (super
->want_stale
)
353 message (D_ERROR
, "Direntry warning", "%s", "Super has want_stale set");
356 MEDATA
->supers
= g_list_remove (MEDATA
->supers
, super
);
358 CALL (free_archive
) (me
, super
);
359 #ifdef ENABLE_VFS_NET
360 vfs_path_element_free (super
->path_element
);
362 g_free (super
->name
);
366 /* --------------------------------------------------------------------------------------------- */
367 /* Support of archives */
368 /* ------------------------ readdir & friends ----------------------------- */
370 static struct vfs_s_inode
*
371 vfs_s_inode_from_path (const vfs_path_t
* vpath
, int flags
)
373 struct vfs_s_super
*super
;
374 struct vfs_s_inode
*ino
;
376 const vfs_path_element_t
*path_element
;
378 q
= vfs_s_get_path (vpath
, &super
, 0);
382 path_element
= vfs_path_get_by_index (vpath
, -1);
385 vfs_s_find_inode (path_element
->class, super
, q
,
386 (flags
& FL_FOLLOW
) != 0 ? LINK_FOLLOW
: LINK_NO_FOLLOW
,
389 /* We are asking about / directory of ftp server: assume it exists */
391 vfs_s_find_inode (path_element
->class, super
, q
,
392 (flags
& FL_FOLLOW
) != 0 ? LINK_FOLLOW
: LINK_NO_FOLLOW
,
393 FL_DIR
| (flags
& ~FL_FOLLOW
));
397 /* --------------------------------------------------------------------------------------------- */
400 vfs_s_opendir (const vfs_path_t
* vpath
)
402 struct vfs_s_inode
*dir
;
403 struct dirhandle
*info
;
404 const vfs_path_element_t
*path_element
;
406 path_element
= vfs_path_get_by_index (vpath
, -1);
408 dir
= vfs_s_inode_from_path (vpath
, FL_DIR
| FL_FOLLOW
);
411 if (!S_ISDIR (dir
->st
.st_mode
))
413 path_element
->class->verrno
= ENOTDIR
;
419 if (dir
->subdir
== NULL
) /* This can actually happen if we allow empty directories */
421 path_element
->class->verrno
= EAGAIN
;
425 info
= g_new (struct dirhandle
, 1);
426 info
->cur
= dir
->subdir
;
432 /* --------------------------------------------------------------------------------------------- */
435 vfs_s_readdir (void *data
)
437 static union vfs_dirent dir
;
438 struct dirhandle
*info
= (struct dirhandle
*) data
;
441 if (info
->cur
== NULL
|| info
->cur
->data
== NULL
)
444 name
= ((struct vfs_s_entry
*) info
->cur
->data
)->name
;
446 g_strlcpy (dir
.dent
.d_name
, name
, MC_MAXPATHLEN
);
448 vfs_die ("Null in structure-cannot happen");
450 info
->cur
= g_list_next (info
->cur
);
452 return (void *) &dir
;
455 /* --------------------------------------------------------------------------------------------- */
458 vfs_s_closedir (void *data
)
460 struct dirhandle
*info
= (struct dirhandle
*) data
;
461 struct vfs_s_inode
*dir
= info
->dir
;
463 vfs_s_free_inode (dir
->super
->me
, dir
);
468 /* --------------------------------------------------------------------------------------------- */
471 vfs_s_chdir (const vfs_path_t
* vpath
)
475 data
= vfs_s_opendir (vpath
);
478 vfs_s_closedir (data
);
482 /* --------------------------------------------------------------------------------------------- */
483 /* --------------------------- stat and friends ---------------------------- */
486 vfs_s_internal_stat (const vfs_path_t
* vpath
, struct stat
*buf
, int flag
)
488 struct vfs_s_inode
*ino
;
490 ino
= vfs_s_inode_from_path (vpath
, flag
);
497 /* --------------------------------------------------------------------------------------------- */
500 vfs_s_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
502 struct vfs_s_inode
*ino
;
504 const vfs_path_element_t
*path_element
;
506 path_element
= vfs_path_get_by_index (vpath
, -1);
508 ino
= vfs_s_inode_from_path (vpath
, 0);
512 if (!S_ISLNK (ino
->st
.st_mode
))
514 path_element
->class->verrno
= EINVAL
;
518 if (ino
->linkname
== NULL
)
520 path_element
->class->verrno
= EFAULT
;
524 len
= strlen (ino
->linkname
);
527 /* readlink() does not append a NUL character to buf */
528 memcpy (buf
, ino
->linkname
, len
);
532 /* --------------------------------------------------------------------------------------------- */
535 vfs_s_read (void *fh
, char *buffer
, size_t count
)
537 struct vfs_class
*me
= FH_SUPER
->me
;
539 if (FH
->linear
== LS_LINEAR_PREOPEN
)
541 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
545 if (FH
->linear
== LS_LINEAR_CLOSED
)
546 vfs_die ("linear_start() did not set linear_state!");
548 if (FH
->linear
== LS_LINEAR_OPEN
)
549 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
551 if (FH
->handle
!= -1)
555 n
= read (FH
->handle
, buffer
, count
);
560 vfs_die ("vfs_s_read: This should not happen\n");
564 /* --------------------------------------------------------------------------------------------- */
567 vfs_s_write (void *fh
, const char *buffer
, size_t count
)
569 struct vfs_class
*me
= FH_SUPER
->me
;
572 vfs_die ("no writing to linear files, please");
575 if (FH
->handle
!= -1)
579 n
= write (FH
->handle
, buffer
, count
);
584 vfs_die ("vfs_s_write: This should not happen\n");
588 /* --------------------------------------------------------------------------------------------- */
591 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
593 off_t size
= FH
->ino
->st
.st_size
;
595 if (FH
->linear
== LS_LINEAR_OPEN
)
596 vfs_die ("cannot lseek() after linear_read!");
598 if (FH
->handle
!= -1)
599 { /* If we have local file opened, we want to work with it */
600 off_t retval
= lseek (FH
->handle
, offset
, whence
);
602 FH
->ino
->super
->me
->verrno
= errno
;
619 else if (offset
< size
)
626 /* --------------------------------------------------------------------------------------------- */
629 vfs_s_close (void *fh
)
632 struct vfs_class
*me
= FH_SUPER
->me
;
637 FH_SUPER
->fd_usage
--;
638 if (!FH_SUPER
->fd_usage
)
639 vfs_stamp_create (me
, FH_SUPER
);
641 if (FH
->linear
== LS_LINEAR_OPEN
)
642 MEDATA
->linear_close (me
, fh
);
643 if (MEDATA
->fh_close
)
644 res
= MEDATA
->fh_close (me
, fh
);
645 if ((MEDATA
->flags
& VFS_S_USETMP
) && FH
->changed
&& MEDATA
->file_store
)
647 char *s
= vfs_s_fullpath (me
, FH
->ino
);
652 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
655 vfs_s_invalidate (me
, FH_SUPER
);
657 if (FH
->handle
!= -1)
660 vfs_s_free_inode (me
, FH
->ino
);
661 if (MEDATA
->fh_free_data
!= NULL
)
662 MEDATA
->fh_free_data (fh
);
667 /* --------------------------------------------------------------------------------------------- */
670 vfs_s_print_stats (const char *fs_name
, const char *action
,
671 const char *file_name
, off_t have
, off_t need
)
674 vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name
, action
,
675 file_name
, (int) ((double) have
* 100 / need
), (long long) have
);
677 vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name
, action
, file_name
,
681 /* --------------------------------------------------------------------------------------------- */
682 /* ------------------------------- mc support ---------------------------- */
685 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
689 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
691 const struct vfs_s_super
*super
= (const struct vfs_s_super
*) iter
->data
;
694 name
= g_strconcat (super
->name
, PATH_SEP_STR
, me
->prefix
, VFS_PATH_URL_DELIMITER
,
695 /* super->current_dir->name, */ (char *) NULL
);
701 /* --------------------------------------------------------------------------------------------- */
704 vfs_s_ferrno (struct vfs_class
*me
)
709 /* --------------------------------------------------------------------------------------------- */
711 * Get local copy of the given file. We reuse the existing file cache
712 * for remote filesystems. Archives use standard VFS facilities.
716 vfs_s_getlocalcopy (const vfs_path_t
* vpath
)
718 vfs_file_handler_t
*fh
;
719 vfs_path_t
*local
= NULL
;
724 fh
= vfs_s_open (vpath
, O_RDONLY
, 0);
728 const struct vfs_class
*me
;
730 me
= vfs_path_get_by_index (vpath
, -1)->class;
731 if ((MEDATA
->flags
& VFS_S_USETMP
) != 0 && (fh
->ino
!= NULL
))
732 local
= vfs_path_from_str_flags (fh
->ino
->localname
, VPF_NO_CANON
);
740 /* --------------------------------------------------------------------------------------------- */
742 * Return the local copy. Since we are using our cache, we do nothing -
743 * the cache will be removed when the archive is closed.
747 vfs_s_ungetlocalcopy (const vfs_path_t
* vpath
, const vfs_path_t
* local
, gboolean has_changed
)
755 /* --------------------------------------------------------------------------------------------- */
758 vfs_s_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
760 const vfs_path_element_t
*path_element
;
762 path_element
= vfs_path_get_by_index (vpath
, -1);
766 case VFS_SETCTL_STALE_DATA
:
768 struct vfs_s_inode
*ino
;
770 ino
= vfs_s_inode_from_path (vpath
, 0);
774 ino
->super
->want_stale
= 1;
777 ino
->super
->want_stale
= 0;
778 vfs_s_invalidate (path_element
->class, ino
->super
);
782 case VFS_SETCTL_LOGFILE
:
783 ((struct vfs_s_subclass
*) path_element
->class->data
)->logfile
= fopen ((char *) arg
, "w");
785 case VFS_SETCTL_FLUSH
:
786 ((struct vfs_s_subclass
*) path_element
->class->data
)->flush
= 1;
793 /* --------------------------------------------------------------------------------------------- */
794 /* ----------------------------- Stamping support -------------------------- */
797 vfs_s_getid (const vfs_path_t
* vpath
)
799 struct vfs_s_super
*archive
= NULL
;
802 p
= vfs_s_get_path (vpath
, &archive
, FL_NO_OPEN
);
806 return (vfsid
) archive
;
809 /* --------------------------------------------------------------------------------------------- */
812 vfs_s_nothingisopen (vfsid id
)
815 /* Our data structures should survive free of superblock at any time */
819 /* --------------------------------------------------------------------------------------------- */
822 vfs_s_free (vfsid id
)
824 vfs_s_free_super (((struct vfs_s_super
*) id
)->me
, (struct vfs_s_super
*) id
);
827 /* --------------------------------------------------------------------------------------------- */
830 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
840 gettimeofday (&tim
, NULL
);
841 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
847 /* --------------------------------------------------------------------------------------------- */
848 /*** public functions ****************************************************************************/
849 /* --------------------------------------------------------------------------------------------- */
852 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
854 struct vfs_s_inode
*ino
;
856 ino
= g_try_new0 (struct vfs_s_inode
, 1);
863 ino
->st
.st_nlink
= 0;
864 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
865 ino
->st
.st_dev
= MEDATA
->rdev
;
869 CALL (init_inode
) (me
, ino
);
874 /* --------------------------------------------------------------------------------------------- */
877 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
880 vfs_die ("Don't pass NULL to me");
882 /* ==0 can happen if freshly created entry is deleted */
883 if (ino
->st
.st_nlink
> 1)
889 while (ino
->subdir
!= NULL
)
890 vfs_s_free_entry (me
, (struct vfs_s_entry
*) ino
->subdir
->data
);
892 CALL (free_inode
) (me
, ino
);
893 g_free (ino
->linkname
);
894 if ((MEDATA
->flags
& VFS_S_USETMP
) != 0 && ino
->localname
!= NULL
)
896 unlink (ino
->localname
);
897 g_free (ino
->localname
);
899 ino
->super
->ino_usage
--;
903 /* --------------------------------------------------------------------------------------------- */
906 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
908 struct vfs_s_entry
*entry
;
910 entry
= g_new0 (struct vfs_s_entry
, 1);
912 entry
->name
= g_strdup (name
);
914 entry
->ino
->ent
= entry
;
915 CALL (init_entry
) (me
, entry
);
921 /* --------------------------------------------------------------------------------------------- */
924 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
926 if (ent
->dir
!= NULL
)
927 ent
->dir
->subdir
= g_list_remove (ent
->dir
->subdir
, ent
);
929 MC_PTR_FREE (ent
->name
);
931 if (ent
->ino
!= NULL
)
933 ent
->ino
->ent
= NULL
;
934 vfs_s_free_inode (me
, ent
->ino
);
940 /* --------------------------------------------------------------------------------------------- */
943 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
949 ent
->ino
->st
.st_nlink
++;
950 dir
->subdir
= g_list_append (dir
->subdir
, ent
);
953 /* --------------------------------------------------------------------------------------------- */
956 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
958 static struct stat st
;
963 myumask
= umask (022);
970 #ifdef HAVE_STRUCT_STAT_ST_RDEV
973 st
.st_uid
= getuid ();
974 st
.st_gid
= getgid ();
975 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
980 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
981 #ifdef HAVE_STRUCT_STAT_ST_MTIM
982 st
.st_atim
.tv_nsec
= st
.st_mtim
.tv_nsec
= st
.st_ctim
.tv_nsec
= 0;
985 vfs_adjust_stat (&st
);
990 /* --------------------------------------------------------------------------------------------- */
992 * Calculate number of st_blocks using st_size and st_blksize.
993 * In according to stat(2), st_blocks is the size in 512-byte units.
999 vfs_adjust_stat (struct stat
*s
)
1001 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1002 if (s
->st_size
== 0)
1006 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1008 blksize_t ioblock_size
;
1010 /* 1. Calculate how many IO blocks are occupied */
1011 ioblocks
= 1 + (s
->st_size
- 1) / s
->st_blksize
;
1012 /* 2. Calculate size of st_blksize in 512-byte units */
1013 ioblock_size
= 1 + (s
->st_blksize
- 1) / 512;
1014 /* 3. Calculate number of blocks */
1015 s
->st_blocks
= ioblocks
* ioblock_size
;
1017 /* Let IO block size is 512 bytes */
1018 s
->st_blocks
= 1 + (s
->st_size
- 1) / 512;
1019 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1021 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
1024 /* --------------------------------------------------------------------------------------------- */
1026 struct vfs_s_entry
*
1027 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
,
1030 struct vfs_s_inode
*inode
;
1033 st
= vfs_s_default_stat (me
, mode
);
1034 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
1036 return vfs_s_new_entry (me
, name
, inode
);
1039 /* --------------------------------------------------------------------------------------------- */
1041 struct vfs_s_inode
*
1042 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
1043 const char *path
, int follow
, int flags
)
1045 struct vfs_s_entry
*ent
;
1047 if (((MEDATA
->flags
& VFS_S_REMOTE
) == 0) && (*path
== '\0'))
1050 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
1051 return (ent
!= NULL
) ? ent
->ino
: NULL
;
1054 /* --------------------------------------------------------------------------------------------- */
1055 /* Ook, these were functions around directory entries / inodes */
1056 /* -------------------------------- superblock games -------------------------- */
1058 * get superlock object by vpath
1061 * @return superlock object or NULL if not found
1064 struct vfs_s_super
*
1065 vfs_get_super_by_vpath (const vfs_path_t
* vpath
)
1068 void *cookie
= NULL
;
1069 const vfs_path_element_t
*path_element
;
1070 struct vfs_s_subclass
*subclass
;
1071 struct vfs_s_super
*super
= NULL
;
1072 vfs_path_t
*vpath_archive
;
1074 path_element
= vfs_path_get_by_index (vpath
, -1);
1075 subclass
= ((struct vfs_s_subclass
*) path_element
->class->data
);
1076 if (subclass
== NULL
)
1079 vpath_archive
= vfs_path_clone (vpath
);
1080 vfs_path_remove_element_by_index (vpath_archive
, -1);
1082 if (subclass
->archive_check
!= NULL
)
1084 cookie
= subclass
->archive_check (vpath_archive
);
1089 for (iter
= subclass
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
1093 super
= (struct vfs_s_super
*) iter
->data
;
1095 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1096 i
= subclass
->archive_same (path_element
, super
, vpath_archive
, cookie
);
1106 vfs_path_free (vpath_archive
);
1110 /* --------------------------------------------------------------------------------------------- */
1112 * get path from last VFS-element and create corresponding superblock
1114 * @param vpath source path object
1115 * @param archive pointer to object for store newly created superblock
1116 * @param flags flags
1118 * @return path from last VFS-element
1121 vfs_s_get_path (const vfs_path_t
* vpath
, struct vfs_s_super
**archive
, int flags
)
1123 const char *retval
= "";
1125 struct vfs_s_super
*super
;
1126 const vfs_path_element_t
*path_element
;
1127 struct vfs_s_subclass
*subclass
;
1129 path_element
= vfs_path_get_by_index (vpath
, -1);
1131 if (path_element
->path
!= NULL
)
1132 retval
= path_element
->path
;
1134 super
= vfs_get_super_by_vpath (vpath
);
1136 goto return_success
;
1138 if (flags
& FL_NO_OPEN
)
1140 path_element
->class->verrno
= EIO
;
1144 super
= vfs_s_new_super (path_element
->class);
1146 subclass
= ((struct vfs_s_subclass
*) path_element
->class->data
);
1147 if (subclass
->open_archive
!= NULL
)
1149 vfs_path_t
*vpath_archive
;
1151 vpath_archive
= vfs_path_clone (vpath
);
1152 vfs_path_remove_element_by_index (vpath_archive
, -1);
1154 result
= subclass
->open_archive (super
, vpath_archive
, path_element
);
1155 vfs_path_free (vpath_archive
);
1159 vfs_s_free_super (path_element
->class, super
);
1160 path_element
->class->verrno
= EIO
;
1164 vfs_die ("You have to fill name\n");
1166 vfs_die ("You have to fill root inode\n");
1168 vfs_s_insert_super (path_element
->class, super
);
1169 vfs_stamp_create (path_element
->class, super
);
1176 /* --------------------------------------------------------------------------------------------- */
1179 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
1181 if (!super
->want_stale
)
1183 vfs_s_free_inode (me
, super
->root
);
1184 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
1188 /* --------------------------------------------------------------------------------------------- */
1191 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1194 ERRNOR (EAGAIN
, NULL
);
1196 if ((MEDATA
->flags
& VFS_S_USETMP
) == 0)
1200 char *path
= g_strdup (ino
->ent
->name
);
1203 ino
= ino
->ent
->dir
;
1204 if (ino
== ino
->super
->root
)
1206 newpath
= g_strconcat (ino
->ent
->name
, PATH_SEP_STR
, path
, (char *) NULL
);
1213 /* remote systems */
1214 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
1215 return g_strdup (ino
->ent
->name
);
1217 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
, ino
->ent
->name
, (char *) NULL
);
1220 /* --------------------------------------------------------------------------------------------- */
1221 /* --------------------------- stat and friends ---------------------------- */
1224 vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
1226 int was_changed
= 0;
1227 vfs_file_handler_t
*fh
;
1228 struct vfs_s_super
*super
;
1230 struct vfs_s_inode
*ino
;
1231 const vfs_path_element_t
*path_element
;
1233 path_element
= vfs_path_get_by_index (vpath
, -1);
1235 q
= vfs_s_get_path (vpath
, &super
, 0);
1238 ino
= vfs_s_find_inode (path_element
->class, super
, q
, LINK_FOLLOW
, FL_NONE
);
1239 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)))
1241 path_element
->class->verrno
= EEXIST
;
1246 char *dirname
, *name
;
1247 struct vfs_s_entry
*ent
;
1248 struct vfs_s_inode
*dir
;
1250 /* If the filesystem is read-only, disable file creation */
1251 if (!(flags
& O_CREAT
) || !(path_element
->class->write
))
1254 dirname
= g_path_get_dirname (q
);
1255 name
= g_path_get_basename (q
);
1256 dir
= vfs_s_find_inode (path_element
->class, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
1263 ent
= vfs_s_generate_entry (path_element
->class, name
, dir
, 0755);
1265 vfs_s_insert_entry (path_element
->class, dir
, ent
);
1266 if ((VFSDATA (path_element
)->flags
& VFS_S_USETMP
) != 0)
1269 vfs_path_t
*tmp_vpath
;
1271 tmp_handle
= vfs_mkstemps (&tmp_vpath
, path_element
->class->name
, name
);
1272 ino
->localname
= g_strdup (vfs_path_as_str (tmp_vpath
));
1273 vfs_path_free (tmp_vpath
);
1274 if (tmp_handle
== -1)
1288 if (S_ISDIR (ino
->st
.st_mode
))
1290 path_element
->class->verrno
= EISDIR
;
1294 fh
= g_new (vfs_file_handler_t
, 1);
1298 fh
->changed
= was_changed
;
1302 if (IS_LINEAR (flags
))
1304 if (VFSDATA (path_element
)->linear_start
)
1306 vfs_print_message ("%s", _("Starting linear transfer..."));
1307 fh
->linear
= LS_LINEAR_PREOPEN
;
1312 struct vfs_s_subclass
*s
;
1314 s
= VFSDATA (path_element
);
1315 if (s
->fh_open
!= NULL
&& s
->fh_open (path_element
->class, fh
, flags
, mode
) != 0)
1317 if (s
->fh_free_data
!= NULL
)
1318 s
->fh_free_data (fh
);
1324 if ((VFSDATA (path_element
)->flags
& VFS_S_USETMP
) != 0 && fh
->ino
->localname
!= NULL
)
1326 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
1327 if (fh
->handle
== -1)
1330 path_element
->class->verrno
= errno
;
1335 /* i.e. we had no open files and now we have one */
1336 vfs_rmstamp (path_element
->class, (vfsid
) super
);
1338 fh
->ino
->st
.st_nlink
++;
1342 /* --------------------------------------------------------------------------------------------- */
1345 vfs_s_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
1347 return vfs_s_internal_stat (vpath
, buf
, FL_FOLLOW
);
1350 /* --------------------------------------------------------------------------------------------- */
1353 vfs_s_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
1355 return vfs_s_internal_stat (vpath
, buf
, FL_NONE
);
1358 /* --------------------------------------------------------------------------------------------- */
1361 vfs_s_fstat (void *fh
, struct stat
*buf
)
1367 /* --------------------------------------------------------------------------------------------- */
1370 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1372 /* If you want reget, you'll have to open file with O_LINEAR */
1377 off_t stat_size
= ino
->st
.st_size
;
1378 vfs_file_handler_t fh
;
1379 vfs_path_t
*tmp_vpath
;
1381 if ((MEDATA
->flags
& VFS_S_USETMP
) == 0)
1384 memset (&fh
, 0, sizeof (fh
));
1389 handle
= vfs_mkstemps (&tmp_vpath
, me
->name
, ino
->ent
->name
);
1390 ino
->localname
= g_strdup (vfs_path_as_str (tmp_vpath
));
1391 vfs_path_free (tmp_vpath
);
1398 if (!MEDATA
->linear_start (me
, &fh
, 0))
1401 /* Clear the interrupt status */
1402 tty_got_interrupt ();
1403 tty_enable_interrupt_key ();
1405 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
))))
1412 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
, total
, stat_size
);
1414 if (tty_got_interrupt ())
1417 t
= write (handle
, buffer
, n
);
1425 MEDATA
->linear_close (me
, &fh
);
1428 tty_disable_interrupt_key ();
1433 MEDATA
->linear_close (me
, &fh
);
1435 tty_disable_interrupt_key ();
1437 unlink (ino
->localname
);
1439 MC_PTR_FREE (ino
->localname
);
1444 /* --------------------------------------------------------------------------------------------- */
1445 /* ----------------------------- Stamping support -------------------------- */
1447 /* Initialize one of our subclasses - fill common functions */
1449 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1452 vclass
->fill_names
= vfs_s_fill_names
;
1453 vclass
->open
= vfs_s_open
;
1454 vclass
->close
= vfs_s_close
;
1455 vclass
->read
= vfs_s_read
;
1456 if (!(sub
->flags
& VFS_S_READONLY
))
1458 vclass
->write
= vfs_s_write
;
1460 vclass
->opendir
= vfs_s_opendir
;
1461 vclass
->readdir
= vfs_s_readdir
;
1462 vclass
->closedir
= vfs_s_closedir
;
1463 vclass
->stat
= vfs_s_stat
;
1464 vclass
->lstat
= vfs_s_lstat
;
1465 vclass
->fstat
= vfs_s_fstat
;
1466 vclass
->readlink
= vfs_s_readlink
;
1467 vclass
->chdir
= vfs_s_chdir
;
1468 vclass
->ferrno
= vfs_s_ferrno
;
1469 vclass
->lseek
= vfs_s_lseek
;
1470 vclass
->getid
= vfs_s_getid
;
1471 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1472 vclass
->free
= vfs_s_free
;
1473 if ((sub
->flags
& VFS_S_USETMP
) != 0)
1475 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1476 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1477 sub
->find_entry
= vfs_s_find_entry_linear
;
1479 else if ((sub
->flags
& VFS_S_REMOTE
) != 0)
1480 sub
->find_entry
= vfs_s_find_entry_linear
;
1482 sub
->find_entry
= vfs_s_find_entry_tree
;
1483 vclass
->setctl
= vfs_s_setctl
;
1484 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1487 /* --------------------------------------------------------------------------------------------- */
1488 /** Find VFS id for given directory name */
1491 vfs_getid (const vfs_path_t
* vpath
)
1493 const vfs_path_element_t
*path_element
;
1495 path_element
= vfs_path_get_by_index (vpath
, -1);
1496 if (!vfs_path_element_valid (path_element
) || path_element
->class->getid
== NULL
)
1499 return (*path_element
->class->getid
) (vpath
);
1502 /* --------------------------------------------------------------------------------------------- */
1503 /* ----------- Utility functions for networked filesystems -------------- */
1505 #ifdef ENABLE_VFS_NET
1507 vfs_s_select_on_two (int fd1
, int fd2
)
1510 struct timeval time_out
;
1512 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1514 time_out
.tv_sec
= 1;
1515 time_out
.tv_usec
= 0;
1519 v
= select (maxfd
, &set
, 0, 0, &time_out
);
1522 if (FD_ISSET (fd1
, &set
))
1524 if (FD_ISSET (fd2
, &set
))
1529 /* --------------------------------------------------------------------------------------------- */
1532 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1534 FILE *logfile
= MEDATA
->logfile
;
1538 for (i
= 0; i
< buf_len
- 1; i
++, buf
++)
1540 if (read (sock
, buf
, sizeof (char)) <= 0)
1546 ret1
= fwrite (buf
, 1, 1, logfile
);
1547 ret2
= fflush (logfile
);
1558 /* Line is too long - terminate buffer and discard the rest of line */
1560 while (read (sock
, &c
, sizeof (c
)) > 0)
1566 ret1
= fwrite (&c
, 1, 1, logfile
);
1567 ret2
= fflush (logfile
);
1577 /* --------------------------------------------------------------------------------------------- */
1580 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1587 tty_enable_interrupt_key ();
1589 for (i
= 0; i
< size
- 1; i
++)
1593 n
= read (fd
, &buffer
[i
], 1);
1594 if (n
== -1 && errno
== EINTR
)
1605 if (buffer
[i
] == '\n')
1613 buffer
[size
- 1] = '\0';
1616 tty_disable_interrupt_key ();
1620 #endif /* ENABLE_VFS_NET */
1622 /* --------------------------------------------------------------------------------------------- */
1624 * Normalize filenames start position
1628 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode
*root_inode
, size_t final_num_spaces
)
1632 for (iter
= root_inode
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
1634 struct vfs_s_entry
*entry
= (struct vfs_s_entry
*) iter
->data
;
1635 if ((size_t) entry
->ino
->data_offset
> final_num_spaces
)
1637 char *source_name
= entry
->name
;
1638 char *spacer
= g_strnfill (entry
->ino
->data_offset
- final_num_spaces
, ' ');
1639 entry
->name
= g_strdup_printf ("%s%s", spacer
, source_name
);
1641 g_free (source_name
);
1643 entry
->ino
->data_offset
= -1;
1647 /* --------------------------------------------------------------------------------------------- */