-
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathfiles.go
More file actions
132 lines (119 loc) · 3.05 KB
/
files.go
File metadata and controls
132 lines (119 loc) · 3.05 KB
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
package executor
import (
"context"
"errors"
"fmt"
"io"
"os"
"path"
"regexp"
"slices"
"strings"
"github.com/codeclysm/extract/v4"
"github.com/dstackai/dstack/runner/internal/gerrors"
"github.com/dstackai/dstack/runner/internal/log"
)
var renameRegex = regexp.MustCompile(`^([^/]*)(/|$)`)
func (ex *RunExecutor) AddFileArchive(id string, src io.Reader) error {
if err := os.MkdirAll(ex.archiveDir, 0o755); err != nil {
return gerrors.Wrap(err)
}
archivePath := path.Join(ex.archiveDir, id)
archive, err := os.Create(archivePath)
if err != nil {
return gerrors.Wrap(err)
}
defer func() { _ = archive.Close() }()
if _, err = io.Copy(archive, src); err != nil {
return gerrors.Wrap(err)
}
return nil
}
// setupFiles must be called from Run
func (ex *RunExecutor) setupFiles(ctx context.Context) error {
homeDir := ex.workingDir
uid := -1
gid := -1
if ex.jobSpec.User != nil {
if ex.jobSpec.User.HomeDir != "" {
homeDir = ex.jobSpec.User.HomeDir
}
if ex.jobSpec.User.Uid != nil {
uid = int(*ex.jobSpec.User.Uid)
}
if ex.jobSpec.User.Gid != nil {
gid = int(*ex.jobSpec.User.Gid)
}
}
for _, fa := range ex.jobSpec.FileArchives {
log.Trace(ctx, "Extracting file archive", "id", fa.Id, "path", fa.Path)
p := path.Clean(fa.Path)
// `~username[/path/to]` is not supported
if p == "~" {
p = homeDir
} else if rest, found := strings.CutPrefix(p, "~/"); found {
p = path.Join(homeDir, rest)
} else if !path.IsAbs(p) {
p = path.Join(ex.workingDir, p)
}
dir, root := path.Split(p)
if err := mkdirAll(ctx, dir, uid, gid); err != nil {
return gerrors.Wrap(err)
}
if err := os.RemoveAll(p); err != nil {
log.Warning(ctx, "Failed to remove", "path", p, "err", err)
}
archivePath := path.Join(ex.archiveDir, fa.Id)
archive, err := os.Open(archivePath)
if err != nil {
return gerrors.Wrap(err)
}
defer func() {
_ = archive.Close()
if err := os.Remove(archivePath); err != nil {
log.Warning(ctx, "Failed to remove archive", "path", archivePath, "err", err)
}
}()
var paths []string
repl := fmt.Sprintf("%s$2", root)
renameAndRemember := func(s string) string {
s = renameRegex.ReplaceAllString(s, repl)
paths = append(paths, s)
return s
}
if err := extract.Tar(ctx, archive, dir, renameAndRemember); err != nil {
return gerrors.Wrap(err)
}
if uid != -1 || gid != -1 {
for _, p := range paths {
if err := os.Chown(path.Join(dir, p), uid, gid); err != nil {
log.Warning(ctx, "Failed to chown", "path", p, "err", err)
}
}
}
}
return nil
}
func mkdirAll(ctx context.Context, p string, uid int, gid int) error {
var paths []string
for {
p = path.Dir(p)
if p == "/" {
break
}
paths = append(paths, p)
}
for _, p := range slices.Backward(paths) {
if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(p, 0o755); err != nil {
return err
}
if err := os.Chown(p, uid, gid); err != nil {
log.Warning(ctx, "Failed to chown", "path", p, "err", err)
}
} else if err != nil {
return err
}
}
return nil
}