util.go 717 Bytes
Newer Older
1 2 3 4
package flatfs

import (
	"io"
ia's avatar
ia committed
5
	"os"
Steven Allen's avatar
Steven Allen committed
6
	"time"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
)

// From: http://stackoverflow.com/questions/30697324/how-to-check-if-directory-on-path-is-empty
func DirIsEmpty(name string) (bool, error) {
	f, err := os.Open(name)
	if err != nil {
		return false, err
	}
	defer f.Close()

	_, err = f.Readdirnames(1) // Or f.Readdir(1)
	if err == io.EOF {
		return true, nil
	}
	return false, err // Either not empty or error, suits both cases
}
Steven Allen's avatar
Steven Allen committed
23 24 25 26 27 28 29 30 31 32 33 34

func readFile(filename string) (data []byte, err error) {
	// Fallback retry for temporary error.
	for i := 0; i < 6; i++ {
		data, err = readFileOnce(filename)
		if err == nil || !isTooManyFDError(err) {
			break
		}
		time.Sleep(time.Duration(i+1) * RetryDelay)
	}
	return data, err
}