Skip to content

Commit

Permalink
support file permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Dec 23, 2023
1 parent 9713637 commit 5259702
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion trzsz/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (t *trzszTransfer) newArchiveWriter(destPath string, srcFile *sourceFile, f
if !srcFile.IsDir {
return nil, simpleTrzszError("Archive is not a directory: %s", srcFile.getFileName())
}
if err := t.doCreateDirectory(fullPath); err != nil {
if err := t.doCreateDirectory(fullPath, srcFile.Perm); err != nil {
return nil, err
}
return &archiveFileWriter{transfer: t, path: destPath}, nil
Expand Down
6 changes: 4 additions & 2 deletions trzsz/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ type sourceFile struct {
IsDir bool `json:"is_dir"`
Archive bool `json:"archive"`
Size int64 `json:"size"`
Perm *uint32 `json:"perm"`
Header string `json:"-"`
SubFiles []*sourceFile `json:"-"`
}
Expand Down Expand Up @@ -351,14 +352,15 @@ func unmarshalTargetFile(target string) (*targetFile, error) {

func checkPathReadable(pathID int, path string, info os.FileInfo, list *[]*sourceFile,
relPath []string, visitedDir map[string]bool) error {
perm := uint32(info.Mode().Perm())
if !info.IsDir() {
if !info.Mode().IsRegular() {
return simpleTrzszError("Not a regular file: %s", path)
}
if syscallAccessRok(path) != nil {
return simpleTrzszError("No permission to read: %s", path)
}
*list = append(*list, &sourceFile{PathID: pathID, AbsPath: path, RelPath: relPath, Size: info.Size()})
*list = append(*list, &sourceFile{PathID: pathID, AbsPath: path, RelPath: relPath, Size: info.Size(), Perm: &perm})
return nil
}
realPath, err := filepath.EvalSymlinks(path)
Expand All @@ -369,7 +371,7 @@ func checkPathReadable(pathID int, path string, info os.FileInfo, list *[]*sourc
return simpleTrzszError("Duplicate link: %s", path)
}
visitedDir[realPath] = true
*list = append(*list, &sourceFile{PathID: pathID, AbsPath: path, RelPath: relPath, IsDir: true})
*list = append(*list, &sourceFile{PathID: pathID, AbsPath: path, RelPath: relPath, IsDir: true, Perm: &perm})
fileObj, err := os.Open(path)
if err != nil {
return simpleTrzszError("Open [%s] error: %v", path, err)
Expand Down
6 changes: 3 additions & 3 deletions trzsz/pty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import (
)

type trzszPty struct {
Stdin io.ReadWriteCloser
Stdout io.ReadWriteCloser
stdin io.WriteCloser
stdout io.ReadCloser
ptmx *os.File
cmd *exec.Cmd
ch chan os.Signal
Expand All @@ -59,7 +59,7 @@ func spawn(name string, arg ...string) (*trzszPty, error) {
if err != nil {
return nil, err
}
return &trzszPty{Stdin: ptmx, Stdout: ptmx, ptmx: ptmx, cmd: cmd}, nil
return &trzszPty{stdin: ptmx, stdout: ptmx, ptmx: ptmx, cmd: cmd}, nil
}

func (t *trzszPty) OnResize(setTerminalColumns func(int32)) {
Expand Down
8 changes: 4 additions & 4 deletions trzsz/pty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import (
)

type trzszPty struct {
Stdin io.ReadWriteCloser
Stdout io.ReadWriteCloser
stdin io.WriteCloser
stdout io.ReadCloser
cpty *conpty.ConPty
width int
height int
Expand Down Expand Up @@ -156,8 +156,8 @@ func spawn(name string, args ...string) (*trzszPty, error) {
}

return &trzszPty{
Stdin: cpty,
Stdout: cpty,
stdin: cpty,
stdout: cpty,
cpty: cpty,
width: width,
height: height,
Expand Down
28 changes: 18 additions & 10 deletions trzsz/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,12 +973,16 @@ func (t *trzszTransfer) addCreatedFiles(path string) {
t.createdFiles = append(t.createdFiles, path)
}

func (t *trzszTransfer) doCreateFile(path string, truncate bool) (fileWriter, error) {
func (t *trzszTransfer) doCreateFile(path string, truncate bool, perm *uint32) (fileWriter, error) {
flag := os.O_RDWR | os.O_CREATE
if truncate {
flag |= os.O_TRUNC
}
file, err := os.OpenFile(path, flag, 0644)
fileMode := fs.FileMode(0644)
if perm != nil {
fileMode = fs.FileMode(*perm) | 0600
}
file, err := os.OpenFile(path, flag, fileMode)
if err != nil {
if e, ok := err.(*fs.PathError); ok {
if errno, ok := e.Unwrap().(syscall.Errno); ok {
Expand All @@ -995,10 +999,14 @@ func (t *trzszTransfer) doCreateFile(path string, truncate bool) (fileWriter, er
return &simpleFileWriter{file}, nil
}

func (t *trzszTransfer) doCreateDirectory(path string) error {
func (t *trzszTransfer) doCreateDirectory(path string, perm *uint32) error {
stat, err := os.Stat(path)
if os.IsNotExist(err) {
err := os.MkdirAll(path, 0755)
fileMode := fs.FileMode(0755)
if perm != nil {
fileMode = fs.FileMode(*perm) | 0700
}
err := os.MkdirAll(path, fileMode)
if err != nil {
return err
}
Expand All @@ -1013,7 +1021,7 @@ func (t *trzszTransfer) doCreateDirectory(path string) error {
return nil
}

func (t *trzszTransfer) createFile(path, fileName string, truncate bool) (fileWriter, string, error) {
func (t *trzszTransfer) createFile(path, fileName string, truncate bool, perm *uint32) (fileWriter, string, error) {
var localName string
if t.transferConfig.Overwrite {
localName = fileName
Expand All @@ -1024,7 +1032,7 @@ func (t *trzszTransfer) createFile(path, fileName string, truncate bool) (fileWr
return nil, "", err
}
}
file, err := t.doCreateFile(filepath.Join(path, localName), truncate)
file, err := t.doCreateFile(filepath.Join(path, localName), truncate, perm)
if err != nil {
return nil, "", err
}
Expand All @@ -1051,7 +1059,7 @@ func (t *trzszTransfer) createDirOrFile(path string, srcFile *sourceFile, trunca
var fullPath string
if len(srcFile.RelPath) > 1 {
p := filepath.Join(append([]string{path, localName}, srcFile.RelPath[1:len(srcFile.RelPath)-1]...)...)
if err := t.doCreateDirectory(p); err != nil {
if err := t.doCreateDirectory(p, srcFile.Perm); err != nil {
return nil, "", err
}
fullPath = filepath.Join(p, srcFile.getFileName())
Expand All @@ -1068,13 +1076,13 @@ func (t *trzszTransfer) createDirOrFile(path string, srcFile *sourceFile, trunca
}

if srcFile.IsDir {
if err := t.doCreateDirectory(fullPath); err != nil {
if err := t.doCreateDirectory(fullPath, srcFile.Perm); err != nil {
return nil, "", err
}
return nil, localName, nil
}

file, err := t.doCreateFile(fullPath, truncate)
file, err := t.doCreateFile(fullPath, truncate, srcFile.Perm)
if err != nil {
return nil, "", err
}
Expand All @@ -1098,7 +1106,7 @@ func (t *trzszTransfer) recvFileName(path string, progress progressCallback) (fi
fileName = srcFile.getFileName()
file, localName, err = t.createDirOrFile(path, srcFile, true)
} else {
file, localName, err = t.createFile(path, fileName, true)
file, localName, err = t.createFile(path, fileName, true, nil)
}
if err != nil {
return nil, "", err
Expand Down
4 changes: 2 additions & 2 deletions trzsz/trzsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TrzszMain() int {

if args.Relay {
// run as relay
NewTrzszRelay(os.Stdin, os.Stdout, pty.Stdin, pty.Stdout, TrzszOptions{
NewTrzszRelay(os.Stdin, os.Stdout, pty.stdin, pty.stdout, TrzszOptions{
DetectTraceLog: args.TraceLog,
})
pty.OnResize(nil)
Expand All @@ -168,7 +168,7 @@ func TrzszMain() int {
fmt.Fprintf(os.Stderr, "pty get columns failed: %v\r\n", err)
return -3
}
filter := NewTrzszFilter(os.Stdin, os.Stdout, pty.Stdin, pty.Stdout, TrzszOptions{
filter := NewTrzszFilter(os.Stdin, os.Stdout, pty.stdin, pty.stdout, TrzszOptions{
TerminalColumns: columns,
DetectDragFile: args.DragFile,
DetectTraceLog: args.TraceLog,
Expand Down

0 comments on commit 5259702

Please sign in to comment.