Browse Source

Big merge: fish now uses proper caching, small fixes almost
everywhere. I hope I did not break it too much.

Pavel Machek 26 years ago
parent
commit
4706638b22
10 changed files with 520 additions and 114 deletions
  1. 6 6
      config.h.in
  2. 0 4
      gtkedit/editcmd.c
  3. 3 3
      lib/mc.ext.in
  4. 4 0
      src/ChangeLog
  5. 9 0
      src/mad.c
  6. 5 0
      src/mad.h
  7. 12 0
      vfs/ChangeLog
  8. 3 3
      vfs/Make-mc.in
  9. 3 3
      vfs/Makefile.in
  10. 475 95
      vfs/direntry.c

+ 6 - 6
config.h.in

@@ -101,9 +101,9 @@
 /* If using the C implementation of alloca, define if you know the
    direction of stack growth for your system; otherwise it will be
    automatically deduced at run-time.
- STACK_DIRECTION > 0 => grows toward higher addresses
- STACK_DIRECTION < 0 => grows toward lower addresses
- STACK_DIRECTION = 0 => direction of growth unknown
+	STACK_DIRECTION > 0 => grows toward higher addresses
+	STACK_DIRECTION < 0 => grows toward lower addresses
+	STACK_DIRECTION = 0 => direction of growth unknown
  */
 #undef STACK_DIRECTION
 
@@ -113,9 +113,6 @@
 /* Define to `int' if <sys/types.h> doesn't define.  */
 #undef uid_t
 
-/* Define if the X Window System is missing or not being used.  */
-#undef X_DISPLAY_MISSING
-
 #undef VERSION
 #undef PACKAGE
 
@@ -408,6 +405,9 @@
 /* Define if you have the truncate function.  */
 #undef HAVE_TRUNCATE
 
+/* Define if you have the valloc function.  */
+#undef HAVE_VALLOC
+
 /* Define if you have the <argz.h> header file.  */
 #undef HAVE_ARGZ_H
 

+ 0 - 4
gtkedit/editcmd.c

@@ -260,11 +260,7 @@ int edit_save_file (WEdit * edit, const char *filename)
 		return 0;
 	    savedir[slashpos - filename + 1] = '\0';
 	}
-#ifdef HAVE_MAD
-	savename = strdup (tempnam (savedir, "cooledit"));
-#else
 	savename = tempnam (savedir, "cooledit");
-#endif
 	if (slashpos)
 	    free (savedir);
 	if (!savename)

+ 3 - 3
lib/mc.ext.in

@@ -543,19 +543,19 @@ type/Microsoft Word Document
 # cpio rules
 
 shell/.cpio.Z
-	Open=%cd cpio:%d/%p/
+	Open=%cd %d/%p#ucpio/
 	View=%view{ascii} compress -dc '%f' | cpio -ictv
 	Extract=compress -dc '%f' | cpio -ic
 	Icon=compressed.xpm
 
 shell/.cpio.gz
-	Open=%cd cpio:%d/%p/
+	Open=%cd %d/%p#ucpio/
 	View=%view{ascii} gzip -dc '%f' | cpio -ictv
 	Extract=gzip -dc '%f' | cpio -ic
 	Icon=compressed.xpm
 
 shell/.cpio
-	Open=%cd cpio:%d/%p/
+	Open=%cd %d/%p#ucpio/
 	View=%view{ascii} cat '%f' | cpio -ictv
 	Extract=cat '%f' | cpio -ic
 	Icon=cpio.xpm

+ 4 - 0
src/ChangeLog

@@ -1,3 +1,7 @@
+Sun Nov 15 00:37:58 1998  Pavel Machek  <pavel@bug.ucw.cz>
+
+	* mad.c (mad_tempnam): needed wrapper for tempnam added
+
 1998-11-17  Miguel de Icaza  <miguel@nuclecu.unam.mx>
 
 	* dlg.c (destroy_dlg): Do not refresh.

+ 9 - 0
src/mad.c

@@ -225,6 +225,15 @@ void mad_free (void *ptr, char *file, int line)
     mem_areas [i].in_use = 0;
 }
 
+char *mad_tempnam (char *a, char *b)
+{
+    char *t, *u;
+    t = tempnam(a,b);
+    u = mad_strdup(t, "(mad_tempnam)", 0);
+    free(t);
+    return u;    
+}
+
 /* Outputs a list of unfreed memory areas,
    to be called as a last thing before exiting */
 void mad_finalize (char *file, int line)

+ 5 - 0
src/mad.h

@@ -26,6 +26,9 @@
 #   undef calloc
 #endif
 
+#define tempnam(x,y)	mad_tempnam (x, y)
+
+
 #define malloc(x)	mad_alloc (x, __FILE__, __LINE__)
 #define calloc(x, y)	mad_alloc ((x) * (y), __FILE__, __LINE__)
 #define realloc(x, y)	mad_realloc (x, y, __FILE__, __LINE__)
