-
Notifications
You must be signed in to change notification settings - Fork 4
/
filesystem.go
143 lines (123 loc) · 2.44 KB
/
filesystem.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
package gryphon
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
func CheckFileExist(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
} else {
return true
}
}
func CreateDir(dirPath string, fileMode os.FileMode) bool {
err := os.MkdirAll(dirPath, fileMode)
if err != nil {
return false
}
return true
}
func RenameFile(pathFile string, name string) error {
err := os.Rename(pathFile, name)
if err != nil {
return err
}
return nil
}
func CreateFile(pathFile string) error {
file, err := os.Create(pathFile)
if err != nil {
return err
}
defer file.Close()
return nil
}
func CreateFileAndWriteData(fileName string, writeData []byte) error {
fileHandle, err := os.Create(fileName)
if err != nil {
return err
}
writer := bufio.NewWriter(fileHandle)
defer fileHandle.Close()
writer.Write(writeData)
writer.Flush()
return nil
}
func CopyFileToDirectory(pathSourceFile string, pathDestFile string) error {
sourceFile, err := os.Open(pathSourceFile)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(pathDestFile)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return err
}
err = destFile.Sync()
if err != nil {
return err
}
sourceFileInfo, err := sourceFile.Stat()
if err != nil {
return err
}
destFileInfo, err := destFile.Stat()
if err != nil {
return err
}
if sourceFileInfo.Size() == destFileInfo.Size() {
} else {
err = errors.New("Bad copy file")
return err
}
return nil
}
func DeleteFile(nameFile string) error {
err := os.Remove(nameFile)
return err
}
func RemoveDirWithContent(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
err = os.RemoveAll(dir)
if err != nil {
return err
}
return nil
}
func WriteToFile(input string) {
file, err := os.OpenFile("", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Could not open example.txt")
return
}
defer file.Close()
_, err2 := file.WriteString("Appending some text to example.txt")
if err2 != nil {
fmt.Println("Could not write text to example.txt")
} else {
fmt.Println("Operation successful! Text has been appended to example.txt")
}
}