123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734 |
- /*
- Directory cache support
- Copyright (C) 1998-2024
- Free Software Foundation, Inc.
- Written by:
- Pavel Machek <pavel@ucw.cz>, 1998
- Slava Zanko <slavazanko@gmail.com>, 2010-2013
- Andrew Borodin <aborodin@vmail.ru> 2010-2022
- This file is part of the Midnight Commander.
- The Midnight Commander is free software: you can redistribute it
- and/or modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation, either version 3 of the License,
- or (at your option) any later version.
- The Midnight Commander is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- \warning Paths here do _not_ begin with '/', so root directory of
- archive/site is simply "".
- */
- /** \file
- * \brief Source: directory cache support
- *
- * So that you do not have copy of this in each and every filesystem.
- *
- * Very loosely based on tar.c from midnight and archives.[ch] from
- * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
- *
- * Unfortunately, I was unable to keep all filesystems
- * uniform. tar-like filesystems use tree structure where each
- * directory has pointers to its subdirectories. We can do this
- * because we have full information about our archive.
- *
- * At ftp-like filesystems, situation is a little bit different. When
- * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
- * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
- * listed. That means that we do not have complete information, and if
- * /usr is symlink to /4, we will not know. Also we have to time out
- * entries and things would get messy with tree-like approach. So we
- * do different trick: root directory is completely special and
- * completely fake, it contains entries such as 'usr', 'usr/src', ...,
- * and we'll try to use custom find_entry function.
- *
- * \author Pavel Machek <pavel@ucw.cz>
- * \date 1998
- *
- */
- #include <config.h>
- #include <errno.h>
- #include <inttypes.h> /* uintmax_t */
- #include <stdarg.h>
- #ifdef HAVE_SYS_SELECT_H
- #include <sys/select.h>
- #endif
- #include <sys/types.h>
- #include <unistd.h>
- #include "lib/global.h"
- #include "lib/tty/tty.h" /* enable/disable interrupt key */
- #include "lib/util.h" /* canonicalize_pathname_custom() */
- #if 0
- #include "lib/widget.h" /* message() */
- #endif
- #include "vfs.h"
- #include "utilvfs.h"
- #include "xdirentry.h"
- #include "gc.h" /* vfs_rmstamp */
- /*** global variables ****************************************************************************/
- /*** file scope macro definitions ****************************************************************/
- #define CALL(x) \
- if (VFS_SUBCLASS (me)->x != NULL) \
- VFS_SUBCLASS (me)->x
- /*** file scope type declarations ****************************************************************/
- struct dirhandle
- {
- GList *cur;
- struct vfs_s_inode *dir;
- };
- /*** file scope variables ************************************************************************/
- /*** forward declarations (file scope functions) *************************************************/
- /* --------------------------------------------------------------------------------------------- */
- /*** file scope functions ************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- /* We were asked to create entries automagically */
- static struct vfs_s_entry *
- vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
- {
- struct vfs_s_entry *res;
- char *sep;
- sep = strchr (path, PATH_SEP);
- if (sep != NULL)
- *sep = '\0';
- res = vfs_s_generate_entry (me, path, dir, (flags & FL_MKDIR) != 0 ? (0777 | S_IFDIR) : 0777);
- vfs_s_insert_entry (me, dir, res);
- if (sep != NULL)
- *sep = PATH_SEP;
- return res;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* If the entry is a symlink, find the entry for its target */
- static struct vfs_s_entry *
- vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
- {
- char *linkname;
- char *fullname = NULL;
- struct vfs_s_entry *target;
- if (follow == LINK_NO_FOLLOW)
- return entry;
- if (follow == 0)
- ERRNOR (ELOOP, NULL);
- if (entry == NULL)
- ERRNOR (ENOENT, NULL);
- if (!S_ISLNK (entry->ino->st.st_mode))
- return entry;
- linkname = entry->ino->linkname;
- if (linkname == NULL)
- ERRNOR (EFAULT, NULL);
- /* make full path from relative */
- if (!IS_PATH_SEP (*linkname))
- {
- char *fullpath;
- fullpath = vfs_s_fullpath (me, entry->dir);
- if (fullpath != NULL)
- {
- fullname = g_strconcat (fullpath, PATH_SEP_STR, linkname, (char *) NULL);
- linkname = fullname;
- g_free (fullpath);
- }
- }
- target = VFS_SUBCLASS (me)->find_entry (me, entry->dir->super->root, linkname, follow - 1,
- FL_NONE);
- g_free (fullname);
- return target;
- }
- /* --------------------------------------------------------------------------------------------- */
- /*
- * Follow > 0: follow links, serves as loop protect,
- * == -1: do not follow links
- */
- static struct vfs_s_entry *
- vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root, const char *a_path,
- int follow, int flags)
- {
- size_t pseg;
- struct vfs_s_entry *ent = NULL;
- char *const pathref = g_strdup (a_path);
- char *path = pathref;
- /* canonicalize as well, but don't remove '../' from path */
- canonicalize_pathname_custom (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
- while (root != NULL)
- {
- GList *iter;
- while (IS_PATH_SEP (*path)) /* Strip leading '/' */
- path++;
- if (path[0] == '\0')
- {
- g_free (pathref);
- return ent;
- }
- for (pseg = 0; path[pseg] != '\0' && !IS_PATH_SEP (path[pseg]); pseg++)
- ;
- for (iter = g_queue_peek_head_link (root->subdir); iter != NULL; iter = g_list_next (iter))
- {
- ent = VFS_ENTRY (iter->data);
- if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
- /* FOUND! */
- break;
- }
- ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
- if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
- ent = vfs_s_automake (me, root, path, flags);
- if (ent == NULL)
- {
- me->verrno = ENOENT;
- goto cleanup;
- }
- path += pseg;
- /* here we must follow leading directories always;
- only the actual file is optional */
- ent = vfs_s_resolve_symlink (me, ent,
- strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
- if (ent == NULL)
- goto cleanup;
- root = ent->ino;
- }
- cleanup:
- g_free (pathref);
- return NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
- static struct vfs_s_entry *
- vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root, const char *a_path,
- int follow, int flags)
- {
- struct vfs_s_entry *ent = NULL;
- char *const path = g_strdup (a_path);
- GList *iter;
- if (root->super->root != root)
- vfs_die ("We have to use _real_ root. Always. Sorry.");
- /* canonicalize as well, but don't remove '../' from path */
- canonicalize_pathname_custom (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
- if ((flags & FL_DIR) == 0)
- {
- char *dirname, *name;
- struct vfs_s_inode *ino;
- dirname = g_path_get_dirname (path);
- name = g_path_get_basename (path);
- ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
- ent = vfs_s_find_entry_tree (me, ino, name, follow, flags);
- g_free (dirname);
- g_free (name);
- g_free (path);
- return ent;
- }
- iter = g_queue_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
- ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
- if (ent != NULL && !VFS_SUBCLASS (me)->dir_uptodate (me, ent->ino))
- {
- #if 1
- vfs_print_message (_ ("Directory cache expired for %s"), path);
- #endif
- vfs_s_free_entry (me, ent);
- ent = NULL;
- }
- if (ent == NULL)
- {
- struct vfs_s_inode *ino;
- ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
- ent = vfs_s_new_entry (me, path, ino);
- if (VFS_SUBCLASS (me)->dir_load (me, ino, path) == -1)
- {
- vfs_s_free_entry (me, ent);
- g_free (path);
- return NULL;
- }
- vfs_s_insert_entry (me, root, ent);
- iter = g_queue_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
- ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
- }
- if (ent == NULL)
- vfs_die ("find_linear: success but directory is not there\n");
- #if 0
- if (vfs_s_resolve_symlink (me, ent, follow) == NULL)
- {
- g_free (path);
- return NULL;
- }
- #endif
- g_free (path);
- return ent;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* Ook, these were functions around directory entries / inodes */
- /* -------------------------------- superblock games -------------------------- */
- static struct vfs_s_super *
- vfs_s_new_super (struct vfs_class *me)
- {
- struct vfs_s_super *super;
- super = g_new0 (struct vfs_s_super, 1);
- super->me = me;
- return super;
- }
- /* --------------------------------------------------------------------------------------------- */
- static inline void
- vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
- {
- VFS_SUBCLASS (me)->supers = g_list_prepend (VFS_SUBCLASS (me)->supers, super);
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
- {
- if (super->root != NULL)
- {
- vfs_s_free_inode (me, super->root);
- super->root = NULL;
- }
- #if 0
- /* FIXME: We currently leak small amount of memory, sometimes. Fix it if you can. */
- if (super->ino_usage != 0)
- message (D_ERROR, "Direntry warning",
- "Super ino_usage is %d, memory leak", super->ino_usage);
- if (super->want_stale)
- message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
- #endif
- VFS_SUBCLASS (me)->supers = g_list_remove (VFS_SUBCLASS (me)->supers, super);
- CALL (free_archive) (me, super);
- #ifdef ENABLE_VFS_NET
- vfs_path_element_free (super->path_element);
- #endif
- g_free (super->name);
- g_free (super);
- }
- /* --------------------------------------------------------------------------------------------- */
- static vfs_file_handler_t *
- vfs_s_new_fh (struct vfs_s_inode *ino, gboolean changed)
- {
- vfs_file_handler_t *fh;
- fh = g_new0 (vfs_file_handler_t, 1);
- vfs_s_init_fh (fh, ino, changed);
- return fh;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- vfs_s_free_fh (struct vfs_s_subclass *s, vfs_file_handler_t *fh)
- {
- if (s->fh_free != NULL)
- s->fh_free (fh);
- g_free (fh);
- }
- /* --------------------------------------------------------------------------------------------- */
- /* Support of archives */
- /* ------------------------ readdir & friends ----------------------------- */
- static struct vfs_s_inode *
- vfs_s_inode_from_path (const vfs_path_t *vpath, int flags)
- {
- struct vfs_s_super *super;
- struct vfs_s_inode *ino;
- const char *q;
- struct vfs_class *me;
- q = vfs_s_get_path (vpath, &super, 0);
- if (q == NULL)
- return NULL;
- me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
- ino = vfs_s_find_inode (me, super, q, (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
- flags & ~FL_FOLLOW);
- if (ino == NULL && *q == '\0')
- /* We are asking about / directory of ftp server: assume it exists */
- ino = vfs_s_find_inode (me, super, q,
- (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
- FL_DIR | (flags & ~FL_FOLLOW));
- return ino;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void *
- vfs_s_opendir (const vfs_path_t *vpath)
- {
- struct vfs_s_inode *dir;
- struct dirhandle *info;
- struct vfs_class *me;
- dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
- if (dir == NULL)
- return NULL;
- me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
- if (!S_ISDIR (dir->st.st_mode))
- {
- me->verrno = ENOTDIR;
- return NULL;
- }
- dir->st.st_nlink++;
- #if 0
- if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
- {
- me->verrno = EAGAIN;
- return NULL;
- }
- #endif
- info = g_new (struct dirhandle, 1);
- info->cur = g_queue_peek_head_link (dir->subdir);
- info->dir = dir;
- return info;
- }
- /* --------------------------------------------------------------------------------------------- */
- static struct vfs_dirent *
- vfs_s_readdir (void *data)
- {
- struct vfs_dirent *dir = NULL;
- struct dirhandle *info = (struct dirhandle *) data;
- const char *name;
- if (info->cur == NULL || info->cur->data == NULL)
- return NULL;
- name = VFS_ENTRY (info->cur->data)->name;
- if (name != NULL)
- dir = vfs_dirent_init (NULL, name, 0);
- else
- vfs_die ("Null in structure-cannot happen");
- info->cur = g_list_next (info->cur);
- return dir;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_closedir (void *data)
- {
- struct dirhandle *info = (struct dirhandle *) data;
- struct vfs_s_inode *dir = info->dir;
- vfs_s_free_inode (dir->super->me, dir);
- g_free (data);
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_chdir (const vfs_path_t *vpath)
- {
- void *data;
- data = vfs_s_opendir (vpath);
- if (data == NULL)
- return (-1);
- vfs_s_closedir (data);
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* --------------------------- stat and friends ---------------------------- */
- static int
- vfs_s_internal_stat (const vfs_path_t *vpath, struct stat *buf, int flag)
- {
- struct vfs_s_inode *ino;
- ino = vfs_s_inode_from_path (vpath, flag);
- if (ino == NULL)
- return (-1);
- *buf = ino->st;
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_readlink (const vfs_path_t *vpath, char *buf, size_t size)
- {
- struct vfs_s_inode *ino;
- size_t len;
- struct vfs_class *me;
- ino = vfs_s_inode_from_path (vpath, 0);
- if (ino == NULL)
- return (-1);
- me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
- if (!S_ISLNK (ino->st.st_mode))
- {
- me->verrno = EINVAL;
- return (-1);
- }
- if (ino->linkname == NULL)
- {
- me->verrno = EFAULT;
- return (-1);
- }
- len = strlen (ino->linkname);
- if (size < len)
- len = size;
- /* readlink() does not append a NUL character to buf */
- memcpy (buf, ino->linkname, len);
- return len;
- }
- /* --------------------------------------------------------------------------------------------- */
- static ssize_t
- vfs_s_read (void *fh, char *buffer, size_t count)
- {
- vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
- struct vfs_class *me = VFS_FILE_HANDLER_SUPER (fh)->me;
- if (file->linear == LS_LINEAR_PREOPEN)
- {
- if (VFS_SUBCLASS (me)->linear_start (me, file, file->pos) == 0)
- return (-1);
- }
- if (file->linear == LS_LINEAR_CLOSED)
- vfs_die ("linear_start() did not set linear_state!");
- if (file->linear == LS_LINEAR_OPEN)
- return VFS_SUBCLASS (me)->linear_read (me, file, buffer, count);
- if (file->handle != -1)
- {
- ssize_t n;
- n = read (file->handle, buffer, count);
- if (n < 0)
- me->verrno = errno;
- return n;
- }
- vfs_die ("vfs_s_read: This should not happen\n");
- return (-1);
- }
- /* --------------------------------------------------------------------------------------------- */
- static ssize_t
- vfs_s_write (void *fh, const char *buffer, size_t count)
- {
- vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
- struct vfs_class *me = VFS_FILE_HANDLER_SUPER (fh)->me;
- if (file->linear != LS_NOT_LINEAR)
- vfs_die ("no writing to linear files, please");
- file->changed = TRUE;
- if (file->handle != -1)
- {
- ssize_t n;
- n = write (file->handle, buffer, count);
- if (n < 0)
- me->verrno = errno;
- return n;
- }
- vfs_die ("vfs_s_write: This should not happen\n");
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static off_t
- vfs_s_lseek (void *fh, off_t offset, int whence)
- {
- vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
- off_t size = file->ino->st.st_size;
- if (file->linear == LS_LINEAR_OPEN)
- vfs_die ("cannot lseek() after linear_read!");
- if (file->handle != -1)
- { /* If we have local file opened, we want to work with it */
- off_t retval;
- retval = lseek (file->handle, offset, whence);
- if (retval == -1)
- VFS_FILE_HANDLER_SUPER (fh)->me->verrno = errno;
- return retval;
- }
- switch (whence)
- {
- case SEEK_CUR:
- offset += file->pos;
- break;
- case SEEK_END:
- offset += size;
- break;
- default:
- break;
- }
- if (offset < 0)
- file->pos = 0;
- else if (offset < size)
- file->pos = offset;
- else
- file->pos = size;
- return file->pos;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_close (void *fh)
- {
- vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
- struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
- struct vfs_class *me = super->me;
- struct vfs_s_subclass *sub = VFS_SUBCLASS (me);
- int res = 0;
- if (me == NULL)
- return (-1);
- super->fd_usage--;
- if (super->fd_usage == 0)
- vfs_stamp_create (me, VFS_FILE_HANDLER_SUPER (fh));
- if (file->linear == LS_LINEAR_OPEN)
- sub->linear_close (me, fh);
- if (sub->fh_close != NULL)
- res = sub->fh_close (me, fh);
- if ((me->flags & VFSF_USETMP) != 0 && file->changed && sub->file_store != NULL)
- {
- char *s;
- s = vfs_s_fullpath (me, file->ino);
- if (s == NULL)
- res = -1;
- else
- {
- res = sub->file_store (me, fh, s, file->ino->localname);
- g_free (s);
- }
- vfs_s_invalidate (me, super);
- }
- if (file->handle != -1)
- {
- close (file->handle);
- file->handle = -1;
- }
- vfs_s_free_inode (me, file->ino);
- vfs_s_free_fh (sub, fh);
- return res;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- vfs_s_print_stats (const char *fs_name, const char *action, const char *file_name, off_t have,
- off_t need)
- {
- if (need != 0)
- vfs_print_message (_ ("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
- file_name, (int) ((double) have * 100 / need), (long long) have);
- else
- vfs_print_message (_ ("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
- (long long) have);
- }
- /* --------------------------------------------------------------------------------------------- */
- /* ------------------------------- mc support ---------------------------- */
- static void
- vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
- {
- GList *iter;
- for (iter = VFS_SUBCLASS (me)->supers; iter != NULL; iter = g_list_next (iter))
- {
- const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
- char *name;
- name = g_strconcat (super->name, PATH_SEP_STR, me->prefix, VFS_PATH_URL_DELIMITER,
- /* super->current_dir->name, */ (char *) NULL);
- func (name);
- g_free (name);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_ferrno (struct vfs_class *me)
- {
- return me->verrno;
- }
- /* --------------------------------------------------------------------------------------------- */
- /**
- * Get local copy of the given file. We reuse the existing file cache
- * for remote filesystems. Archives use standard VFS facilities.
- */
- static vfs_path_t *
- vfs_s_getlocalcopy (const vfs_path_t *vpath)
- {
- vfs_file_handler_t *fh;
- vfs_path_t *local = NULL;
- if (vpath == NULL)
- return NULL;
- fh = vfs_s_open (vpath, O_RDONLY, 0);
- if (fh != NULL)
- {
- const struct vfs_class *me;
- me = vfs_path_get_last_path_vfs (vpath);
- if ((me->flags & VFSF_USETMP) != 0 && fh->ino != NULL)
- local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
- vfs_s_close (fh);
- }
- return local;
- }
- /* --------------------------------------------------------------------------------------------- */
- /**
- * Return the local copy. Since we are using our cache, we do nothing -
- * the cache will be removed when the archive is closed.
- */
- static int
- vfs_s_ungetlocalcopy (const vfs_path_t *vpath, const vfs_path_t *local, gboolean has_changed)
- {
- (void) vpath;
- (void) local;
- (void) has_changed;
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- vfs_s_setctl (const vfs_path_t *vpath, int ctlop, void *arg)
- {
- struct vfs_class *me;
- me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
- switch (ctlop)
- {
- case VFS_SETCTL_STALE_DATA:
- {
- struct vfs_s_inode *ino;
- ino = vfs_s_inode_from_path (vpath, 0);
- if (ino == NULL)
- return 0;
- if (arg != NULL)
- ino->super->want_stale = TRUE;
- else
- {
- ino->super->want_stale = FALSE;
- vfs_s_invalidate (me, ino->super);
- }
- return 1;
- }
- case VFS_SETCTL_LOGFILE:
- me->logfile = fopen ((char *) arg, "w");
- return 1;
- case VFS_SETCTL_FLUSH:
- me->flush = TRUE;
- return 1;
- default:
- return 0;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- /* ----------------------------- Stamping support -------------------------- */
- static vfsid
- vfs_s_getid (const vfs_path_t *vpath)
- {
- struct vfs_s_super *archive = NULL;
- const char *p;
- p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
- if (p == NULL)
- return NULL;
- return (vfsid) archive;
- }
- /* --------------------------------------------------------------------------------------------- */
- static gboolean
- vfs_s_nothingisopen (vfsid id)
- {
- return (VFS_SUPER (id)->fd_usage <= 0);
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- vfs_s_free (vfsid id)
- {
- vfs_s_free_super (VFS_SUPER (id)->me, VFS_SUPER (id));
- }
- /* --------------------------------------------------------------------------------------------- */
- static gboolean
- vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
- {
- gint64 tim;
- if (me->flush)
- {
- me->flush = FALSE;
- return 0;
- }
- tim = g_get_monotonic_time ();
- return (tim < ino->timestamp);
- }
- /* --------------------------------------------------------------------------------------------- */
- /*** public functions ****************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- struct vfs_s_inode *
- vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
- {
- struct vfs_s_inode *ino;
- ino = g_try_new0 (struct vfs_s_inode, 1);
- if (ino == NULL)
- return NULL;
- if (initstat != NULL)
- ino->st = *initstat;
- ino->super = super;
- ino->subdir = g_queue_new ();
- ino->st.st_nlink = 0;
- ino->st.st_ino = VFS_SUBCLASS (me)->inode_counter++;
- ino->st.st_dev = VFS_SUBCLASS (me)->rdev;
- super->ino_usage++;
- CALL (init_inode) (me, ino);
- return ino;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
- {
- if (ino == NULL)
- vfs_die ("Don't pass NULL to me");
- /* ==0 can happen if freshly created entry is deleted */
- if (ino->st.st_nlink > 1)
- {
- ino->st.st_nlink--;
- return;
- }
- while (g_queue_get_length (ino->subdir) != 0)
- {
- struct vfs_s_entry *entry;
- entry = VFS_ENTRY (g_queue_peek_head (ino->subdir));
- vfs_s_free_entry (me, entry);
- }
- g_queue_free (ino->subdir);
- ino->subdir = NULL;
- CALL (free_inode) (me, ino);
- g_free (ino->linkname);
- if ((me->flags & VFSF_USETMP) != 0 && ino->localname != NULL)
- {
- unlink (ino->localname);
- g_free (ino->localname);
- }
- ino->super->ino_usage--;
- g_free (ino);
- }
- /* --------------------------------------------------------------------------------------------- */
- struct vfs_s_entry *
- vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
- {
- struct vfs_s_entry *entry;
- entry = g_new0 (struct vfs_s_entry, 1);
- entry->name = g_strdup (name);
- entry->ino = inode;
- entry->ino->ent = entry;
- CALL (init_entry) (me, entry);
- return entry;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
- {
- if (ent->dir != NULL)
- g_queue_remove (ent->dir->subdir, ent);
- MC_PTR_FREE (ent->name);
- if (ent->ino != NULL)
- {
- ent->ino->ent = NULL;
- vfs_s_free_inode (me, ent->ino);
- }
- g_free (ent);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
- {
- (void) me;
- ent->dir = dir;
- ent->ino->st.st_nlink++;
- g_queue_push_tail (dir->subdir, ent);
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_entry_compare (const void *a, const void *b)
- {
- const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
- const char *name = (const char *) b;
- return strcmp (e->name, name);
- }
- /* --------------------------------------------------------------------------------------------- */
- struct stat *
- vfs_s_default_stat (struct vfs_class *me, mode_t mode)
- {
- static struct stat st;
- mode_t myumask;
- (void) me;
- myumask = umask (022);
- umask (myumask);
- mode &= ~myumask;
- st.st_mode = mode;
- st.st_ino = 0;
- st.st_dev = 0;
- #ifdef HAVE_STRUCT_STAT_ST_RDEV
- st.st_rdev = 0;
- #endif
- st.st_uid = getuid ();
- st.st_gid = getgid ();
- #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
- st.st_blksize = 512;
- #endif
- st.st_size = 0;
- vfs_zero_stat_times (&st);
- vfs_adjust_stat (&st);
- return &st;
- }
- /* --------------------------------------------------------------------------------------------- */
- /**
- * Calculate number of st_blocks using st_size and st_blksize.
- * In according to stat(2), st_blocks is the size in 512-byte units.
- *
- * @param s stat info
- */
- void
- vfs_adjust_stat (struct stat *s)
- {
- #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
- if (s->st_size == 0)
- s->st_blocks = 0;
- else
- {
- #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
- blkcnt_t ioblocks;
- blksize_t ioblock_size;
- /* 1. Calculate how many IO blocks are occupied */
- ioblocks = 1 + (s->st_size - 1) / s->st_blksize;
- /* 2. Calculate size of st_blksize in 512-byte units */
- ioblock_size = 1 + (s->st_blksize - 1) / 512;
- /* 3. Calculate number of blocks */
- s->st_blocks = ioblocks * ioblock_size;
- #else
- /* Let IO block size is 512 bytes */
- s->st_blocks = 1 + (s->st_size - 1) / 512;
- #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
- }
- #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
- }
- /* --------------------------------------------------------------------------------------------- */
- struct vfs_s_entry *
- vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
- mode_t mode)
- {
- struct vfs_s_inode *inode;
- struct stat *st;
- st = vfs_s_default_stat (me, mode);
- inode = vfs_s_new_inode (me, parent->super, st);
- return vfs_s_new_entry (me, name, inode);
- }
- /* --------------------------------------------------------------------------------------------- */
- struct vfs_s_inode *
- vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super, const char *path,
- int follow, int flags)
- {
- struct vfs_s_entry *ent;
- if (((me->flags & VFSF_REMOTE) == 0) && (*path == '\0'))
- return super->root;
- ent = VFS_SUBCLASS (me)->find_entry (me, super->root, path, follow, flags);
- return (ent != NULL ? ent->ino : NULL);
- }
- /* --------------------------------------------------------------------------------------------- */
- /* Ook, these were functions around directory entries / inodes */
- /* -------------------------------- superblock games -------------------------- */
- /**
- * get superlock object by vpath
- *
- * @param vpath path
- * @return superlock object or NULL if not found
- */
- struct vfs_s_super *
- vfs_get_super_by_vpath (const vfs_path_t *vpath)
- {
- GList *iter;
- void *cookie = NULL;
- const vfs_path_element_t *path_element;
- struct vfs_s_subclass *subclass;
- struct vfs_s_super *super = NULL;
- vfs_path_t *vpath_archive;
- path_element = vfs_path_get_by_index (vpath, -1);
- subclass = VFS_SUBCLASS (path_element->class);
- vpath_archive = vfs_path_clone (vpath);
- vfs_path_remove_element_by_index (vpath_archive, -1);
- if (subclass->archive_check != NULL)
- {
- cookie = subclass->archive_check (vpath_archive);
- if (cookie == NULL)
- goto ret;
- }
- if (subclass->archive_same == NULL)
- goto ret;
- for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
- {
- int i;
- super = VFS_SUPER (iter->data);
- /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
- i = subclass->archive_same (path_element, super, vpath_archive, cookie);
- if (i == 1)
- goto ret;
- if (i != 0)
- break;
- super = NULL;
- }
- ret:
- vfs_path_free (vpath_archive, TRUE);
- return super;
- }
- /* --------------------------------------------------------------------------------------------- */
- /**
- * get path from last VFS-element and create corresponding superblock
- *
- * @param vpath source path object
- * @param archive pointer to object for store newly created superblock
- * @param flags flags
- *
- * @return path from last VFS-element
- */
- const char *
- vfs_s_get_path (const vfs_path_t *vpath, struct vfs_s_super **archive, int flags)
- {
- const char *retval = "";
- int result = -1;
- struct vfs_s_super *super;
- const vfs_path_element_t *path_element;
- struct vfs_s_subclass *subclass;
- path_element = vfs_path_get_by_index (vpath, -1);
- if (path_element->path != NULL)
- retval = path_element->path;
- super = vfs_get_super_by_vpath (vpath);
- if (super != NULL)
- goto return_success;
- if ((flags & FL_NO_OPEN) != 0)
- {
- path_element->class->verrno = EIO;
- return NULL;
- }
- subclass = VFS_SUBCLASS (path_element->class);
- super = subclass->new_archive != NULL ? subclass->new_archive (path_element->class)
- : vfs_s_new_super (path_element->class);
- if (subclass->open_archive != NULL)
- {
- vfs_path_t *vpath_archive;
- vpath_archive = vfs_path_clone (vpath);
- vfs_path_remove_element_by_index (vpath_archive, -1);
- result = subclass->open_archive (super, vpath_archive, path_element);
- vfs_path_free (vpath_archive, TRUE);
- }
- if (result == -1)
- {
- vfs_s_free_super (path_element->class, super);
- path_element->class->verrno = EIO;
- return NULL;
- }
- if (super->name == NULL)
- vfs_die ("You have to fill name\n");
- if (super->root == NULL)
- vfs_die ("You have to fill root inode\n");
- vfs_s_insert_super (path_element->class, super);
- vfs_stamp_create (path_element->class, super);
- return_success:
- *archive = super;
- return retval;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
- {
- if (!super->want_stale)
- {
- vfs_s_free_inode (me, super->root);
- super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- char *
- vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
- {
- if (ino->ent == NULL)
- ERRNOR (EAGAIN, NULL);
- if ((me->flags & VFSF_USETMP) == 0)
- {
- /* archives */
- char *path;
- path = g_strdup (ino->ent->name);
- while (TRUE)
- {
- char *newpath;
- ino = ino->ent->dir;
- if (ino == ino->super->root)
- break;
- newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
- g_free (path);
- path = newpath;
- }
- return path;
- }
- /* remote systems */
- if (ino->ent->dir == NULL || ino->ent->dir->ent == NULL)
- return g_strdup (ino->ent->name);
- return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_s_init_fh (vfs_file_handler_t *fh, struct vfs_s_inode *ino, gboolean changed)
- {
- fh->ino = ino;
- fh->handle = -1;
- fh->changed = changed;
- fh->linear = LS_NOT_LINEAR;
- }
- /* --------------------------------------------------------------------------------------------- */
- /* --------------------------- stat and friends ---------------------------- */
- void *
- vfs_s_open (const vfs_path_t *vpath, int flags, mode_t mode)
- {
- gboolean was_changed = FALSE;
- vfs_file_handler_t *fh;
- struct vfs_s_super *super;
- const char *q;
- struct vfs_s_inode *ino;
- struct vfs_class *me;
- struct vfs_s_subclass *s;
- q = vfs_s_get_path (vpath, &super, 0);
- if (q == NULL)
- return NULL;
- me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
- ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
- if (ino != NULL && (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
- {
- me->verrno = EEXIST;
- return NULL;
- }
- s = VFS_SUBCLASS (me);
- if (ino == NULL)
- {
- char *name;
- struct vfs_s_entry *ent;
- struct vfs_s_inode *dir;
- /* If the filesystem is read-only, disable file creation */
- if ((flags & O_CREAT) == 0 || me->write == NULL)
- return NULL;
- name = g_path_get_dirname (q);
- dir = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_DIR);
- g_free (name);
- if (dir == NULL)
- return NULL;
- name = g_path_get_basename (q);
- ent = vfs_s_generate_entry (me, name, dir, 0755);
- ino = ent->ino;
- vfs_s_insert_entry (me, dir, ent);
- if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0)
- {
- int tmp_handle;
- vfs_path_t *tmp_vpath;
- tmp_handle = vfs_mkstemps (&tmp_vpath, me->name, name);
- ino->localname = vfs_path_free (tmp_vpath, FALSE);
- if (tmp_handle == -1)
- {
- g_free (name);
- return NULL;
- }
- close (tmp_handle);
- }
- g_free (name);
- was_changed = TRUE;
- }
- if (S_ISDIR (ino->st.st_mode))
- {
- me->verrno = EISDIR;
- return NULL;
- }
- fh = s->fh_new != NULL ? s->fh_new (ino, was_changed) : vfs_s_new_fh (ino, was_changed);
- if (IS_LINEAR (flags))
- {
- if (s->linear_start != NULL)
- {
- vfs_print_message ("%s", _ ("Starting linear transfer..."));
- fh->linear = LS_LINEAR_PREOPEN;
- }
- }
- else
- {
- if (s->fh_open != NULL && s->fh_open (me, fh, flags, mode) != 0)
- {
- vfs_s_free_fh (s, fh);
- return NULL;
- }
- }
- if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0 && fh->ino->localname != NULL)
- {
- fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
- if (fh->handle == -1)
- {
- vfs_s_free_fh (s, fh);
- me->verrno = errno;
- return NULL;
- }
- }
- /* i.e. we had no open files and now we have one */
- vfs_rmstamp (me, (vfsid) super);
- super->fd_usage++;
- fh->ino->st.st_nlink++;
- return fh;
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_stat (const vfs_path_t *vpath, struct stat *buf)
- {
- return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_lstat (const vfs_path_t *vpath, struct stat *buf)
- {
- return vfs_s_internal_stat (vpath, buf, FL_NONE);
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_fstat (void *fh, struct stat *buf)
- {
- *buf = VFS_FILE_HANDLER (fh)->ino->st;
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
- {
- /* If you want reget, you'll have to open file with O_LINEAR */
- off_t total = 0;
- char buffer[BUF_8K];
- int handle;
- ssize_t n;
- off_t stat_size = ino->st.st_size;
- vfs_file_handler_t *fh = NULL;
- vfs_path_t *tmp_vpath;
- struct vfs_s_subclass *s = VFS_SUBCLASS (me);
- if ((me->flags & VFSF_USETMP) == 0)
- return (-1);
- handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
- ino->localname = vfs_path_free (tmp_vpath, FALSE);
- if (handle == -1)
- {
- me->verrno = errno;
- goto error_4;
- }
- fh = s->fh_new != NULL ? s->fh_new (ino, FALSE) : vfs_s_new_fh (ino, FALSE);
- if (s->linear_start (me, fh, 0) == 0)
- goto error_3;
- /* Clear the interrupt status */
- tty_got_interrupt ();
- tty_enable_interrupt_key ();
- while ((n = s->linear_read (me, fh, buffer, sizeof (buffer))) != 0)
- {
- int t;
- if (n < 0)
- goto error_1;
- total += n;
- vfs_s_print_stats (me->name, _ ("Getting file"), ino->ent->name, total, stat_size);
- if (tty_got_interrupt ())
- goto error_1;
- t = write (handle, buffer, n);
- if (t != n)
- {
- if (t == -1)
- me->verrno = errno;
- goto error_1;
- }
- }
- s->linear_close (me, fh);
- close (handle);
- tty_disable_interrupt_key ();
- vfs_s_free_fh (s, fh);
- return 0;
- error_1:
- s->linear_close (me, fh);
- error_3:
- tty_disable_interrupt_key ();
- close (handle);
- unlink (ino->localname);
- error_4:
- MC_PTR_FREE (ino->localname);
- if (fh != NULL)
- vfs_s_free_fh (s, fh);
- return (-1);
- }
- /* --------------------------------------------------------------------------------------------- */
- /* ----------------------------- Stamping support -------------------------- */
- /* Initialize one of our subclasses - fill common functions */
- void
- vfs_init_class (struct vfs_class *vclass, const char *name, vfs_flags_t flags, const char *prefix)
- {
- memset (vclass, 0, sizeof (struct vfs_class));
- vclass->name = name;
- vclass->flags = flags;
- vclass->prefix = prefix;
- vclass->fill_names = vfs_s_fill_names;
- vclass->open = vfs_s_open;
- vclass->close = vfs_s_close;
- vclass->read = vfs_s_read;
- if ((vclass->flags & VFSF_READONLY) == 0)
- vclass->write = vfs_s_write;
- vclass->opendir = vfs_s_opendir;
- vclass->readdir = vfs_s_readdir;
- vclass->closedir = vfs_s_closedir;
- vclass->stat = vfs_s_stat;
- vclass->lstat = vfs_s_lstat;
- vclass->fstat = vfs_s_fstat;
- vclass->readlink = vfs_s_readlink;
- vclass->chdir = vfs_s_chdir;
- vclass->ferrno = vfs_s_ferrno;
- vclass->lseek = vfs_s_lseek;
- vclass->getid = vfs_s_getid;
- vclass->nothingisopen = vfs_s_nothingisopen;
- vclass->free = vfs_s_free;
- vclass->setctl = vfs_s_setctl;
- if ((vclass->flags & VFSF_USETMP) != 0)
- {
- vclass->getlocalcopy = vfs_s_getlocalcopy;
- vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- vfs_init_subclass (struct vfs_s_subclass *sub, const char *name, vfs_flags_t flags,
- const char *prefix)
- {
- struct vfs_class *vclass = VFS_CLASS (sub);
- size_t len;
- char *start;
- vfs_init_class (vclass, name, flags, prefix);
- len = sizeof (struct vfs_s_subclass) - sizeof (struct vfs_class);
- start = (char *) sub + sizeof (struct vfs_class);
- memset (start, 0, len);
- if ((vclass->flags & VFSF_USETMP) != 0)
- sub->find_entry = vfs_s_find_entry_linear;
- else if ((vclass->flags & VFSF_REMOTE) != 0)
- sub->find_entry = vfs_s_find_entry_linear;
- else
- sub->find_entry = vfs_s_find_entry_tree;
- sub->dir_uptodate = vfs_s_dir_uptodate;
- }
- /* --------------------------------------------------------------------------------------------- */
- /** Find VFS id for given directory name */
- vfsid
- vfs_getid (const vfs_path_t *vpath)
- {
- const struct vfs_class *me;
- me = vfs_path_get_last_path_vfs (vpath);
- if (me == NULL || me->getid == NULL)
- return NULL;
- return me->getid (vpath);
- }
- /* --------------------------------------------------------------------------------------------- */
- /* ----------- Utility functions for networked filesystems -------------- */
- #ifdef ENABLE_VFS_NET
- int
- vfs_s_select_on_two (int fd1, int fd2)
- {
- fd_set set;
- struct timeval time_out;
- int v;
- int maxfd = MAX (fd1, fd2) + 1;
- time_out.tv_sec = 1;
- time_out.tv_usec = 0;
- FD_ZERO (&set);
- FD_SET (fd1, &set);
- FD_SET (fd2, &set);
- v = select (maxfd, &set, 0, 0, &time_out);
- if (v <= 0)
- return v;
- if (FD_ISSET (fd1, &set))
- return 1;
- if (FD_ISSET (fd2, &set))
- return 2;
- return (-1);
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
- {
- FILE *logfile = me->logfile;
- int i;
- char c;
- for (i = 0; i < buf_len - 1; i++, buf++)
- {
- if (read (sock, buf, sizeof (char)) <= 0)
- return 0;
- if (logfile != NULL)
- {
- size_t ret1;
- int ret2;
- ret1 = fwrite (buf, 1, 1, logfile);
- ret2 = fflush (logfile);
- (void) ret1;
- (void) ret2;
- }
- if (*buf == term)
- {
- *buf = '\0';
- return 1;
- }
- }
- /* Line is too long - terminate buffer and discard the rest of line */
- *buf = '\0';
- while (read (sock, &c, sizeof (c)) > 0)
- {
- if (logfile != NULL)
- {
- size_t ret1;
- int ret2;
- ret1 = fwrite (&c, 1, 1, logfile);
- ret2 = fflush (logfile);
- (void) ret1;
- (void) ret2;
- }
- if (c == '\n')
- return 1;
- }
- return 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- int
- vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
- {
- int i;
- int res = 0;
- (void) me;
- tty_enable_interrupt_key ();
- for (i = 0; i < size - 1; i++)
- {
- ssize_t n;
- n = read (fd, &buffer[i], 1);
- if (n == -1 && errno == EINTR)
- {
- buffer[i] = '\0';
- res = EINTR;
- goto ret;
- }
- if (n == 0)
- {
- buffer[i] = '\0';
- goto ret;
- }
- if (buffer[i] == '\n')
- {
- buffer[i] = '\0';
- res = 1;
- goto ret;
- }
- }
- buffer[size - 1] = '\0';
- ret:
- tty_disable_interrupt_key ();
- return res;
- }
- #endif /* ENABLE_VFS_NET */
- /* --------------------------------------------------------------------------------------------- */
- /**
- * Normalize filenames start position
- */
- void
- vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
- {
- GList *iter;
- for (iter = g_queue_peek_head_link (root_inode->subdir); iter != NULL;
- iter = g_list_next (iter))
- {
- struct vfs_s_entry *entry = VFS_ENTRY (iter->data);
- if ((size_t) entry->leading_spaces > final_num_spaces)
- {
- char *source_name, *spacer;
- source_name = entry->name;
- spacer = g_strnfill ((size_t) entry->leading_spaces - final_num_spaces, ' ');
- entry->name = g_strconcat (spacer, source_name, (char *) NULL);
- g_free (spacer);
- g_free (source_name);
- }
- entry->leading_spaces = -1;
- }
- }
- /* --------------------------------------------------------------------------------------------- */
|