From b802d810bb35423451c6364c93103ed1bf2da5c9 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 12:28:02 +0800 Subject: [PATCH 1/7] fix code scan --- action/consignment_transfer.go | 8 ++++---- action/protocol/staking/handlers_test.go | 4 ++-- ioctl/cmd/bc/bcbucketlist.go | 12 +++++++----- ioctl/cmd/contract/contractshare.go | 8 ++++---- ioctl/newcmd/bc/bcbucketlist.go | 17 ++++++++++------- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/action/consignment_transfer.go b/action/consignment_transfer.go index 264e9bfe68..d8115112f6 100644 --- a/action/consignment_transfer.go +++ b/action/consignment_transfer.go @@ -57,8 +57,8 @@ type ( // ConsignMsgEther is the consignment message format of Ethereum ConsignMsgEther struct { - BucketIdx int `json:"bucket"` - Nonce int `json:"nonce"` + BucketIdx uint64 `json:"bucket"` + Nonce uint64 `json:"nonce"` Recipient string `json:"recipient"` Reclaim string `json:"reclaim"` } @@ -148,8 +148,8 @@ func NewConsignMsg(sigType, recipient string, bucketIdx, nonce uint64) ([]byte, switch sigType { case "Ethereum": msg := ConsignMsgEther{ - BucketIdx: int(bucketIdx), - Nonce: int(nonce), + BucketIdx: bucketIdx, + Nonce: nonce, Recipient: recipient, Reclaim: _reclaim, } diff --git a/action/protocol/staking/handlers_test.go b/action/protocol/staking/handlers_test.go index 8f5231c0db..1b9727afe5 100644 --- a/action/protocol/staking/handlers_test.go +++ b/action/protocol/staking/handlers_test.go @@ -2057,7 +2057,7 @@ func TestProtocol_HandleConsignmentTransfer(t *testing.T) { // transfer to test.to through consignment var consign []byte if !test.nilPayload { - consign = newconsignment(require, int(test.sigIndex), int(test.sigNonce), test.bucketOwner, test.to.String(), test.consignType, test.reclaim, test.wrongSig) + consign = newconsignment(require, test.sigIndex, test.sigNonce, test.bucketOwner, test.to.String(), test.consignType, test.reclaim, test.wrongSig) } act, err := action.NewTransferStake(1, caller.String(), 0, consign, gasLimit, gasPrice) @@ -2713,7 +2713,7 @@ func depositGas(ctx context.Context, sm protocol.StateManager, gasFee *big.Int) return nil, accountutil.StoreAccount(sm, actionCtx.Caller, acc) } -func newconsignment(r *require.Assertions, bucketIdx, nonce int, senderPrivate, recipient, consignTpye, reclaim string, wrongSig bool) []byte { +func newconsignment(r *require.Assertions, bucketIdx, nonce uint64, senderPrivate, recipient, consignTpye, reclaim string, wrongSig bool) []byte { msg := action.ConsignMsgEther{ BucketIdx: bucketIdx, Nonce: nonce, diff --git a/ioctl/cmd/bc/bcbucketlist.go b/ioctl/cmd/bc/bcbucketlist.go index c1fc6526cf..4a73035075 100644 --- a/ioctl/cmd/bc/bcbucketlist.go +++ b/ioctl/cmd/bc/bcbucketlist.go @@ -87,24 +87,26 @@ func (m *bucketlistMessage) String() string { // getBucketList get bucket list from chain func getBucketList(method, addr string, args ...string) (err error) { - offset, limit := uint64(0), uint64(1000) + offset, limit := uint32(0), uint32(1000) if len(args) > 0 { - offset, err = strconv.ParseUint(args[0], 10, 64) + val, err := strconv.ParseUint(args[0], 10, 32) if err != nil { return output.NewError(output.ValidationError, "invalid offset", err) } + offset = uint32(val) } if len(args) > 1 { - limit, err = strconv.ParseUint(args[1], 10, 64) + val, err := strconv.ParseUint(args[1], 10, 32) if err != nil { return output.NewError(output.ValidationError, "invalid limit", err) } + limit = uint32(val) } switch method { case _bucketlistMethodByVoter: - return getBucketListByVoter(addr, uint32(offset), uint32(limit)) + return getBucketListByVoter(addr, offset, limit) case _bucketlistMethodByCandidate: - return getBucketListByCand(addr, uint32(offset), uint32(limit)) + return getBucketListByCand(addr, offset, limit) } return output.NewError(output.InputError, "unknown ", nil) } diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index 5fcbc769ad..8ee125e29f 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -209,7 +209,7 @@ func share(args []string) error { log.Println("send get response: ", err) break } - log.Println("share: " + _givenPath + "/" + getPayloadPath) + log.Printf("share: %s/%s\n", _givenPath, getPayloadPath) case "rename": c := make(chan bool) @@ -223,7 +223,7 @@ func share(args []string) error { log.Println("send get response: ", err) break } - log.Println("rename: " + _givenPath + "/" + oldPath + " to " + _givenPath + "/" + newPath) + log.Printf("rename: %s/%s to %s/%s\n", _givenPath, oldPath, _givenPath, newPath) case "set": t := request.Payload @@ -238,10 +238,10 @@ func share(args []string) error { log.Println("send set response: ", err) break } - log.Println("set: " + _givenPath + "/" + setPath) + log.Printf("set: %s/%s\n", _givenPath, setPath) default: - log.Println("Don't support this IDE yet. Can not handle websocket method: " + request.Key) + log.Printf("Don't support this IDE yet. Can not handle websocket method: %s\n" + request.Key) } } diff --git a/ioctl/newcmd/bc/bcbucketlist.go b/ioctl/newcmd/bc/bcbucketlist.go index 6bb4f07ac9..1b4fcaaadc 100644 --- a/ioctl/newcmd/bc/bcbucketlist.go +++ b/ioctl/newcmd/bc/bcbucketlist.go @@ -65,37 +65,40 @@ func NewBCBucketListCmd(client ioctl.Client) *cobra.Command { err error ) - offset, limit := uint64(0), uint64(1000) - method, addr := args[0], args[1] - s := args[2:] + offset, limit := uint32(0), uint32(1000) + method, addr, s := args[0], args[1], args[2:] if len(s) > 0 { - offset, err = strconv.ParseUint(s[0], 10, 64) + val, err := strconv.ParseUint(s[0], 10, 32) if err != nil { return errors.Wrap(err, "invalid offset") } + offset = uint32(val) } if len(s) > 1 { - limit, err = strconv.ParseUint(s[1], 10, 64) + val, err := strconv.ParseUint(s[1], 10, 32) if err != nil { return errors.Wrap(err, "invalid limit") } + limit = uint32(val) } + switch method { case MethodVoter: address, err = client.AddressWithDefaultIfNotExist(addr) if err != nil { return err } - bl, err = getBucketListByVoterAddress(client, address, uint32(offset), uint32(limit)) + bl, err = getBucketListByVoterAddress(client, address, offset, limit) case MethodCandidate: - bl, err = getBucketListByCandidateName(client, addr, uint32(offset), uint32(limit)) + bl, err = getBucketListByCandidateName(client, addr, offset, limit) default: return errors.New("unknown ") } if err != nil { return err } + var lines []string if len(bl.Buckets) == 0 { lines = append(lines, "Empty bucketlist with given address") From f12ae844e0fb2ace2b798a6a26866b5e55733edd Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 19:02:35 +0800 Subject: [PATCH 2/7] add path clean --- ioctl/cmd/contract/contractshare.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index 8ee125e29f..a029399c15 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -117,7 +117,7 @@ func isExist(path string) bool { } func rename(oldPath string, newPath string, c chan bool) { - if isExist(_givenPath + "/" + oldPath) { + if isExist(oldPath) { if err := os.Rename(oldPath, newPath); err != nil { log.Println("Rename file failed: ", err) } @@ -127,7 +127,7 @@ func rename(oldPath string, newPath string, c chan bool) { } func share(args []string) error { - _givenPath = args[0] + _givenPath = filepath.Clean(args[0]) if len(_givenPath) == 0 { return output.NewError(output.ReadFileError, "failed to get directory", nil) } @@ -197,7 +197,7 @@ func share(args []string) error { t := request.Payload getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - getPayloadPath := getPayload["path"].(string) + getPayloadPath := filepath.Clean(getPayload["path"].(string)) upload, err := os.ReadFile(_givenPath + "/" + getPayloadPath) if err != nil { log.Println("read file failed: ", err) @@ -215,30 +215,33 @@ func share(args []string) error { c := make(chan bool) t := request.Payload renamePayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - oldPath := renamePayload["oldPath"].(string) - newPath := renamePayload["newPath"].(string) + oldPath := _givenPath + "/" + filepath.Clean(renamePayload["oldPath"].(string)) + newPath := _givenPath + "/" + filepath.Clean(renamePayload["newPath"].(string)) go rename(oldPath, newPath, c) response.Payload = <-c if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Printf("rename: %s/%s to %s/%s\n", _givenPath, oldPath, _givenPath, newPath) + log.Printf("rename: %s to %s\n", oldPath, newPath) case "set": t := request.Payload setPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - setPath := setPayload["path"].(string) + setPath := filepath.Clean(setPayload["path"].(string)) content := setPayload["content"].(string) - err := os.WriteFile(_givenPath+"/"+setPath, []byte(content), 0777) - if err != nil { + newPath := _givenPath + "/" + setPath + if err := os.MkdirAll(filepath.Dir(newPath), 0755); err != nil { + log.Println("mkdir failed: ", err) + } + if err := os.WriteFile(newPath, []byte(content), 0644); err != nil { log.Println("set file failed: ", err) } if err := conn.WriteJSON(&response); err != nil { log.Println("send set response: ", err) break } - log.Printf("set: %s/%s\n", _givenPath, setPath) + log.Printf("set: %s\n", newPath) default: log.Printf("Don't support this IDE yet. Can not handle websocket method: %s\n" + request.Key) @@ -249,5 +252,4 @@ func share(args []string) error { log.Fatal(http.ListenAndServe(*_addr, nil)) return nil - } From 846af4cd265c5b5aeeeb94eee3678ad6e9fbd452 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 21:19:11 +0800 Subject: [PATCH 3/7] add escapeString --- ioctl/cmd/contract/contractshare.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index a029399c15..77f0aa6bfb 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -209,7 +209,7 @@ func share(args []string) error { log.Println("send get response: ", err) break } - log.Printf("share: %s/%s\n", _givenPath, getPayloadPath) + log.Println("share: " + _givenPath + "/" + easpcapeString(getPayloadPath)) case "rename": c := make(chan bool) @@ -223,7 +223,7 @@ func share(args []string) error { log.Println("send get response: ", err) break } - log.Printf("rename: %s to %s\n", oldPath, newPath) + log.Println("rename: " + easpcapeString(oldPath) + " to " + easpcapeString(newPath)) case "set": t := request.Payload @@ -241,10 +241,10 @@ func share(args []string) error { log.Println("send set response: ", err) break } - log.Printf("set: %s\n", newPath) + log.Println("set: " + easpcapeString(newPath)) default: - log.Printf("Don't support this IDE yet. Can not handle websocket method: %s\n" + request.Key) + log.Println("Don't support this IDE yet. Can not handle websocket method: " + easpcapeString(request.Key)) } } @@ -253,3 +253,8 @@ func share(args []string) error { return nil } + +func easpcapeString(str string) string { + escaped := strings.Replace(str, "\n", "", -1) + return strings.Replace(escaped, "\r", "", -1) +} From 93a987cacbc9f1eca0c8f322e653481754e12d94 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 21:50:57 +0800 Subject: [PATCH 4/7] add filepath.join --- ioctl/cmd/contract/contractshare.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index 77f0aa6bfb..a2bce44092 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -186,7 +186,7 @@ func share(args []string) error { case "list": payload := make(map[string]bool) for _, ele := range _fileList { - payload[ele] = isReadOnly(_givenPath + "/" + ele) + payload[ele] = isReadOnly(filepath.Join(_givenPath, ele)) } response.Payload = payload if err := conn.WriteJSON(&response); err != nil { @@ -197,26 +197,27 @@ func share(args []string) error { t := request.Payload getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - getPayloadPath := filepath.Clean(getPayload["path"].(string)) - upload, err := os.ReadFile(_givenPath + "/" + getPayloadPath) + getPayloadPath := getPayload["path"].(string) + payloadPath := filepath.Join(_givenPath, filepath.Clean(getPayloadPath)) + upload, err := os.ReadFile(payloadPath) if err != nil { log.Println("read file failed: ", err) } payload["content"] = string(upload) - payload["readonly"] = isReadOnly(_givenPath + "/" + getPayloadPath) + payload["readonly"] = isReadOnly(payloadPath) response.Payload = payload if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Println("share: " + _givenPath + "/" + easpcapeString(getPayloadPath)) + log.Println("share: " + easpcapeString(payloadPath)) case "rename": c := make(chan bool) t := request.Payload renamePayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - oldPath := _givenPath + "/" + filepath.Clean(renamePayload["oldPath"].(string)) - newPath := _givenPath + "/" + filepath.Clean(renamePayload["newPath"].(string)) + oldPath := filepath.Join(_givenPath, filepath.Clean(renamePayload["oldPath"].(string))) + newPath := filepath.Join(_givenPath, filepath.Clean(renamePayload["newPath"].(string))) go rename(oldPath, newPath, c) response.Payload = <-c if err := conn.WriteJSON(&response); err != nil { @@ -228,9 +229,9 @@ func share(args []string) error { case "set": t := request.Payload setPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - setPath := filepath.Clean(setPayload["path"].(string)) + setPath := setPayload["path"].(string) content := setPayload["content"].(string) - newPath := _givenPath + "/" + setPath + newPath := filepath.Join(_givenPath, filepath.Clean(setPath)) if err := os.MkdirAll(filepath.Dir(newPath), 0755); err != nil { log.Println("mkdir failed: ", err) } From 7de85f0d4c7952f6fbdcc1d3fffad84cf24ed614 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 22:26:02 +0800 Subject: [PATCH 5/7] add full path --- ioctl/cmd/contract/contractshare.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index a2bce44092..1142fd91c1 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -127,7 +127,11 @@ func rename(oldPath string, newPath string, c chan bool) { } func share(args []string) error { - _givenPath = filepath.Clean(args[0]) + path, err := filepath.Abs(filepath.Clean(args[0])) + if err != nil { + return err + } + _givenPath = path if len(_givenPath) == 0 { return output.NewError(output.ReadFileError, "failed to get directory", nil) } From 66d0dc54e200f90e62ed4e3ed7acbdf182da2574 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Tue, 12 Jul 2022 23:08:26 +0800 Subject: [PATCH 6/7] add path clean --- ioctl/cmd/contract/contractshare.go | 64 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index 1142fd91c1..a0af1a1b39 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -127,11 +127,7 @@ func rename(oldPath string, newPath string, c chan bool) { } func share(args []string) error { - path, err := filepath.Abs(filepath.Clean(args[0])) - if err != nil { - return err - } - _givenPath = path + _givenPath = filepath.Clean(args[0]) if len(_givenPath) == 0 { return output.NewError(output.ReadFileError, "failed to get directory", nil) } @@ -201,52 +197,73 @@ func share(args []string) error { t := request.Payload getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - getPayloadPath := getPayload["path"].(string) - payloadPath := filepath.Join(_givenPath, filepath.Clean(getPayloadPath)) - upload, err := os.ReadFile(payloadPath) + getPayloadPath, err := cleanPath(getPayload["path"].(string)) + if err != nil { + log.Println("clean file path failed: ", err) + break + } + getPayloadPath = filepath.Join(_givenPath, getPayloadPath) + upload, err := os.ReadFile(getPayloadPath) if err != nil { log.Println("read file failed: ", err) + break } payload["content"] = string(upload) - payload["readonly"] = isReadOnly(payloadPath) + payload["readonly"] = isReadOnly(getPayloadPath) response.Payload = payload if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Println("share: " + easpcapeString(payloadPath)) + log.Println("share: " + easpcapeString(getPayloadPath)) case "rename": c := make(chan bool) t := request.Payload renamePayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - oldPath := filepath.Join(_givenPath, filepath.Clean(renamePayload["oldPath"].(string))) - newPath := filepath.Join(_givenPath, filepath.Clean(renamePayload["newPath"].(string))) - go rename(oldPath, newPath, c) + oldRenamePath, err := cleanPath(renamePayload["oldPath"].(string)) + if err != nil { + log.Println("clean file path failed: ", err) + break + } + newRenamePath, err := cleanPath(renamePayload["newPath"].(string)) + if err != nil { + log.Println("clean file path failed: ", err) + break + } + oldRenamePath = filepath.Join(_givenPath, oldRenamePath) + newRenamePath = filepath.Join(_givenPath, newRenamePath) + go rename(oldRenamePath, newRenamePath, c) response.Payload = <-c if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Println("rename: " + easpcapeString(oldPath) + " to " + easpcapeString(newPath)) + log.Println("rename: " + easpcapeString(oldRenamePath) + " to " + easpcapeString(newRenamePath)) case "set": t := request.Payload setPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - setPath := setPayload["path"].(string) content := setPayload["content"].(string) - newPath := filepath.Join(_givenPath, filepath.Clean(setPath)) - if err := os.MkdirAll(filepath.Dir(newPath), 0755); err != nil { + setPath, err := cleanPath(setPayload["path"].(string)) + if err != nil { + log.Println("clean file path failed: ", err) + break + } + setPath = filepath.Join(_givenPath, setPath) + if err := os.MkdirAll(filepath.Dir(setPath), 0755); err != nil { log.Println("mkdir failed: ", err) + break } - if err := os.WriteFile(newPath, []byte(content), 0644); err != nil { + if err := os.WriteFile(setPath, []byte(content), 0644); err != nil { log.Println("set file failed: ", err) + break } if err := conn.WriteJSON(&response); err != nil { log.Println("send set response: ", err) break } - log.Println("set: " + easpcapeString(newPath)) + log.Println("set: " + easpcapeString(setPath)) default: log.Println("Don't support this IDE yet. Can not handle websocket method: " + easpcapeString(request.Key)) @@ -259,6 +276,15 @@ func share(args []string) error { return nil } +func cleanPath(path string) (string, error) { + path = filepath.Clean(filepath.Join("/", path)) + real, err := filepath.Rel("/", path) + if err != nil { + return "", err + } + return real, nil +} + func easpcapeString(str string) string { escaped := strings.Replace(str, "\n", "", -1) return strings.Replace(escaped, "\r", "", -1) From 353c071466345717632b66f6ac222106d5c83066 Mon Sep 17 00:00:00 2001 From: huof6890 <68298506@qq.com> Date: Thu, 14 Jul 2022 14:40:48 +0800 Subject: [PATCH 7/7] just fix: Incorrect conversion between integer types --- action/consignment_transfer.go | 8 +-- action/protocol/staking/handlers_test.go | 4 +- ioctl/cmd/contract/contractshare.go | 68 ++++++------------------ 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/action/consignment_transfer.go b/action/consignment_transfer.go index d8115112f6..264e9bfe68 100644 --- a/action/consignment_transfer.go +++ b/action/consignment_transfer.go @@ -57,8 +57,8 @@ type ( // ConsignMsgEther is the consignment message format of Ethereum ConsignMsgEther struct { - BucketIdx uint64 `json:"bucket"` - Nonce uint64 `json:"nonce"` + BucketIdx int `json:"bucket"` + Nonce int `json:"nonce"` Recipient string `json:"recipient"` Reclaim string `json:"reclaim"` } @@ -148,8 +148,8 @@ func NewConsignMsg(sigType, recipient string, bucketIdx, nonce uint64) ([]byte, switch sigType { case "Ethereum": msg := ConsignMsgEther{ - BucketIdx: bucketIdx, - Nonce: nonce, + BucketIdx: int(bucketIdx), + Nonce: int(nonce), Recipient: recipient, Reclaim: _reclaim, } diff --git a/action/protocol/staking/handlers_test.go b/action/protocol/staking/handlers_test.go index 1b9727afe5..8f5231c0db 100644 --- a/action/protocol/staking/handlers_test.go +++ b/action/protocol/staking/handlers_test.go @@ -2057,7 +2057,7 @@ func TestProtocol_HandleConsignmentTransfer(t *testing.T) { // transfer to test.to through consignment var consign []byte if !test.nilPayload { - consign = newconsignment(require, test.sigIndex, test.sigNonce, test.bucketOwner, test.to.String(), test.consignType, test.reclaim, test.wrongSig) + consign = newconsignment(require, int(test.sigIndex), int(test.sigNonce), test.bucketOwner, test.to.String(), test.consignType, test.reclaim, test.wrongSig) } act, err := action.NewTransferStake(1, caller.String(), 0, consign, gasLimit, gasPrice) @@ -2713,7 +2713,7 @@ func depositGas(ctx context.Context, sm protocol.StateManager, gasFee *big.Int) return nil, accountutil.StoreAccount(sm, actionCtx.Caller, acc) } -func newconsignment(r *require.Assertions, bucketIdx, nonce uint64, senderPrivate, recipient, consignTpye, reclaim string, wrongSig bool) []byte { +func newconsignment(r *require.Assertions, bucketIdx, nonce int, senderPrivate, recipient, consignTpye, reclaim string, wrongSig bool) []byte { msg := action.ConsignMsgEther{ BucketIdx: bucketIdx, Nonce: nonce, diff --git a/ioctl/cmd/contract/contractshare.go b/ioctl/cmd/contract/contractshare.go index a0af1a1b39..5fcbc769ad 100644 --- a/ioctl/cmd/contract/contractshare.go +++ b/ioctl/cmd/contract/contractshare.go @@ -117,7 +117,7 @@ func isExist(path string) bool { } func rename(oldPath string, newPath string, c chan bool) { - if isExist(oldPath) { + if isExist(_givenPath + "/" + oldPath) { if err := os.Rename(oldPath, newPath); err != nil { log.Println("Rename file failed: ", err) } @@ -127,7 +127,7 @@ func rename(oldPath string, newPath string, c chan bool) { } func share(args []string) error { - _givenPath = filepath.Clean(args[0]) + _givenPath = args[0] if len(_givenPath) == 0 { return output.NewError(output.ReadFileError, "failed to get directory", nil) } @@ -186,7 +186,7 @@ func share(args []string) error { case "list": payload := make(map[string]bool) for _, ele := range _fileList { - payload[ele] = isReadOnly(filepath.Join(_givenPath, ele)) + payload[ele] = isReadOnly(_givenPath + "/" + ele) } response.Payload = payload if err := conn.WriteJSON(&response); err != nil { @@ -197,76 +197,51 @@ func share(args []string) error { t := request.Payload getPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - getPayloadPath, err := cleanPath(getPayload["path"].(string)) - if err != nil { - log.Println("clean file path failed: ", err) - break - } - getPayloadPath = filepath.Join(_givenPath, getPayloadPath) - upload, err := os.ReadFile(getPayloadPath) + getPayloadPath := getPayload["path"].(string) + upload, err := os.ReadFile(_givenPath + "/" + getPayloadPath) if err != nil { log.Println("read file failed: ", err) - break } payload["content"] = string(upload) - payload["readonly"] = isReadOnly(getPayloadPath) + payload["readonly"] = isReadOnly(_givenPath + "/" + getPayloadPath) response.Payload = payload if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Println("share: " + easpcapeString(getPayloadPath)) + log.Println("share: " + _givenPath + "/" + getPayloadPath) case "rename": c := make(chan bool) t := request.Payload renamePayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) - oldRenamePath, err := cleanPath(renamePayload["oldPath"].(string)) - if err != nil { - log.Println("clean file path failed: ", err) - break - } - newRenamePath, err := cleanPath(renamePayload["newPath"].(string)) - if err != nil { - log.Println("clean file path failed: ", err) - break - } - oldRenamePath = filepath.Join(_givenPath, oldRenamePath) - newRenamePath = filepath.Join(_givenPath, newRenamePath) - go rename(oldRenamePath, newRenamePath, c) + oldPath := renamePayload["oldPath"].(string) + newPath := renamePayload["newPath"].(string) + go rename(oldPath, newPath, c) response.Payload = <-c if err := conn.WriteJSON(&response); err != nil { log.Println("send get response: ", err) break } - log.Println("rename: " + easpcapeString(oldRenamePath) + " to " + easpcapeString(newRenamePath)) + log.Println("rename: " + _givenPath + "/" + oldPath + " to " + _givenPath + "/" + newPath) case "set": t := request.Payload setPayload := reflect.ValueOf(t).Index(0).Interface().(map[string]interface{}) + setPath := setPayload["path"].(string) content := setPayload["content"].(string) - setPath, err := cleanPath(setPayload["path"].(string)) + err := os.WriteFile(_givenPath+"/"+setPath, []byte(content), 0777) if err != nil { - log.Println("clean file path failed: ", err) - break - } - setPath = filepath.Join(_givenPath, setPath) - if err := os.MkdirAll(filepath.Dir(setPath), 0755); err != nil { - log.Println("mkdir failed: ", err) - break - } - if err := os.WriteFile(setPath, []byte(content), 0644); err != nil { log.Println("set file failed: ", err) - break } if err := conn.WriteJSON(&response); err != nil { log.Println("send set response: ", err) break } - log.Println("set: " + easpcapeString(setPath)) + log.Println("set: " + _givenPath + "/" + setPath) default: - log.Println("Don't support this IDE yet. Can not handle websocket method: " + easpcapeString(request.Key)) + log.Println("Don't support this IDE yet. Can not handle websocket method: " + request.Key) } } @@ -274,18 +249,5 @@ func share(args []string) error { log.Fatal(http.ListenAndServe(*_addr, nil)) return nil -} - -func cleanPath(path string) (string, error) { - path = filepath.Clean(filepath.Join("/", path)) - real, err := filepath.Rel("/", path) - if err != nil { - return "", err - } - return real, nil -} -func easpcapeString(str string) string { - escaped := strings.Replace(str, "\n", "", -1) - return strings.Replace(escaped, "\r", "", -1) }