-
Notifications
You must be signed in to change notification settings - Fork 82
/
fileutil.go
104 lines (94 loc) · 2.13 KB
/
fileutil.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
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
97
98
99
100
101
102
103
104
// Package fileutil implements file utilities.
package fileutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"time"
)
// MkTmpDir creates a temp directory.
func MkTmpDir(baseDir string, pfx string) (dir string) {
if baseDir == "" {
baseDir = os.TempDir()
}
var err error
dir, err = ioutil.TempDir(baseDir, pfx)
if err != nil {
panic(err)
}
return dir
}
// WriteTempFile writes data to a temporary file.
func WriteTempFile(d []byte) (path string, err error) {
var f *os.File
f, err = ioutil.TempFile(os.TempDir(), fmt.Sprintf("%X", time.Now().UnixNano()))
if err != nil {
return "", err
}
path = f.Name()
_, err = f.Write(d)
f.Close()
return path, err
}
// WriteToTempDir writes data to a temporary directory.
func WriteToTempDir(p string, d []byte) (path string, err error) {
path = filepath.Join(os.TempDir(), p)
var f *os.File
f, err = os.Create(path)
if err != nil {
return "", err
}
path = f.Name()
_, err = f.Write(d)
f.Close()
return path, err
}
// Exist returns true if a file or directory exists.
func Exist(name string) bool {
_, err := os.Stat(name)
return err == nil
}
// Copy copies a file.
func Copy(src, dst string) error {
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return fmt.Errorf("mkdirall: %v", err)
}
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("open(%q): %v", src, err)
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create(%q): %v", dst, err)
}
defer w.Close()
if _, err = io.Copy(w, r); err != nil {
return err
}
if err := w.Sync(); err != nil {
return err
}
if _, err := w.Seek(0, 0); err != nil {
return err
}
return nil
}
// EnsureExecutable sets the executable file mode bits, for all users, to ensure that we can execute a file
func EnsureExecutable(p string) error {
s, err := os.Stat(p)
if err != nil {
return fmt.Errorf("error doing stat on %q: %v", p, err)
}
m := s.Mode()
if m&(syscall.S_IXOTH|syscall.S_IXGRP|syscall.S_IXUSR) != 0 {
return nil
}
if err := os.Chmod(p, s.Mode()|0111); err != nil {
return fmt.Errorf("error doing chmod on %q: %v", p, err)
}
return nil
}