Skip to content

Commit

Permalink
pkg/ctime: Factor libpod/finished* into a separate package
Browse files Browse the repository at this point in the history
This removes some boilerplate from the libpod package, so we can focus
on container stuff there.  And it gives us a tidy sub-package for
focusing on ctime extraction, so we can focus on unit testing and
portability of the extraction utility there.

For the unsupported implementation, I'm falling back to Go's ModTime
[1].  That's obviously not the creation time, but it's likely to be
closer than the uninitialized Time structure from cc6f0e8 (more
changes to compile darwin, 2018-07-04, #1047).  Especially for our use
case in libpod/oci, where we're looking at write-once exit files.

The test is more complicated than I initially expected, because on
Linux filesystem timestamps come from a truncated clock without
interpolation [2] (and network filesystems can be completely decoupled
[3]).  So even for local disks, creation times can be up to a jiffie
earlier than 'before'.  This test ensures at least monotonicity by
creating two files and ensuring the reported creation time for the
second is greater than or equal to the reported creation time for the
first.  It also checks that both creation times are within the window
from one second earlier than 'before' through 'after'.  That should be
enough of a window for local disks, even if the kernel for those
systems has an abnormally large jiffie.  It might be ok on network
filesystems, although it will not be very resilient to network clock
lagging behind the local system clock.

[1]: https://golang.org/pkg/os/#FileInfo
[2]: https://groups.google.com/d/msg/linux.kernel/mdeXx2TBYZA/_4eJEuJoAQAJ
     Subject: Re: Apparent backward time travel in timestamps on file creation
     Date: Thu, 30 Mar 2017 20:20:02 +0200
     Message-ID: <tqMPU-1Sb-21@gated-at.bofh.it>
[3]: https://groups.google.com/d/msg/linux.kernel/mdeXx2TBYZA/cTKj4OBuAQAJ
     Subject: Re: Apparent backward time travel in timestamps on file creation
     Date: Thu, 30 Mar 2017 22:10:01 +0200
     Message-ID: <tqOyl-36A-1@gated-at.bofh.it>

Signed-off-by: W. Trevor King <wking@tremily.us>

Closes: #1050
Approved by: mheon
  • Loading branch information
wking authored and rh-atomic-bot committed Jul 6, 2018
1 parent aaab26f commit b2344b8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 33 deletions.
16 changes: 0 additions & 16 deletions libpod/finished_32.go

This file was deleted.

12 changes: 0 additions & 12 deletions libpod/finished_unsupported.go

This file was deleted.

3 changes: 2 additions & 1 deletion libpod/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
12 changes: 12 additions & 0 deletions pkg/ctime/ctime.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions pkg/ctime/ctime_linux_32.go
Original file line number Diff line number Diff line change
@@ -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))
}
6 changes: 2 additions & 4 deletions libpod/finished_64.go → pkg/ctime/ctime_linux_64.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// +build !arm,!386,linux

package libpod
package ctime

import (
"os"
"syscall"
"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)
}
63 changes: 63 additions & 0 deletions pkg/ctime/ctime_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 12 additions & 0 deletions pkg/ctime/ctime_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !linux

package ctime

import (
"os"
"time"
)

func created(fi os.FileInfo) time.Time {
return fi.ModTime()
}

0 comments on commit b2344b8

Please sign in to comment.