Skip to content

Commit

Permalink
osusers.go: introduce Simulate() for glenda
Browse files Browse the repository at this point in the history
This is just an quick and dirty hack, a better implementation exists.
  • Loading branch information
Shamar committed Dec 23, 2015
1 parent fcdc311 commit a1de5f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 18 additions & 0 deletions osusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ninep

import (
"errors"
"os/user"
"strconv"
"sync"
Expand All @@ -19,6 +20,7 @@ type osUser struct {
}

type osUsers struct {
simulating *osUser
groups map[int]*osGroup
sync.Mutex
}
Expand Down Expand Up @@ -48,6 +50,7 @@ func (g *osGroup) Members() []User { return nil }
func initOsusers() {
OsUsers = new(osUsers)
OsUsers.groups = make(map[int]*osGroup)
OsUsers.simulating = nil
}

func newUser(u *user.User) *osUser {
Expand All @@ -60,6 +63,17 @@ func newUser(u *user.User) *osUser {
return &osUser{u, uid, gid}
}

func (up *osUsers) Simulate(u *user.User) error {
once.Do(initOsusers)
_, err := user.Lookup(u.Username)
if err != nil {
OsUsers.simulating = newUser(u)
return nil
}
return errors.New("cannot simulate an existing user.")
}


func (up *osUsers) Uid2User(uid int) User {
u, err := user.LookupId(strconv.Itoa(uid))
if err != nil {
Expand All @@ -69,6 +83,10 @@ func (up *osUsers) Uid2User(uid int) User {
}

func (up *osUsers) Uname2User(uname string) User {
once.Do(initOsusers)
if OsUsers.simulating != nil && uname == OsUsers.simulating.Username {
return OsUsers.simulating
}
u, err := user.Lookup(uname)
if err != nil {
return nil
Expand Down
15 changes: 9 additions & 6 deletions srv/examples/ufs/ufs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ package main
import (
"flag"
"log"
"os/user"

"github.com/lionkov/ninep"
"github.com/lionkov/ninep/srv/ufs"
)

var (
debug = flag.Int("d", 0, "print debug messages")
addr = flag.String("addr", ":5640", "network address")
user = flag.String("user", "", "user name")
)

func main() {
Expand All @@ -24,12 +25,14 @@ func main() {
ufs.Id = "ufs"
ufs.Debuglevel = *debug
ufs.Start(ufs)
if *user != "" {
u := ufs.Upool.Uname2User(*user)
if u == nil {
log.Printf("Warning: Adding %v failed", *user)
}

u, uerr := user.Current()
if uerr != nil {
log.Fatalln(uerr)
}
u.Username = "glenda"
u.HomeDir = "/usr/glenda"
ninep.OsUsers.Simulate(u)

err := ufs.StartNetListener("tcp", *addr)
if err != nil {
Expand Down

0 comments on commit a1de5f9

Please sign in to comment.