-
Notifications
You must be signed in to change notification settings - Fork 79
/
fileutil.go
164 lines (148 loc) · 3.59 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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
}
// GetTempFilePath creates a file path to a temporary file that does not exist yet.
func GetTempFilePath() (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()
f.Close()
return path, nil
}
// Exist returns true if a file or directory exists.
func Exist(name string) bool {
if name == "" {
return false
}
_, err := os.Stat(name)
return err == nil
}
// Copy copies a file and writes/overwrites to the destination 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()
f, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create(%q): %v", dst, err)
}
defer f.Close()
if _, err = io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if _, err := f.Seek(0, 0); err != nil {
return err
}
return nil
}
// CopyAppend copies a file and appends to the destination file.
func CopyAppend(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()
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return fmt.Errorf("open(%q): %v", dst, err)
}
defer f.Close()
if _, err = io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if _, err := f.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
}
// IsDirWriteable checks if dir is writable by writing and removing a file.
// It returns error if dir is NOT writable.
// If the director does not exist, it returns nil.
func IsDirWriteable(dir string) error {
if !Exist(dir) {
return nil
}
f := filepath.Join(dir, ".touch")
// grants owner to make/remove files inside the directory
if err := ioutil.WriteFile(f, []byte(""), 0700); err != nil {
return err
}
return os.Remove(f)
}