CodeSampleX

Sample

golang.org/x/sys v0.22.0: unix.O_TRUNC

Verified sample for golang golang.org/x/sys v0.22.0: unix.O_TRUNC. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:f1535347f49538683a6d9da5c866bd986adbbb4da2992e6c6bcc79102f9d176a

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go go

Verification-run environments

Environment Contract Stages Run
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-09-15

Case

HOW
Goal
verify golang.org/x/sys/unix.O_TRUNC in pkg:golang/golang.org/x/sys@v0.22.0
Packages
Symbols
  • golang.org/x/sys/unix.O_TRUNC
Created
2026-09-15T09:40:37Z

Contract

  1. unix.O_TRUNC is a positive non-zero flag matching syscall.O_TRUNC and os.O_TRUNC
  2. unix.Open with unix.O_TRUNC truncates an existing non-empty file to zero length
  3. unix.Open with unix.O_TRUNC preserves the existing file inode and permissions
  4. unix.Open with unix.O_TRUNC and unix.O_WRONLY allows writing new content from offset zero
  5. unix.Open without unix.O_TRUNC preserves existing file content
  6. unix.Openat with unix.O_TRUNC truncates an existing relative file to zero length

Files

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

Download the source artifact (tar.gz)

Source

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 golang.org/x/sys/unix.O_TRUNC in pkg:golang/golang.org/x/sys@v0.22.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/sys@v0.22.0
Demonstrate these symbols/APIs:
  - golang.org/x/sys/unix.O_TRUNC

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:b0a11febbc60bf4b43267498656514eea6a30f71a2a9598bcb58c20fdca551ec","contract":["unix.O_TRUNC is a positive non-zero flag matching syscall.O_TRUNC and os.O_TRUNC","unix.Open with unix.O_TRUNC truncates an existing non-empty file to zero length","unix.Open with unix.O_TRUNC preserves the existing file inode and permissions","unix.Open with unix.O_TRUNC and unix.O_WRONLY allows writing new content from offset zero","unix.Open without unix.O_TRUNC preserves existing file content","unix.Openat with unix.O_TRUNC truncates an existing relative file to zero length"],"goal":"verify golang.org/x/sys/unix.O_TRUNC in pkg:golang/golang.org/x/sys@v0.22.0","kind":"HOW","packages":["pkg:golang/golang.org/x/sys@v0.22.0"],"schemaVersion":1,"symbols":["golang.org/x/sys/unix.O_TRUNC"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/sys@v0.22.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/sys@v0.22.0","symbols":["golang.org/x/sys/unix.O_TRUNC"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22

require golang.org/x/sys v0.22.0
go.sum
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
main.go
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"golang.org/x/sys/unix"
)

func main() {
	tmpDir, err := os.MkdirTemp("", "csx-example-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create temp dir: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	filePath := filepath.Join(tmpDir, "example.txt")

	// Create a file with initial content
	if err := os.WriteFile(filePath, []byte("Existing file content"), 0600); err != nil {
		fmt.Fprintf(os.Stderr, "failed to write file: %v\n", err)
		os.Exit(1)
	}

	// Open the file with unix.O_TRUNC to truncate its content to zero
	fd, err := unix.Open(filePath, unix.O_WRONLY|unix.O_TRUNC, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unix.Open with O_TRUNC failed: %v\n", err)
		os.Exit(1)
	}

	// Write new content to the truncated file
	newContent := []byte("Truncated and replaced")
	if _, err := unix.Write(fd, newContent); err != nil {
		unix.Close(fd)
		fmt.Fprintf(os.Stderr, "unix.Write failed: %v\n", err)
		os.Exit(1)
	}
	unix.Close(fd)

	fmt.Println("File truncated and written successfully using unix.O_TRUNC")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/sys/unix.O_TRUNC in pkg:golang/golang.org/x/sys@v0.22.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/sys@v0.22.0"
  ],
  "symbols": [
    "golang.org/x/sys/unix.O_TRUNC"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"syscall"

	"golang.org/x/sys/unix"
)

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed successfully.")
}

