Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/x/plan9/ninep/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186406
  max_deposit = "8000000ugnot"

ninep.gno

// Package ninep is a Plan 9 shaped file abstraction for gno.
//
// It implements the SEMANTICS of 9P2000, not its wire format: there is no
// socket on a chain, the VM call is the transport. What it keeps is the part
// that made 9P useful, namely that every resource answers the same four
// questions (stat, walk, read, readdir), so a client written today can browse
// a file server deployed tomorrow.
//
// One interface, not two. 9P reads a directory with the same Tread it uses for
// a file, so splitting File from Dir would be less faithful, and a single
// interface means no type assertion across a realm boundary.
//
// Deliberate divergences from 9P2000, all of them forced:
//
//   - Data is a string, not []byte. Every consumer on this chain is text and
//     Render returns a string.
//   - Mtime is a block height, not a wall clock. It is the only clock that
//     every validating node agrees on.
//   - There is no open/clunk pair. Without a session there are no fids, so
//     Walk returns the file itself and nothing has to be released.
//   - File is READ-ONLY. A crossing method would mint the caller's realm frame
//     for the callee, which is a confused-deputy hazard (compare r/gov/dao's
//     Executor, and p/nt/grc20's deliberate refusal to make Teller crossing).
//     Mutation lives in Mutable, which is only safe on a tree your own realm
//     owns.
//
// See gno.land/p/nym-moultest123/x/plan9/ns for the namespace that binds these trees
// together, and gno.land/p/nym-moultest123/x/plan9/memfs for the reference server.
//
// NOTICE. Plan 9 from Bell Labs is the work of the Computing Science Research
// Center at Bell Labs; the name and the marks are theirs, and the copyright is
// held by the Plan 9 Foundation (https://p9f.org). This package is not
// affiliated with, endorsed by, or sponsored by them, and contains no Plan 9
// code: it borrows the vocabulary so that the design reads without a glossary,
// and it is an homage, asking what that ecosystem's spirit looks like on a
// chain. Full attribution: NOTICE.md at the root of moul/gno-contracts.
package ninep

import (
	"errors"
	"path"
	"strconv"
	"strings"
)

// Qid type bits, as in 9P2000.
const (
	QTFILE   uint8 = 0x00 // a plain file
	QTTMP    uint8 = 0x04 // not archived
	QTAUTH   uint8 = 0x08 // authentication file
	QTMOUNT  uint8 = 0x10 // mounted channel
	QTEXCL   uint8 = 0x20 // exclusive use
	QTAPPEND uint8 = 0x40 // append only
	QTDIR    uint8 = 0x80 // a directory
)

// Perm holds the 9P mode word: the low nine bits are rwx for owner, group and
// other, the high bits are the DM* kind flags.
type Perm uint32

// Mode bits, as in 9P2000.
const (
	DMTMP    Perm = 0x04000000
	DMAUTH   Perm = 0x08000000
	DMMOUNT  Perm = 0x10000000
	DMEXCL   Perm = 0x20000000
	DMAPPEND Perm = 0x40000000
	DMDIR    Perm = 0x80000000

	PermMask Perm = 0777 // the rwxrwxrwx bits

	// Conventional defaults, matching what Plan 9's ramfs hands out.
	DirPerm  Perm = DMDIR | 0755
	FilePerm Perm = 0644
)

// IsDir reports whether the mode marks a directory.
func (p Perm) IsDir() bool { return p&DMDIR != 0 }

// String renders the mode the way ls -l does: a kind letter then nine rwx
// bits. The kind letter is 'd' for a directory, 'a' for append-only, 'l' for
// exclusive-use, '-' otherwise.
func (p Perm) String() string {
	var b strings.Builder
	switch {
	case p&DMDIR != 0:
		b.WriteByte('d')
	case p&DMAPPEND != 0:
		b.WriteByte('a')
	case p&DMEXCL != 0:
		b.WriteByte('l')
	default:
		b.WriteByte('-')
	}
	const rwx = "rwxrwxrwx"
	for i := 0; i < 9; i++ {
		if p&(1<<uint(8-i)) != 0 {
			b.WriteByte(rwx[i])
		} else {
			b.WriteByte('-')
		}
	}
	return b.String()
}

