Skip to content

Commit 3a65db1

Browse files
committed
dirhash: forward to golang.org/x/mod/sumdb/dirhash
It seems like x/mod/sumdb/dirhash appeared way back in 2019, and we had not noticed until now.
1 parent 361e7d2 commit 3a65db1

File tree

2 files changed

+12
-218
lines changed

2 files changed

+12
-218
lines changed

dirhash/hash.go

+12-84
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,31 @@
1-
// Copyright 2018 The Go Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style
3-
// license that can be found in the LICENSE file.
4-
5-
// Package dirhash defines hashes over directory trees.
1+
// Package dirhash is a thin forwarding layer on top of
2+
// [golang.org/x/mod/sumdb/dirhash]. See that package for documentation.
3+
//
4+
// Deprecated: use [golang.org/x/mod/sumdb/dirhash] instead.
65
package dirhash
76

87
import (
9-
"archive/zip"
10-
"crypto/sha256"
11-
"encoding/base64"
12-
"errors"
13-
"fmt"
148
"io"
15-
"os"
16-
"path/filepath"
17-
"sort"
18-
"strings"
9+
10+
"golang.org/x/mod/sumdb/dirhash"
1911
)
2012

21-
var DefaultHash = Hash1
13+
var DefaultHash = dirhash.Hash1
2214

23-
type Hash func(files []string, open func(string) (io.ReadCloser, error)) (string, error)
15+
type Hash = dirhash.Hash
2416

2517
func Hash1(files []string, open func(string) (io.ReadCloser, error)) (string, error) {
26-
h := sha256.New()
27-
files = append([]string(nil), files...)
28-
sort.Strings(files)
29-
for _, file := range files {
30-
if strings.Contains(file, "\n") {
31-
return "", errors.New("filenames with newlines are not supported")
32-
}
33-
r, err := open(file)
34-
if err != nil {
35-
return "", err
36-
}
37-
hf := sha256.New()
38-
_, err = io.Copy(hf, r)
39-
r.Close()
40-
if err != nil {
41-
return "", err
42-
}
43-
fmt.Fprintf(h, "%x %s\n", hf.Sum(nil), file)
44-
}
45-
return "h1:" + base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
18+
return dirhash.Hash1(files, open)
4619
}
4720

4821
func HashDir(dir, prefix string, hash Hash) (string, error) {
49-
files, err := DirFiles(dir, prefix)
50-
if err != nil {
51-
return "", err
52-
}
53-
osOpen := func(name string) (io.ReadCloser, error) {
54-
return os.Open(filepath.Join(dir, strings.TrimPrefix(name, prefix)))
55-
}
56-
return hash(files, osOpen)
22+
return dirhash.HashDir(dir, prefix, hash)
5723
}
5824

5925
func DirFiles(dir, prefix string) ([]string, error) {
60-
var files []string
61-
dir = filepath.Clean(dir)
62-
err := filepath.Walk(dir, func(file string, info os.FileInfo, err error) error {
63-
if err != nil {
64-
return err
65-
}
66-
if info.IsDir() {
67-
return nil
68-
}
69-
rel := file
70-
if dir != "." {
71-
rel = file[len(dir)+1:]
72-
}
73-
f := filepath.Join(prefix, rel)
74-
files = append(files, filepath.ToSlash(f))
75-
return nil
76-
})
77-
if err != nil {
78-
return nil, err
79-
}
80-
return files, nil
26+
return dirhash.DirFiles(dir, prefix)
8127
}
8228

8329
func HashZip(zipfile string, hash Hash) (string, error) {
84-
z, err := zip.OpenReader(zipfile)
85-
if err != nil {
86-
return "", err
87-
}
88-
defer z.Close()
89-
var files []string
90-
zfiles := make(map[string]*zip.File)
91-
for _, file := range z.File {
92-
files = append(files, file.Name)
93-
zfiles[file.Name] = file
94-
}
95-
zipOpen := func(name string) (io.ReadCloser, error) {
96-
f := zfiles[name]
97-
if f == nil {
98-
return nil, fmt.Errorf("file %q not found in zip", name) // should never happen
99-
}
100-
return f.Open()
101-
}
102-
return hash(files, zipOpen)
30+
return dirhash.HashZip(zipfile, hash)
10331
}

dirhash/hash_test.go

-134
This file was deleted.

0 commit comments

Comments
 (0)