File: main.go

package info (click to toggle)
golang-github-djherbis-times 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: sh: 10; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 1,809 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/djherbis/times"
)

func main() {
	switch len(os.Args) {
	case 1:
		tempFile()
		fmt.Println()
		tempDir()

	default:
		printTimes(os.Args[1])
	}
}

func tempDir() {
	name, err := ioutil.TempDir("", "")
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(name)
	fmt.Println("# DIR: " + name)

	symname := filepath.Join(filepath.Dir(name), "sym-"+filepath.Base(name))
	if err := os.Symlink(name, symname); err != nil {
		log.Fatal(err)
	}
	defer os.Remove(symname)

	newAtime := time.Now().Add(-10 * time.Second)
	newMtime := time.Now().Add(10 * time.Second)
	if err := os.Chtimes(name, newAtime, newMtime); err != nil {
		log.Fatal(err)
	}

	printTimes(symname)
}

func tempFile() {
	f, err := ioutil.TempFile("", "")
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(f.Name())
	defer f.Close()
	fmt.Println("# FILE: " + f.Name())

	symname := filepath.Join(filepath.Dir(f.Name()), "sym-"+filepath.Base(f.Name()))
	if err := os.Symlink(f.Name(), symname); err != nil {
		log.Fatal(err)
	}
	defer os.Remove(symname)

	newAtime := time.Now().Add(-10 * time.Second)
	newMtime := time.Now().Add(10 * time.Second)
	if err := os.Chtimes(f.Name(), newAtime, newMtime); err != nil {
		log.Fatal(err)
	}

	printTimes(symname)
}

func printTimes(name string) {
	fmt.Println("## Stat:", name)
	printTimespec(times.Stat(name))

	fmt.Println("\n## Lstat:", name)
	printTimespec(times.Lstat(name))
}

func printTimespec(ts times.Timespec, err error) {
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("AccessTime:", ts.AccessTime())
	fmt.Println("ModTime:", ts.ModTime())

	if ts.HasChangeTime() {
		fmt.Println("ChangeTime:", ts.ChangeTime())
	}

	if ts.HasBirthTime() {
		fmt.Println("BirthTime:", ts.BirthTime())
	}
}