/
util.go
265 lines (226 loc) · 7.5 KB
/
util.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package utils
/*
* This file contains miscellaneous functions that are generally useful and
* don't fit into any other file.
*/
import (
"crypto/sha256"
"fmt"
"os"
"os/exec"
"os/signal"
path "path/filepath"
"regexp"
"strings"
"time"
"github.com/cloudberrydb/gp-common-go-libs/dbconn"
"github.com/cloudberrydb/gp-common-go-libs/gplog"
"github.com/cloudberrydb/gp-common-go-libs/operating"
"github.com/cloudberrydb/gpbackup/filepath"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
const MINIMUM_GPDB4_VERSION = "1.1.0"
const MINIMUM_GPDB5_VERSION = "1.1.0"
/*
* General helper functions
*/
func CommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func RemoveFileIfExists(filename string) error {
baseFilename := path.Base(filename)
if FileExists(filename) {
gplog.Debug("File %s: Exists, Attempting Removal", baseFilename)
err := os.Remove(filename)
if err != nil {
gplog.Error("File %s: Failed to remove. Error %s", baseFilename, err.Error())
return err
}
gplog.Debug("File %s: Successfully removed", baseFilename)
} else {
gplog.Debug("File %s: Does not exist. No removal needed", baseFilename)
}
return nil
}
func OpenFileForWrite(filename string) (*os.File, error) {
baseFilename := path.Base(filename)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
gplog.Error("File %s: Failed to open. Error %s", baseFilename, err.Error())
}
return file, err
}
func WriteToFileAndMakeReadOnly(filename string, contents []byte) error {
baseFilename := path.Base(filename)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
gplog.Error("File %s: Failed to open. Error %s", baseFilename, err.Error())
return err
}
_, err = file.Write(contents)
if err != nil {
gplog.Error("File %s: Could not write to file. Error %s", baseFilename, err.Error())
return err
}
err = file.Chmod(0444)
if err != nil {
gplog.Error("File %s: Could not chmod. Error %s", baseFilename, err.Error())
return err
}
err = file.Sync()
if err != nil {
gplog.Error("File %s: Could not sync. Error %s", baseFilename, err.Error())
return err
}
return file.Close()
}
// Dollar-quoting logic is based on appendStringLiteralDQ() in pg_dump.
func DollarQuoteString(literal string) string {
delimStr := "_XXXXXXX"
quoteStr := ""
for i := range delimStr {
testStr := "$" + delimStr[0:i]
if !strings.Contains(literal, testStr) {
quoteStr = testStr + "$"
break
}
}
return quoteStr + literal + quoteStr
}
// This function assumes that all identifiers are already appropriately quoted
func MakeFQN(schema string, object string) string {
return fmt.Sprintf("%s.%s", schema, object)
}
// Since we currently split schema and table on the dot (.), we can't allow
// users to filter backup or restore tables with dots in the schema or table.
func ValidateFQNs(tableList []string) error {
validFormat := regexp.MustCompile(`^[^.]+\.[^.]+$`)
for _, fqn := range tableList {
if !validFormat.Match([]byte(fqn)) {
return errors.Errorf(`Table "%s" is not correctly fully-qualified. Please ensure table is in the format "schema.table" and both the schema and table does not contain a dot (.).`, fqn)
}
}
return nil
}
func ValidateFullPath(path string) error {
if len(path) > 0 && !(strings.HasPrefix(path, "/") || strings.HasPrefix(path, "~")) {
return errors.Errorf("%s is not an absolute path.", path)
}
return nil
}
// A description of compression levels for some compression type
type CompressionLevelsDescription struct {
Min int
Max int
}
func ValidateCompressionTypeAndLevel(compressionType string, compressionLevel int) error {
compressionLevelsForType := map[string]CompressionLevelsDescription{
"gzip": {Min: 1, Max: 9},
"zstd": {Min: 1, Max: 19},
}
if levelsDescription, ok := compressionLevelsForType[compressionType]; ok {
if compressionLevel < levelsDescription.Min || compressionLevel > levelsDescription.Max {
return fmt.Errorf("compression type '%s' only allows compression levels between %d and %d, but the provided level is %d", compressionType, levelsDescription.Min, levelsDescription.Max, compressionLevel)
}
} else {
return fmt.Errorf("unknown compression type '%s'", compressionType)
}
return nil
}
func InitializeSignalHandler(cleanupFunc func(bool), procDesc string, termFlag *bool) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, unix.SIGINT, unix.SIGTERM)
go func() {
sig := <-signalChan
fmt.Println() // Add newline after "^C" is printed
switch sig {
case unix.SIGINT:
gplog.Warn("Received an interrupt signal, aborting %s", procDesc)
case unix.SIGTERM:
gplog.Warn("Received a termination signal, aborting %s", procDesc)
}
*termFlag = true
cleanupFunc(true)
os.Exit(2)
}()
}
// TODO: Uniquely identify COPY commands in the multiple data file case to allow terminating sessions
func TerminateHangingCopySessions(connectionPool *dbconn.DBConn, fpInfo filepath.FilePathInfo, appName string) {
var query string
copyFileName := fpInfo.GetSegmentPipePathForCopyCommand()
query = fmt.Sprintf(`SELECT
pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE application_name = '%s'
AND query LIKE '%%%s%%'
AND pid <> pg_backend_pid()`, appName, copyFileName)
// We don't check the error as the connection may have finished or been previously terminated
_, _ = connectionPool.Exec(query)
}
func ValidateGPDBVersionCompatibility(connectionPool *dbconn.DBConn) {
}
func LogExecutionTime(start time.Time, name string) {
elapsed := time.Since(start)
gplog.Debug(fmt.Sprintf("%s took %s", name, elapsed))
}
func Exists(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func SchemaIsExcludedByUser(inSchemasUserInput []string, exSchemasUserInput []string, schemaName string) bool {
included := Exists(inSchemasUserInput, schemaName) || len(inSchemasUserInput) == 0
excluded := Exists(exSchemasUserInput, schemaName)
return excluded || !included
}
func RelationIsExcludedByUser(inRelationsUserInput []string, exRelationsUserInput []string, tableFQN string) bool {
included := Exists(inRelationsUserInput, tableFQN) || len(inRelationsUserInput) == 0
excluded := Exists(exRelationsUserInput, tableFQN)
return excluded || !included
}
func UnquoteIdent(ident string) string {
if len(ident) <= 1 {
return ident
}
if ident[0] == '"' && ident[len(ident)-1] == '"' {
ident = ident[1 : len(ident)-1]
unescape := strings.NewReplacer(`""`, `"`)
ident = unescape.Replace(ident)
}
return ident
}
func QuoteIdent(connectionPool *dbconn.DBConn, ident string) string {
return dbconn.MustSelectString(connectionPool, fmt.Sprintf(`SELECT quote_ident('%s')`, EscapeSingleQuotes(ident)))
}
func SliceToQuotedString(slice []string) string {
quotedStrings := make([]string, len(slice))
for i, str := range slice {
quotedStrings[i] = fmt.Sprintf("'%s'", EscapeSingleQuotes(str))
}
return strings.Join(quotedStrings, ",")
}
func EscapeSingleQuotes(str string) string {
return strings.Replace(str, "'", "''", -1)
}
func GetFileHash(filename string) ([32]byte, error) {
contents, err := operating.System.ReadFile(filename)
if err != nil {
gplog.Error("Failed to read contents of file %s: %v", filename, err)
return [32]byte{}, err
}
filehash := sha256.Sum256(contents)
if err != nil {
gplog.Error("Failed to hash contents of file %s: %v", filename, err)
return [32]byte{}, err
}
return filehash, nil
}