about summary refs log tree commit diff
path: root/tools/nixery/storage
diff options
context:
space:
mode:
authorJérôme Petazzoni <jerome.petazzoni@gmail.com>2021-04-22T14·52+0200
committerVincent Ambo <mail@tazj.in>2021-06-25T23·27+0200
commit94e04a76b6d2baf0cc5060c3168a428fae6c28ab (patch)
tree04a5038cb960b7f49aa7a18baf4f094423ce057c /tools/nixery/storage
parent3efbbfcd4e22811dd549c6ed46374736308b0b82 (diff)
feat(storage): Store blob content-type in extended attributes
After the discussion in #116, this stores the blob content types
in extended attributes when using the filesystem backend.

If the underlying filesystem doesn't support extended attributes,
storing blobs won't work; also, if extended attributes get removed,
blobs won't be served anymore. We can relax this behavior if
needed (i.e. log errors but still accept to store or serve blobs).
However, since the Docker Engine (and possibly other container
engines) won't accept to pull images from a registry that doesn't
use correct content types for manifest files, it could be argued
that it's better to give a hard fail. (Otherwise, the container
engine gives cryptic error messages like "missing signature key".)

I can change that behavior (and log errors but still store/serve
blobs to the filesystem) if you think it's better.
Diffstat (limited to 'tools/nixery/storage')
-rw-r--r--tools/nixery/storage/filesystem.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/tools/nixery/storage/filesystem.go b/tools/nixery/storage/filesystem.go
index bd757587b4db..2be3457f324a 100644
--- a/tools/nixery/storage/filesystem.go
+++ b/tools/nixery/storage/filesystem.go
@@ -23,6 +23,7 @@ import (
 	"os"
 	"path"
 
+	"github.com/pkg/xattr"
 	log "github.com/sirupsen/logrus"
 )
 
@@ -49,8 +50,7 @@ func (b *FSBackend) Name() string {
 	return fmt.Sprintf("Filesystem (%s)", b.path)
 }
 
-// TODO(tazjin): Implement support for persisting content-types for the filesystem backend.
-func (b *FSBackend) Persist(ctx context.Context, key, _type string, f Persister) (string, int64, error) {
+func (b *FSBackend) Persist(ctx context.Context, key, contentType string, f Persister) (string, int64, error) {
 	full := path.Join(b.path, key)
 	dir := path.Dir(full)
 	err := os.MkdirAll(dir, 0755)
@@ -66,6 +66,12 @@ func (b *FSBackend) Persist(ctx context.Context, key, _type string, f Persister)
 	}
 	defer file.Close()
 
+	err = xattr.Set(full, "user.mime_type", []byte(contentType))
+	if err != nil {
+		log.WithError(err).WithField("file", full).Error("failed to store file type in xattrs")
+		return "", 0, err
+	}
+
 	return f(file)
 }
 
@@ -92,6 +98,13 @@ func (b *FSBackend) Serve(digest string, r *http.Request, w http.ResponseWriter)
 		"path":   p,
 	}).Info("serving blob from filesystem")
 
+	contentType, err := xattr.Get(p, "user.mime_type")
+	if err != nil {
+		log.WithError(err).WithField("file", p).Error("failed to read file type from xattrs")
+		return err
+	}
+	w.Header().Add("Content-Type", string(contentType))
+
 	http.ServeFile(w, r, p)
 	return nil
 }