diff options
-rw-r--r-- | libpod/finished_32.go | 16 | ||||
-rw-r--r-- | libpod/finished_unsupported.go | 12 | ||||
-rw-r--r-- | libpod/oci.go | 3 | ||||
-rw-r--r-- | pkg/ctime/ctime.go | 12 | ||||
-rw-r--r-- | pkg/ctime/ctime_linux_32.go | 14 | ||||
-rw-r--r-- | pkg/ctime/ctime_linux_64.go (renamed from libpod/finished_64.go) | 6 | ||||
-rw-r--r-- | pkg/ctime/ctime_test.go | 63 | ||||
-rw-r--r-- | pkg/ctime/ctime_unsupported.go | 12 |
8 files changed, 105 insertions, 33 deletions
diff --git a/libpod/finished_32.go b/libpod/finished_32.go deleted file mode 100644 index d66974177..000000000 --- a/libpod/finished_32.go +++ /dev/null @@ -1,16 +0,0 @@ -// +build arm 386 - -package libpod - -import ( - "os" - "syscall" - "time" -) - -// Get created time of a file -// Only works on 32-bit OSes -func getFinishedTime(fi os.FileInfo) time.Time { - st := fi.Sys().(*syscall.Stat_t) - return time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec)) -} diff --git a/libpod/finished_unsupported.go b/libpod/finished_unsupported.go deleted file mode 100644 index a893d668c..000000000 --- a/libpod/finished_unsupported.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build darwin,!linux - -package libpod - -import ( - "os" - "time" -) - -func getFinishedTime(fi os.FileInfo) time.Time { - return time.Time{} -} diff --git a/libpod/oci.go b/libpod/oci.go index fcb36241b..612935aed 100644 --- a/libpod/oci.go +++ b/libpod/oci.go @@ -19,6 +19,7 @@ import ( "github.com/opencontainers/selinux/go-selinux" "github.com/opencontainers/selinux/go-selinux/label" "github.com/pkg/errors" + "github.com/projectatomic/libpod/pkg/ctime" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" kwait "k8s.io/apimachinery/pkg/util/wait" @@ -422,7 +423,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container) error { return nil } - ctr.state.FinishedTime = getFinishedTime(fi) + ctr.state.FinishedTime = ctime.Created(fi) statusCodeStr, err := ioutil.ReadFile(exitFile) if err != nil { return errors.Wrapf(err, "failed to read exit file for container %s", ctr.ID()) diff --git a/pkg/ctime/ctime.go b/pkg/ctime/ctime.go new file mode 100644 index 000000000..f5c69c7e3 --- /dev/null +++ b/pkg/ctime/ctime.go @@ -0,0 +1,12 @@ +// Package ctime includes a utility for determining file-creation times. +package ctime + +import ( + "os" + "time" +) + +// Created returns the file-creation time. +func Created(fi os.FileInfo) time.Time { + return created(fi) +} diff --git a/pkg/ctime/ctime_linux_32.go b/pkg/ctime/ctime_linux_32.go new file mode 100644 index 000000000..00c1a9a8e --- /dev/null +++ b/pkg/ctime/ctime_linux_32.go @@ -0,0 +1,14 @@ +// +build arm,linux 386,linux + +package ctime + +import ( + "os" + "syscall" + "time" +) + +func created(fi os.FileInfo) time.Time { + st := fi.Sys().(*syscall.Stat_t) + return time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec)) +} diff --git a/libpod/finished_64.go b/pkg/ctime/ctime_linux_64.go index 3688afa84..1251c1ded 100644 --- a/libpod/finished_64.go +++ b/pkg/ctime/ctime_linux_64.go @@ -1,6 +1,6 @@ // +build !arm,!386,linux -package libpod +package ctime import ( "os" @@ -8,9 +8,7 @@ import ( "time" ) -// Get the created time of a file -// Only works on 64-bit OSes -func getFinishedTime(fi os.FileInfo) time.Time { +func created(fi os.FileInfo) time.Time { st := fi.Sys().(*syscall.Stat_t) return time.Unix(st.Ctim.Sec, st.Ctim.Nsec) } diff --git a/pkg/ctime/ctime_test.go b/pkg/ctime/ctime_test.go new file mode 100644 index 000000000..abfc627da --- /dev/null +++ b/pkg/ctime/ctime_test.go @@ -0,0 +1,63 @@ +package ctime + +import ( + "io/ioutil" + "os" + "testing" + "time" +) + +func TestCreated(t *testing.T) { + before := time.Now() + + fileA, err := ioutil.TempFile("", "ctime-test-") + if err != nil { + t.Error(err) + } + defer os.Remove(fileA.Name()) + + fileB, err := ioutil.TempFile("", "ctime-test-") + if err != nil { + t.Error(err) + } + defer os.Remove(fileB.Name()) + + after := time.Now() + + infoA, err := fileA.Stat() + if err != nil { + t.Error(err) + } + + err = fileA.Close() + if err != nil { + t.Error(err) + } + + infoB, err := fileB.Stat() + if err != nil { + t.Error(err) + } + + err = fileB.Close() + if err != nil { + t.Error(err) + } + + createdA := Created(infoA) + beforeToCreateA := createdA.Sub(before) + if beforeToCreateA.Nanoseconds() < -1000000000 { + t.Errorf("created file A %s is %v nanoseconds before %s", createdA, -beforeToCreateA.Nanoseconds(), before) + } + + createdB := Created(infoB) + createAToCreateB := createdB.Sub(createdA) + if createAToCreateB.Nanoseconds() < 0 { + t.Errorf("created file B %s is %v nanoseconds before file A %s", createdB, -createAToCreateB.Nanoseconds(), createdA) + } + + createBToAfter := after.Sub(createdB) + if createBToAfter.Nanoseconds() < 0 { + t.Errorf("created file B %s is %v nanoseconds after %s", createdB, -createBToAfter.Nanoseconds(), after) + } +} diff --git a/pkg/ctime/ctime_unsupported.go b/pkg/ctime/ctime_unsupported.go new file mode 100644 index 000000000..325731353 --- /dev/null +++ b/pkg/ctime/ctime_unsupported.go @@ -0,0 +1,12 @@ +// +build !linux + +package ctime + +import ( + "os" + "time" +) + +func created(fi os.FileInfo) time.Time { + return fi.ModTime() +} |