-
Notifications
You must be signed in to change notification settings - Fork 82
/
util.go
166 lines (154 loc) · 4.15 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
// Copyright 2019 PayPal Inc.
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lib
import (
"bytes"
"errors"
"github.com/paypal/hera/common"
"github.com/paypal/hera/utility"
"github.com/paypal/hera/utility/encoding/netstring"
"io"
"net"
"os"
"strings"
"syscall"
)
// WriteAll writes data in a loop until all is sent
// TODO: is this needed? isn't net.Conn.Write() all or nothing?
func WriteAll(w io.Writer, data []byte) error {
towrite := len(data)
for towrite > 0 {
n, err := w.Write(data)
if err != nil {
return err
}
towrite -= n
}
return nil
}
// ParseBool returns true of the string is one of "1", "t", "T", "true", "TRUE", "True", "y", "Y" and
// false if it is one of "0", "f", "F", "false", "FALSE", "False", "n", "N"
func ParseBool(str string) (value bool, err error) {
switch str {
case "1", "t", "T", "true", "TRUE", "True", "y", "Y":
return true, nil
case "0", "f", "F", "false", "FALSE", "False", "n", "N":
return false, nil
}
return false, errors.New("ParseBool syntax error")
}
// DebugString truncates the string if it is larger than 200 bytes
func DebugString(data []byte) string {
if len(data) > 200 {
var buf bytes.Buffer
buf.WriteString(string(data[0:90]))
buf.WriteString("...")
buf.WriteString(string(data[len(data)-90:]))
return buf.String()
}
return string(data)
}
// IsPidRunning checks to see if pid still associates to a running process
func IsPidRunning(pid int) (isRunning bool) {
process, err := os.FindProcess(pid)
if err != nil {
return false
}
err = process.Signal(syscall.Signal(0))
return (err == nil)
}
/*
1st return value: the number
2nd return value: the number of digits
*/
func atoi(bf []byte) (int, int) {
sz := len(bf)
ret := 0
for i := 0; i < sz; i++ {
digit := int(bf[i] - '0')
if (digit < 0) || (digit > 9) {
return ret, i
}
ret = ret*10 + digit
}
return ret, sz
}
/*
1st return value: the number
2nd return value: the number of digits
*/
func atoui(str string) (uint64, int) {
sz := len(str)
var ret uint64
for i := 0; i < sz; i++ {
digit := uint64(str[i] - '0')
if (digit < 0) || (digit > 9) {
return ret, i
}
ret = ret*10 + digit
}
return ret, sz
}
// IPAddrStr stringifies the IP address
func IPAddrStr(address net.Addr) string {
str := address.String()
if 0 == len(str) {
return ""
}
lidx := strings.LastIndex(str, ":")
if -1 == lidx {
return str
}
return str[:lidx]
}
// NetstringFromBytes creates a netstring containing data as payload.
func NetstringFromBytes(data []byte) (*netstring.Netstring, error) {
reader := bytes.NewReader(data)
ns, err := netstring.NewNetstring(reader)
if err != nil {
return nil, err
}
return ns, err
}
// ExtractSQLHash parse request to see if it has an embedded PREPARE statement.
// if there is one, compute and return the sqlhash and true.
// otherwise, return 0 and false
func ExtractSQLHash(request *netstring.Netstring) (uint32, bool) {
//
// create a collection of flat netstrings (e.g. de-subnetstring)
//
var nss []*netstring.Netstring
var err error
if request.Cmd == (netstring.CodeSubCommand - '0') {
nss, err = netstring.SubNetstrings(request)
if err != nil {
return 0, false
}
} else {
nss = append(nss, request)
}
for _, v := range nss {
switch v.Cmd {
case common.CmdPrepare, common.CmdPrepareV2, common.CmdPrepareSpecial:
if v.Payload == nil {
return 0, false
}
return utility.GetSQLHash(string(v.Payload)), true
}
}
return 0, false
}