/
file.go
62 lines (52 loc) · 1.32 KB
/
file.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
package util
import (
"io"
"os"
"path/filepath"
"fyne.io/fyne"
)
// Exists will return true if the passed path exists on the current system.
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return !os.IsNotExist(err)
}
return true
}
// CopyFile copies the content of a regular file, source, into target path.
func CopyFile(source, target string) error {
return copyFileMode(source, target, 0644)
}
// CopyExeFile copies the content of an executable file, source, into target path.
func CopyExeFile(src, tgt string) error {
return copyFileMode(src, tgt, 0755)
}
// EnsureSubDir will make sure a named directory exists within the parent - creating it if not.
func EnsureSubDir(parent, name string) string {
path := filepath.Join(parent, name)
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.Mkdir(path, os.ModePerm)
if err != nil {
fyne.LogError("Failed to create dirrectory", err)
}
}
return path
}
func copyFileMode(src, tgt string, perm os.FileMode) error {
_, err := os.Stat(src)
if err != nil {
return err
}
source, err := os.Open(filepath.Clean(src))
if err != nil {
return err
}
defer source.Close()
target, err := os.OpenFile(tgt, os.O_RDWR|os.O_CREATE, perm)
if err != nil {
return err
}
defer target.Close()
_, err = io.Copy(target, source)
return err
}