lib9p

Go 9P library.
Log | Files | Refs | LICENSE

auth.go (1256B)


      1 package lib9p
      2 
      3 import (
      4 	"fmt"
      5 	"io"
      6 	"io/fs"
      7 )
      8 
      9 // An AuthFile is a file allocated by Auth function of Server struct.
     10 // If authentication is desired, Auth member of Server struct must be set
     11 // and it should set an AuthFile ready to Read/Write via R/W member.
     12 type AuthFile struct {
     13 	Qid    Qid
     14 	Uname  string
     15 	Aname  string
     16 	AuthOK bool
     17 	W      io.Writer
     18 	R      io.Reader
     19 }
     20 
     21 func (af *AuthFile) Stat() (*FileInfo, error) {
     22 	if af.Qid.Type&QTAUTH == 0 {
     23 		panic("QTAUTH bit not set")
     24 	}
     25 	s := Stat{
     26 		Qid:  af.Qid,
     27 		Mode: fs.ModeAppend | fs.ModeExclusive | fs.ModeTemporary | 0666, // TODO: right?
     28 		Name: "auth",
     29 		Uid:  af.Uname,
     30 		Gid:  af.Uname,
     31 		Muid: af.Uname,
     32 	}
     33 	return &FileInfo{Stat: s}, nil
     34 }
     35 
     36 // Close closes af and its underlying reader/writer if they implement io.Closer.
     37 // TODO: should I left W/R not closed?
     38 func (af *AuthFile) Close() error {
     39 	var we, re error
     40 	if w, ok := af.W.(io.Closer); ok {
     41 		we = w.Close()
     42 	}
     43 	if r, ok := af.R.(io.Closer); ok {
     44 		re = r.Close()
     45 	}
     46 	if we != nil || re != nil {
     47 		return fmt.Errorf("close writer: %v, close reader: %v", we, re)
     48 	}
     49 	return nil
     50 }
     51 
     52 func (af *AuthFile) Read(p []byte) (int, error)  { return af.R.Read(p) }
     53 func (af *AuthFile) Write(p []byte) (int, error) { return af.W.Write(p) }