@@ -39,6 +42,8 @@ void *mad_realloc (void *ptr, int newsize, char *file, int line);
 char *mad_strdup (const char *s, char *file, int line);
 void mad_free (void *ptr, char *file, int line);
 void mad_finalize (char *file, int line);
+char *mad_tempnam (char *s1, char *s2);
+
 #else
 
 #define mad_finalize(x, y)

+ 12 - 0
vfs/ChangeLog

@@ -1,3 +1,15 @@
+Sat Nov  7 20:19:14 1998  Pavel Machek  <pavel@bug.ucw.cz>
+
+	* vfs.c: Converted g_return_if_fail into vfs_dies(), removed them
+ 	where unneccessary. I do not see why passing NULL to vfs_add_stamp
+ 	should be invalid -> removed check.
+
+Mon Nov  2 23:09:55 1998  Pavel Machek  <pavel@bug.ucw.cz>
+
+	* direntry.c, xdirentry.h: should be now prepared to handle
+ 	ftp-like filesystems. This is rather BIG change, altrough it does
+ 	not look so. I hope I do not break too many things.
+
 Mon Nov 16 16:39:39 1998  Pavel Machek  <pavel@atrey.karlin.mff.cuni.cz>
 
 	* vfs.c (vfs_parse_ls_lga): Report unrecognized lines.

+ 3 - 3
vfs/Make-mc.in

@@ -99,13 +99,13 @@ EXTFSSTUFF = $(EXTFS_MISC) $(EXTFS_CONST) $(EXTFS_IN)
 
 VFSSOOBJS = tcputil.so fish.so ftpfs.so mcfs.so utilvfs.so local.so vfs.so \
 	tar.so sfs.so names.so container.so extfs.so util-alone.so \
-	util.sor utilunix.sor
+	util.sor utilunix.sor direntry.so
 
 %.sor:	../src/%.c
-	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
+	$(CC) -fpic -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
 
 %.so:	%.c
-	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
+	$(CC) -fpic -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
 
 libvfs.so: $(VFSSOOBJS)
 	gcc $(VFSSOOBJS) -shared -o libvfs.so

+ 3 - 3
vfs/Makefile.in

@@ -40,13 +40,13 @@ EXTFSSTUFF = $(EXTFS_MISC) $(EXTFS_CONST) $(EXTFS_IN)
 # Commands to build standalone version (.so)
 #
 
-VFSSOOBJS = tcputil.so ftpfs.so mcfs.so utilvfs.so local.so vfs.so tar.so names.so container.so extfs.so util-alone.so util.sor utilunix.sor sfs.so fish.so
+VFSSOOBJS = tcputil.so ftpfs.so mcfs.so utilvfs.so local.so vfs.so tar.so names.so container.so extfs.so util-alone.so util.sor utilunix.sor sfs.so fish.so direntry.so
 
 %.sor:	../src/%.c
-	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
+	$(CC) -c -fpic $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
 
 %.so:	%.c
-	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
+	$(CC) -c -fpic $(CPPFLAGS) $(DEFS) $(CFLAGS) -DVFS_STANDALONE $< -o $@
 
 libvfs.so: $(VFSSOOBJS)
 	gcc $(VFSSOOBJS) -shared -o libvfs.so

+ 475 - 95
vfs/direntry.c

@@ -4,25 +4,52 @@
  * Written at 1998 by Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
  *
  * Based on tar.c from midnight and archives.[ch] from avfs by Miklos
- * Szeredi (mszeredi@inf.bme.hu) */
+ * 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. 
+ *
+ * Paths here do _not_ begin with '/', so root directory of
+ * archive/site is simply "". Beware. */
+
+static volatile int total_inodes = 0, total_entries = 0;
 
 #include "xdirentry.h"
 
 #define CALL(x) if (MEDATA->x) MEDATA->x
 
-vfs_s_inode *vfs_s_new_inode (vfs *me, struct stat *initstat)
+vfs_s_inode *vfs_s_new_inode (vfs *me, vfs_s_super *super, struct stat *initstat)
 {
     vfs_s_inode *ino;
 
     ino = xmalloc(sizeof (vfs_s_inode), "Dcache inode");
     if (!ino) return NULL;
 
+
     ino->linkname = ino->localname = NULL;
     ino->subdir = NULL;
     if (initstat)
         ino->st = *initstat;
+    ino->super = super;
+    ino->ent = NULL;
+    ino->flags = 0;
     ino->st.st_nlink = 0;
 
+    super->fd_usage++;
+    total_inodes++;
+    
     CALL(init_inode) (me, ino);
 
     return ino;
@@ -33,44 +60,67 @@ vfs_s_entry *vfs_s_new_entry (vfs *me, char *name, vfs_s_inode *inode)
     vfs_s_entry *entry;
 
     entry = (struct vfs_s_entry *) xmalloc (sizeof (struct vfs_s_entry), "Dcache entry");
+    total_entries++;
 
-    entry->name = strdup (name);
+    if (name)
+	entry->name = strdup (name);
+    else entry->name = NULL;
     entry->dir = NULL;
     entry->next = NULL;
     entry->prevp = NULL;
     entry->ino = inode;
+    entry->ino->ent = entry;
     CALL(init_entry) (me, entry);
 
     return entry;
 }
 
