You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.8 KiB
147 lines
3.8 KiB
|
2 months ago
|
package systemtime
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
DefaultZoneinfoRoot = "/usr/share/zoneinfo"
|
||
|
|
DefaultLocaltimePath = "/etc/localtime"
|
||
|
|
maxZoneinfoBytes = 1024 * 1024
|
||
|
|
)
|
||
|
|
|
||
|
|
type OSApplier struct {
|
||
|
|
zoneinfoRoot string
|
||
|
|
localtimePath string
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewOSApplier(zoneinfoRoot, localtimePath string) (*OSApplier, error) {
|
||
|
|
if !filepath.IsAbs(zoneinfoRoot) {
|
||
|
|
return nil, errors.New("zoneinfo root must be an absolute path")
|
||
|
|
}
|
||
|
|
if !filepath.IsAbs(localtimePath) {
|
||
|
|
return nil, errors.New("localtime path must be an absolute path")
|
||
|
|
}
|
||
|
|
return &OSApplier{zoneinfoRoot: filepath.Clean(zoneinfoRoot), localtimePath: filepath.Clean(localtimePath)}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *OSApplier) ApplyTimezone(ctx context.Context, timezone string) (func() error, error) {
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
normalized, err := NormalizeTimezone(timezone)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
source := filepath.Join(a.zoneinfoRoot, filepath.FromSlash(normalized))
|
||
|
|
if relative, err := filepath.Rel(a.zoneinfoRoot, source); err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) || filepath.IsAbs(relative) {
|
||
|
|
return nil, errors.New("timezone resolves outside the zoneinfo root")
|
||
|
|
}
|
||
|
|
zoneData, err := readBoundedFile(source, maxZoneinfoBytes)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("read timezone %q: %w", normalized, err)
|
||
|
|
}
|
||
|
|
previous, previousMode, existed, err := readExistingLocaltime(a.localtimePath)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := writeAtomicFile(a.localtimePath, zoneData, 0o644); err != nil {
|
||
|
|
return nil, fmt.Errorf("apply timezone %q: %w", normalized, err)
|
||
|
|
}
|
||
|
|
rollback := func() error {
|
||
|
|
if !existed {
|
||
|
|
if err := os.Remove(a.localtimePath); err != nil && !os.IsNotExist(err) {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return writeAtomicFile(a.localtimePath, previous, previousMode)
|
||
|
|
}
|
||
|
|
return rollback, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *OSApplier) SetTime(ctx context.Context, value time.Time) error {
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return setSystemTime(value.UTC())
|
||
|
|
}
|
||
|
|
|
||
|
|
func readExistingLocaltime(path string) ([]byte, os.FileMode, bool, error) {
|
||
|
|
info, err := os.Stat(path)
|
||
|
|
if err != nil {
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
return nil, 0, false, nil
|
||
|
|
}
|
||
|
|
return nil, 0, false, fmt.Errorf("inspect current localtime: %w", err)
|
||
|
|
}
|
||
|
|
if !info.Mode().IsRegular() {
|
||
|
|
return nil, 0, false, errors.New("current localtime is not a regular file")
|
||
|
|
}
|
||
|
|
content, err := readBoundedFile(path, maxZoneinfoBytes)
|
||
|
|
if err != nil {
|
||
|
|
return nil, 0, false, fmt.Errorf("read current localtime: %w", err)
|
||
|
|
}
|
||
|
|
return content, info.Mode().Perm(), true, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func readBoundedFile(path string, limit int64) ([]byte, error) {
|
||
|
|
file, err := os.Open(path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer file.Close()
|
||
|
|
content, err := io.ReadAll(io.LimitReader(file, limit+1))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if int64(len(content)) > limit {
|
||
|
|
return nil, errors.New("file exceeds the allowed size")
|
||
|
|
}
|
||
|
|
return content, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func writeAtomicFile(path string, content []byte, mode os.FileMode) error {
|
||
|
|
directory := filepath.Dir(path)
|
||
|
|
temporary, err := os.CreateTemp(directory, ".tera-localtime-*")
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
temporaryPath := temporary.Name()
|
||
|
|
keep := false
|
||
|
|
defer func() {
|
||
|
|
_ = temporary.Close()
|
||
|
|
if !keep {
|
||
|
|
_ = os.Remove(temporaryPath)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
if _, err := temporary.Write(content); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := temporary.Chmod(mode.Perm()); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := temporary.Sync(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := temporary.Close(); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := os.Rename(temporaryPath, path); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
keep = true
|
||
|
|
if directoryHandle, err := os.Open(directory); err == nil {
|
||
|
|
_ = directoryHandle.Sync()
|
||
|
|
_ = directoryHandle.Close()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|