-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
138 lines (119 loc) · 3.16 KB
/
paths.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
// Copyright (c) 2021-2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package paths
import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"time"
"golang.org/x/sys/unix"
)
func FileWritable(path string) (writable bool) {
if fi, err := os.Stat(path); err == nil {
if fi.Mode().IsRegular() {
writable = unix.Access(path, unix.W_OK) == nil
}
}
return
}
func IsPipe(fh *os.File) (piped bool) {
if fi, err := fh.Stat(); err == nil {
if mode := fi.Mode(); mode > 0 {
piped = mode&os.ModeNamedPipe != 0
}
}
return
}
func IsDevice(path string) (present bool) {
if fi, err := os.Stat(path); err == nil {
if mode := fi.Mode(); mode > 0 {
present = mode&(os.ModeDevice|os.ModeCharDevice) != 0
}
}
return
}
func IsFile(path string) (found bool) {
if fi, err := os.Stat(path); err == nil {
found = fi.Mode().IsRegular()
}
return
}
func IsDir(path string) (found bool) {
if fi, err := os.Stat(path); err == nil {
found = fi.Mode().IsDir()
}
return
}
func MakeDir(path string, perm os.FileMode) error {
if IsDir(path) {
return fmt.Errorf("directory exists")
}
if IsFile(path) {
return fmt.Errorf("given path is a file")
}
return os.MkdirAll(path, perm)
}
func ReadFile(path string) (content string, err error) {
if IsFile(path) {
var bytes []byte
if bytes, err = ioutil.ReadFile(path); err == nil {
content = string(bytes)
}
return
}
return "", fmt.Errorf("not a file or file not found: %v", path)
}
func WriteFile(path string, content string) (err error) {
err = WriteFileWithPerms(path, content, 0644)
return
}
func WriteFileWithPerms(path string, content string, perm fs.FileMode) (err error) {
err = ioutil.WriteFile(path, []byte(content), perm)
return
}
func CopyFile(src, dst string) (nBytes int64, err error) {
// see: https://opensource.com/article/18/6/copying-files-go
var srcInfo os.FileInfo
if srcInfo, err = os.Stat(src); err != nil {
return 0, err
}
if !srcInfo.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
var srcFile *os.File
if srcFile, err = os.Open(src); err != nil {
return 0, err
}
defer srcFile.Close()
var destination *os.File
if destination, err = os.Create(dst); err != nil {
return 0, err
}
defer destination.Close()
nBytes, err = io.Copy(destination, srcFile)
return
}
// BackupFile will copy a file to the same location, with the same file name and
// suffixed with the given tag and finally a datestamp in the format of:
// YYYYMMDDHHMMSS
func BackupFile(tag, path string) (err error) {
if IsFile(path) {
stamp := time.Now().Format("20060102150405")
dst := fmt.Sprintf("%v.%v.%v", path, tag, stamp)
_, err = CopyFile(path, dst)
}
return
}