build: create parent directories for tar output in lazy writer

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2025-10-22 11:50:32 +02:00
parent abf6ab4a37
commit 28b6750599
3 changed files with 39 additions and 8 deletions
+33 -5
View File
@@ -876,11 +876,7 @@ func CreateExports(entries []*buildflags.ExportEntry) ([]client.ExportEntry, []s
if err == nil && fi.IsDir() {
return nil, nil, errors.Errorf("destination file %s is a directory", entry.Destination)
}
f, err := os.Create(entry.Destination)
if err != nil {
return nil, nil, errors.Errorf("failed to open %s", err)
}
out.Output = wrapWriteCloser(f)
out.Output = wrapWriteCloserLazy(entry.Destination)
localPaths = append(localPaths, entry.Destination)
}
}
@@ -896,6 +892,38 @@ func wrapWriteCloser(wc io.WriteCloser) func(map[string]string) (io.WriteCloser,
}
}
type lazyFileWriter struct {
path string
file *os.File
}
func (w *lazyFileWriter) Write(p []byte) (int, error) {
if w.file == nil {
if err := os.MkdirAll(filepath.Dir(w.path), 0755); err != nil {
return 0, err
}
f, err := os.Create(w.path)
if err != nil {
return 0, err
}
w.file = f
}
return w.file.Write(p)
}
func (w *lazyFileWriter) Close() error {
if w.file != nil {
return w.file.Close()
}
return nil
}
func wrapWriteCloserLazy(path string) func(map[string]string) (io.WriteCloser, error) {
return func(map[string]string) (io.WriteCloser, error) {
return &lazyFileWriter{path: path}, nil
}
}
func CreateCaches(entries []*buildflags.CacheOptionsEntry) []client.CacheOptionsEntry {
var outs []client.CacheOptionsEntry
if len(entries) == 0 {