-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfunc.go
123 lines (112 loc) · 3.04 KB
/
dbfunc.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
package tardigrade
import (
"fmt"
"io"
"os"
"path/filepath"
)
// CreatedDBCopy creates a copy of the Database and store in UserHomeDir().
func (tar *Tardigrade) CreatedDBCopy(db string) (msg string, status bool) {
status = true
dirname, err := os.UserHomeDir()
CheckError("CreatedDBCopy(0)", err)
target := "tardigradecopy.db"
src := db
if !tar.fileExists(src) {
msg = fmt.Sprintf("Failed: database %s missing!", src)
return msg, false
}
fin, err := os.Open(src)
CheckError("CreatedDBCopy(1)", err)
defer fin.Close()
PATH_SEPARATOR := tar.GetOS()
dst := fmt.Sprintf("%s%s%s", dirname, string(PATH_SEPARATOR), target)
buf := make([]byte, 1024)
tmp, err := os.Create(dst)
CheckError("CreatedDBCopy(2)", err)
defer tmp.Close()
buffering:
for {
n, err := fin.Read(buf)
if err != nil && err != io.EOF {
CheckError("CreatedDBCopy(3)", err)
msg = "Failed: buffer error failed to create database!"
return msg, false
}
if n == 0 {
fin.Close()
tmp.Close()
break buffering
}
if _, err := tmp.Write(buf[:n]); err != nil {
CheckError("CreatedDBCopy(4)", err)
msg = "Failed: permission error failed to create database!"
return msg, false
}
}
msg = fmt.Sprintf("Copy: %s", dst)
return msg, true
}
// CreateDB - This function will create a database file if it does not exist and return true | false
func (tar *Tardigrade) CreateDB(db string) (msg string, status bool) {
status = true
fname := db
pwd, _ := filepath.Abs(fname)
if !tar.fileExists(fname) {
_, err := os.Create(fname)
CheckError("CreateDB(2)", err)
if !tar.fileExists(fname) {
CheckError("CreateDB(3)", err)
status = false
return fmt.Sprintf("Failed: %v", pwd), status
}
} else {
status = false
return fmt.Sprintf("Exist: %v", pwd), status
}
return fmt.Sprintf("Created: %v", pwd), status
}
// DeleteDB - WARNING - this function delete the database file return true | false
func (tar *Tardigrade) DeleteDB(db string) (msg string, status bool) {
fname := db
status = true
pwd, _ := filepath.Abs(fname)
if tar.fileExists(fname) {
delete := os.Remove(fname)
CheckError("DeleteDB(1)", delete)
if tar.fileExists(fname) {
status = false
return fmt.Sprintf("Failed: %v", pwd), status
}
} else {
status = false
return fmt.Sprintf("Unavailable: %v", pwd), status
}
return fmt.Sprintf("Removed: %v", pwd), status
}
// fileExists function will check if the database exists and return true / false
func (tar *Tardigrade) fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// EmptyDB function - WARNING - this will destroy the database and all data stored in it!
func (tar *Tardigrade) EmptyDB(db string) (msg string, status bool) {
_, status = tar.DeleteDB(db)
if status {
_, status = tar.CreateDB(db)
if !status {
status = false
msg = "Failed: no permission to re-create!"
return msg, status
}
} else {
status = false
msg = "Missing: could not find database!"
return msg, status
}
msg = "Empty: database now clean!"
return msg, status
}