// restPath returns the path to the given resource. func restPath(url *url.URL, h backend.Handle) string { u := *url var dir string switch h.Type { case backend.Config: dir = "" h.Name = "config" case backend.Data: dir = backend.Paths.Data case backend.Snapshot: dir = backend.Paths.Snapshots case backend.Index: dir = backend.Paths.Index case backend.Lock: dir = backend.Paths.Locks case backend.Key: dir = backend.Paths.Keys default: dir = string(h.Type) } u.Path = path.Join(url.Path, dir, h.Name) return u.String() }
func memLoad(be *MemoryBackend, h backend.Handle, p []byte, off int64) (int, error) { if err := h.Valid(); err != nil { return 0, err } be.m.Lock() defer be.m.Unlock() if h.Type == backend.Config { h.Name = "" } debug.Log("MemoryBackend.Load", "get %v offset %v len %v", h, off, len(p)) if _, ok := be.data[entry{h.Type, h.Name}]; !ok { return 0, errors.New("no such data") } buf := be.data[entry{h.Type, h.Name}] if off > int64(len(buf)) { return 0, errors.New("offset beyond end of file") } buf = buf[off:] n := copy(p, buf) if len(p) > len(buf) { return n, io.ErrUnexpectedEOF } return n, nil }
func memStat(be *MemoryBackend, h backend.Handle) (backend.BlobInfo, error) { be.m.Lock() defer be.m.Unlock() if err := h.Valid(); err != nil { return backend.BlobInfo{}, err } if h.Type == backend.Config { h.Name = "" } debug.Log("MemoryBackend.Stat", "stat %v", h) e, ok := be.data[entry{h.Type, h.Name}] if !ok { return backend.BlobInfo{}, errors.New("no such data") } return backend.BlobInfo{Size: int64(len(e))}, nil }
func memSave(be *MemoryBackend, h backend.Handle, p []byte) error { if err := h.Valid(); err != nil { return err } be.m.Lock() defer be.m.Unlock() if h.Type == backend.Config { h.Name = "" } if _, ok := be.data[entry{h.Type, h.Name}]; ok { return errors.New("file already exists") } debug.Log("MemoryBackend.Save", "save %v bytes at %v", len(p), h) buf := make([]byte, len(p)) copy(buf, p) be.data[entry{h.Type, h.Name}] = buf return nil }