Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeutil: fix unknown time zone 'posixrules' (#20560) #20605

Merged
merged 7 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions util/timeutil/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ type locCache struct {
locMap map[string]*time.Location
}

// inferOneStepLinkForPath only read one step link for the path, not like filepath.EvalSymlinks, which gets the
// recursive final linked file of the path.
func inferOneStepLinkForPath(path string) (string, error) {
fileInfo, err := os.Lstat(path)
if err != nil {
return path, err
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
path, err = os.Readlink(path)
if err != nil {
return path, err
}
}
return path, nil
}

// InferSystemTZ reads system timezone from `TZ`, the path of the soft link of `/etc/localtime`. If both of them are failed, system timezone will be set to `UTC`.
// It is exported because we need to use it during bootstap stage. And it should be only used at that stage.
func InferSystemTZ() string {
Expand All @@ -72,6 +88,13 @@ func InferSystemTZ() string {
case !ok:
path, err1 := filepath.EvalSymlinks("/etc/localtime")
if err1 == nil {
if strings.Index(path, "posixrules") != -1 {
path, err1 = inferOneStepLinkForPath("/etc/localtime")
if err1 != nil {
logutil.BgLogger().Error("locate timezone files failed", zap.Error(err1))
return ""
}
}
name, err2 := inferTZNameFromFileName(path)
if err2 == nil {
return name
Expand Down
23 changes: 23 additions & 0 deletions util/timeutil/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package timeutil

import (
"os"
"path/filepath"
"strings"
"testing"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -64,3 +66,24 @@ func (s *testTimeSuite) TestLocal(c *C) {
c.Assert(loc.String(), Equals, "UTC")
os.Unsetenv("TZ")
}

func (s *testTimeSuite) TestInferOneStepLinkForPath(c *C) {
os.Remove("/tmp/testlink1")
os.Remove("/tmp/testlink2")
os.Remove("/tmp/testlink3")
var link2, link3 string
var err error
var link1 *os.File
link1, err = os.Create("/tmp/testlink1")
c.Assert(err, IsNil)
err = os.Symlink(link1.Name(), "/tmp/testlink2")
c.Assert(err, IsNil)
err = os.Symlink("/tmp/testlink2", "/tmp/testlink3")
c.Assert(err, IsNil)
link2, err = inferOneStepLinkForPath("/tmp/testlink3")
c.Assert(err, IsNil)
c.Assert(link2, Equals, "/tmp/testlink2")
link3, err = filepath.EvalSymlinks("/tmp/testlink3")
c.Assert(err, IsNil)
c.Assert(strings.Index(link3, link1.Name()), Not(Equals), -1)
}