From 83a162bfa6e6eba78ae439e193682c19b9cd4744 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 15 Feb 2026 13:44:39 +0700 Subject: all: fix chmod/chown if destination is directory Given the following command #put!+0644 src/file dst/ If the dst is a directory, it would cause the directory permission changes to 0644. This changes fix it by checking if the destination is a directory first. If we cannot stat the dst, skip the chmod/chown command. --- session.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'session.go') diff --git a/session.go b/session.go index 42fd0a4..2ac1c60 100644 --- a/session.go +++ b/session.go @@ -265,6 +265,17 @@ func (ses *Session) SudoCopy(ctx context.Context, req *ExecRequest, stmt *Statem return fmt.Errorf("%s: %w", logp, err) } + dstFileInfo, err := os.Stat(dst) + if err != nil { + log.Printf(`%s: stat fails, skip setting file mode and/or owner: %s`, + logp, err) + return nil + } + if dstFileInfo.IsDir() { + srcBase := filepath.Base(src) + dst = filepath.Join(dst, srcBase) + } + if stmt.mode != 0 { var ( fsmode = strconv.FormatUint(uint64(stmt.mode), 8) -- cgit v1.3