forked from elastic/package-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
47 lines (41 loc) · 1.37 KB
/
fs.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
// 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 (
"fmt"
"io/fs"
"os"
"path/filepath"
)
var _ fs.FS = (*LinksFS)(nil)
// LinksFS is a filesystem that handles linked files.
// It wraps another filesystem and checks for linked files with the ".link" extension.
// If a linked file is found, it reads the link file to determine the target file
// and its checksum. If the target file is up to date, it returns the target file.
// Otherwise, it returns an error.
type LinksFS struct {
workDir string
inner fs.FS
}
// NewLinksFS creates a new LinksFS.
func NewLinksFS(workDir string, inner fs.FS) *LinksFS {
return &LinksFS{workDir: workDir, inner: inner}
}
// Open opens a file in the filesystem.
func (lfs *LinksFS) Open(name string) (fs.File, error) {
const linkExtension = ".link"
if filepath.Ext(name) != linkExtension {
return lfs.inner.Open(name)
}
pathName := filepath.Join(lfs.workDir, name)
l, err := NewLinkedFile(pathName)
if err != nil {
return nil, err
}
if !l.UpToDate {
return nil, fmt.Errorf("linked file %s is not up to date", name)
}
includedPath := filepath.Join(lfs.workDir, filepath.Dir(name), l.IncludedFilePath)
return os.Open(includedPath)
}