Allow creation of empty files

- change to a pointer type so we can distinguish empty from unset.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2017-06-20 13:22:58 -07:00
parent b928a9b203
commit 618dc424de
3 changed files with 13 additions and 8 deletions

View File

@ -568,7 +568,11 @@ func filesystem(m Moby, tw *tar.Writer) error {
if dirMode&0007 != 0 {
dirMode |= 0001
}
if !f.Directory && f.Contents == "" && f.Symlink == "" {
var contents []byte
if f.Contents != nil {
contents = []byte(*f.Contents)
}
if !f.Directory && f.Contents == nil && f.Symlink == "" {
if f.Source == "" {
return errors.New("Contents of file not specified")
}
@ -583,12 +587,11 @@ func filesystem(m Moby, tw *tar.Writer) error {
continue
}
}
contents, err := ioutil.ReadFile(f.Source)
var err error
contents, err = ioutil.ReadFile(f.Source)
if err != nil {
return err
}
f.Contents = string(contents)
}
// we need all the leading directories
parts := strings.Split(path.Dir(f.Path), "/")
@ -617,7 +620,7 @@ func filesystem(m Moby, tw *tar.Writer) error {
}
addedFiles[f.Path] = true
if f.Directory {
if f.Contents != "" {
if f.Contents != nil {
return errors.New("Directory with contents not allowed")
}
hdr := &tar.Header{
@ -644,13 +647,13 @@ func filesystem(m Moby, tw *tar.Writer) error {
hdr := &tar.Header{
Name: f.Path,
Mode: mode,
Size: int64(len(f.Contents)),
Size: int64(len(contents)),
}
err := tw.WriteHeader(hdr)
if err != nil {
return err
}
_, err = tw.Write([]byte(f.Contents))
_, err = tw.Write(contents)
if err != nil {
return err
}

View File

@ -31,7 +31,7 @@ type Moby struct {
Path string
Directory bool
Symlink string
Contents string
Contents *string
Source string
Optional bool
Mode string

View File

@ -29,6 +29,8 @@ services:
files:
- path: etc/docker/daemon.json
contents: '{"debug": true}'
- path: /empty
contents: ""
trust:
org:
- library