Skip files in contentHash()

If we cannot open file for some reason it is better to skip it instead
of exit. Also we should skip symlinks and directories.

Signed-off-by: Petr Fedchenkov <giggsoff@gmail.com>
This commit is contained in:
Petr Fedchenkov 2022-09-08 16:48:12 +03:00
parent 9b636cbf25
commit 49f8faffe0
No known key found for this signature in database
GPG Key ID: 01AB26025D699586

View File

@ -94,9 +94,20 @@ func (g git) contentHash() (string, error) {
} }
scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(out))) scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(out)))
for scanner.Scan() { for scanner.Scan() {
f, err := os.Open(filepath.Join(g.dir, scanner.Text())) filename := filepath.Join(g.dir, scanner.Text())
info, err := os.Lstat(filename)
if err != nil { if err != nil {
return "", err log.Debugf("cannot stat %s: %s, skipped", filename, err)
continue
}
if info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
// we do not want to calculate hash of directory or symlinks
continue
}
f, err := os.Open(filename)
if err != nil {
log.Debugf("cannot open %s: %s, skipped", filename, err)
continue
} }
if _, err := io.Copy(hash, f); err != nil { if _, err := io.Copy(hash, f); err != nil {
_ = f.Close() _ = f.Close()