CodeSampleX

示例

github.com/pkg/sftp v1.10.1: NewClientPipe

已验证示例 — golang github.com/pkg/sftp v1.10.1: NewClientPipe. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: sftp.NewClientPipe(rd, wr, opts...)…

sha256:ebe9e208a8895c9de55bb2043f195e14d220dd0a63ae942b93cba61e35077b4a

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-20

案例

HOW
目标
verify NewClientPipe in pkg:golang/github.com/pkg/sftp@v1.10.1
包
符号
  • NewClientPipe
创建时间
2026-10-20T14:38:04Z

契约

  1. sftp.NewClientPipe(rd, wr, opts...) completes the SFTP v3 init/version handshake over an io.Reader and io.WriteCloser pair and returns an initialized *sftp.Client ready for file operations
  2. sftp.NewClientPipe returns an error and closes wr immediately if any ClientOption (such as MaxPacket(0)) fails validation
  3. sftp.NewClientPipe returns an error and closes wr if the underlying Reader or WriteCloser fails during the initial handshake

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify NewClientPipe in pkg:golang/github.com/pkg/sftp@v1.10.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/pkg/sftp@v1.10.1
Demonstrate these symbols/APIs:
  - NewClientPipe

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:89b46ba438c7f1c7c5dd94e66afd30ebafbf3986c1d6364725b8ae37caa7deb9","contract":["sftp.NewClientPipe(rd, wr, opts...) completes the SFTP v3 init/version handshake over an io.Reader and io.WriteCloser pair and returns an initialized *sftp.Client ready for file operations","sftp.NewClientPipe returns an error and closes wr immediately if any ClientOption (such as MaxPacket(0)) fails validation","sftp.NewClientPipe returns an error and closes wr if the underlying Reader or WriteCloser fails during the initial handshake"],"goal":"verify NewClientPipe in pkg:golang/github.com/pkg/sftp@v1.10.1","kind":"HOW","packages":["pkg:golang/github.com/pkg/sftp@v1.10.1"],"schemaVersion":1,"symbols":["NewClientPipe"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/pkg/sftp@v1.10.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/pkg/sftp@v1.10.1","symbols":["NewClientPipe"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.21

require github.com/pkg/sftp v1.10.1

require (
	github.com/kr/fs v0.1.0 // indirect
	github.com/pkg/errors v0.8.1 // indirect
	golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 // indirect
	golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
)
go.sum
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1 h1:VasscCm72135zRysgrJDKsntdmPN+OuU3+nnHYA9wyc=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify NewClientPipe in pkg:golang/github.com/pkg/sftp@v1.10.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/pkg/sftp@v1.10.1"
  ],
  "symbols": [
    "NewClientPipe"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"github.com/pkg/sftp"
)

type pipeRWC struct {
	io.Reader
	io.WriteCloser
}

func (p pipeRWC) Close() error {
	if c, ok := p.Reader.(io.Closer); ok {
		_ = c.Close()
	}
	return p.WriteCloser.Close()
}

func main() {
	// Case 1: Connecting NewClientPipe to an in-memory SFTP RequestServer over io.Pipe
	c2sReader, c2sWriter := io.Pipe()
	s2cReader, s2cWriter := io.Pipe()

	serverRWC := pipeRWC{Reader: c2sReader, WriteCloser: s2cWriter}
	server := sftp.NewRequestServer(serverRWC, sftp.InMemHandler())
	serverDone := make(chan error, 1)
	go func() {
		serverDone <- server.Serve()
	}()

	client, err := sftp.NewClientPipe(s2cReader, c2sWriter, sftp.MaxPacket(32768), sftp.MaxConcurrentRequestsPerFile(16))
	if err != nil {
		fmt.Fprintf(os.Stderr, "NewClientPipe failed: %v\n", err)
		os.Exit(1)
	}

	// Write a file through the client pipe, stat it, and read its exact bytes back
	f, err := client.Create("/hello.txt")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Create failed: %v\n", err)
		os.Exit(1)
	}
	payload := []byte("verified via NewClientPipe")
	n, err := f.Write(payload)
	if err != nil || n != len(payload) {
		fmt.Fprintf(os.Stderr, "Write failed: n=%d err=%v\n", n, err)
		os.Exit(1)
	}
	if err := f.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "file Close failed: %v\n", err)
		os.Exit(1)
	}

	fi, err := client.Stat("/hello.txt")
	if err != nil || fi.Size() != int64(len(payload)) {
		fmt.Fprintf(os.Stderr, "Stat failed: fi=%v err=%v\n", fi, err)
		os.Exit(1)
	}

	rf, err := client.Open("/hello.txt")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Open failed: %v\n", err)
		os.Exit(1)
	}
	got := make([]byte, len(payload))
	rn, err := rf.Read(got)
	if (err != nil && err != io.EOF) || rn != len(payload) {
		fmt.Fprintf(os.Stderr, "Read failed: rn=%d err=%v\n", rn, err)
		os.Exit(1)
	}
	if err := rf.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "rf Close failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(got, payload) {
		fmt.Fprintf(os.Stderr, "read mismatch: got %q, want %q\n", got, payload)
		os.Exit(1)
	}

	// Close server then client cleanly
	_ = server.Close()
	if err := client.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "client Close failed: %v\n", err)
		os.Exit(1)
	}
	<-serverDone

	// Case 2: Invalid ClientOption returns an error from NewClientPipe before handshake and closes the WriteCloser
	r2, w2 := io.Pipe()
	defer r2.Close()
	_, optErr := sftp.NewClientPipe(r2, w2, sftp.MaxPacket(0))
	if optErr == nil {
		fmt.Fprintf(os.Stderr, "expected error for MaxPacket(0), got nil\n")
		os.Exit(1)
	}

	// Case 3: Closed pipe during version handshake returns an error from NewClientPipe
	r3, w3 := io.Pipe()
	_ = r3.Close()
	_ = w3.Close()
	_, pipeErr := sftp.NewClientPipe(r3, w3)
	if pipeErr == nil {
		fmt.Fprintf(os.Stderr, "expected error when pipe is closed during init, got nil\n")
		os.Exit(1)
	}
}

原始种子者

匿名