This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
expand.go
156 lines (135 loc) · 3.22 KB
/
expand.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2023 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package kontext
import (
"context"
"io"
"io/fs"
"os"
"path/filepath"
"golang.org/x/sync/errgroup"
)
const (
// StoragePath is where in the container image the files are placed.
StoragePath = "/var/run/kontext"
)
func copyFile(src, dest string) error {
from, err := os.Open(src)
if err != nil {
return err
}
defer from.Close()
to, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer to.Close()
_, err = io.Copy(to, from)
return err
}
func expand(ctx context.Context, base string) error {
targetPath, err := os.Getwd()
if err != nil {
return err
}
// In the first pass, expand all of the files as quickly as possible,
// granting broad file permissions.
{
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(100)
if err := filepath.WalkDir(base, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == base {
return nil
}
// Add each file to the backlog.
eg.Go(func() (err error) {
// If the context is canceled, then bail out early.
select {
case <-ctx.Done():
return ctx.Err()
default:
}
relativePath := path[len(base)+1:]
target := filepath.Join(targetPath, relativePath)
if err := os.MkdirAll(filepath.Dir(target), 0777); err != nil {
return err
}
fi, err := info.Info()
if err != nil {
return err
}
if info.IsDir() {
return os.MkdirAll(target, fi.Mode())
} else if info.Type()&fs.ModeSymlink != 0 {
// It is not practical to test this path because there is not
// a portable way to change the mtime of the symlink
// https://github.com/golang/go/issues/3951
link, err := os.Readlink(path)
if err != nil {
return err
}
return os.Symlink(link, target)
}
return copyFile(path, target)
})
return nil
}); err != nil {
return err
}
if err := eg.Wait(); err != nil {
return err
}
}
// In the final pass, fixup permissions and mtimes
{
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(100)
if err := filepath.WalkDir(base, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
// Add each file to the backlog.
eg.Go(func() (err error) {
// If the context is canceled, then bail out early.
select {
case <-ctx.Done():
return ctx.Err()
default:
}
target := targetPath
if path != base {
relativePath := path[len(base)+1:]
target = filepath.Join(targetPath, relativePath)
}
fi, err := info.Info()
if err != nil {
return err
}
// Set the permissions and mtime
if err := os.Chmod(target, fi.Mode()); err != nil {
return err
}
// Skip symlinks due to:
// https://github.com/golang/go/issues/3951
if info.Type()&fs.ModeSymlink != 0 {
return nil
}
return os.Chtimes(target, fi.ModTime(), fi.ModTime())
})
return nil
}); err != nil {
return err
}
// Wait for the work to be done.
return eg.Wait()
}
}
// Expand recursively copies the current working directory into StoragePath.
func Expand(ctx context.Context) error {
return expand(ctx, StoragePath)
}