-void vfs_s_free_entry (vfs *me, vfs_s_entry *ent)
+void vfs_s_free_inode (vfs *me, vfs_s_inode *ino)
 {
-    *ent->prevp = ent->next;
-    if (ent->next) ent->next->prevp = ent->prevp;
+    if (!ino) vfs_die("Don't pass NULL to me");
 
-    free(ent->name);
-    ent->name = NULL;
-    if(ent->ino->st.st_nlink == 1) {
-	CALL(free_inode) (me, ent->ino);
-	ifree(ent->ino->linkname);
-	ifree(ent->ino->localname);
-	ifree(ent->ino);
-	ent->ino = NULL;
-    } else ent->ino->st.st_nlink--;
+    /* ==0 can happen if freshly created entry is deleted */
+    if(ino->st.st_nlink <= 1) {
+	while(ino->subdir) {
+	    vfs_s_entry *ent;
+	    ent = ino->subdir;
+	    vfs_s_free_entry(me, ent);
+	}
 
-    free(ent);
+	CALL(free_inode) (me, ino);
+	ifree(ino->linkname);
+	if (ino->localname) {
+	    unlink(ino->localname);
+	    free(ino->localname);
+	}
+	total_inodes--;
+	ino->super->fd_usage--;
+	free(ino);
+    } else ino->st.st_nlink--;
 }
 
-void vfs_s_free_tree (vfs *me, vfs_s_inode *dir, vfs_s_inode *parentdir)
+void vfs_s_free_entry (vfs *me, vfs_s_entry *ent)
 {
-    vfs_s_entry *ent;
+    int is_dot = 0;
+    if (ent->prevp) {	/* It is possible that we are deleting freshly created entry */
+	*ent->prevp = ent->next;
+	if (ent->next) ent->next->prevp = ent->prevp;
+    }
 
-    while(dir->subdir) {
-	ent = dir->subdir;
-	if(ent->ino != dir && ent->ino != parentdir) vfs_s_free_tree(me, ent->ino, dir);
-	vfs_s_free_entry(me, ent);
+    if (ent->name) {
+	is_dot = (!strcmp(ent->name, ".")) || (!strcmp(ent->name, ".."));
+	free (ent->name);
+	ent->name = NULL;
+    }
+	
+    if (!is_dot && ent->ino) {
+	ent->ino->ent = NULL;
+	vfs_s_free_inode (me, ent->ino);
+	ent->ino = NULL;
     }
+
+    total_entries--;
+    free(ent);
 }
 
 void vfs_s_insert_entry (vfs *me, vfs_s_inode *dir, vfs_s_entry *ent)
@@ -103,7 +153,6 @@ struct stat *vfs_s_default_stat (vfs *me, mode_t mode)
     st.st_gid = getgid ();
     st.st_size = 0;
     st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
-    st.st_nlink = 1;
 
     return &st;
 }
@@ -115,12 +164,10 @@ void vfs_s_add_dots (vfs *me, vfs_s_inode *dir, vfs_s_inode *parent)
     if (!parent)
         parent = dir;
     dot = vfs_s_new_entry (me, ".", dir);
-    dot->ino = dir;
     dotdot = vfs_s_new_entry (me, "..", parent);
-    dot->ino = parent;
     vfs_s_insert_entry(me, dir, dot);
     vfs_s_insert_entry(me, dir, dotdot);
-    dir->st.st_nlink += 2;
+    dir->st.st_nlink--;	parent->st.st_nlink--; /* We do not count "." and ".." into nlinks */
 }
 
 struct vfs_s_entry *vfs_s_generate_entry (vfs *me, char *name, struct vfs_s_inode *parent, mode_t mode)
