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

View File

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

View File

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