Skip to content

Commit

Permalink
Digest: Add a utility function for getting xattr names
Browse files Browse the repository at this point in the history
In this PR for Bazel I am adding support for reading hashes from
extended attributes:

bazelbuild/bazel#11662

This can speed up Bazel's file system access, as it no longer needs to
read the full file contents to compute the digest.

The PR for Bazel does not set any standard for extended attribute
naming. Let's go ahead and pick a naming scheme on our end. We could
have also used Buildbox's "user.checksum.*", but the format of those
extended attributes is different. Bazel expects binary hashes, while
Buildbox uses base16 (hexadecimal) encoding.
  • Loading branch information
EdSchouten committed Aug 8, 2020
1 parent 5abd517 commit 3662a98
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/digest/digest.go
Original file line number Diff line number Diff line change
@@ -242,6 +242,27 @@ func (d Digest) GetKey(format KeyFormat) string {
}
}

// GetHashXAttrName returns the extended file attribute retrievable
// through getxattr() that can be used to store a cached copy of the
// object's hash.
func (d Digest) GetHashXAttrName() string {
hashEnd, _, _ := d.unpack()
switch hashEnd {
case md5.Size * 2:
return "user.buildbarn.hash.md5"
case sha1.Size * 2:
return "user.buildbarn.hash.sha1"
case sha256.Size * 2:
return "user.buildbarn.hash.sha256"
case sha512.Size384 * 2:
return "user.buildbarn.hash.sha384"
case sha512.Size * 2:
return "user.buildbarn.hash.sha512"
default:
panic("Digest hash is of unknown type")
}
}

func (d Digest) String() string {
return d.GetKey(KeyWithInstance)
}
14 changes: 14 additions & 0 deletions pkg/digest/digest_test.go
Original file line number Diff line number Diff line change
@@ -249,6 +249,20 @@ func TestDigestGetKey(t *testing.T) {
d.GetKey(digest.KeyWithInstance))
}

func TestDigestGetHashXAttrName(t *testing.T) {
for _, e := range []struct{ hash, xattrName string }{
{"8b1a9953c4611296a827abf8c47804d7", "user.buildbarn.hash.md5"},
{"f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0", "user.buildbarn.hash.sha1"},
{"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969", "user.buildbarn.hash.sha256"},
{"3519fe5ad2c596efe3e276a6f351b8fc0b03db861782490d45f7598ebd0ab5fd5520ed102f38c4a5ec834e98668035fc", "user.buildbarn.hash.sha384"},
{"3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315", "user.buildbarn.hash.sha512"},
} {
require.Equal(t,
e.xattrName,
digest.MustNewDigest("hello", e.hash, 123).GetHashXAttrName())
}
}

func TestDigestString(t *testing.T) {
require.Equal(
t,

0 comments on commit 3662a98

Please sign in to comment.