Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use seekdir/telldir in Golang?

Tags:

go

I need to walk a filesystem in Golang and need to be able to resume scanning after restarting.

In C, I can do this via telldir() and then a seekdir() while resuming.

Golang only offers a filepath.Walk() function but that provides no way to start walking the filesystem from a specific path or point.

This makes it inefficient for large filesystems

Any way around it?

like image 884
steve landiss Avatar asked Nov 25 '25 08:11

steve landiss


1 Answers

The signature for filepath.walk is:

func Walk(root string, walkFn WalkFunc) error

The documentation states that it starts at the directory specified by root and the signature of the callback function (walkFn) is:

type WalkFunc func(path string, info os.FileInfo, err error) error

So you can start your scan at any given directory and walk the filesystem with that as the root. You cannot start part-way through a directory, but you can selectively prune the tree that you are walking.

There's also a "magic" return value, filepath.SkipDir, which skips either walking a directory (if returned when the callback is invoked on a directory) or the remaining files i the directory (if returned when the callback is invoked on a file).

This MAY be enough to get the behaviour that you want, but it is a little hard to tell from your question. You cannot break out of a filepath.Walk invocation, then resume it later on. However, you may be able to work around that limitation by spawning goroutines from within your walkFn callback, if you're mainly concerned with the callback taking time to complete.

like image 77
Vatine Avatar answered Nov 28 '25 02:11

Vatine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!