Skip to content

Commit

Permalink
Merge pull request #1468 from HassanAlsamahi/change-incus-file-push-o…
Browse files Browse the repository at this point in the history
…peration-to-sftp

incus/file/push Use SFTP client instead of file API
  • Loading branch information
stgraber authored Dec 5, 2024
2 parents 78d8d53 + d33c948 commit 9fd7ac6
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (c *cmdFileCreate) Run(cmd *cobra.Command, args []string) error {
paths = []string{targetPath, symlinkTargetPath}
}

err = c.file.sftpCreateFile(resource, paths, fileArgs)
err = c.file.sftpCreateFile(resource, paths, fileArgs, false)
if err != nil {
progress.Done("")
return err
Expand Down Expand Up @@ -962,7 +962,7 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
}, f)

logger.Infof("Pushing %s to %s (%s)", f.Name(), fpath, args.Type)
err = resource.server.CreateInstanceFile(resource.name, fpath, args)
err = c.file.sftpCreateFile(resource, []string{fpath}, args, true)
if err != nil {
progress.Done("")
return err
Expand All @@ -975,20 +975,45 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
}

func (c *cmdFile) setOwnerMode(sftpConn *sftp.Client, targetPath string, args incus.InstanceFileArgs) error {
err := sftpConn.Chown(targetPath, int(args.UID), int(args.GID))
// Get the current stat information.
st, err := sftpConn.Stat(targetPath)
if err != nil {
return err
}

err = sftpConn.Chmod(targetPath, fs.FileMode(args.Mode))
if err != nil {
return err
fileStat, ok := st.Sys().(*sftp.FileStat)
if !ok {
return fmt.Errorf("Invalid filestat data for %q", targetPath)
}

// Set owner.
if args.UID >= 0 || args.GID >= 0 {
if args.UID == -1 {
args.UID = int64(fileStat.UID)
}

if args.GID == -1 {
args.GID = int64(fileStat.GID)
}

err = sftpConn.Chown(targetPath, int(args.UID), int(args.GID))
if err != nil {
return err
}
}

// Set mode.
if args.Mode >= 0 {
err = sftpConn.Chmod(targetPath, fs.FileMode(args.Mode))
if err != nil {
return err
}
}

return nil
}

func (c *cmdFile) sftpCreateFile(resource remoteResource, targetPath []string, args incus.InstanceFileArgs) error {
func (c *cmdFile) sftpCreateFile(resource remoteResource, targetPath []string, args incus.InstanceFileArgs, push bool) error {
sftpConn, err := resource.server.GetInstanceFileSFTP(resource.name)
if err != nil {
return err
Expand All @@ -998,11 +1023,20 @@ func (c *cmdFile) sftpCreateFile(resource remoteResource, targetPath []string, a

switch args.Type {
case "file":
_, err := sftpConn.OpenFile(targetPath[0], os.O_CREATE)
file, err := sftpConn.OpenFile(targetPath[0], os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return err
}

defer func() { _ = file.Close() }()

if push {
_, err = io.Copy(file, args.Content)
if err != nil {
return err
}
}

err = c.setOwnerMode(sftpConn, targetPath[0], args)
if err != nil {
return err
Expand Down

0 comments on commit 9fd7ac6

Please sign in to comment.