Skip to content

Commit

Permalink
Merge pull request lxc#1354 from HassanAlsamahi/change-incus-file-cre…
Browse files Browse the repository at this point in the history
…ate-operation-to-sftp

incus/file/create: Use SFTP client instead of file API
  • Loading branch information
stgraber authored Nov 7, 2024
2 parents 6cfd388 + 7113c51 commit 5157bfe
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,14 @@ 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 +935,61 @@ func (c *cmdFilePush) Run(cmd *cobra.Command, args []string) error {
return nil
}

func (c *cmdFile) setOwnerMode(sftpConn *sftp.Client, targetPath string, args incus.InstanceFileArgs) error {
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 func() { _ = sftpConn.Close() }()

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

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

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

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

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

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 5157bfe

Please sign in to comment.