61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// HasExt reports whether path has one of the provided extensions (case-insensitive, with or without dot).
|
|
func HasExt(path string, exts []string) bool {
|
|
if len(exts) == 0 {
|
|
return true
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
for _, e := range exts {
|
|
e = strings.ToLower(e)
|
|
if !strings.HasPrefix(e, ".") {
|
|
e = "." + e
|
|
}
|
|
if ext == e {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// WaitForStable waits until the file size has not changed for stableFor duration.
|
|
func WaitForStable(path string, stableFor time.Duration, timeout time.Duration) error {
|
|
start := time.Now()
|
|
var lastSize int64 = -1
|
|
var stableStart time.Time
|
|
|
|
for {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
size := info.Size()
|
|
|
|
if size == lastSize {
|
|
if stableStart.IsZero() {
|
|
stableStart = time.Now()
|
|
}
|
|
if time.Since(stableStart) >= stableFor {
|
|
return nil
|
|
}
|
|
} else {
|
|
stableStart = time.Time{}
|
|
}
|
|
|
|
lastSize = size
|
|
|
|
if time.Since(start) > timeout {
|
|
return os.ErrDeadlineExceeded
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|