func runTests() error {
	// 1. unix.O_TRUNC is a positive non-zero flag matching syscall.O_TRUNC and os.O_TRUNC
	if unix.O_TRUNC <= 0 {
		return fmt.Errorf("expected unix.O_TRUNC > 0, got %d", unix.O_TRUNC)
	}
	if unix.O_TRUNC != syscall.O_TRUNC {
		return fmt.Errorf("expected unix.O_TRUNC == syscall.O_TRUNC (%d), got %d", syscall.O_TRUNC, unix.O_TRUNC)
	}
	if unix.O_TRUNC != os.O_TRUNC {
		return fmt.Errorf("expected unix.O_TRUNC == os.O_TRUNC (%d), got %d", os.O_TRUNC, unix.O_TRUNC)
	}

	tmpDir, err := os.MkdirTemp("", "csx-test-trunc-*")
	if err != nil {
		return fmt.Errorf("failed to create temp dir: %w", err)
	}
	defer os.RemoveAll(tmpDir)

	// 2. unix.Open with unix.O_TRUNC truncates an existing non-empty file to zero length
	testFile1 := filepath.Join(tmpDir, "trunc1.dat")
	initialData := []byte("hello initial data to be truncated")
	if err := os.WriteFile(testFile1, initialData, 0600); err != nil {
		return fmt.Errorf("failed to write initial file: %w", err)
	}

	fd1, err := unix.Open(testFile1, unix.O_WRONLY|unix.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("unix.Open with O_TRUNC failed: %w", err)
	}
	unix.Close(fd1)

	var stat1 unix.Stat_t
	if err := unix.Stat(testFile1, &stat1); err != nil {
		return fmt.Errorf("unix.Stat failed: %w", err)
	}
	if stat1.Size != 0 {
		return fmt.Errorf("expected truncated file size 0, got %d", stat1.Size)
	}

	// 3. unix.Open with unix.O_TRUNC preserves the existing file inode and permissions
	testFile2 := filepath.Join(tmpDir, "trunc2.dat")
	if err := os.WriteFile(testFile2, []byte("preserve inode and mode test"), 0640); err != nil {
		return fmt.Errorf("failed to write testFile2: %w", err)
	}
	var beforeStat unix.Stat_t
	if err := unix.Stat(testFile2, &beforeStat); err != nil {
		return fmt.Errorf("unix.Stat before truncate failed: %w", err)
	}

	fd2, err := unix.Open(testFile2, unix.O_WRONLY|unix.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("unix.Open with O_TRUNC failed: %w", err)
	}
	unix.Close(fd2)

	var afterStat unix.Stat_t
	if err := unix.Stat(testFile2, &afterStat); err != nil {
		return fmt.Errorf("unix.Stat after truncate failed: %w", err)
	}
	if afterStat.Ino != beforeStat.Ino {
		return fmt.Errorf("expected inode %d preserved, got %d", beforeStat.Ino, afterStat.Ino)
	}
	if (afterStat.Mode & 0777) != (beforeStat.Mode & 0777) {
		return fmt.Errorf("expected mode %o preserved, got %o", beforeStat.Mode&0777, afterStat.Mode&0777)
	}
	if afterStat.Size != 0 {
		return fmt.Errorf("expected size 0, got %d", afterStat.Size)
	}

	// 4. unix.Open with unix.O_TRUNC and unix.O_WRONLY allows writing new content from offset zero
	testFile3 := filepath.Join(tmpDir, "trunc3.dat")
	longContent := []byte("this is a long string that should be completely overwritten by shorter content")
	if err := os.WriteFile(testFile3, longContent, 0600); err != nil {
		return fmt.Errorf("failed to write testFile3: %w", err)
	}

	fd3, err := unix.Open(testFile3, unix.O_WRONLY|unix.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("unix.Open with O_TRUNC failed: %w", err)
	}
	newContent := []byte("short")
	n, err := unix.Write(fd3, newContent)
	unix.Close(fd3)
	if err != nil {
		return fmt.Errorf("unix.Write failed: %w", err)
	}
	if n != len(newContent) {
		return fmt.Errorf("expected %d bytes written, got %d", len(newContent), n)
	}

	readContent, err := os.ReadFile(testFile3)
	if err != nil {
		return fmt.Errorf("os.ReadFile failed: %w", err)
	}
	if !bytes.Equal(readContent, newContent) {
		return fmt.Errorf("expected content %q, got %q", string(newContent), string(readContent))
	}

	// 5. unix.Open without unix.O_TRUNC preserves existing file content
	testFile4 := filepath.Join(tmpDir, "trunc4.dat")
	original4 := []byte("abcdefghijklmnop")
	if err := os.WriteFile(testFile4, original4, 0600); err != nil {
		return fmt.Errorf("failed to write testFile4: %w", err)
	}

	fd4, err := unix.Open(testFile4, unix.O_WRONLY, 0600)
	if err != nil {
		return fmt.Errorf("unix.Open without O_TRUNC failed: %w", err)
	}
	if _, err := unix.Write(fd4, []byte("ZZ")); err != nil {
		unix.Close(fd4)
		return fmt.Errorf("unix.Write failed: %w", err)
	}
	unix.Close(fd4)

	content4, err := os.ReadFile(testFile4)
	if err != nil {
		return fmt.Errorf("os.ReadFile failed: %w", err)
	}
	expected4 := []byte("ZZcdefghijklmnop")
	if !bytes.Equal(content4, expected4) {
		return fmt.Errorf("expected %q, got %q", string(expected4), string(content4))
	}

	// 6. unix.Openat with unix.O_TRUNC truncates an existing relative file to zero length
	testFile5 := filepath.Join(tmpDir, "trunc5.dat")
	if err := os.WriteFile(testFile5, []byte("openat truncate test data"), 0600); err != nil {
		return fmt.Errorf("failed to write testFile5: %w", err)
	}

	dirFd, err := unix.Open(tmpDir, unix.O_RDONLY, 0)
	if err != nil {
		return fmt.Errorf("failed to open dirFd: %w", err)
	}
	defer unix.Close(dirFd)

	fd5, err := unix.Openat(dirFd, "trunc5.dat", unix.O_WRONLY|unix.O_TRUNC, 0600)
	if err != nil {
		return fmt.Errorf("unix.Openat with O_TRUNC failed: %w", err)
	}
	unix.Close(fd5)

	var stat5 unix.Stat_t
	if err := unix.Stat(testFile5, &stat5); err != nil {
		return fmt.Errorf("unix.Stat failed: %w", err)
	}
	if stat5.Size != 0 {
		return fmt.Errorf("expected size 0 after Openat O_TRUNC, got %d", stat5.Size)
	}

	return nil
}

Origin Seeder

anonymous