Can't IsDir() from FileInfo with filepath.Walk

534 views
Skip to first unread message

micke....@gmail.com

unread,
Sep 26, 2015, 4:49:39 AM9/26/15
to golang-nuts
Hi

I trying to get all files on my system. I start "walking" the filesystem from / (root) with filepath.Walk(). It works fine if I dont use FileInfo.IsDir().
If I use FileInfo.IsDir() the application panic.

Here is an example of the working code:
1. 

And here is an exmaple then it wont work:
2. 

If I use example number 2, but point to /home insteed of / , it will work, no crashes.

Here is an dump of the error message:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x40102d]

goroutine 1 [running]:
main.GetFile(0xc820393b40, 0x10, 0x0, 0x0, 0x7fafa3633248, 0xc8204fe2a0, 0x0, 0x0)
/home/mickesommar/Code/golang/workspace/src/bitbucket.com/mickesommar/simplewalk/main.go:14 +0x2d
path/filepath.walk(0xc820393a40, 0xe, 0x7fafa36331c0, 0xc8203814a0, 0x5478d0, 0x0, 0x0)
/usr/lib/go/src/path/filepath/path.go:370 +0x415
path/filepath.walk(0xc820393620, 0xb, 0x7fafa36331c0, 0xc820380eb0, 0x5478d0, 0x0, 0x0)
/usr/lib/go/src/path/filepath/path.go:374 +0x4fc
path/filepath.walk(0xc82000a780, 0x5, 0x7fafa36331c0, 0xc820013310, 0x5478d0, 0x0, 0x0)
/usr/lib/go/src/path/filepath/path.go:374 +0x4fc
path/filepath.walk(0x512478, 0x1, 0x7fafa36331c0, 0xc820012140, 0x5478d0, 0x0, 0x0)
/usr/lib/go/src/path/filepath/path.go:374 +0x4fc
path/filepath.Walk(0x512478, 0x1, 0x5478d0, 0x0, 0x0)
/usr/lib/go/src/path/filepath/path.go:396 +0xe1
main.main()
/home/mickesommar/Code/golang/workspace/src/bitbucket.com/mickesommar/simplewalk/main.go:22 +0x50



I running Linux Arch with kernel: Linux 3.14.52-1-lts #1 SMP Thu Sep 17 20:31:50 CEST 2015 x86_64 GNU/Linux

I'm using: go version go1.5.1 linux/amd64

I have glibc version: 
lib/   lib64/ 
[mickesommar@xps simplewalk]$ /lib64/libc.so.6 
GNU C Library (GNU libc) stable release version 2.22, by Roland McGrath et al.
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 5.2.0.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:

I have also used the same code/program on an Debian machine: 
Kernel: Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux

Am I doing something wrong? Am I using .IsDir() in the wrong way? Or is it an bug or limitation in my operatingsystem?

Best regards Micke Sommar

Kiki Sugiaman

unread,
Sep 26, 2015, 5:13:15 AM9/26/15
to golan...@googlegroups.com
You should check the error inside the WalkFunc, e.g. return it.

http://play.golang.org/p/drALju4BcA

In general, when Walk() is performed inside a root-dir with fast
changing contents (say, files being created and deleted in the middle of
Walk), expect to encounter some errors.

Jan Mercl

unread,
Sep 26, 2015, 5:18:56 AM9/26/15
to golang-nuts
On Sat, Sep 26, 2015 at 10:49 AM <micke....@gmail.com> wrote:

> And here is an exmaple then it wont work:
> 2.
http://play.golang.org/p/k2Vw-1ksKy

--

-j

Tamás Gulácsi

unread,
Sep 26, 2015, 5:21:28 AM9/26/15
to golang-nuts
Please check the error before using FileInfo!
Walk will call the callback with non-nil error if can't read the file.
This can happen easily: permissions.

micke....@gmail.com

unread,
Sep 26, 2015, 5:58:29 AM9/26/15
to golang-nuts, micke....@gmail.com
Hi All

Thanks for the responses, stupid me, that dident check the error before using IsDir().
I executed the application as root, but dident think of /proc. Then I execute the code as root, I got 16 errors from /proc.

Thanks!

// Micke
Reply all
Reply to author
Forward
0 new messages