From 7113c51e6748021a3ffbb0091424da6ae61eb7c7 Mon Sep 17 00:00:00 2001 From: HassanAlsamahi Date: Tue, 5 Nov 2024 17:01:18 +0100 Subject: [PATCH] incus/file/create: Use SFTP client instead of file API Signed-off-by: HassanAlsamahi --- cmd/incus/file.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/cmd/incus/file.go b/cmd/incus/file.go index 3448c9507fc..579854737f0 100644 --- a/cmd/incus/file.go +++ b/cmd/incus/file.go @@ -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 @@ -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 {