Use io.Copy when creating files
This commit is contained in:
parent
5ba9510ee8
commit
b3ec877b60
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"github.com/containers/image/manifest"
|
"github.com/containers/image/manifest"
|
||||||
"github.com/docker/docker/builder/dockerfile/instructions"
|
"github.com/docker/docker/builder/dockerfile/instructions"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -65,11 +64,12 @@ func (c *CopyCommand) ExecuteCommand(config *manifest.Schema2Config) error {
|
||||||
} else {
|
} else {
|
||||||
// ... Else, we want to copy over a file
|
// ... Else, we want to copy over a file
|
||||||
logrus.Infof("Copying file %s to %s", file, destPath)
|
logrus.Infof("Copying file %s to %s", file, destPath)
|
||||||
contents, err := ioutil.ReadFile(filepath.Join(c.buildcontext, file))
|
srcFile, err := os.Open(filepath.Join(c.buildcontext, file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := util.CreateFile(destPath, contents, fi.Mode()); err != nil {
|
defer srcFile.Close()
|
||||||
|
if err := util.CreateFile(destPath, srcFile, fi.Mode()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import (
|
||||||
"github.com/containers/image/docker"
|
"github.com/containers/image/docker"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -124,8 +123,8 @@ func FilepathExists(path string) bool {
|
||||||
return !os.IsNotExist(err)
|
return !os.IsNotExist(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateFile creates a file at path with contents specified
|
// CreateFile creates a file at path and copies over contents from the reader
|
||||||
func CreateFile(path string, contents []byte, perm os.FileMode) error {
|
func CreateFile(path string, reader io.Reader, perm os.FileMode) error {
|
||||||
// Create directory path if it doesn't exist
|
// Create directory path if it doesn't exist
|
||||||
baseDir := filepath.Dir(path)
|
baseDir := filepath.Dir(path)
|
||||||
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
||||||
|
|
@ -134,5 +133,12 @@ func CreateFile(path string, contents []byte, perm os.FileMode) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ioutil.WriteFile(path, contents, perm)
|
dest, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(dest, reader); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return dest.Chmod(perm)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue