public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] [OT] Why directories aren't files?
@ 2006-09-18  9:10 Matteo Pillon
  2006-09-18  9:42 ` Roman Zilka
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Matteo Pillon @ 2006-09-18  9:10 UTC (permalink / raw
  To: gentoo-user

Hi all,

I was wondering why Linux doesn't treat directories like files, as many
other unix implementations do.
For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
Why? There is a practical reason?

Forgive me this OT, I wasn't able to find a suitable list.

Thanks for replies.
Bye.

-- 
 * Pillon Matteo
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18  9:10 [gentoo-user] [OT] Why directories aren't files? Matteo Pillon
@ 2006-09-18  9:42 ` Roman Zilka
  2006-09-18 11:37   ` Matteo Pillon
  2006-09-18  9:59 ` Dirk Heinrichs
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 14+ messages in thread
From: Roman Zilka @ 2006-09-18  9:42 UTC (permalink / raw
  To: gentoo-user

> I was wondering why Linux doesn't treat directories like files, as many
> other unix implementations do.
> For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> Why? There is a practical reason?

I'd say it's not a matter of how Linux treats directories
(putting aside the problem of diverse filesystems), but how
coreutils or "cat", to be precise, treats directories. You could just as
well implement such a feature into 'cat' which would make it behave like
it does on FreeBSD when called on a directory. As to why Linux's "cat"
acts the way it does...try asking GNU guys.:)

Btw, in my place:
$ uname -a
FreeBSD howdy123 6.1-PRERELEASE FreeBSD 6.1-PRERELEASE #0: Wed Apr  5
12:22:42 CEST 2006     root@howdy123:/usr/obj/usr/src/sys/GORGO  i386
$ cat .
cat: .: Is a directory
$

...which is exactly the same behavior as on my Gentoo.

-rz
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18  9:10 [gentoo-user] [OT] Why directories aren't files? Matteo Pillon
  2006-09-18  9:42 ` Roman Zilka
@ 2006-09-18  9:59 ` Dirk Heinrichs
  2006-09-18 14:30 ` Hans-Werner Hilse
  2006-09-18 16:25 ` Ryan Tandy
  3 siblings, 0 replies; 14+ messages in thread
