-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (162 loc) · 4.55 KB
/
main.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"log"
"os"
"path/filepath"
"syscall"
"time"
)
const (
not_symlink = "NULL"
deleted_symlink = "DELETED"
)
/*
Error types:
1X: Input errors
10: Invalid number of arguments
2X: File operation errors
20: Can not find the specified file
3X: Checksum errors
30: Can not open the specified file
31: Error copying the file in the hash interface
*/
func fatal_error(err error, code int) {
if err != nil {
log.Fatalf("[%d] %v", code, err.Error())
}
}
func _get_stats(file_path string) os.FileInfo {
stats, err := os.Lstat(file_path)
fatal_error(err, 20)
return stats
}
func get_filename(file_path string) string {
stats := _get_stats(file_path)
return stats.Name()
}
func get_filepath(file_path string) string {
filepath, err := filepath.Abs(file_path)
fatal_error(err, 20)
return filepath
}
func get_file_size(file_path string) int64 {
stats := _get_stats(file_path)
return stats.Size()
}
func get_permissions(file_path string) os.FileMode {
stats := _get_stats(file_path)
return stats.Mode()
}
func get_file_modtime(file_path string) time.Time {
stats := _get_stats(file_path)
return stats.ModTime()
}
func file_is_dir(file_path string) bool {
stats := _get_stats(file_path)
return stats.IsDir()
}
func file_is_symbolic(file_path string) bool {
stats := _get_stats(file_path)
return stats.Mode()&os.ModeSymlink != 0
}
func get_file_inode(file_path string) uint64 {
stats := _get_stats(file_path)
return stats.Sys().(*syscall.Stat_t).Ino
}
func get_file_ino(file_path string) uint64 {
stats := _get_stats(file_path)
return stats.Sys().(*syscall.Stat_t).Ino
}
func get_file_hardlinks(file_path string) uint64 {
stats := _get_stats(file_path)
return stats.Sys().(*syscall.Stat_t).Nlink
}
func get_stats(file_path string) (string, int64, os.FileMode, time.Time, bool, uint64, uint64, bool, string) {
stats := _get_stats(file_path)
is_symbolic := stats.Mode()&os.ModeSymlink != 0
var resolve_symbolic string = not_symlink
var err error
if is_symbolic {
resolve_symbolic, err = os.Readlink(file_path)
if err != nil {
log.Print("[WARNING] Unable to resolve the symlink", file_path)
}
if _, err := os.Stat(file_path); os.IsNotExist(err) {
log.Print("[WARNING] Symbolic link %s goes to non existent file %s", file_path, resolve_symbolic)
resolve_symbolic = deleted_symlink
}
}
return get_filepath(file_path), stats.Size(), stats.Mode(), stats.ModTime(), stats.IsDir(), stats.Sys().(*syscall.Stat_t).Ino, stats.Sys().(*syscall.Stat_t).Nlink, is_symbolic, resolve_symbolic
}
func get_checksum(file_path string) string {
// Initialize the file md5 string
var file_md5 string
// Open the specified file path
file, err := os.Open(file_path)
fatal_error(err, 20)
// Close the file when the current functions returns
defer file.Close()
// Open hash interface
hash := md5.New()
// Copy the file in the hash interface and check for any error
if _, err := io.Copy(hash, file); err != nil {
fatal_error(err, 31)
}
// Get the 16 bytes hash
hash_in_bytes := hash.Sum(nil)[:16]
// Converts the bytes to string and return it
file_md5 = hex.EncodeToString(hash_in_bytes)
return file_md5
}
func file_monitoring(sleep_time float32) {
for true {
name, size, mode, modtime, isdir, ino, hlinks, slink, resolve_symbolic := get_stats(os.Args[1])
// Get the file name
fmt.Println("Name:\t\t", name)
// Get file size
fmt.Println("Size:\t\t", size)
// Get the file mode
fmt.Println("Mode:\t\t", mode)
// Get the file modification time
fmt.Println("ModTime:\t", modtime)
// Check if is file or a directory
fmt.Println("IsDir:\t\t", isdir)
// Get the file inode
fmt.Println("Inode:\t\t", ino)
// Get the file hardlinks
fmt.Println("Hlinks:\t\t", hlinks)
// Check if it is a symbolic link
fmt.Println("IsSymlink:\t", slink)
// Follow symbolic link
fmt.Println("RSymlink:\t", resolve_symbolic)
if resolve_symbolic != deleted_symlink {
// Get the file md5 hash
fmt.Println("MD5:\t\t", get_checksum(os.Args[1]))
} else {
fmt.Println("MD5:\t\t N/A")
}
time.Sleep(time.Duration(sleep_time) * time.Second)
}
}
func main() {
// Create the log file
logger, err := os.OpenFile("fim.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println("[FATAL] Error opening file: %v", err)
}
defer logger.Close()
log.SetOutput(logger)
// Check the users args
if len(os.Args) != 2 {
fmt.Println("[ERROR] You must specify one file path")
os.Exit(10)
}
go file_monitoring(5.0)
for true {
time.Sleep(time.Second * 60)
}
}