@@ -130,7 +177,7 @@ struct vfs_s_entry *vfs_s_generate_entry (vfs *me, char *name, struct vfs_s_inod
     struct stat *st;
 
     st = vfs_s_default_stat (me, mode);
-    inode = vfs_s_new_inode (me, st);
+    inode = vfs_s_new_inode (me, parent->super, st);
     if (S_ISDIR (mode))
         vfs_s_add_dots (me, inode, parent);
 
@@ -139,8 +186,7 @@ struct vfs_s_entry *vfs_s_generate_entry (vfs *me, char *name, struct vfs_s_inod
     return entry;
 }
 
-/* We were asked to create entries automagically
- */
+/* We were asked to create entries automagically */
 vfs_s_entry *vfs_s_automake(vfs *me, vfs_s_inode *dir, char *path, int flags)
 {
     struct vfs_s_entry *res;
@@ -153,9 +199,8 @@ vfs_s_entry *vfs_s_automake(vfs *me, vfs_s_inode *dir, char *path, int flags)
 }
 
 /* Follow > 0: follow links, serves as loop protect,
- *       == -1: do not follow links
- */
-vfs_s_entry *vfs_s_find_entry(vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
+ *       == -1: do not follow links */
+vfs_s_entry *vfs_s_find_entry_tree(vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
 {
     unsigned int pseg;
     vfs_s_entry* ent = NULL;
@@ -170,12 +215,12 @@ vfs_s_entry *vfs_s_find_entry(vfs *me, vfs_s_inode *root, char *path, int follow
 
 	found = 0;
 	for(ent = root->subdir; ent != NULL; ent = ent->next)
-	    if(strlen(ent->name) == pseg && strncmp(ent->name, path, pseg) == 0)
+	    if(strlen(ent->name) == pseg && (!strncmp(ent->name, path, pseg)))
 		/* FOUND! */
 		break;
 
 	if (!ent && (flags & (FL_MKFILE | FL_MKDIR)))
-	    ent = vfs_s_automake(me, root, path/*+pseg*/, flags);
+	    ent = vfs_s_automake(me, root, path, flags);
 	if (!ent) ERRNOR (ENOENT, NULL);
 	path += pseg;
 	if (!vfs_s_resolve_symlink(me, ent, follow)) return NULL;
@@ -183,12 +228,83 @@ vfs_s_entry *vfs_s_find_entry(vfs *me, vfs_s_inode *root, char *path, int follow
     }
 }
 
+static void split_dir_name(vfs *me, char *path, char **dir, char **name, char **save)
+{
+    char *s;
+    s = strrchr(path, DIR_SEP_CHAR);
+    if (!s) {
+	*save = NULL;
+	*name = path;
+	*dir = "";
+    } else {
+	*save = s;
+	*dir = path;
+	*s++ = 0;
+	if (!*s)	/* This can happen if someone does stat("/"); */
+	    *name = "";
+	else
+	    *name = s;
+    }
+}
+
+vfs_s_entry *vfs_s_find_entry_linear(vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
+{
+    char *s;
+    vfs_s_entry* ent = NULL;
+
+    if (!(flags & FL_DIR)) {
+	char *dirname, *name, *save;
+	vfs_s_inode *ino;
+	vfs_s_entry *ent;
+	split_dir_name(me, path, &dirname, &name, &save);
+	ino = vfs_s_find_inode(me, root, dirname, follow, flags | FL_DIR);
+	if (save)
+	    *save = DIR_SEP_CHAR;
+	ent = vfs_s_find_entry_tree(me, ino, name, follow, flags);
+	return ent;
+    }
+
+    for(ent = root->subdir; ent != NULL; ent = ent->next)
+	if (!strcmp(ent->name, path))
+	    break;
+
+    if (ent && (! (MEDATA->dir_uptodate) (me, ent->ino))) {
+#if 0
+	message_1s( 1, "Dir cache expired for", path);
+#endif
+	vfs_s_free_entry (me, ent);
+    }
+
+    if (!ent) {
+	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 ((MEDATA->dir_load) (me, ino, path) == -1) {
+	    vfs_s_free_entry(me, ent);
+	    return NULL;
+	}
+	vfs_s_insert_entry(me, root, ent);
+
+	for(ent = root->subdir; ent != NULL; ent = ent->next)
+	    if (!strcmp(ent->name, path))
+		break;
+    }
+    if (!ent) 
+	vfs_die("find_linear: success but directory is not there\n");
+
+#if 0
+    if (!vfs_s_resolve_symlink(me, ent, follow)) return NULL;
+#endif
+    return ent;
+}
+
 vfs_s_inode *vfs_s_find_inode(vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
 {
     vfs_s_entry *ent;
-    if (!path || !path[0])
+    if ((MEDATA->find_entry == vfs_s_find_entry_tree) && (!*path))
 	return root;
-    ent = vfs_s_find_entry(me, root, path, follow, flags);
+    ent = (MEDATA->find_entry)(me, root, path, follow, flags);
     if (!ent)
 	return NULL;
     return ent->ino;
@@ -213,10 +329,11 @@ vfs_s_entry *vfs_s_resolve_symlink (vfs *me, vfs_s_entry *entry, int follow)
 	return entry;
 
     /* We have to handle "/" by ourself; "." and ".." have
-       corresponding entries, so there's no problem with them. */
+       corresponding entries, so there's no problem with them.
+       FIXME: no longer true */
     if (*entry->ino->linkname == '/') dir = vfs_s_find_root(me, entry); 
     else dir = entry->dir;
-    return vfs_s_find_entry (me, dir, entry->ino->linkname, follow-1, 0);
+    return (MEDATA->find_entry) (me, dir, entry->ino->linkname, follow-1, 0);
 }
 
 /* Ook, these were functions around direcory entries / inodes */
@@ -227,11 +344,15 @@ vfs_s_super *vfs_s_new_super (vfs *me)
     vfs_s_super *super;
 
     super = xmalloc( sizeof( struct vfs_s_super ), "Direntry: superblock" );
+    bzero(super, sizeof(struct vfs_s_super));
     super->root = NULL;
     super->name = NULL;
     super->fd_usage = 0;
     super->me = me;
+}
 
+void vfs_s_insert_super (vfs *me, vfs_s_super *super)
+{
     super->next = MEDATA->supers;
     super->prevp = &MEDATA->supers;
 
@@ -242,17 +363,29 @@ vfs_s_super *vfs_s_new_super (vfs *me)
 
 void vfs_s_free_super (vfs *me, vfs_s_super *super)
 {
-    if (super->root)
-	vfs_s_free_tree (me, super->root, NULL);
+    if (super->root) {
+	vfs_s_free_inode (me, super->root);
+	super->root = NULL;
+    }
 
-    *super->prevp = super->next;
-    if (super->next) super->next->prevp = super->prevp;
+#if 0
+    /* We currently leak small ammount of memory, sometimes. Fix it if you can. */
+    if (super->fd_usage)
+	message_1s1d (1, " Direntry warning ", "Super fd_usage is %d, memory leak", super->fd_usage);
+#endif
+
+    if (super->prevp) {
+	*super->prevp = super->next;
+	if (super->next) super->next->prevp = super->prevp;
+    }
 
     CALL(free_archive) (me, super);
+    ifree(super->name);
+    super->name = NULL;
     free(super);
 }
 
-/* ========================================================================= */
+/* ------------------------------------------------------------------------= */
 
 static void vfs_s_stamp_me (vfs *me, struct vfs_s_super *psup, char *fs_name)
 {
@@ -276,7 +409,7 @@ char *vfs_s_get_path_mangle (vfs *me, char *inname, struct vfs_s_super **archive
 {
     char *local, *op, *archive_name;
     int result = -1;
-    struct vfs_s_super *psup;
+    struct vfs_s_super *super;
     void *cookie;
     
     archive_name = inname;
@@ -284,12 +417,13 @@ char *vfs_s_get_path_mangle (vfs *me, char *inname, struct vfs_s_super **archive
     if (!local)
         local = "";
 
-    if (! (cookie = MEDATA->archive_check (me, archive_name, op)))
-	return NULL;
+    if (MEDATA->archive_check)
+	if (! (cookie = MEDATA->archive_check (me, archive_name, op)))
+	    return NULL;
 
-    for (psup = MEDATA->supers; psup != NULL; psup = psup->next) {
+    for (super = MEDATA->supers; super != NULL; super = super->next) {
 	int i; /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
-	if ((i = MEDATA->archive_same (me, psup, archive_name, cookie))) {
+	if ((i = MEDATA->archive_same (me, super, archive_name, op, cookie))) {
 	    if (i==1) goto return_success;
 	    else break;
 	}
@@ -297,17 +431,22 @@ char *vfs_s_get_path_mangle (vfs *me, char *inname, struct vfs_s_super **archive
 
     if (flags & FL_NO_OPEN) ERRNOR (EIO, NULL);
 
-    psup = vfs_s_new_super (me);
-    result = MEDATA->open_archive (me, psup, archive_name, op);
+    super = vfs_s_new_super (me);
+    result = MEDATA->open_archive (me, super, archive_name, op);
     if (result == -1) {
-	vfs_s_free_super (me, psup);
+	vfs_s_free_super (me, super);
 	ERRNOR (EIO, NULL);
     }
+    if (!super->name)
+	vfs_die( "You have to fill name\n" );
+    if (!super->root)
+	vfs_die( "You have to fill root inode\n" );
 
-    vfs_s_stamp_me (me, psup, archive_name);
+    vfs_s_insert_super (me, super);
+    vfs_s_stamp_me (me, super, archive_name);
 
 return_success:
-    *archive = psup;
+    *archive = super;
     return local;
 }
 
@@ -322,41 +461,73 @@ char *vfs_s_get_path (vfs *me, char *inname, struct vfs_s_super **archive, int f
     return res2;
 }
 
+void vfs_s_invalidate (vfs *me, 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 (vfs *me, vfs_s_inode *ino)
+{	/* For now, usable only on filesystems with _linear structure */
+    if (MEDATA->find_entry != vfs_s_find_entry_linear)
+	vfs_die( "Implement me!" );
+    if ((!ino->ent) || (!ino->ent->dir) || (!ino->ent->dir->ent))
+	ERRNOR(EAGAIN, NULL);
+    return copy_strings( ino->ent->dir->ent->name, "/", ino->ent->name, NULL );
+}
+
 /* Support of archives */
-/* ========================== readdir & friends ========================================= */
+/* ------------------------ readdir & friends ----------------------------- */
+
+vfs_s_super *vfs_s_super_from_path (vfs *me, char *name)
+{
+    struct vfs_s_super *super;
+
+    if (!vfs_s_get_path_mangle (me, name, &super, 0))
+	return NULL;
+    return super;
+}
 
 vfs_s_inode *vfs_s_inode_from_path (vfs *me, char *name, int flags)
 {
     struct vfs_s_super *super;
-    char *q;
     struct vfs_s_inode *ino;
+    char *q;
 
     if (!(q = vfs_s_get_path_mangle (me, name, &super, 0))) 
 	return NULL;
 
-    ino = vfs_s_find_inode (me, super->root, q, flags & FL_FOLLOW ? FOLLOW : NO_FOLLOW, FL_NONE);
+    ino = vfs_s_find_inode (me, super->root, q, flags & FL_FOLLOW ? FOLLOW : NO_FOLLOW, flags & ~FL_FOLLOW);
     if (ino) return ino;
 
-    if ((flags & FL_DIR) && MEDATA->load_dir) {
-	MEDATA->load_dir (me, super, q);
-	ino = vfs_s_find_inode (me, super->root, q, flags & FL_FOLLOW ? FOLLOW : NO_FOLLOW, FL_NONE);
-    }
-
     return ino;
+
 }
 
+struct dirhandle {
+    vfs_s_entry *cur;
+    vfs_s_inode *dir;
+};
+
 void * vfs_s_opendir (vfs *me, char *dirname)
 {
-    struct vfs_s_inode *ino;
-    struct vfs_s_entry **info;
+    struct vfs_s_inode *dir;
+    struct dirhandle *info;
 
-    ino = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
-    if (!ino) return NULL;
-    if (!S_ISDIR (ino->st.st_mode)) ERRNOR (ENOTDIR, NULL);
+    dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
+    if (!dir) return NULL;
+    if (!S_ISDIR (dir->st.st_mode)) ERRNOR (ENOTDIR, NULL);
 
-    info = (struct vfs_s_entry **) xmalloc (2*sizeof (struct vfs_s_entry *), "Dentry opendir");
-    info[0] = ino->subdir;
-    info[1] = ino->subdir;
+    dir->st.st_nlink++;
+#if 0
+    if (!dir->subdir)	/* This can actually happen if we allow empty directories */
+	ERRNOR(EAGAIN, NULL);
+#endif
+    info = (struct dirhandle *) xmalloc (sizeof (struct dirhandle), "Shared opendir");
+    info->cur = dir->subdir;
+    info->dir = dir;
 
     return info;
 }
@@ -370,30 +541,33 @@ void * vfs_s_readdir (void *data)
 #endif
     } dir;
 
-    struct vfs_s_entry **info = (struct vfs_s_entry **) data;
+    struct dirhandle *info = (struct dirhandle *) data;
 
-    if (!*info)
+    if (!(info->cur))
     	return NULL;
 
-    strcpy (&(dir.dir.d_name [0]), (*info)->name);
+    if (info->cur->name)
+	strcpy (&(dir.dir.d_name [0]), info->cur->name);
+    else
+	vfs_die( "Null in structure-can not happen");
     
 #ifndef DIRENT_LENGTH_COMPUTED
     dir.d_namlen = strlen (dir.dir.d_name);
 #endif
-    *info = (*info)->next;
+    info->cur = info->cur->next;
     
     return (void *)&dir;
 }
 
 int vfs_s_telldir (void *data)
 {
-    struct vfs_s_entry **info = (struct vfs_s_entry **) data;
+    struct dirhandle *info = (struct dirhandle *) data;
     struct vfs_s_entry *cur;
     int num = 0;
 
-    cur = info[1];
+    cur = info->dir->subdir;
     while (cur!=NULL) {
-        if (cur == info[0]) return num;
+        if (cur == info->cur) return num;
 	num++;
 	cur = cur->next;
     }
@@ -402,15 +576,19 @@ int vfs_s_telldir (void *data)
 
 void vfs_s_seekdir (void *data, int offset)
 {
-    struct vfs_s_entry **info = (struct vfs_s_entry **) data;
+    struct dirhandle *info = (struct dirhandle *) data;
     int i;
-    info[0] = info[1];
+    info->cur = info->dir->subdir;
     for (i=0; i<offset; i++)
         vfs_s_readdir( data );
 }
 
 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);
     free (data);
     return 0;
 }
@@ -424,7 +602,7 @@ int vfs_s_chdir (vfs *me, char *path)
     return 0;
 }
 
-/* =========================== stat and friends ============================== */
+/* --------------------------- stat and friends ---------------------------- */
 
 static int vfs_s_internal_stat (vfs *me, char *path, struct stat *buf, int flag)
 {
@@ -466,6 +644,7 @@ int vfs_s_readlink (vfs *me, char *path, char *buf, int size)
 
 void *vfs_s_open (vfs *me, char *file, int flags, int mode)
 {
+    int was_changed = 0;
     struct vfs_s_fh *fh;
     vfs_s_super *super;
     char *q;
@@ -473,31 +652,111 @@ void *vfs_s_open (vfs *me, char *file, int flags, int mode)
 
     if ((q = vfs_s_get_path_mangle (me, file, &super, 0)) == NULL)
 	return NULL;
-    if (!(ino = vfs_s_find_inode (me, super->root, q, FOLLOW, FL_NONE)))
-    	return NULL;
-    if (S_ISDIR (ino->st.st_mode)) ERRNOR (EISDIR, NULL);
-    if ((flags & O_ACCMODE) != O_RDONLY) ERRNOR (EROFS, NULL);
+    ino = vfs_s_find_inode (me, super->root, q, FOLLOW, FL_NONE);
+    if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
+	ERRNOR (EEXIST, NULL);
+    if (!ino) { 
+	char *dirname, *name, *save;
+	vfs_s_entry *ent;
+	vfs_s_inode *dir;
+	if (!(flags & O_CREAT))
+	    return NULL;
+
+	split_dir_name(me, q, &dirname, &name, &save);
+	dir = vfs_s_find_inode(me, super->root, dirname, FOLLOW, FL_DIR);
+	if (save)
+	    *save = DIR_SEP_CHAR;
+	ent = vfs_s_generate_entry (me, name, dir, 0755);
+	ino = ent->ino;
+	vfs_s_insert_entry(me, dir, ent);
+	ino->localname = tempnam (NULL, me->name);
+	was_changed = 1;
+    }
+
+    if (ino && S_ISDIR (ino->st.st_mode)) ERRNOR (EISDIR, NULL);
     
     fh = (struct vfs_s_fh *) xmalloc (sizeof (struct vfs_s_fh), "Direntry: filehandle");
-    fh->super = super;
     fh->pos = 0;
     fh->ino = ino;
     fh->handle = -1;
+    fh->changed = was_changed;
+    fh->linear = 0;
     if (MEDATA->fh_open)
-	if (MEDATA->fh_open (me, fh)) {
+	if (MEDATA->fh_open (me, fh, flags, mode)) {
 	    free(fh);
 	    return NULL;
 	}
 
+    if (fh->ino->localname) {
+	fh->handle = open(fh->ino->localname, flags, mode);
+	if (fh->handle == -1) {
+	    free(fh);
+	    ERRNOR(errno, NULL);
+	}
+    }
+
      /* i.e. we had no open files and now we have one */
     vfs_rmstamp (&vfs_tarfs_ops, (vfsid) super, 1);
     super->fd_usage++;
+    fh->ino->st.st_nlink++;
     return fh;
 }
 
+int vfs_s_read (void *fh, char *buffer, int count)
+{
+    int n;
+    vfs *me = FH_SUPER->me;
+    
+    if (FH->linear == LS_LINEAR_CLOSED) {
+        print_vfs_message ("Starting linear transfer...");
+	if (!MEDATA->linear_start (me, FH, 0))
+	    return -1;
+    }
+
+    if (FH->linear == LS_LINEAR_CLOSED)
+        vfs_die ("linear_start() did not set linear_state!");
+
+    if (FH->linear == LS_LINEAR_OPEN)
+        return MEDATA->linear_read (me, FH, buffer, count);
+        
+    if (FH->handle) {
+	n = read (FH->handle, buffer, count);
+	if (n < 0)
+	    me->verrno = errno;
+	return n;
+    }
+    vfs_die( "vfs_s_read: This should not happen\n" );
+}
+
+int vfs_s_write (void *fh, char *buffer, int count)
+{
+    int n;
+    vfs *me = FH_SUPER->me;
+    
+    if (FH->linear)
+	vfs_die ("no writing to linear files, please" );
+        
+    FH->changed = 1;
+    if (FH->handle) {
+	n = write (FH->handle, buffer, count);
+	if (n < 0)
+	    me->verrno = errno;
+	return n;
+    }
+    vfs_die( "vfs_s_write: This should not happen\n" );
+}
+
 int vfs_s_lseek (void *fh, off_t offset, int whence)
 {
     off_t size = FH->ino->st.st_size;
+
+    if (FH->handle) {	/* If we have local file opened, we want to work with it */
+	int retval = lseek(FH->handle, offset, whence);
+	if (retval == -1)
+	    FH->ino->super->me->verrno = errno;
+	return retval;
+    }
+
     switch (whence) {
     	case SEEK_CUR:
     	    offset += FH->pos; break;
@@ -515,31 +774,42 @@ int vfs_s_lseek (void *fh, off_t offset, int whence)
 
 int vfs_s_close (void *fh)
 {
-    vfs *me = FH->super->me;
+    int res = 0;
+    vfs *me = FH_SUPER->me;
 
-    FH->super->fd_usage--;
-    if (!FH->super->fd_usage) {
+    FH_SUPER->fd_usage--;
+    if (!FH_SUPER->fd_usage) {
         struct vfs_stamping *parent;
         vfs *v;
         
-	v = vfs_type (FH->super->name);
+	v = vfs_type (FH_SUPER->name);
 	if (v == &vfs_local_ops) {
 	    parent = NULL;
 	} else {
 	    parent = xmalloc (sizeof (struct vfs_stamping), "vfs stamping");
 	    parent->v = v;
 	    parent->next = 0;
-	    parent->id = (*v->getid) (v, FH->super->name, &(parent->parent));
+	    parent->id = (*v->getid) (v, FH_SUPER->name, &(parent->parent));
 	}
-        vfs_add_noncurrent_stamps (&vfs_tarfs_ops, (vfsid) (FH->super), parent);
+        vfs_add_noncurrent_stamps (&vfs_tarfs_ops, (vfsid) (FH_SUPER), parent);
 	vfs_rm_parents (parent);
     }
-    CALL(fh_close) (me, fh);
+    if (FH->linear == LS_LINEAR_OPEN)
+	MEDATA->linear_close (me, fh);
+    if (MEDATA->fh_close)
+	res = MEDATA->fh_close (me, fh);
+    if (FH->changed && MEDATA->file_store) {
+	char *s = vfs_s_fullpath( me, FH->ino );
+	if (!s) res = -1;
+	   else res = MEDATA->file_store (me, FH_SUPER, s, FH->ino->localname);
+	vfs_s_invalidate(me, FH_SUPER);
+    }
     if (FH->handle)
-	mc_close(FH->handle);
+	close(FH->handle);
 	
+    vfs_s_free_inode (me, FH->ino);
     free (fh);
-    return 0;
+    return res;
 }
 
 /* ------------------------------- mc support ---------------------------- */
@@ -599,7 +869,34 @@ char *vfs_s_getlocalcopy (vfs *me, char *path)
     return ino->localname;
 }
 
-/* ----------------------------- Stamping support ----------------------------- */
+int 
+vfs_s_setctl (vfs *me, char *path, int ctlop, char *arg)
+{
+    vfs_s_inode *ino = vfs_s_inode_from_path(me, path, 0);
+    if (!ino)
+	return 0;
+    switch (ctlop) {
+        case MCCTL_WANT_STALE_DATA:
+#warning Should se this to 1
+	    ino->super->want_stale = 0;
+	    return 1;
+        case MCCTL_NO_STALE_DATA:
+	    ino->super->want_stale = 0;
+	    vfs_s_invalidate(me, ino->super);
+	    return 1;
+#if 0	/* FIXME: We should implement these */
+	case MCCTL_REMOVELOCALCOPY:
+	    return remove_temp_file (path);
+        case MCCTL_FORGET_ABOUT:
+	    my_forget(path);
+	    return 0;
+#endif
+    }
+    return 0;
+}
+
+
+/* ----------------------------- Stamping support -------------------------- */
 
 vfsid vfs_s_getid (vfs *me, char *path, struct vfs_stamping **parent)
 {
@@ -637,3 +934,86 @@ void vfs_s_free (vfsid id)
 {
     vfs_s_free_super (((vfs_s_super *)id)->me, (vfs_s_super *)id);
 }
+
+/* ----------- Utility functions for networked filesystems  -------------- */
+
+int
+vfs_s_select_on_two (int fd1, int fd2)
+{
+    fd_set set;
+    struct timeval timeout;
+    int v;
+    int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
+
+    timeout.tv_sec  = 1;
+    timeout.tv_usec = 0;
+    FD_ZERO(&set);
+    FD_SET(fd1, &set);
+    FD_SET(fd2, &set);
+    v = select (maxfd, &set, 0, 0, &timeout);
+    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 (vfs *me, int sock, char *buf, int buf_len, char term)
+{
+    FILE *logfile = MEDATA->logfile;
+    int i, status;
+    char c;
+
+    for (i = 0; i < buf_len; i++, buf++) {
+	if (read(sock, buf, sizeof(char)) <= 0)
+	    return 0;
+	if (logfile){
+	    fwrite (buf, 1, 1, logfile);
+	    fflush (logfile);
+	}
+	if (*buf == term) {
+	    *buf = 0;
+	    return 1;
+	}
+    }
+    *buf = 0;
+    while ((status = read(sock, &c, sizeof(c))) > 0){
+	if (logfile){
+	    fwrite (&c, 1, 1, logfile);
+	    fflush (logfile);
+	}
+	if (c == '\n')
+	    return 1;
+    }
+    return 0;
+}
+
+int
+vfs_s_get_line_interruptible (vfs *me, char *buffer, int size, int fd)
+{
+    int n;
+    int i = 0;
+
+    enable_interrupt_key();
+    for (i = 0; i < size-1; i++) {
+	n = read (fd, buffer+i, 1);
+	disable_interrupt_key();
+	if (n == -1 && errno == EINTR){
+	    buffer [i] = 0;
+	    return EINTR;
+	}
+	if (n == 0){
+	    buffer [i] = 0;
+	    return 0;
+	}
+	if (buffer [i] == '\n'){
+	    buffer [i] = 0;
+	    return 1;
+	}
+    }
+    buffer [size-1] = 0;
+    return 0;
+}

Some files were not shown because too many files changed in this diff