-
Notifications
You must be signed in to change notification settings - Fork 394
/
sync.go
66 lines (56 loc) · 1.22 KB
/
sync.go
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
// Copyright 2016-2020 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !plan9
// +build !plan9
// sync command in Go.
//
// Synopsis:
// sync [-df] [FILE]
//
package main
import (
"flag"
"fmt"
"log"
"os"
"syscall"
"golang.org/x/sys/unix"
)
var (
data = flag.Bool("data", false, "sync file data, no metadata")
filesystem = flag.Bool("filesystem", false, "commit filesystem caches to disk")
)
func init() {
flag.BoolVar(data, "d", false, "")
flag.BoolVar(filesystem, "f", false, "")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION] [FILE]...\n", os.Args[0])
flag.PrintDefaults()
}
}
func doSyscall(syscallNum uintptr) {
for _, fileName := range flag.Args() {
f, err := os.OpenFile(fileName, syscall.O_RDONLY|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0644)
if err != nil {
log.Fatal(err)
}
_, _, err = syscall.Syscall(syscallNum, uintptr(f.Fd()), 0, 0)
if err != nil {
log.Fatal(err)
}
f.Close()
}
}
func main() {
flag.Parse()
switch {
case *data:
doSyscall(unix.SYS_FDATASYNC)
case *filesystem:
doSyscall(unix.SYS_SYNCFS)
default:
syscall.Sync()
os.Exit(0)
}
}