-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
146 lines (113 loc) · 2.6 KB
/
files.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
package main
import (
"fmt"
"io"
"math"
"os"
)
var allFiles []string
// GetFileSize: Gets the size of the given file in bytes
func GetFileSize(_file string) int64 {
var fileSize int64
fileSize = -1
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_Arguments, GetFunctionName()), "_file: ", _file)
}
if len(_file) > 0 {
fileInfo, err := os.Stat(_file)
if err == nil {
fileSize = fileInfo.Size()
if fileSize == 0 {
if DEBUG == true {
fmt.Println(UI_EmptyFile)
}
fileSize = -1
}
if fileSize < 0 {
if DEBUG == true {
fmt.Println(UI_InvalidFileSize)
}
fileSize = -1
}
if fileSize > math.MaxInt64 {
if DEBUG == true {
fmt.Println(UI_FileTooBig)
}
fileSize = -1
}
} else {
if DEBUG == true {
fmt.Println(UI_NoFileSize, _file, err.Error())
}
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_file:", _file)
}
return fileSize
}
// ReadFile: Reads given file from start to end
func ReadFile(_file string) int64 {
const bufferSize = 1000
var totalBytesRead int
totalBytesRead = -1
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_Arguments, GetFunctionName()), "_file: ", _file)
}
if len(_file) > 0 {
fileHandle, err := os.OpenFile(_file, os.O_RDONLY, 0)
if err == nil {
buffer := make([]byte, bufferSize)
for {
bytesRead, err := fileHandle.Read(buffer)
totalBytesRead += bytesRead
if err != nil {
if err == io.EOF {
if DEBUG == true {
fmt.Println(_file, ":", UI_EOF)
}
break
} else {
if DEBUG == true {
fmt.Println(_file, ":", err)
}
totalBytesRead = -1
break
}
} else {
if DEBUG == true {
fmt.Println(_file, ":", totalBytesRead)
}
}
}
totalBytesRead += 1
} else {
if DEBUG == true {
fmt.Println(UI_OpenFileError, _file)
}
totalBytesRead = -1
}
fileHandle.Close()
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_file: ", _file)
}
return int64(totalBytesRead)
}
// SkipDir: Returns true if given directory is to be skipped, false otherwise
func SkipDir(_dir string) bool {
var skipDir bool
skipDir = false
if DEBUG == true {
fmt.Println(fmt.Sprintf(UI_Arguments, GetFunctionName()), "_dir: ", _dir, "skipDirs: ", skipDirs)
}
if len(_dir) > 0 && skipDirs != nil && len(skipDirs) > 0 {
for _, currentDir := range skipDirs {
if currentDir == _dir {
skipDir = true
break
}
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_dir:", _dir, "skipDirs: ", skipDirs)
}
return skipDir
}