Skip to content

Commit

Permalink
llb: add timestamp override to fileop
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Feb 1, 2019
1 parent e153c46 commit 69a4c26
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 134 deletions.
42 changes: 37 additions & 5 deletions client/llb/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "crypto/sha256"
"os"
"path"
"time"

"github.com/moby/buildkit/solver/pb"
digest "github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -149,6 +150,7 @@ func (a *fileActionMkdir) toProtoAction(parent string, base pb.InputIndex) pb.Is
Mode: int32(a.mode & 0777),
MakeParents: a.info.MakeParents,
Owner: a.info.ChownOpt.marshal(base),
Timestamp: marshalTime(a.info.CreatedTime),
},
}
}
Expand Down Expand Up @@ -180,6 +182,7 @@ func WithParents(b bool) MkdirOption {
type MkdirInfo struct {
MakeParents bool
ChownOpt *ChownOpt
CreatedTime *time.Time
}

func (mi *MkdirInfo) SetMkdirOption(mi2 *MkdirInfo) {
Expand Down Expand Up @@ -260,7 +263,8 @@ type MkfileOption interface {
}

type MkfileInfo struct {
ChownOpt *ChownOpt
ChownOpt *ChownOpt
CreatedTime *time.Time
}

func (mi *MkfileInfo) SetMkfileOption(mi2 *MkfileInfo) {
Expand All @@ -279,10 +283,11 @@ type fileActionMkfile struct {
func (a *fileActionMkfile) toProtoAction(parent string, base pb.InputIndex) pb.IsFileAction {
return &pb.FileAction_Mkfile{
Mkfile: &pb.FileActionMkFile{
Path: normalizePath(parent, a.file),
Mode: int32(a.mode & 0777),
Data: a.dt,
Owner: a.info.ChownOpt.marshal(base),
Path: normalizePath(parent, a.file),
Mode: int32(a.mode & 0777),
Data: a.dt,
Owner: a.info.ChownOpt.marshal(base),
Timestamp: marshalTime(a.info.CreatedTime),
},
}
}
Expand Down Expand Up @@ -391,6 +396,7 @@ type CopyInfo struct {
AllowWildcard bool
AllowEmptyWildcard bool
ChownOpt *ChownOpt
CreatedTime *time.Time
}

func (mi *CopyInfo) SetCopyOption(mi2 *CopyInfo) {
Expand Down Expand Up @@ -418,6 +424,7 @@ func (a *fileActionCopy) toProtoAction(parent string, base pb.InputIndex) pb.IsF
DirCopyContents: a.info.CopyDirContentsOnly,
AttemptUnpack: a.info.AttemptUnpack,
CreateDestPath: a.info.CreateDestPath,
Timestamp: marshalTime(a.info.CreatedTime),
}
if a.info.Mode != nil {
c.Mode = int32(*a.info.Mode)
Expand All @@ -441,6 +448,31 @@ func (c *fileActionCopy) sourcePath() string {
return p
}

type CreatedTime time.Time

func WithCreatedTime(t time.Time) CreatedTime {
return CreatedTime(t)
}

func (c CreatedTime) SetMkdirOption(mi *MkdirInfo) {
mi.CreatedTime = (*time.Time)(&c)
}

func (c CreatedTime) SetMkfileOption(mi *MkfileInfo) {
mi.CreatedTime = (*time.Time)(&c)
}

func (c CreatedTime) SetCopyOption(mi *CopyInfo) {
mi.CreatedTime = (*time.Time)(&c)
}

func marshalTime(t *time.Time) int64 {
if t == nil {
return -1
}
return t.UnixNano()
}

type FileOp struct {
MarshalCache
action *FileAction
Expand Down
46 changes: 46 additions & 0 deletions client/llb/fileop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package llb

import (
"testing"
"time"

"github.com/moby/buildkit/solver/pb"
digest "github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -39,6 +40,7 @@ func TestFileMkdir(t *testing.T) {

require.Equal(t, "/foo", mkdir.Path)
require.Equal(t, 0700, int(mkdir.Mode))
require.Equal(t, int64(-1), mkdir.Timestamp)
}

func TestFileMkdirChain(t *testing.T) {
Expand Down Expand Up @@ -126,6 +128,7 @@ func TestFileMkfile(t *testing.T) {
require.Equal(t, "/foo", mkdir.Path)
require.Equal(t, 0700, int(mkdir.Mode))
require.Equal(t, "data", string(mkdir.Data))
require.Equal(t, int64(-1), mkdir.Timestamp)
}

func TestFileRm(t *testing.T) {
Expand Down Expand Up @@ -270,6 +273,7 @@ func TestFileCopy(t *testing.T) {

require.Equal(t, "/etc/foo", copy.Src)
require.Equal(t, "/tmp/bar", copy.Dest)
require.Equal(t, int64(-1), copy.Timestamp)
}

func TestFileCopyFromAction(t *testing.T) {
Expand Down Expand Up @@ -560,6 +564,48 @@ func TestFileCopyOwner(t *testing.T) {
require.Equal(t, -1, int(copy.Owner.Group.Input))
}

func TestFileCreatedTime(t *testing.T) {
t.Parallel()

dt := time.Now()
dt2 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
dt3 := time.Date(2019, time.November, 10, 23, 0, 0, 0, time.UTC)

st := Image("foo").File(
Mkdir("/foo", 0700, WithCreatedTime(dt)).
Mkfile("bar", 0600, []byte{}, WithCreatedTime(dt2)).
Copy(Scratch(), "src", "dst", WithCreatedTime(dt3)))
def, err := st.Marshal()

require.NoError(t, err)

m, arr := parseDef(t, def.Def)
require.Equal(t, 3, len(arr))

dgst, idx := last(t, arr)
require.Equal(t, 0, idx)
require.Equal(t, m[dgst], arr[1])

f := arr[1].Op.(*pb.Op_File).File
require.Equal(t, len(arr[1].Inputs), 1)
require.Equal(t, m[arr[1].Inputs[0].Digest], arr[0])
require.Equal(t, 0, int(arr[1].Inputs[0].Index))

require.Equal(t, 3, len(f.Actions))

action := f.Actions[0]
mkdir := action.Action.(*pb.FileAction_Mkdir).Mkdir
require.Equal(t, dt.UnixNano(), mkdir.Timestamp)

action = f.Actions[1]
mkfile := action.Action.(*pb.FileAction_Mkfile).Mkfile
require.Equal(t, dt2.UnixNano(), mkfile.Timestamp)

action = f.Actions[2]
copy := action.Action.(*pb.FileAction_Copy).Copy
require.Equal(t, dt3.UnixNano(), copy.Timestamp)
}

func parseDef(t *testing.T, def [][]byte) (map[digest.Digest]pb.Op, []pb.Op) {
m := map[digest.Digest]pb.Op{}
arr := make([]pb.Op, 0, len(def))
Expand Down
Loading

0 comments on commit 69a4c26

Please sign in to comment.