Skip to content

Commit

Permalink
implement trzsz client
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed May 22, 2022
1 parent f4b0c76 commit c15665c
Show file tree
Hide file tree
Showing 10 changed files with 1,138 additions and 43 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ Similar to lrzsz ( rz / sz ), command `trz` to upload files, command `tsz /path/
For more information, see the website of trzsz: [https://trzsz.github.io](https://trzsz.github.io/).


## Configuration

`trzsz` looks for configuration at `~/.trzsz.conf`. e.g.:

```
DefaultUploadPath =
DefaultDownloadPath = /Users/username/Downloads
```

* If the `DefaultUploadPath` is not empty, the path will be opened by default while choosing upload files.

* If the `DefaultDownloadPath` is not empty, downloading files will be saved to the path automatically instead of asking each time.


## Contact

Feel free to email me <[email protected]>.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ require (
github.com/ncruces/zenity v0.8.6
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171
golang.org/x/text v0.3.7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
124 changes: 124 additions & 0 deletions trzsz/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
MIT License
Copyright (c) 2022 Lonny Wong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package trzsz

import (
"bytes"
"time"
)

type TrzszBuffer struct {
bufCh chan []byte
stopCh chan bool
nextBuf []byte
nextIdx int
readBuf *bytes.Buffer
}

func NewTrzszBuffer() *TrzszBuffer {
return &TrzszBuffer{make(chan []byte, 10), make(chan bool, 1), nil, 0, new(bytes.Buffer)}
}

func (b *TrzszBuffer) addBuffer(buf []byte) {
b.bufCh <- buf
}

func (b *TrzszBuffer) stopBuffer() {
select {
case b.stopCh <- true:
default:
}
}

func (b *TrzszBuffer) drainBuffer() {
for {
select {
case <-b.bufCh:
default:
return
}
}
}

func (b *TrzszBuffer) nextBuffer(timeout <-chan time.Time) ([]byte, error) {
if b.nextBuf != nil && b.nextIdx < len(b.nextBuf) {
return b.nextBuf[b.nextIdx:], nil
}
select {
case b.nextBuf = <-b.bufCh:
b.nextIdx = 0
return b.nextBuf, nil
case <-b.stopCh:
return nil, newTrzszError("Stopped")
case <-timeout:
return nil, newTrzszError("Receive data timeout")
}
}

func (b *TrzszBuffer) readLine(timeout <-chan time.Time) ([]byte, error) {
b.readBuf.Reset()
for {
buf, err := b.nextBuffer(timeout)
if err != nil {
return nil, err
}
newLineIdx := bytes.IndexByte(buf, '\n')
if newLineIdx >= 0 {
b.nextIdx += newLineIdx + 1 // +1 to ignroe the '\n'
buf = buf[0:newLineIdx]
} else {
b.nextIdx += len(buf)
}
if bytes.IndexByte(buf, '\x03') >= 0 { // `ctrl + c` to interrupt
return nil, newTrzszError("Interrupted")
}
b.readBuf.Write(buf)
if newLineIdx >= 0 {
return b.readBuf.Bytes(), nil
}
}
}

func (b *TrzszBuffer) readBinary(size int, timeout <-chan time.Time) ([]byte, error) {
b.readBuf.Reset()
if b.readBuf.Cap() < size {
b.readBuf.Grow(size)
}
for b.readBuf.Len() < size {
buf, err := b.nextBuffer(timeout)
if err != nil {
return nil, err
}
left := size - b.readBuf.Len()
if len(buf) > left {
b.nextIdx += left
buf = buf[0:left]
} else {
b.nextIdx += len(buf)
}
b.readBuf.Write(buf)
}
return b.readBuf.Bytes(), nil
}
98 changes: 95 additions & 3 deletions trzsz/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
"bytes"
"compress/zlib"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
)

type PtyIO interface {
Expand All @@ -38,7 +43,11 @@ type PtyIO interface {
}

type ProgressCallback interface {
// TODO
onNum(num int64)
onName(name string)
onSize(size int64)
onStep(step int64)
onDone(name string)
}

func encodeBytes(buf []byte) string {
Expand Down Expand Up @@ -66,12 +75,95 @@ func decodeString(str string) ([]byte, error) {
return ioutil.ReadAll(z)
}

type TrzszError struct {
message string
errType string
trace bool
}

func NewTrzszError(message string, errType string, trace bool) *TrzszError {
if errType == "fail" || errType == "FAIL" || errType == "EXIT" {
msg, err := decodeString(message)
if err != nil {
message = fmt.Sprintf("decode [%s] error: %s", message, err)
} else {
message = string(msg)
}
} else if len(errType) > 0 {
message = fmt.Sprintf("[TrzszError] %s: %s", errType, message)
}
err := &TrzszError{message, errType, trace}
if err.isTraceBack() {
err.message = fmt.Sprintf("%s\n%s", err.message, string(debug.Stack()))
}
return err
}

func newTrzszError(message string) *TrzszError {
return NewTrzszError(message, "", false)
}

func (e *TrzszError) Error() string {
return e.message
}

func (e *TrzszError) isTraceBack() bool {
if e.errType == "fail" {
return false
}
return e.trace
}

func (e *TrzszError) isRemoteExit() bool {
return e.errType == "EXIT"
}

func (e *TrzszError) isRemoteFail() bool {
return e.errType == "fail" || e.errType == "FAIL"
}

func checkPathWritable(path string) error {
// TODO
fileInfo, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return newTrzszError(fmt.Sprintf("No such directory: %s", path))
}
if !fileInfo.IsDir() {
return newTrzszError(fmt.Sprintf("Not a directory: %s", path))
}
if fileInfo.Mode().Perm()&(1<<7) == 0 {
return newTrzszError(fmt.Sprintf("No permission to write: %s", path))
}
return nil
}

func checkFilesReadable(files []string) error {
// TODO
for _, file := range files {
fileInfo, err := os.Stat(file)
if errors.Is(err, os.ErrNotExist) {
return newTrzszError(fmt.Sprintf("No such file: %s", file))
}
if fileInfo.IsDir() {
return newTrzszError(fmt.Sprintf("Is a directory: %s", file))
}
if !fileInfo.Mode().IsRegular() {
return newTrzszError(fmt.Sprintf("Not a regular file: %s", file))
}
if fileInfo.Mode().Perm()&(1<<8) == 0 {
return newTrzszError(fmt.Sprintf("No permission to read: %s", file))
}
}
return nil
}

func getNewName(path, name string) (string, error) {
if _, err := os.Stat(filepath.Join(path, name)); errors.Is(err, os.ErrNotExist) {
return name, nil
}
for i := 0; i < 1000; i++ {
newName := fmt.Sprintf("%s.%d", name, i)
if _, err := os.Stat(filepath.Join(path, newName)); errors.Is(err, os.ErrNotExist) {
return newName, nil
}
}
return "", newTrzszError("Fail to assign new file name")
}
Loading

0 comments on commit c15665c

Please sign in to comment.