C's readdir equivalent

89 views
Skip to first unread message

Bakul Shah

unread,
Oct 19, 2017, 1:54:55 AM10/19/17
to golang-nuts
os.Readdirnames() just returns dir enry names[1], while
os.Readdir() lstats all the dir entries.

Pretty much every OS's (C lang) readdir() returns a direntry's
type without having to stat the underlying file. More often
than not this is all one wants -- not everyone wants to walk a
directory tree -- and even there filepath.Walk() lstats all
the files unnecessarily.

I have directories with thousands of files and a very smal
number of subdirs so the lstat overhead is considerable.

Is there a package that does ReadDirents() returning a pair
([]string, []byte) or []struct {type byte; name string}?

Thanks,
Bakul

[1] Not to mention the name "Readdirnames" is not quite right
as it does not read the directory's names!

Ian Lance Taylor

unread,
Oct 19, 2017, 12:29:37 PM10/19/17
to Bakul Shah, golang-nuts
On Wed, Oct 18, 2017 at 10:54 PM, Bakul Shah <ba...@bitblocks.com> wrote:
>
> os.Readdirnames() just returns dir enry names[1], while
> os.Readdir() lstats all the dir entries.
>
> Pretty much every OS's (C lang) readdir() returns a direntry's
> type without having to stat the underlying file. More often
> than not this is all one wants -- not everyone wants to walk a
> directory tree -- and even there filepath.Walk() lstats all
> the files unnecessarily.

(As far as I know Solaris does not yet provide the direntry's type.)


> I have directories with thousands of files and a very smal
> number of subdirs so the lstat overhead is considerable.
>
> Is there a package that does ReadDirents() returning a pair
> ([]string, []byte) or []struct {type byte; name string}?

I'm not aware of one. It would need to be somewhat system-specific as
different systems lay out the directory entries differently. It
should be possible to write such a package building on ReadDirent and
ParseDirent in golang.org/x/sys/unix.

Ian

Bakul Shah

unread,
Oct 19, 2017, 1:02:08 PM10/19/17
to Ian Lance Taylor, golang-nuts
On Thu, 19 Oct 2017 09:29:09 -0700 Ian Lance Taylor <ia...@golang.org> wrote:
Ian Lance Taylor writes:
> On Wed, Oct 18, 2017 at 10:54 PM, Bakul Shah <ba...@bitblocks.com> wrote:
> >
> > os.Readdirnames() just returns dir enry names[1], while
> > os.Readdir() lstats all the dir entries.
> >
> > Pretty much every OS's (C lang) readdir() returns a direntry's
> > type without having to stat the underlying file. More often
> > than not this is all one wants -- not everyone wants to walk a
> > directory tree -- and even there filepath.Walk() lstats all
> > the files unnecessarily.
>
> (As far as I know Solaris does not yet provide the direntry's type.)

I should've checked. It is not in the POSIX defn of dirent
either. I guess most older filesystems won't....

> > I have directories with thousands of files and a very smal
> > number of subdirs so the lstat overhead is considerable.
> >
> > Is there a package that does ReadDirents() returning a pair
> > ([]string, []byte) or []struct {type byte; name string}?
>
> I'm not aware of one. It would need to be somewhat system-specific as
> different systems lay out the directory entries differently. It
> should be possible to write such a package building on ReadDirent and
> ParseDirent in golang.org/x/sys/unix.

Ok, I'll put something together. It will return DT_UNKNOWN for
each entry on OSes that don't support this feaure.

Thanks!

Bakul
Reply all
Reply to author
Forward
0 new messages