Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some file upload issues #16

Merged
merged 3 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package podrick

import "io"
import (
"io"
"os"
)

// ContainerConfig is used by runtimes to start
// containers.
Expand All @@ -26,9 +29,10 @@ type Ulimit struct {
}

// File describes a file in a container.
// All fields are mandatory
// All fields are mandatory.
type File struct {
Content io.Reader
Path string
Size int
Mode os.FileMode
}
2 changes: 1 addition & 1 deletion runtimes/docker/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func uploadFiles(ctx context.Context, client *docker.Client, cID string, files .
}
err = archive.WriteHeader(&tar.Header{
Name: f.Path,
Mode: 0644,
Mode: int64(f.Mode),
Size: int64(f.Size),
})
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions runtimes/podman/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package podman

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -40,6 +41,12 @@ func uploadFile(ctx context.Context, mountDir string, file podrick.File) (err er
return fmt.Errorf("file paths must be absolute: %q", file.Path)
}
dest := filepath.Join(mountDir, path)
if _, err := os.Stat(filepath.Dir(dest)); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(filepath.Dir(dest), 0777)
if err != nil {
return fmt.Errorf("failed to create parent directory: %w", err)
}
}
target, err := os.Create(dest)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
Expand All @@ -55,5 +62,10 @@ func uploadFile(ctx context.Context, mountDir string, file podrick.File) (err er
return fmt.Errorf("failed to copy file contents: %w", err)
}

err = os.Chmod(dest, file.Mode)
if err != nil {
return fmt.Errorf("failed to set file permissions: %w", err)
}

return nil
}