talos
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package utils
6
7import (
8"io/fs"
9"os"
10"path/filepath"
11"time"
12)
13
14// TouchFiles updates mtime for all the files under root if SOURCE_DATE_EPOCH is set.
15func TouchFiles(printf func(string, ...any), root string) error {
16epochInt, ok, err := SourceDateEpoch()
17if err != nil {
18return err
19}
20
21if !ok {
22return nil
23}
24
25timestamp := time.Unix(epochInt, 0)
26
27printf("changing timestamps under %q to %s", root, timestamp)
28
29return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
30if err != nil {
31return err
32}
33
34return os.Chtimes(path, timestamp, timestamp)
35})
36}
37