forked from elastic/package-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedfiles.go
98 lines (83 loc) · 2.26 KB
/
linkedfiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package linkedfiles
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// A Link represents a linked file.
// It contains the path to the link file, the checksum of the linked file,
// the path to the target file, and the checksum of the included file contents.
// It also contains a boolean indicating whether the link is up to date.
type Link struct {
LinkFilePath string
LinkChecksum string
IncludedFilePath string
IncludedFileContentsChecksum string
UpToDate bool
}
// NewLinkedFile creates a new Link from the given link file path.
func NewLinkedFile(linkFilePath string) (Link, error) {
var l Link
firstLine, err := readFirstLine(linkFilePath)
if err != nil {
return Link{}, err
}
l.LinkFilePath = linkFilePath
fields := strings.Fields(firstLine)
l.IncludedFilePath = fields[0]
if len(fields) == 2 {
l.LinkChecksum = fields[1]
}
pathName := filepath.Join(filepath.Dir(linkFilePath), filepath.FromSlash(l.IncludedFilePath))
cs, err := getLinkedFileChecksum(pathName)
if err != nil {
return Link{}, fmt.Errorf("could not collect file %v: %w", l.IncludedFilePath, err)
}
if l.LinkChecksum == cs {
l.UpToDate = true
}
l.IncludedFileContentsChecksum = cs
return l, nil
}
func getLinkedFileChecksum(path string) (string, error) {
b, err := os.ReadFile(filepath.FromSlash(path))
if err != nil {
return "", err
}
cs, err := checksum(b)
if err != nil {
return "", err
}
return cs, nil
}
func readFirstLine(filePath string) (string, error) {
file, err := os.Open(filepath.FromSlash(filePath))
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
return scanner.Text(), nil
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("file is empty or first line is missing")
}
func checksum(b []byte) (string, error) {
hash := sha256.New()
if _, err := io.Copy(hash, bytes.NewReader(b)); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}