From: Dirk Heinrichs @ 2006-09-18  9:59 UTC (permalink / raw
  To: gentoo-user

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

Am Montag, 18. September 2006 11:10 schrieb ext Matteo Pillon:

> I was wondering why Linux doesn't treat directories like files, as many
> other unix implementations do.

It's not Linux, but the applications.

> For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> Why? There is a practical reason?

Because one is GNU cat and one is BSD cat. "less ." and "vi ." work just 
fine, while "more ." doesn't (even though more is less on Linux).

Bye...

	Dirk
-- 
Dirk Heinrichs          | Tel:  +49 (0)162 234 3408
Configuration Manager   | Fax:  +49 (0)211 47068 111
Capgemini Deutschland   | Mail: dirk.heinrichs@capgemini.com
Hambornerstraße 55      | Web:  http://www.capgemini.com
D-40472 Düsseldorf      | ICQ#: 110037733
GPG Public Key C2E467BB | Keyserver: www.keyserver.net

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18  9:42 ` Roman Zilka
@ 2006-09-18 11:37   ` Matteo Pillon
  2006-09-18 13:04     ` Roman Zilka
  0 siblings, 1 reply; 14+ messages in thread
From: Matteo Pillon @ 2006-09-18 11:37 UTC (permalink / raw
  To: gentoo-user

On Mon, Sep 18, 2006 at 11:42:29AM +0200, Roman Zilka wrote:
> > I was wondering why Linux doesn't treat directories like files, as many
> > other unix implementations do.
> > For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> > Why? There is a practical reason?
> 
> I'd say it's not a matter of how Linux treats directories
> (putting aside the problem of diverse filesystems), but how
> coreutils or "cat", to be precise, treats directories. You could just as
> well implement such a feature into 'cat' which would make it behave like
> it does on FreeBSD when called on a directory. As to why Linux's "cat"
> acts the way it does...try asking GNU guys.:)

I was not talking about cat itself, but about open() and read().

> Btw, in my place:
> $ uname -a
> FreeBSD howdy123 6.1-PRERELEASE FreeBSD 6.1-PRERELEASE #0: Wed Apr  5
> 12:22:42 CEST 2006     root@howdy123:/usr/obj/usr/src/sys/GORGO  i386
> $ cat .
> cat: .: Is a directory
> $

Strange, my FreeBSD box, things works differently...
I was asking because, in FreeBSD I can do this bit of magic, but in
Linux not (not really useful, just proving that in Linux, not everything
is a file ;-).

pmatthew@watson:~/ $ uname -a
FreeBSD watson.octopus 6.1-STABLE FreeBSD 6.1-STABLE #14: Wed Aug 23 14:47:09 CEST 2006     root@watson.octopus:/usr/obj/usr/src/sys/WATSON  i386
pmatthew@watson:~ $ mkdir prova
pmatthew@watson:~ $ od -c prova/
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  364 001 004 002   .   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
pmatthew@watson:~ $ cd prova/
pmatthew@watson:~/prova $ touch file
pmatthew@watson:~/prova $ touch file2
pmatthew@watson:~/prova $ od -c .
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020   \f  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
pmatthew@watson:~/prova $ rm file
pmatthew@watson:~/prova $ od -c .
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  034  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
pmatthew@watson:~/prova $ ls
file2

And it's not caused by special code in od, it's using simple open()
and read() calls, as proved by this simple C code:
pmatthew@watson:~ $ cat mycat.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
  int fd;
  char buffer;

  fd=open(argv[1], O_RDONLY);
  while (read(fd, &buffer, 1) == 1)
    printf("%c", buffer);

  exit(EXIT_SUCCESS);
}
pmatthew@watson:~ $ make mycat
cc -O2 -pipe -march=pentium4  mycat.c  -o mycat
pmatthew@watson:~ $ ./mycat prova | od -c
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  034  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000


Bye.


-- 
 * Pillon Matteo
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 11:37   ` Matteo Pillon
@ 2006-09-18 13:04     ` Roman Zilka
  2006-09-18 13:12       ` Dirk Heinrichs
  0 siblings, 1 reply; 14+ messages in thread
From: Roman Zilka @ 2006-09-18 13:04 UTC (permalink / raw
  To: gentoo-user

Hm, this is all pretty weird. I cut'n'pasted and compiled your piece
of code and again got the same results under Linux and FreeBSD: no
output at all. I don't know if some local FreeBSD admin hacked/patched
the kernel source to make its syscalls behave Linux-alike, but it's very
unlikely.
According to the outputs I get on the OS's I have available here,
there's no difference between Linux and FreeBSD so far.

-rz
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 13:04     ` Roman Zilka
@ 2006-09-18 13:12       ` Dirk Heinrichs
  2006-09-18 14:51         ` Roman Zilka
  0 siblings, 1 reply; 14+ messages in thread
From: Dirk Heinrichs @ 2006-09-18 13:12 UTC (permalink / raw
  To: gentoo-user

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

Am Montag, 18. September 2006 15:04 schrieb ext Roman Zilka:
> Hm, this is all pretty weird. I cut'n'pasted and compiled your piece
> of code and again got the same results under Linux and FreeBSD: no
> output at all. I don't know if some local FreeBSD admin hacked/patched
> the kernel source to make its syscalls behave Linux-alike, but it's very
> unlikely.

Hmm, maybe OP is $LD_PRELOADing something?

Bye...

	Dirk
-- 
Dirk Heinrichs          | Tel:  +49 (0)162 234 3408
Configuration Manager   | Fax:  +49 (0)211 47068 111
Capgemini Deutschland   | Mail: dirk.heinrichs@capgemini.com
Hambornerstraße 55      | Web:  http://www.capgemini.com
D-40472 Düsseldorf      | ICQ#: 110037733
GPG Public Key C2E467BB | Keyserver: www.keyserver.net

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18  9:10 [gentoo-user] [OT] Why directories aren't files? Matteo Pillon
  2006-09-18  9:42 ` Roman Zilka
  2006-09-18  9:59 ` Dirk Heinrichs
@ 2006-09-18 14:30 ` Hans-Werner Hilse
  2006-09-18 15:13   ` alain.didierjean
                     ` (2 more replies)
  2006-09-18 16:25 ` Ryan Tandy
  3 siblings, 3 replies; 14+ messages in thread
From: Hans-Werner Hilse @ 2006-09-18 14:30 UTC (permalink / raw
  To: gentoo-user

Hi,

On Mon, 18 Sep 2006 11:10:57 +0200 Matteo Pillon
<matteo.pillon@gmail.com> wrote:

> I was wondering why Linux doesn't treat directories like files, as
> many other unix implementations do.

Pragmatic answer:

because nobody implemented it for most filesystems. Most filesystems
just define "generic_read_dir" as handling function for "readdir".
"generic_read_dir" always returns -EISDIR.

(see /usr/src/linux/fs/libfs.c and /usr/src/linux/fs/*/dir.c)

> For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> Why? There is a practical reason?

Well, I think it would be just another unstable API that clueless
programmers would get trapped by. What would be the benefit of being
able to open it?

> Forgive me this OT, I wasn't able to find a suitable list.

That would be the LKML :-)

-hwh
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 13:12       ` Dirk Heinrichs
@ 2006-09-18 14:51         ` Roman Zilka
  2006-09-19  5:45           ` Dirk Heinrichs
  0 siblings, 1 reply; 14+ messages in thread
From: Roman Zilka @ 2006-09-18 14:51 UTC (permalink / raw
  To: gentoo-user

> > Hm, this is all pretty weird. I cut'n'pasted and compiled your piece
> > of code and again got the same results under Linux and FreeBSD: no
> > output at all. I don't know if some local FreeBSD admin hacked/patched
> > the kernel source to make its syscalls behave Linux-alike, but it's very
> > unlikely.
> 
> Hmm, maybe OP is $LD_PRELOADing something?

My LD_PRELOAD is unset. I'm not sure if I understand "OP" correctly -
if that sudo-like utility is what you mean, then I can't help you here (but
in that case, you probably didn't want any answer I guess:)... I'm shutting
up now).

Happy hacking
-rz
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 14:30 ` Hans-Werner Hilse
@ 2006-09-18 15:13   ` alain.didierjean
  2006-09-18 16:11     ` Hans-Werner Hilse
  2006-09-18 15:54   ` Matteo Pillon
  2006-09-18 16:23   ` Hans-Werner Hilse
  2 siblings, 1 reply; 14+ messages in thread
From: alain.didierjean @ 2006-09-18 15:13 UTC (permalink / raw
  To: gentoo-user

Selon Hans-Werner Hilse <hilse@web.de>:

> Hi,
>
> On Mon, 18 Sep 2006 11:10:57 +0200 Matteo Pillon
> <matteo.pillon@gmail.com> wrote:
>
> > I was wondering why Linux doesn't treat directories like files, as
> > many other unix implementations do.
>
> Pragmatic answer:
>
> because nobody implemented it for most filesystems. Most filesystems
> just define "generic_read_dir" as handling function for "readdir".
> "generic_read_dir" always returns -EISDIR.
>
> (see /usr/src/linux/fs/libfs.c and /usr/src/linux/fs/*/dir.c)
>
> > For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> > Why? There is a practical reason?
>
>
Try vim . or, better view .

--
~adj~


-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 14:30 ` Hans-Werner Hilse
  2006-09-18 15:13   ` alain.didierjean
@ 2006-09-18 15:54   ` Matteo Pillon
  2006-09-18 16:23   ` Hans-Werner Hilse
  2 siblings, 0 replies; 14+ messages in thread
From: Matteo Pillon @ 2006-09-18 15:54 UTC (permalink / raw
  To: gentoo-user

On Mon, Sep 18, 2006 at 04:30:52PM +0200, Hans-Werner Hilse wrote:
> On Mon, 18 Sep 2006 11:10:57 +0200 Matteo Pillon <matteo.pillon@gmail.com> wrote:
> > I was wondering why Linux doesn't treat directories like files, as
> > many other unix implementations do.
> 
> Pragmatic answer:
> 
> because nobody implemented it for most filesystems. Most filesystems
> just define "generic_read_dir" as handling function for "readdir".
> "generic_read_dir" always returns -EISDIR.
> 
> (see /usr/src/linux/fs/libfs.c and /usr/src/linux/fs/*/dir.c)
> 
> > For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> > Why? There is a practical reason?
> 
> Well, I think it would be just another unstable API that clueless
> programmers would get trapped by. What would be the benefit of being
> able to open it?

Yeah, you're right, it's just funny and I was curious why Linux
behaves differently.
Thanks for your explanation.

> That would be the LKML :-)

Ok.

-- 
 * Pillon Matteo
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 15:13   ` alain.didierjean
@ 2006-09-18 16:11     ` Hans-Werner Hilse
  0 siblings, 0 replies; 14+ messages in thread
From: Hans-Werner Hilse @ 2006-09-18 16:11 UTC (permalink / raw
  To: gentoo-user

Hi,

On Mon, 18 Sep 2006 17:13:11 +0200
alain.didierjean@free.fr wrote:

> > > For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> > > Why? There is a practical reason?
> >
> Try vim . or, better view .

It was mentioned before that applications have support for "reading
directories". But application level is mostly irrelevant. The kernel
doesn't support reading directories (i.e. there's no implementation for
the "read" syscall for all filesystems currently in the kernel -- if I
didn't miss one). Vim just does an "readdir" syscall after failing to
"read", but that's application logic. I think this thread is meant to
be more general. Since the kernel doesn't support "read" on
directories, it is valid to claim that "linux doesn't". It can,
however: Filesystems _can_ implement read() for directories. They
currently just don't.

Also note that according to "man 2 read" the return of an error EISDIR
is conforming to SVr4, 4.3BSD, POSIX.1-2001. "The Open Group Base
Specifications Issue 6" does at least say that implementations that do
not support read()ing from directories will return this error (and that
applications should use readdir() instead). So Linux is compliant here.
It could behave differently, if filesystem designers would chose to do
so.

-hwh
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 14:30 ` Hans-Werner Hilse
  2006-09-18 15:13   ` alain.didierjean
  2006-09-18 15:54   ` Matteo Pillon
@ 2006-09-18 16:23   ` Hans-Werner Hilse
  2 siblings, 0 replies; 14+ messages in thread
From: Hans-Werner Hilse @ 2006-09-18 16:23 UTC (permalink / raw
  To: gentoo-user

Hi,

> because nobody implemented it for most filesystems. Most filesystems
> just define "generic_read_dir" as handling function for "readdir".
> "generic_read_dir" always returns -EISDIR.

sorry short correction, should read:
... as handling function for "read".

"readdir" of course should be implemented for each individual
filesystem...

-hwh
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18  9:10 [gentoo-user] [OT] Why directories aren't files? Matteo Pillon
                   ` (2 preceding siblings ...)
  2006-09-18 14:30 ` Hans-Werner Hilse
@ 2006-09-18 16:25 ` Ryan Tandy
  3 siblings, 0 replies; 14+ messages in thread
From: Ryan Tandy @ 2006-09-18 16:25 UTC (permalink / raw
  To: gentoo-user

Matteo Pillon wrote:
> For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> Why? There is a practical reason?

I don't know why, but I do know that you can do 'less .'.
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [gentoo-user] [OT] Why directories aren't files?
  2006-09-18 14:51         ` Roman Zilka
@ 2006-09-19  5:45           ` Dirk Heinrichs
  0 siblings, 0 replies; 14+ messages in thread
From: Dirk Heinrichs @ 2006-09-19  5:45 UTC (permalink / raw
  To: gentoo-user

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

Am Montag, 18. September 2006 16:51 schrieb ext Roman Zilka:
> > > Hm, this is all pretty weird. I cut'n'pasted and compiled your piece
> > > of code and again got the same results under Linux and FreeBSD: no
> > > output at all. I don't know if some local FreeBSD admin
> > > hacked/patched the kernel source to make its syscalls behave
> > > Linux-alike, but it's very unlikely.
> >
> > Hmm, maybe OP is $LD_PRELOADing something?
>
> My LD_PRELOAD is unset. I'm not sure if I understand "OP" correctly -

OP = Original Poster, the one who started the thread.

Bye...

	Dirk
-- 
Dirk Heinrichs          | Tel:  +49 (0)162 234 3408
Configuration Manager   | Fax:  +49 (0)211 47068 111
Capgemini Deutschland   | Mail: dirk.heinrichs@capgemini.com
Hambornerstraße 55      | Web:  http://www.capgemini.com
D-40472 Düsseldorf      | ICQ#: 110037733
GPG Public Key C2E467BB | Keyserver: www.keyserver.net

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2006-09-19  5:56 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-18  9:10 [gentoo-user] [OT] Why directories aren't files? Matteo Pillon
2006-09-18  9:42 ` Roman Zilka
2006-09-18 11:37   ` Matteo Pillon
2006-09-18 13:04     ` Roman Zilka
2006-09-18 13:12       ` Dirk Heinrichs
2006-09-18 14:51         ` Roman Zilka
2006-09-19  5:45           ` Dirk Heinrichs
2006-09-18  9:59 ` Dirk Heinrichs
2006-09-18 14:30 ` Hans-Werner Hilse
2006-09-18 15:13   ` alain.didierjean
2006-09-18 16:11     ` Hans-Werner Hilse
2006-09-18 15:54   ` Matteo Pillon
2006-09-18 16:23   ` Hans-Werner Hilse
2006-09-18 16:25 ` Ryan Tandy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox