Esempio n. 1
0
func (k *Clone) Read(fid *srv.FFid, buf []byte, offset uint64) (int, error) {
	var err error

	// we only allow a single read from us, change the offset and we're done
	if offset > uint64(0) {
		return 0, nil
	}

	user := p.OsUsers.Uid2User(os.Geteuid())

	k.clones++
	name := strconv.Itoa(k.clones)
	f := new(srv.File)
	err = f.Add(root, name, user, nil, p.DMDIR|0777, f)
	if err != nil {
		return 0, &p.Error{"can not create dir", 0}
	}

	ctl := new(CtlFile)
	ctl.datafile = make([]byte, 0)
	err = ctl.Add(f, "ctl", user, nil, 0777, ctl)
	if err != nil {
		return 0, &p.Error{"cannot create ctl file", 0}
	}

	b := []byte(name)
	if len(buf) < len(b) {
		f.Remove()
		return 0, &p.Error{"not enough buffer space for result", 0}
	}

	copy(buf, b)
	return len(b), nil
}
Esempio n. 2
0
func (cl *Clone) Read(fid *srv.FFid, buf []byte, offset uint64) (int, error) {
	var err error

	var dir *srv.File
	var ctl *ClientCtl
	var body *ClientBody
	var parsed *srv.File

	var outb []byte

	// we only allow a single read from us, change the offset and we're done
	if offset > uint64(0) {
		return 0, nil
	}

	id := fmt.Sprintf("%d", cl.clones)

	client := new(HttpClient)
	client.id = cl.clones
	client.req = &http.Request{
		Method:     "GET",
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header:     make(http.Header),
	}

	// web/0
	dir = new(srv.File)
	if p9err := dir.Add(root, id, user, user, p.DMDIR|0555, nil); p9err != nil {
		err = fmt.Errorf("clone: can't add client dir: %s", p9err)
		goto remove
	}
	client.dir = dir

	// web/0/ctl
	ctl = new(ClientCtl)
	if p9err := ctl.Add(dir, "ctl", user, user, 0666, ctl); p9err != nil {
		err = fmt.Errorf("clone: can't make ctl: %s", p9err)
		goto remove
	}
	ctl.c = client

	// web/0/body
	body = new(ClientBody)
	if p9err := body.Add(dir, "body", user, user, 0444, body); p9err != nil {
		err = fmt.Errorf("clone: can't make body: %s", p9err)
		goto remove
	}
	body.c = client

	// web/0/parsed/
	parsed = new(srv.File)
	if p9err := parsed.Add(dir, "parsed", user, user, p.DMDIR|0555, nil); p9err != nil {
		err = fmt.Errorf("clone: can't add parsed dir: %s", p9err)
		goto remove
	}

	for ptype, fname := range parsedtab {
		pfile := &ParsedFile{ptype: ptype, c: client}
		if p9err := pfile.Add(parsed, fname, user, user, 0444, pfile); p9err != nil {
			err = fmt.Errorf("clone: can't add parsed file %s: %s", p9err)
			goto remove
		}
	}

	outb = []byte(id + "\n")
	if len(buf) < len(outb) {
		err = fmt.Errorf("not enough buffer space for result")
		goto remove
	}

	fid.F = &ctl.File
	cl.clones++

	copy(buf, outb)
	return len(outb), nil

remove:
	if dir != nil {
		dir.Remove()
	}
	return 0, err
}