9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: rog@vitanuova.com
To: 9fans@cse.psu.edu
Subject: Re: [9fans] ls, rc question -- proposed change to rc/glob.c
Date: Sun, 21 Mar 2004 20:47:46 +0000	[thread overview]
Message-ID: <393a7f63e7c4973e661988f1009f5d7a@vitanuova.com> (raw)
In-Reply-To: <240892daccaec2131635fbd4917479de@hamnavoe.com>

[-- Attachment #1: Type: text/plain, Size: 3518 bytes --]

for programs that require read-write access to a namespace,
requirements are different, as charles points out.

however, most tools do not write to the namespace.

for me, one of plan 9's fundamental strengths is that while a tool is
often written to access a predefined portion of the namespace, the
actual namespace that it sees can be arranged at will for the tool in
question.

thus if a program accesses files in directory /d, i think it's
reasonable to expect that if i bind another directory, say /b, before
/d, i can expect the program to treat /d exactly as if i had really
replaced the files therein with files from /b.

the program shouldn't have to *care* whether it's dealing with a union
directory or not - that should be solely the concern of whoever's
arranging the namespace.

i think the difference between:

	bind /dev/null /bin/ls

and

	mkdir /tmp/x
	> /tmp/x/ls
	bind /dev/null /tmp/x/ls
	bind -b /tmp/x /bin

should be invisible, particularly to shell scripts, which have no
concept of sequentially reading a directory, and which are often short
and trivial, but for which the above invariants are still important.

russ:
> if a file is covered up, it should be completely hidden.
> it turns out that actually implementing this is quite hard,

i.e.  the visibility of mutiple names in a directory is an
implementation issue.  it's something that's easily solved at user
level (no problems with arbitrary buffering there, and programs can
eliminate duplicates in their own way if they like as, for example,
du(1), mk(1), cron(1) listen(1) do).

i think it should be made as easy as possible to write programs that
behave in a namespace-agnostic way.  i think that if a version of
dirreadall was available that eliminated duplicates, most (all?)
programs would wish to use it.

i think it's particularly silly that

	wc */*.[ch]

should give me erroneous results in a union directory,
and that

	echo /bin/ape/*

should be different from

	echo /bin/ape*/*

when there's only one accessible directory starting "ape" in /bin, and
the shell's sorting the globbed output anyway.

i think eliminating duplicates in two core places would break nothing,
but make parts of the system work more smoothly together.

it's one less thing to think about.

geoff points out many programs that use dirread.  a random inspection
turns up many programs that already do their own elimination of
duplicates (in different ways), and others that behave badly in the
face of duplicate directory entries (e.g.  diff).

this seems to me to point towards a systematic problem, which should
be solved centrally.

towards this end, i've attached a stable sort routine, along the same
lines as qsort(2), which can optionally eliminate duplicates.  i've
tested it to a degree, but not greatly; i haven't done any performance
analysis of it, but i doubt that's a limiting factor in this case.

i envisage ls(1) being changed to use this routine (with an option to
show/suppress duplicate entries), and perhaps a version of dirreadall
that sorts the directory entries alphabetically and eliminates
duplicates, to make it easy to replace it in existing programs.


PS it's interesting to note that ls(1) does make some sort of an
effort to do a stable sort:

	if(i == 0)
		i = (a<b? -1 : 1);

but it's simple minded (in fact, i'm a little bit surprised it doesn't
kill qsort).  a sequence field in the NDir structure could fix it.

