Skip to content

Commit

Permalink
incus/file/create: Use SFTP client instead of file API
Browse files Browse the repository at this point in the history
Signed-off-by: HassanAlsamahi <[email protected]>
  • Loading branch information
HassanAlsamahi committed Nov 5, 2024
1 parent baa2fad commit 6b1840f
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"

"github.com/lxc/incus/v6/client"
incus "github.com/lxc/incus/v6/client"
cli "github.com/lxc/incus/v6/internal/cmd"
"github.com/lxc/incus/v6/internal/i18n"
internalIO "github.com/lxc/incus/v6/internal/io"
Expand Down Expand Up @@ -288,9 +288,15 @@ func (c *cmdFileCreate) Run(cmd *cobra.Command, args []string) error {
}, fileArgs.Content)
}

err = resource.server.CreateInstanceFile(resource.name, targetPath, fileArgs)
var paths []string
if c.flagType != "symlink" {
paths = []string{targetPath}
} else {
paths = []string{targetPath, symlinkTargetPath}
}
err = c.file.sftpCreateFile(resource, paths, fileArgs)

if err != nil {
progress.Done("")
return err
}

Expand Down Expand Up @@ -928,6 +934,62 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
return nil
}

func (c *cmdFile) setOwnMode(sftpConn *sftp.Client, targetPath string, args incus.InstanceFileArgs) error {

Check failure on line 937 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (oldstable)

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 937 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (stable)

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 937 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (tip)

empty-lines: extra empty line at the start of a block (revive)

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

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 {
sftpConn, err := resource.server.GetInstanceFileSFTP(resource.name)
if err != nil {
return err
}

defer sftpConn.Close()

Check failure on line 958 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (oldstable)

Error return value of `sftpConn.Close` is not checked (errcheck)

Check failure on line 958 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (stable)

Error return value of `sftpConn.Close` is not checked (errcheck)

Check failure on line 958 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (tip)

Error return value of `sftpConn.Close` is not checked (errcheck)

switch args.Type {

Check failure on line 960 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (oldstable)

empty-lines: extra empty line at the end of a block (revive)

Check failure on line 960 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (stable)

empty-lines: extra empty line at the end of a block (revive)

Check failure on line 960 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (tip)

empty-lines: extra empty line at the end of a block (revive)
case "file":
_, err := sftpConn.OpenFile(targetPath[0], os.O_CREATE)
if err != nil {
return err
}

err = c.setOwnMode(sftpConn, targetPath[0], args)
if err != nil {
return err
}

case "directory":
err := sftpConn.MkdirAll(targetPath[0])
if err != nil {
return err
}

err = c.setOwnMode(sftpConn, targetPath[0], args)
if err != nil {
return err
}

case "symlink":
err = sftpConn.Symlink(targetPath[0], targetPath[1])
if err != nil {
return err
}

}

Check failure on line 989 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (oldstable)

unnecessary trailing newline (whitespace)

Check failure on line 989 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (stable)

unnecessary trailing newline (whitespace)

Check failure on line 989 in cmd/incus/file.go

View workflow job for this annotation

GitHub Actions / Code (tip)

unnecessary trailing newline (whitespace)
return nil
}

func (c *cmdFile) recursivePullFile(d incus.InstanceServer, inst string, p string, targetDir string) error {
buf, resp, err := d.GetInstanceFile(inst, p)
if err != nil {
Expand Down

0 comments on commit 6b1840f

Please sign in to comment.