// Qid is the server's unique handle for a file. Path identifies the file
// within one server for its whole lifetime; Version increments on every write,
// so a client can tell "same file, changed" from "different file" without
// reading either.
type Qid struct {
	Type    uint8
	Version uint32
	Path    uint64
}

// IsDir reports whether the qid marks a directory.
func (q Qid) IsDir() bool { return q.Type&QTDIR != 0 }

// String renders the qid as Plan 9 does, "(path version type)", with the path
// in hex.
func (q Qid) String() string {
	kind := "f"
	if q.IsDir() {
		kind = "d"
	}
	return "(" + strconv.FormatUint(q.Path, 16) + " " +
		strconv.FormatUint(uint64(q.Version), 10) + " " + kind + ")"
}

// Stat is 9P's directory entry, minus the fields that only mean something on a
// wire (type, dev) or on a host clock (atime).
type Stat struct {
	Qid    Qid
	Mode   Perm
	Mtime  int64 // block height of the last write
	Length int64 // in bytes; zero for a directory, as in 9P
	Name   string
	Uid    string // owner; a bech32 address, or a well-known name
	Gid    string // group
	Muid   string // last writer
}

// IsDir reports whether the entry is a directory.
func (s Stat) IsDir() bool { return s.Mode.IsDir() }

// Line renders the entry the way ls -l does.
func (s Stat) Line() string {
	return s.Mode.String() + " " + pad(s.Uid, 12) + " " + pad(s.Gid, 8) + " " +
		lpad(strconv.FormatInt(s.Length, 10), 7) + " " + s.Name
}

// pad right-pads to n runes. ufmt has no width flags in gno, so padding is by
// hand everywhere in this suite.
func pad(s string, n int) string {
	for len(s) < n {
		s += " "
	}
	return s
}

func lpad(s string, n int) string {
	for len(s) < n {
		s = " " + s
	}
	return s
}

// File is a 9P file server's whole read surface. Every method must be free of
// side effects: a File is routinely reached across a realm boundary, where the
// running frame belongs to the CALLER, so mutating anything here would be both
// a VM error and a confused deputy.
type File interface {
	// Stat returns the entry for this file.
	Stat() Stat
	// Walk resolves exactly one path element. It returns ErrNotDir on a
	// plain file and ErrNotExist when the name is absent. It never sees
	// "." or "..": both are removed lexically before resolution starts.
	Walk(name string) (File, error)
	// Read returns at most count bytes starting at off. A negative count
	// means "to the end". It returns ErrIsDir on a directory.
	Read(off, count int64) (string, error)
	// ReadDir returns the directory's entries in a deterministic order. It
	// returns ErrNotDir on a plain file.
	ReadDir() ([]Stat, error)
}

// Mutable is the write half, kept out of File on purpose.
//
// It is NOT safe across a realm boundary: a non-crossing method runs in the
// caller's frame, so a foreign realm calling these would be trying to mutate
// objects it does not own. Only call Mutable on a tree your own realm created.
// The caller supplies now (a block height) rather than the tree reading the
// chain itself, so the same code is testable off chain.
type Mutable interface {
	File
	Create(name string, perm Perm, now int64) (File, error)
	Remove(name string) error
	Write(off int64, data string, now int64) (int64, error)
	Truncate(size int64, now int64) error
}

// Plan 9 error strings, kept lowercase and verbatim where they exist.
var (
	ErrNotExist = errors.New("file does not exist")
	ErrNotDir   = errors.New("not a directory")
	ErrIsDir    = errors.New("is a directory")
	ErrExist    = errors.New("file already exists")
	ErrPerm     = errors.New("permission denied")
	ErrNoCreate = errors.New("create prohibited")
	ErrReadOnly = errors.New("read-only file server")
	ErrBadName  = errors.New("bad character in file name")
	ErrNotEmpty = errors.New("directory not empty")
	ErrTooDeep  = errors.New("path too deep")
)

// MaxDepth bounds a walk. Resolution costs one cross-realm call per element
// per union member, so depth is capped rather than trusted.
const MaxDepth = 32