[-- Attachment #2: mergesort.c --]
[-- Type: text/plain, Size: 1025 bytes --]

#include <u.h>
#include <libc.h>
	
int
mergesort1(int uniq, char *a, int r, int width, int (*compare)(void*, void*), char *b)
{
	int m, n0, n1, i, j, k, e, c;
	if(r <= width)
		return r;
	m = ((r/width-1)/2 + 1) * width;
	n0 = mergesort1(uniq, a, m, width, compare, b);
	n1 = mergesort1(uniq, a+m, r-m, width, compare, b);
	memcpy(b, a, n0);
	memcpy(b+n0, a+m, n1);
	e = n0+n1;
	i = 0;
	j = n0;
	k = 0;
	for(; i < n0 && j < e; k += width){
		c = (*compare)(b+i, b+j);
		if(c > 0){
			memcpy(a+k, b+j, width);
			j += width;
		}else{
			memcpy(a+k, b+i, width);
			i += width;
			if(c == 0 && uniq)
				j += width;
		}
	}
	if(i < n0){
		memcpy(a+k, b+i, n0-i);
		k += n0-i;
	}else if(j < e){
		memcpy(a+k, b+j, e-j);
		k += e-j;
	}
	return k;
}

int
mergesort(int uniq, void *a, long n, long width, int (*compare)(void*, void*))
{
	void *b;
	n *= width;
	b = malloc(n);
	if(b == nil)
		return -1;
	n = mergesort1(uniq, a, n, width, compare, b);
	free(b);
	return n / width;
}

  parent reply	other threads:[~2004-03-21 20:47 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-18 21:40 [9fans] ls question David Tolpin
2004-03-18 21:58 ` Russ Cox
2004-03-18 22:05   ` Russ Cox
2004-03-18 23:00     ` [9fans] ls, rc question David Tolpin
2004-03-18 23:31       ` [9fans] dirread David Tolpin
2004-03-18 23:49         ` ron minnich
2004-03-19  0:14         ` boyd, rounin
2004-03-19  3:38         ` rsc
2004-03-19  3:41       ` [9fans] ls, rc question rsc
2004-03-19  5:32         ` David Tolpin
2004-03-19  5:45           ` boyd, rounin
2004-03-19  5:50           ` ron minnich
2004-03-19  6:45             ` boyd, rounin
2004-03-19  9:07             ` Charles Forsyth
2004-03-19  9:24               ` Richard Miller
2004-03-19  9:33                 ` boyd, rounin
2004-03-19  9:39                   ` Richard Miller
2004-03-19  9:46                     ` Geoff Collyer
2004-03-19 10:11                   ` Richard Miller
2004-03-19 10:42                     ` Charles Forsyth
2004-03-19 10:03                 ` Charles Forsyth
2004-03-19  9:38               ` [9fans] Bind, look, everything is duplicated David Tolpin
2004-03-19  7:01           ` [9fans] ls, rc question Micah Stetson
2004-03-19  7:57             ` [9fans] ls, rc question -- proposed change to rc/glob.c David Tolpin
2004-03-19  8:13               ` Rob Pike
2004-03-19  8:18                 ` David Tolpin
2004-03-19  8:24                   ` David Tolpin
2004-03-19  8:27                   ` Rob Pike
2004-03-19  8:52                     ` David Tolpin
2004-03-19  9:16                     ` Richard Miller
2004-03-19  9:29                       ` boyd, rounin
2004-03-19  9:41                       ` Geoff Collyer
2004-03-19 10:09                         ` boyd, rounin
2004-03-19 10:50                         ` Geoff Collyer
2004-03-19 11:12                           ` David Tolpin
2004-03-19 12:31                             ` Charles Forsyth
2004-03-19 12:53                               ` boyd, rounin
2004-03-19 13:59                             ` David Presotto
2004-03-19 14:44                               ` David Tolpin
2004-03-19 17:57                                 ` Russ Cox
2004-03-19 18:04                                   ` David Tolpin
2004-03-19 20:31                               ` Geoff Collyer
2004-03-22 22:56                           ` rog
2004-03-22 23:19                             ` Scott Schwartz
2004-03-22 23:50                             ` Charles Forsyth
2004-03-23  0:28                               ` rog
2004-03-23  0:40                                 ` Charles Forsyth
2004-03-23  0:49                             ` Charles Forsyth
2004-03-23  2:12                               ` ron minnich
2004-03-23  2:16                                 ` boyd, rounin
2004-03-23  3:15                                 ` rog
2004-03-23 11:13                             ` a
2004-03-23 11:47                               ` Geoff Collyer
2004-03-19  8:31                 ` Richard Miller
2004-03-19  8:47                   ` Geoff Collyer
2004-03-19  9:07                   ` Rob Pike
2004-03-19  9:34                     ` David Tolpin
2004-03-19  9:52                     ` Scott Schwartz
2004-03-19 14:42                       ` ron minnich
2004-03-19 16:18                         ` 9nut
2004-03-19 15:34                           ` david presotto
2004-03-19 15:43                             ` ron minnich
2004-03-19 16:00                               ` Charles Forsyth
2004-03-19 16:02                             ` Charles Forsyth
2004-03-19 16:23                               ` David Presotto
2004-03-19 16:34                             ` Richard Miller
2004-03-19 16:47                               ` a
2004-03-19 16:52                                 ` Richard Miller
2004-03-19 17:15                               ` David Presotto
2004-03-21 20:47                               ` rog [this message]
2004-03-21 20:50                                 ` boyd, rounin
2004-03-21 21:53                                 ` ron minnich
2004-03-21 22:05                                   ` Charles Forsyth
2004-03-21 23:29                                   ` Enache Adrian
2004-03-22  1:30                                     ` boyd, rounin
2004-03-22 10:09                                 ` Douglas A. Gwyn
2004-03-22 10:49                                   ` Charles Forsyth
2004-03-22 12:15                                     ` boyd, rounin
2004-03-22 18:23                                       ` Derek Fawcus
2004-03-23  0:06                                         ` boyd, rounin
2004-03-19 16:34                             ` a
2004-03-19 15:53                           ` lucio
2004-03-19 16:01                             ` Charles Forsyth
2004-03-19 16:08                             ` andrey mirtchovski
2004-03-19 16:12                               ` ron minnich
2004-03-19 16:22                               ` lucio
2004-03-19 19:42                         ` boyd, rounin
2004-03-19 14:13                     ` Russ Cox
2004-03-19 14:37                       ` David Tolpin
2004-03-19  8:35                 ` boyd, rounin
2004-03-19 14:19                 ` Russ Cox
2004-03-18 21:59 ` [9fans] ls question David Presotto
2004-03-18 22:05 ` matt
2004-03-18 22:04   ` David Tolpin
2004-03-18 22:08   ` boyd, rounin
2004-03-19 15:59 [9fans] ls, rc question -- proposed change to rc/glob.c Tiit Lankots

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=393a7f63e7c4973e661988f1009f5d7a@vitanuova.com \
    --to=rog@vitanuova.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).