go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/connection/ssh/scp/fs_file.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package scp
     5  
     6  import (
     7  	"bytes"
     8  	"errors"
     9  	"io"
    10  	"os"
    11  
    12  	scp_client "github.com/hnakamur/go-scp"
    13  )
    14  
    15  // FileOpen copies a file into buffer
    16  // TODO: check handling for directories
    17  // TODO: not suited for large files, we should offload those into a temp directory
    18  func FileOpen(scpClient *scp_client.SCP, path string) (*File, error) {
    19  	// download file
    20  	var buf bytes.Buffer
    21  	_, err := scpClient.Receive(path, &buf)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return &File{
    26  		buf:       &buf,
    27  		path:      path,
    28  		scpClient: scpClient,
    29  	}, nil
    30  }
    31  
    32  type File struct {
    33  	buf       *bytes.Buffer
    34  	path      string
    35  	scpClient *scp_client.SCP
    36  }
    37  
    38  func (f *File) Close() error {
    39  	return nil
    40  }
    41  
    42  func (f *File) Name() string {
    43  	return f.path
    44  }
    45  
    46  func (f *File) Stat() (os.FileInfo, error) {
    47  	return f.scpClient.Receive(f.path, io.Discard)
    48  }
    49  
    50  func (f *File) Sync() error {
    51  	return nil
    52  }
    53  
    54  func (f *File) Truncate(size int64) error {
    55  	return nil
    56  }
    57  
    58  func (f *File) Read(b []byte) (n int, err error) {
    59  	return f.buf.Read(b)
    60  }
    61  
    62  func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
    63  	return 0, errors.New("not implemented")
    64  }
    65  
    66  func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
    67  	return nil, errors.New("not implemented")
    68  }
    69  
    70  func (f *File) Readdirnames(n int) (names []string, err error) {
    71  	return nil, errors.New("not implemented")
    72  }
    73  
    74  func (f *File) Seek(offset int64, whence int) (int64, error) {
    75  	return 0, errors.New("not implemented")
    76  }
    77  
    78  func (f *File) Write(b []byte) (n int, err error) {
    79  	return f.buf.Write(b)
    80  }
    81  
    82  func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
    83  	return 0, errors.New("not implemented")
    84  }
    85  
    86  func (f *File) WriteString(s string) (ret int, err error) {
    87  	return f.buf.WriteString(s)
    88  }