Documentation
¶
Overview ¶
Package file provides chunked Upload and Download helpers for transferring files to and from a remote Windows host over WinRM.
The implementation runs PowerShell commands via the existing CMD shell (encoded-command path). It chunks file content, base64-encodes each chunk and appends to a temp file on the target, then renames atomically. Download streams base64-encoded bytes from the host and decodes locally.
Reference: WinRb/winrm-fs (Ruby).
Index ¶
Constants ¶
const DefaultChunkSize = 1024
DefaultChunkSize is the plaintext chunk size used when the caller doesn't override it. The bound is set by cmd.exe's command-line cap (~8 KiB), not by the WinRM envelope: we ship each chunk via powershell.exe -EncodedCommand which goes through cmd, and overflow silently truncates and produces a non-zero exit with no stderr.
Worst-case sizing for 1024 plaintext bytes:
1024 (plaintext) → ~1370 (chunk b64) + ~290 (PS wrapper) + ~40 ($ProgressPreference prefix) → ~1700 char script × 2 (UTF-16 LE) → ~3400 bytes × 4/3 (outer base64) → ~4530 char cmdline + "powershell.exe -EncodedCommand " → ~4570 char cmdline
Sits comfortably under cmd.exe's ~8191 char limit. Callers using a PSRP pathway can raise FileManager.ChunkSize freely.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CommandRunner ¶
type CommandRunner interface {
RunPS(ctx context.Context, script string) (stdout, stderr string, exitCode int, err error)
}
CommandRunner abstracts the PowerShell execution surface so the FileManager can be unit-tested without a real WinRM transport.
stdout/stderr are returned as strings; exitCode is the remote process exit code. err is non-nil only on transport-level failures.
type FileManager ¶
type FileManager struct {
Runner CommandRunner
ChunkSize int
}
FileManager bundles upload and download operations against a CommandRunner (typically a *winrm.Client adapter).
func NewFileManager ¶
func NewFileManager(runner CommandRunner) *FileManager
NewFileManager constructs a FileManager using the default chunk size.
func (*FileManager) Download ¶
func (fm *FileManager) Download(ctx context.Context, remotePath, localPath string, progress ProgressFunc) error
Download copies remotePath from the WinRM target to localPath. The remote file is read in chunks; each chunk is base64-encoded by PowerShell, returned via stdout and decoded locally.
func (*FileManager) Upload ¶
func (fm *FileManager) Upload(ctx context.Context, localPath, remotePath string, progress ProgressFunc) error
Upload copies localPath to remotePath on the WinRM target. The remote path may use either forward or backward slashes; both are accepted.
Strategy: create the parent directory if missing, stream the file in chunks (each chunk decoded server-side and appended to a temp file), then atomically Move-Item to the final destination.
type ProgressFunc ¶
type ProgressFunc func(sent, total int64)
ProgressFunc is invoked after each chunk is processed. sent is the number of plaintext bytes transferred so far; total is the file size when known (zero for streaming download initial calls).