// ValidName reports whether name is usable as a single path element. Plan 9
// rejects the empty name, "." and "..", and any name containing a slash.
func ValidName(name string) bool {
	if name == "" || name == "." || name == ".." {
		return false
	}
	return !strings.Contains(name, "/")
}

// Clean returns p as a cleaned absolute path. "." and ".." are resolved
// lexically, before any server sees them, which is what makes ".." undo the
// name you typed rather than the directory you landed in (see "Lexical File
// Names in Plan 9").
func Clean(p string) string {
	if !strings.HasPrefix(p, "/") {
		p = "/" + p
	}
	return path.Clean(p)
}

// Abs resolves p against cwd, then cleans it.
func Abs(cwd, p string) string {
	if p == "" {
		return Clean(cwd)
	}
	if strings.HasPrefix(p, "/") {
		return Clean(p)
	}
	if cwd == "" {
		cwd = "/"
	}
	return Clean(cwd + "/" + p)
}

// Elems splits a cleaned absolute path into its elements. The root yields nil.
func Elems(p string) []string {
	p = Clean(p)
	if p == "/" {
		return nil
	}
	return strings.Split(p[1:], "/")
}

// Base returns the last element of p, or "/" for the root.
func Base(p string) string { return path.Base(Clean(p)) }

// Dir returns p's parent.
func Dir(p string) string { return path.Dir(Clean(p)) }

// Join appends name to dir.
func Join(dir, name string) string { return Clean(dir + "/" + name) }

// Walk resolves elems from f, one element at a time. It is the plain,
// namespace-free walk: no binds, no unions. Use ns.Ns for those.
func Walk(f File, elems []string) (File, error) {
	if len(elems) > MaxDepth {
		return nil, ErrTooDeep
	}
	for _, e := range elems {
		next, err := f.Walk(e)
		if err != nil {
			return nil, err
		}
		f = next
	}
	return f, nil
}

// ReadAll reads a whole file.
func ReadAll(f File) (string, error) { return f.Read(0, -1) }

// Slice applies 9P's read window to s: at most count bytes from off, with a
// negative count meaning "to the end". An offset past the end reads empty,
// which is what makes a read loop terminate rather than fail.
func Slice(s string, off, count int64) string {
	if off < 0 {
		off = 0
	}
	n := int64(len(s))
	if off >= n {
		return ""
	}
	end := n
	if count >= 0 && off+count < n {
		end = off + count
	}
	return s[off:end]
}

ninep_test.gno

package ninep

import "testing"

func TestPermString(t *testing.T) {
	tests := []struct {
		name string
		perm Perm
		want string
	}{
		{"dir 0755", DMDIR | 0755, "drwxr-xr-x"},
		{"file 0644", 0644, "-rw-r--r--"},
		{"file 0600", 0600, "-rw-------"},
		{"file 0777", 0777, "-rwxrwxrwx"},
		{"file 0000", 0, "----------"},
		{"append only", DMAPPEND | 0644, "arw-r--r--"},
		{"exclusive", DMEXCL | 0600, "lrw-------"},
		{"dir default", DirPerm, "drwxr-xr-x"},
		{"file default", FilePerm, "-rw-r--r--"},
	}
	for _, tt := range tests {
		if got := tt.perm.String(); got != tt.want {
			t.Errorf("%s: got %q, want %q", tt.name, got, tt.want)
		}
	}
}

func TestPermIsDir(t *testing.T) {
	if !(DMDIR | 0755).IsDir() {
		t.Error("DMDIR|0755 should be a directory")
	}
	if Perm(0644).IsDir() {
		t.Error("0644 should not be a directory")
	}
}

func TestQidString(t *testing.T) {
	tests := []struct {
		name string
		qid  Qid
		want string
	}{
		{"root", Qid{Type: QTDIR, Version: 0, Path: 0}, "(0 0 d)"},
		{"file", Qid{Type: QTFILE, Version: 3, Path: 17}, "(11 3 f)"},
		{"big path", Qid{Type: QTFILE, Version: 1, Path: 255}, "(ff 1 f)"},
	}
	for _, tt := range tests {
		if got := tt.qid.String(); got != tt.want {
			t.Errorf("%s: got %q, want %q", tt.name, got, tt.want)
		}
	}
}

