-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileHandler.go
More file actions
184 lines (156 loc) · 3.51 KB
/
fileHandler.go
File metadata and controls
184 lines (156 loc) · 3.51 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* build with go run *.go */
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
)
func discoverFileLocks() map[string]*sync.RWMutex {
existingFileLocks := make(map[string]*sync.RWMutex)
files, err := ioutil.ReadDir(DIRECTORY)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
existingFileLocks[f.Name()] = &sync.RWMutex{}
}
return existingFileLocks
}
func doesFileExist(path, fileName string) bool {
if _, err := os.Stat(path + fileName); err == nil {
//If file exists but no lock exists for that file, then create the lock
if _, ok := fileLocks[fileName]; !ok {
fileLocks[fileName] = &sync.RWMutex{}
}
// Success
return true
}
// The path does not exist or some error occurred.
return false
}
func createFile(path, fileName string) {
createFileLock.Lock()
defer createFileLock.Unlock()
// detect if file exists
var _, err = os.Stat(path + fileName)
// create file if none exists
if os.IsNotExist(err) {
var file, err = os.Create(path + fileName)
if isError(err) {
// ERROR: Error creating file
return
}
defer file.Close()
fileLocks[file.Name()] = &sync.RWMutex{}
}
// Success
}
func appendBytesFile(path, fileName string, message []byte) {
file, err := os.OpenFile(path+fileName, os.O_APPEND|os.O_WRONLY, 0600)
if isError(err) {
// ERROR: error opening file
return
}
defer file.Close()
if lock, ok := fileLocks[file.Name()]; ok {
lock.Lock()
defer lock.Unlock()
if _, err = file.Write(message); isError(err) {
// ERROR: error writing to file
return
}
if err = file.Sync(); isError(err) {
// ERROR: Error syncing file
return
}
//Success
return
}
// Error: File lock does not exist
return
}
func appendFile(path, fileName, message string) {
file, err := os.OpenFile(path+fileName, os.O_APPEND|os.O_WRONLY, 0600)
if isError(err) {
return
}
defer file.Close()
if lock, ok := fileLocks[file.Name()]; ok {
lock.Lock()
defer lock.Unlock()
if _, err = file.WriteString(message); isError(err) {
// ERROR: Error writing to file
return
}
if err = file.Sync(); isError(err) {
// ERROR: Error syncing file
return
}
//Success
return
}
// Error: File lock does not exist
return
}
func getLogFileLength(path, fileName string) int64 {
contents := strings.Split(string(readFile(path, fileName)), "\n")
fileToCommitName := contents[0]
if doesFileExist(DIRECTORY, fileToCommitName) {
file, err := os.Open(DIRECTORY + fileToCommitName)
defer file.Close()
if lock, ok := fileLocks[file.Name()]; ok {
lock.RLock()
defer lock.RUnlock()
if isError(err) {
// ERROR: Could not open file, IO error
return 0
}
fi, err := file.Stat()
if err != nil {
// ERROR: Could not obtain stat, handle error
return 0
}
// Success
return fi.Size()
}
// ERROR: File lock does not exist
return 0
}
// Success
return 0
}
func readFile(path, fileName string) []byte {
// read whole file into memory from FILENAME
if lock, ok := fileLocks[fileName]; ok {
lock.RLock()
defer lock.RUnlock()
data, err := ioutil.ReadFile(path + fileName)
if err != nil {
// ERROR: error reading file
return []byte{}
}
// Success
return data
}
// ERROR: File lock does not exist
return []byte{}
}
func deleteFile(path, fileName string) {
var err = os.Remove(path + fileName)
if isError(err) {
// ERROR: error deleting file
return
}
delete(fileLocks, fileName)
// Success
return
}
func isError(err error) bool {
if err != nil {
fmt.Println(err.Error())
}
return (err != nil)
}