Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Fix issue whereby files are failed to be chown'ed due to not existing #2

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (fs FileSystem) Filecmd(request *sftp.Request) error {
}
}

isRemoval := false
switch request.Method {
case "Setstat":
if err := os.Chmod(p, request.Attributes().FileMode()); err != nil {
Expand Down Expand Up @@ -200,6 +201,8 @@ func (fs FileSystem) Filecmd(request *sftp.Request) error {
return sftp.ErrSshFxFailure
}

isRemoval = true

break
case "Mkdir":
if !fs.can("create-files") {
Expand Down Expand Up @@ -237,6 +240,8 @@ func (fs FileSystem) Filecmd(request *sftp.Request) error {
return sftp.ErrSshFxFailure
}

isRemoval = true

return sftp.ErrSshFxOk
default:
return sftp.ErrSshFxOpUnsupported
Expand All @@ -249,8 +254,10 @@ func (fs FileSystem) Filecmd(request *sftp.Request) error {

// Not failing here is intentional. We still made the file, it is just owned incorrectly
// and will likely cause some issues.
if err := os.Chown(fileLocation, fs.User.Uid, fs.User.Gid); err != nil {
logger.Get().Warnw("error chowning file", zap.String("file", fileLocation), zap.Error(err))
if !isRemoval { // We can't perform a chown on a file that has been removed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just check if the request.Method is Rmdir or Remove?

if err := os.Chown(fileLocation, fs.User.Uid, fs.User.Gid); err != nil {
logger.Get().Warnw("error chowning file", zap.String("file", fileLocation), zap.Error(err))
}
}

return sftp.ErrSshFxOk
Expand Down