|
| 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 internal |
| 6 | + |
| 7 | +import ( |
| 8 | +"fmt" |
| 9 | +"io" |
| 10 | +"os" |
| 11 | +"path/filepath" |
| 12 | +"strings" |
| 13 | + |
| 14 | +"golang.org/x/mod/sumdb/dirhash" |
| 15 | +) |
| 16 | + |
| 17 | +// Slightly modified copy of [dirhash.HashDir]. |
| 18 | +// https://github.com/golang/mod/blob/v0.28.0/sumdb/dirhash/hash.go#L67-L79 |
| 19 | +funchashDir(dir,prefixstring,hash dirhash.Hash) (string,error) { |
| 20 | +files,err:=dirFiles(dir,prefix) |
| 21 | +iferr!=nil { |
| 22 | +return"",err |
| 23 | +} |
| 24 | + |
| 25 | +osOpen:=func(namestring) (io.ReadCloser,error) { |
| 26 | +returnos.Open(filepath.Join(dir,strings.TrimPrefix(name,prefix))) |
| 27 | +} |
| 28 | + |
| 29 | +returnhash(files,osOpen) |
| 30 | +} |
| 31 | + |
| 32 | +// Modified copy of [dirhash.DirFiles]. |
| 33 | +// https://github.com/golang/mod/blob/v0.28.0/sumdb/dirhash/hash.go#L81-L109 |
| 34 | +// And adapted to globally follows the rules from https://github.com/golang/mod/blob/v0.28.0/zip/zip.go |
| 35 | +funcdirFiles(dir,prefixstring) ([]string,error) { |
| 36 | +varfiles []string |
| 37 | + |
| 38 | +dir=filepath.Clean(dir) |
| 39 | + |
| 40 | +err:=filepath.Walk(dir,func(filestring,info os.FileInfo,errerror)error { |
| 41 | +iferr!=nil { |
| 42 | +returnerr |
| 43 | +} |
| 44 | + |
| 45 | +ifinfo.IsDir() { |
| 46 | +ifdir==file { |
| 47 | +// Don't skip the top-level directory. |
| 48 | +returnnil |
| 49 | +} |
| 50 | + |
| 51 | +switchinfo.Name() { |
| 52 | +// Skip vendor and node directories. |
| 53 | +case"vendor","node_modules": |
| 54 | +returnfilepath.SkipDir |
| 55 | + |
| 56 | +// Skip VCS directories. |
| 57 | +case".bzr",".git",".hg",".svn": |
| 58 | +returnfilepath.SkipDir |
| 59 | +} |
| 60 | + |
| 61 | +// Skip submodules (directories containing go.mod files). |
| 62 | +ifgoModInfo,err:=os.Lstat(filepath.Join(dir,"go.mod"));err==nil&&!goModInfo.IsDir() { |
| 63 | +returnfilepath.SkipDir |
| 64 | +} |
| 65 | + |
| 66 | +returnnil |
| 67 | +} |
| 68 | + |
| 69 | +iffile==dir { |
| 70 | +returnfmt.Errorf("%s is not a directory",dir) |
| 71 | +} |
| 72 | + |
| 73 | +if!info.Mode().IsRegular() { |
| 74 | +returnnil |
| 75 | +} |
| 76 | + |
| 77 | +rel:=file |
| 78 | + |
| 79 | +ifdir!="." { |
| 80 | +rel=file[len(dir)+1:] |
| 81 | +} |
| 82 | + |
| 83 | +f:=filepath.Join(prefix,rel) |
| 84 | + |
| 85 | +files=append(files,filepath.ToSlash(f)) |
| 86 | + |
| 87 | +returnnil |
| 88 | +}) |
| 89 | +iferr!=nil { |
| 90 | +returnnil,err |
| 91 | +} |
| 92 | +returnfiles,nil |
| 93 | +} |