func TestStatLine(t *testing.T) {
	s := Stat{
		Qid:    Qid{Type: QTFILE, Path: 2, Version: 1},
		Mode:   0644,
		Length: 12,
		Name:   "greeting",
		Uid:    "glenda",
		Gid:    "sys",
	}
	want := "-rw-r--r-- glenda       sys           12 greeting"
	if got := s.Line(); got != want {
		t.Errorf("got %q, want %q", got, want)
	}
}

func TestValidName(t *testing.T) {
	tests := []struct {
		name string
		want bool
	}{
		{"dev", true},
		{"a.b", true},
		{"with space", true},
		{"", false},
		{".", false},
		{"..", false},
		{"a/b", false},
		{"/", false},
	}
	for _, tt := range tests {
		if got := ValidName(tt.name); got != tt.want {
			t.Errorf("ValidName(%q): got %v, want %v", tt.name, got, tt.want)
		}
	}
}

func TestClean(t *testing.T) {
	tests := []struct {
		in   string
		want string
	}{
		{"", "/"},
		{"/", "/"},
		{"dev", "/dev"},
		{"/dev/", "/dev"},
		{"/dev//height", "/dev/height"},
		{"/dev/./height", "/dev/height"},
		{"/dev/../srv", "/srv"},
		{"/../..", "/"},
		{"/a/b/../../c", "/c"},
	}
	for _, tt := range tests {
		if got := Clean(tt.in); got != tt.want {
			t.Errorf("Clean(%q): got %q, want %q", tt.in, got, tt.want)
		}
	}
}

func TestAbs(t *testing.T) {
	tests := []struct {
		cwd  string
		in   string
		want string
	}{
		{"/", "dev", "/dev"},
		{"/usr/glenda", "bin", "/usr/glenda/bin"},
		{"/usr/glenda", "/dev", "/dev"},
		{"/usr/glenda", "..", "/usr"},
		{"/usr/glenda", "", "/usr/glenda"},
		{"", "dev", "/dev"},
	}
	for _, tt := range tests {
		if got := Abs(tt.cwd, tt.in); got != tt.want {
			t.Errorf("Abs(%q, %q): got %q, want %q", tt.cwd, tt.in, got, tt.want)
		}
	}
}

func TestElems(t *testing.T) {
	tests := []struct {
		in   string
		want []string
	}{
		{"/", nil},
		{"", nil},
		{"/dev", []string{"dev"}},
		{"/dev/height", []string{"dev", "height"}},
		{"/a/b/../c", []string{"a", "c"}},
	}
	for _, tt := range tests {
		got := Elems(tt.in)
		if len(got) != len(tt.want) {
			t.Errorf("Elems(%q): got %v, want %v", tt.in, got, tt.want)
			continue
		}
		for i := range got {
			if got[i] != tt.want[i] {
				t.Errorf("Elems(%q)[%d]: got %q, want %q", tt.in, i, got[i], tt.want[i])
			}
		}
	}
}

func TestBaseDirJoin(t *testing.T) {
	if got := Base("/dev/height"); got != "height" {
		t.Errorf("Base: got %q", got)
	}
	if got := Base("/"); got != "/" {
		t.Errorf("Base of root: got %q", got)
	}
	if got := Dir("/dev/height"); got != "/dev" {
		t.Errorf("Dir: got %q", got)
	}
	if got := Join("/dev", "height"); got != "/dev/height" {
		t.Errorf("Join: got %q", got)
	}
	if got := Join("/", "dev"); got != "/dev" {
		t.Errorf("Join at root: got %q", got)
	}
}

func TestSlice(t *testing.T) {
	const s = "hello world"
	tests := []struct {
		name  string
		off   int64
		count int64
		want  string
	}{
		{"whole", 0, -1, "hello world"},
		{"prefix", 0, 5, "hello"},
		{"middle", 6, 5, "world"},
		{"past end", 99, 5, ""},
		{"count past end", 6, 99, "world"},
		{"negative offset clamps", -4, 5, "hello"},
		{"zero count", 0, 0, ""},
	}
	for _, tt := range tests {
		if got := Slice(s, tt.off, tt.count); got != tt.want {
			t.Errorf("%s: got %q, want %q", tt.name, got, tt.want)
		}
	}
}