From 89430ffe65636b45cab834c94f176c1dc1d8a167 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 10 May 2018 11:06:53 -0700 Subject: hooks: Order injection by collated JSON filename We also considered ordering with sort.Strings, but Matthew rejected that because it uses a byte-by-byte UTF-8 comparison [1] which would fail many language-specific conventions [2]. There's some more discussion of the localeToLanguage mapping in [3]. Currently language.Parse does not handle either 'C' or 'POSIX', returning: und, language: tag is not well-formed for both. [1]: https://github.com/projectatomic/libpod/pull/686#issuecomment-387914358 [2]: https://en.wikipedia.org/wiki/Alphabetical_order#Language-specific_conventions [3]: https://github.com/golang/go/issues/25340 Signed-off-by: W. Trevor King Closes: #686 Approved by: mheon --- libpod/container_internal.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 992c1d07b..1c0bcb07f 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -32,6 +32,7 @@ import ( "github.com/sirupsen/logrus" "github.com/ulule/deepcopier" "golang.org/x/sys/unix" + "golang.org/x/text/language" ) const ( @@ -39,6 +40,15 @@ const ( artifactsDir = "artifacts" ) +var ( + // localeToLanguage maps from locale values to language tags. + localeToLanguage = map[string]string{ + "": "und-u-va-posix", + "c": "und-u-va-posix", + "posix": "und-u-va-posix", + } +) + // rootFsSize gets the size of the container's root filesystem // A container FS is split into two parts. The first is the top layer, a // mutable layer, and the rest is the RootFS: the set of immutable layers @@ -1287,7 +1297,34 @@ func (c *Container) setupOCIHooks(ctx context.Context, g *generate.Generator) er return nil } - manager, err := hooks.New(ctx, []string{c.runtime.config.HooksDir}) + var locale string + var ok bool + for _, envVar := range []string{ + "LC_ALL", + "LC_COLLATE", + "LANG", + } { + locale, ok = os.LookupEnv(envVar) + if ok { + break + } + } + + langString, ok := localeToLanguage[strings.ToLower(locale)] + if !ok { + langString = locale + } + + lang, err := language.Parse(langString) + if err != nil { + logrus.Warnf("failed to parse language %q: %s", langString, err) + lang, err = language.Parse("und-u-va-posix") + if err != nil { + return err + } + } + + manager, err := hooks.New(ctx, []string{c.runtime.config.HooksDir}, lang) if err != nil { if c.runtime.config.HooksDirNotExistFatal || !os.IsNotExist(err) { return err -- cgit v1.2.3-54-g00ecf