1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
23 ErrReadOnlyFile = errors.New("read-only file")
24 ErrNegativeOffset = errors.New("cannot seek to negative offset")
25 ErrFileExists = errors.New("file exists")
26 ErrInvalidOperation = errors.New("invalid operation")
27 ErrInvalidArgument = errors.New("invalid argument")
28 ErrDirectoryNotEmpty = errors.New("directory not empty")
29 ErrWriteOnlyMode = errors.New("file is O_WRONLY")
30 ErrSyncNotSupported = errors.New("O_SYNC flag is not supported")
31 ErrIsDirectory = errors.New("cannot rename file to overwrite existing directory")
32 ErrPermission = os.ErrPermission
34 maxBlockSize = 1 << 26
37 // A File is an *os.File-like interface for reading and writing files
38 // in a CollectionFileSystem.
45 Readdir(int) ([]os.FileInfo, error)
46 Stat() (os.FileInfo, error)
50 type keepClient interface {
51 ReadAt(locator string, p []byte, off int) (int, error)
52 PutB(p []byte) (string, int, error)
55 type fileinfo struct {
62 // Name implements os.FileInfo.
63 func (fi fileinfo) Name() string {
67 // ModTime implements os.FileInfo.
68 func (fi fileinfo) ModTime() time.Time {
72 // Mode implements os.FileInfo.
73 func (fi fileinfo) Mode() os.FileMode {
77 // IsDir implements os.FileInfo.
78 func (fi fileinfo) IsDir() bool {
79 return fi.mode&os.ModeDir != 0
82 // Size implements os.FileInfo.
83 func (fi fileinfo) Size() int64 {
87 // Sys implements os.FileInfo.
88 func (fi fileinfo) Sys() interface{} {
92 // A CollectionFileSystem is an http.Filesystem plus Stat() and
93 // support for opening writable files. All methods are safe to call
94 // from multiple goroutines.
95 type CollectionFileSystem interface {
98 // analogous to os.Stat()
99 Stat(name string) (os.FileInfo, error)
101 // analogous to os.Create(): create/truncate a file and open it O_RDWR.
102 Create(name string) (File, error)
104 // Like os.OpenFile(): create or open a file or directory.
106 // If flag&os.O_EXCL==0, it opens an existing file or
107 // directory if one exists. If flag&os.O_CREATE!=0, it creates
108 // a new empty file or directory if one does not already
111 // When creating a new item, perm&os.ModeDir determines
112 // whether it is a file or a directory.
114 // A file can be opened multiple times and used concurrently
115 // from multiple goroutines. However, each File object should
116 // be used by only one goroutine at a time.
117 OpenFile(name string, flag int, perm os.FileMode) (File, error)
119 Mkdir(name string, perm os.FileMode) error
120 Remove(name string) error
121 RemoveAll(name string) error
122 Rename(oldname, newname string) error
124 // Flush all file data to Keep and return a snapshot of the
125 // filesystem suitable for saving as (Collection)ManifestText.
126 // Prefix (normally ".") is a top level directory, effectively
127 // prepended to all paths in the returned manifest.
128 MarshalManifest(prefix string) (string, error)
131 type fileSystem struct {
135 func (fs *fileSystem) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
136 return fs.dirnode.OpenFile(name, flag, perm)
139 func (fs *fileSystem) Open(name string) (http.File, error) {
140 return fs.dirnode.OpenFile(name, os.O_RDONLY, 0)
143 func (fs *fileSystem) Create(name string) (File, error) {
144 return fs.dirnode.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0)
147 func (fs *fileSystem) Stat(name string) (fi os.FileInfo, err error) {
148 node := fs.dirnode.lookupPath(name)
157 type inode interface {
159 Read([]byte, filenodePtr) (int, filenodePtr, error)
160 Write([]byte, filenodePtr) (int, filenodePtr, error)
161 Truncate(int64) error
162 Readdir() []os.FileInfo
170 // filenode implements inode.
171 type filenode struct {
175 // number of times `segments` has changed in a
176 // way that might invalidate a filenodePtr
178 memsize int64 // bytes in memSegments
182 // filenodePtr is an offset into a file that is (usually) efficient to
183 // seek to. Specifically, if filenode.repacked==filenodePtr.repacked
185 // filenode.segments[filenodePtr.segmentIdx][filenodePtr.segmentOff]
186 // corresponds to file offset filenodePtr.off. Otherwise, it is
187 // necessary to reexamine len(filenode.segments[0]) etc. to find the
188 // correct segment and offset.
189 type filenodePtr struct {
196 // seek returns a ptr that is consistent with both startPtr.off and
197 // the current state of fn. The caller must already hold fn.RLock() or
200 // If startPtr is beyond EOF, ptr.segment* will indicate precisely
205 // ptr.segmentIdx == len(filenode.segments) // i.e., at EOF
207 // filenode.segments[ptr.segmentIdx].Len() > ptr.segmentOff
208 func (fn *filenode) seek(startPtr filenodePtr) (ptr filenodePtr) {
211 // meaningless anyway
213 } else if ptr.off >= fn.fileinfo.size {
214 ptr.segmentIdx = len(fn.segments)
216 ptr.repacked = fn.repacked
218 } else if ptr.repacked == fn.repacked {
219 // segmentIdx and segmentOff accurately reflect
220 // ptr.off, but might have fallen off the end of a
222 if ptr.segmentOff >= fn.segments[ptr.segmentIdx].Len() {
229 ptr.repacked = fn.repacked
231 if ptr.off >= fn.fileinfo.size {
232 ptr.segmentIdx, ptr.segmentOff = len(fn.segments), 0
235 // Recompute segmentIdx and segmentOff. We have already
236 // established fn.fileinfo.size > ptr.off >= 0, so we don't
237 // have to deal with edge cases here.
239 for ptr.segmentIdx, ptr.segmentOff = 0, 0; off < ptr.off; ptr.segmentIdx++ {
240 // This would panic (index out of range) if
241 // fn.fileinfo.size were larger than
242 // sum(fn.segments[i].Len()) -- but that can't happen
243 // because we have ensured fn.fileinfo.size is always
245 segLen := int64(fn.segments[ptr.segmentIdx].Len())
246 if off+segLen > ptr.off {
247 ptr.segmentOff = int(ptr.off - off)
255 // caller must have lock
256 func (fn *filenode) appendSegment(e segment) {
257 fn.segments = append(fn.segments, e)
258 fn.fileinfo.size += int64(e.Len())
261 func (fn *filenode) Parent() inode {
267 func (fn *filenode) Readdir() []os.FileInfo {
271 // Read reads file data from a single segment, starting at startPtr,
272 // into p. startPtr is assumed not to be up-to-date. Caller must have
274 func (fn *filenode) Read(p []byte, startPtr filenodePtr) (n int, ptr filenodePtr, err error) {
275 ptr = fn.seek(startPtr)
277 err = ErrNegativeOffset
280 if ptr.segmentIdx >= len(fn.segments) {
284 n, err = fn.segments[ptr.segmentIdx].ReadAt(p, int64(ptr.segmentOff))
288 if ptr.segmentOff == fn.segments[ptr.segmentIdx].Len() {
291 if ptr.segmentIdx < len(fn.segments) && err == io.EOF {
299 func (fn *filenode) Size() int64 {
302 return fn.fileinfo.Size()
305 func (fn *filenode) Stat() os.FileInfo {
311 func (fn *filenode) Truncate(size int64) error {
314 return fn.truncate(size)
317 func (fn *filenode) truncate(size int64) error {
318 if size == fn.fileinfo.size {
322 if size < fn.fileinfo.size {
323 ptr := fn.seek(filenodePtr{off: size})
324 for i := ptr.segmentIdx; i < len(fn.segments); i++ {
325 if seg, ok := fn.segments[i].(*memSegment); ok {
326 fn.memsize -= int64(seg.Len())
329 if ptr.segmentOff == 0 {
330 fn.segments = fn.segments[:ptr.segmentIdx]
332 fn.segments = fn.segments[:ptr.segmentIdx+1]
333 switch seg := fn.segments[ptr.segmentIdx].(type) {
335 seg.Truncate(ptr.segmentOff)
336 fn.memsize += int64(seg.Len())
338 fn.segments[ptr.segmentIdx] = seg.Slice(0, ptr.segmentOff)
341 fn.fileinfo.size = size
344 for size > fn.fileinfo.size {
345 grow := size - fn.fileinfo.size
348 if len(fn.segments) == 0 {
350 fn.segments = append(fn.segments, seg)
351 } else if seg, ok = fn.segments[len(fn.segments)-1].(*memSegment); !ok || seg.Len() >= maxBlockSize {
353 fn.segments = append(fn.segments, seg)
355 if maxgrow := int64(maxBlockSize - seg.Len()); maxgrow < grow {
358 seg.Truncate(seg.Len() + int(grow))
359 fn.fileinfo.size += grow
365 // Write writes data from p to the file, starting at startPtr,
366 // extending the file size if necessary. Caller must have Lock.
367 func (fn *filenode) Write(p []byte, startPtr filenodePtr) (n int, ptr filenodePtr, err error) {
368 if startPtr.off > fn.fileinfo.size {
369 if err = fn.truncate(startPtr.off); err != nil {
370 return 0, startPtr, err
373 ptr = fn.seek(startPtr)
375 err = ErrNegativeOffset
378 for len(p) > 0 && err == nil {
380 if len(cando) > maxBlockSize {
381 cando = cando[:maxBlockSize]
383 // Rearrange/grow fn.segments (and shrink cando if
384 // needed) such that cando can be copied to
385 // fn.segments[ptr.segmentIdx] at offset
387 cur := ptr.segmentIdx
388 prev := ptr.segmentIdx - 1
390 if cur < len(fn.segments) {
391 _, curWritable = fn.segments[cur].(*memSegment)
393 var prevAppendable bool
394 if prev >= 0 && fn.segments[prev].Len() < maxBlockSize {
395 _, prevAppendable = fn.segments[prev].(*memSegment)
397 if ptr.segmentOff > 0 && !curWritable {
398 // Split a non-writable block.
399 if max := fn.segments[cur].Len() - ptr.segmentOff; max <= len(cando) {
400 // Truncate cur, and insert a new
403 fn.segments = append(fn.segments, nil)
404 copy(fn.segments[cur+1:], fn.segments[cur:])
406 // Split cur into two copies, truncate
407 // the one on the left, shift the one
408 // on the right, and insert a new
409 // segment between them.
410 fn.segments = append(fn.segments, nil, nil)
411 copy(fn.segments[cur+2:], fn.segments[cur:])
412 fn.segments[cur+2] = fn.segments[cur+2].Slice(ptr.segmentOff+len(cando), -1)
417 seg.Truncate(len(cando))
418 fn.memsize += int64(len(cando))
419 fn.segments[cur] = seg
420 fn.segments[prev] = fn.segments[prev].Slice(0, ptr.segmentOff)
425 } else if curWritable {
426 if fit := int(fn.segments[cur].Len()) - ptr.segmentOff; fit < len(cando) {
431 // Shrink cando if needed to fit in
433 if cangrow := maxBlockSize - fn.segments[prev].Len(); cangrow < len(cando) {
434 cando = cando[:cangrow]
438 if cur == len(fn.segments) {
439 // ptr is at EOF, filesize is changing.
440 fn.fileinfo.size += int64(len(cando))
441 } else if el := fn.segments[cur].Len(); el <= len(cando) {
442 // cando is long enough that we won't
443 // need cur any more. shrink cando to
444 // be exactly as long as cur
445 // (otherwise we'd accidentally shift
446 // the effective position of all
447 // segments after cur).
449 copy(fn.segments[cur:], fn.segments[cur+1:])
450 fn.segments = fn.segments[:len(fn.segments)-1]
452 // shrink cur by the same #bytes we're growing prev
453 fn.segments[cur] = fn.segments[cur].Slice(len(cando), -1)
459 ptr.segmentOff = fn.segments[prev].Len()
460 fn.segments[prev].(*memSegment).Truncate(ptr.segmentOff + len(cando))
461 fn.memsize += int64(len(cando))
465 // Insert a segment between prev and
466 // cur, and advance prev/cur.
467 fn.segments = append(fn.segments, nil)
468 if cur < len(fn.segments) {
469 copy(fn.segments[cur+1:], fn.segments[cur:])
473 // appending a new segment does
474 // not invalidate any ptrs
477 seg.Truncate(len(cando))
478 fn.memsize += int64(len(cando))
479 fn.segments[cur] = seg
485 // Finally we can copy bytes from cando to the current segment.
486 fn.segments[ptr.segmentIdx].(*memSegment).WriteAt(cando, ptr.segmentOff)
490 ptr.off += int64(len(cando))
491 ptr.segmentOff += len(cando)
492 if ptr.segmentOff >= maxBlockSize {
493 fn.pruneMemSegments()
495 if fn.segments[ptr.segmentIdx].Len() == ptr.segmentOff {
500 fn.fileinfo.modTime = time.Now()
505 // Write some data out to disk to reduce memory use. Caller must have
507 func (fn *filenode) pruneMemSegments() {
508 // TODO: async (don't hold Lock() while waiting for Keep)
509 // TODO: share code with (*dirnode)sync()
510 // TODO: pack/flush small blocks too, when fragmented
511 for idx, seg := range fn.segments {
512 seg, ok := seg.(*memSegment)
513 if !ok || seg.Len() < maxBlockSize {
516 locator, _, err := fn.parent.kc.PutB(seg.buf)
518 // TODO: stall (or return errors from)
519 // subsequent writes until flushing
523 fn.memsize -= int64(seg.Len())
524 fn.segments[idx] = storedSegment{
534 // FileSystem returns a CollectionFileSystem for the collection.
535 func (c *Collection) FileSystem(client *Client, kc keepClient) (CollectionFileSystem, error) {
536 var modTime time.Time
537 if c.ModifiedAt == nil {
540 modTime = *c.ModifiedAt
542 fs := &fileSystem{dirnode: dirnode{
547 mode: os.ModeDir | 0755,
551 inodes: make(map[string]inode),
553 fs.dirnode.parent = &fs.dirnode
554 if err := fs.dirnode.loadManifest(c.ManifestText); err != nil {
560 type filehandle struct {
566 unreaddirs []os.FileInfo
569 func (f *filehandle) Read(p []byte) (n int, err error) {
571 return 0, ErrWriteOnlyMode
574 defer f.inode.RUnlock()
575 n, f.ptr, err = f.inode.Read(p, f.ptr)
579 func (f *filehandle) Seek(off int64, whence int) (pos int64, err error) {
580 size := f.inode.Size()
591 return f.ptr.off, ErrNegativeOffset
593 if ptr.off != f.ptr.off {
595 // force filenode to recompute f.ptr fields on next
599 return f.ptr.off, nil
602 func (f *filehandle) Truncate(size int64) error {
603 return f.inode.Truncate(size)
606 func (f *filehandle) Write(p []byte) (n int, err error) {
608 return 0, ErrReadOnlyFile
611 defer f.inode.Unlock()
612 if fn, ok := f.inode.(*filenode); ok && f.append {
614 off: fn.fileinfo.size,
615 segmentIdx: len(fn.segments),
617 repacked: fn.repacked,
620 n, f.ptr, err = f.inode.Write(p, f.ptr)
624 func (f *filehandle) Readdir(count int) ([]os.FileInfo, error) {
625 if !f.inode.Stat().IsDir() {
626 return nil, ErrInvalidOperation
629 return f.inode.Readdir(), nil
631 if f.unreaddirs == nil {
632 f.unreaddirs = f.inode.Readdir()
634 if len(f.unreaddirs) == 0 {
637 if count > len(f.unreaddirs) {
638 count = len(f.unreaddirs)
640 ret := f.unreaddirs[:count]
641 f.unreaddirs = f.unreaddirs[count:]
645 func (f *filehandle) Stat() (os.FileInfo, error) {
646 return f.inode.Stat(), nil
649 func (f *filehandle) Close() error {
653 type dirnode struct {
658 inodes map[string]inode
662 // sync flushes in-memory data (for all files in the tree rooted at
663 // dn) to persistent storage. Caller must hold dn.Lock().
664 func (dn *dirnode) sync() error {
665 type shortBlock struct {
669 var pending []shortBlock
672 flush := func(sbs []shortBlock) error {
676 block := make([]byte, 0, maxBlockSize)
677 for _, sb := range sbs {
678 block = append(block, sb.fn.segments[sb.idx].(*memSegment).buf...)
680 locator, _, err := dn.kc.PutB(block)
685 for _, sb := range sbs {
686 data := sb.fn.segments[sb.idx].(*memSegment).buf
687 sb.fn.segments[sb.idx] = storedSegment{
695 sb.fn.memsize -= int64(len(data))
700 names := make([]string, 0, len(dn.inodes))
701 for name := range dn.inodes {
702 names = append(names, name)
706 for _, name := range names {
707 fn, ok := dn.inodes[name].(*filenode)
713 for idx, seg := range fn.segments {
714 seg, ok := seg.(*memSegment)
718 if seg.Len() > maxBlockSize/2 {
719 if err := flush([]shortBlock{{fn, idx}}); err != nil {
724 if pendingLen+seg.Len() > maxBlockSize {
725 if err := flush(pending); err != nil {
731 pending = append(pending, shortBlock{fn, idx})
732 pendingLen += seg.Len()
735 return flush(pending)
738 func (dn *dirnode) MarshalManifest(prefix string) (string, error) {
741 return dn.marshalManifest(prefix)
744 // caller must have read lock.
745 func (dn *dirnode) marshalManifest(prefix string) (string, error) {
747 type filepart struct {
752 var fileparts []filepart
756 if err := dn.sync(); err != nil {
760 names := make([]string, 0, len(dn.inodes))
761 for name, node := range dn.inodes {
762 names = append(names, name)
768 for _, name := range names {
769 switch node := dn.inodes[name].(type) {
771 subdir, err := node.marshalManifest(prefix + "/" + name)
775 subdirs = subdirs + subdir
777 if len(node.segments) == 0 {
778 fileparts = append(fileparts, filepart{name: name})
781 for _, seg := range node.segments {
782 switch seg := seg.(type) {
784 if len(blocks) > 0 && blocks[len(blocks)-1] == seg.locator {
785 streamLen -= int64(seg.size)
787 blocks = append(blocks, seg.locator)
791 offset: streamLen + int64(seg.offset),
792 length: int64(seg.length),
794 if prev := len(fileparts) - 1; prev >= 0 &&
795 fileparts[prev].name == name &&
796 fileparts[prev].offset+fileparts[prev].length == next.offset {
797 fileparts[prev].length += next.length
799 fileparts = append(fileparts, next)
801 streamLen += int64(seg.size)
803 // This can't happen: we
804 // haven't unlocked since
806 panic(fmt.Sprintf("can't marshal segment type %T", seg))
810 panic(fmt.Sprintf("can't marshal inode type %T", node))
813 var filetokens []string
814 for _, s := range fileparts {
815 filetokens = append(filetokens, fmt.Sprintf("%d:%d:%s", s.offset, s.length, manifestEscape(s.name)))
817 if len(filetokens) == 0 {
819 } else if len(blocks) == 0 {
820 blocks = []string{"d41d8cd98f00b204e9800998ecf8427e+0"}
822 return manifestEscape(prefix) + " " + strings.Join(blocks, " ") + " " + strings.Join(filetokens, " ") + "\n" + subdirs, nil
825 func (dn *dirnode) loadManifest(txt string) error {
827 streams := strings.Split(txt, "\n")
828 if streams[len(streams)-1] != "" {
829 return fmt.Errorf("line %d: no trailing newline", len(streams))
831 streams = streams[:len(streams)-1]
832 segments := []storedSegment{}
833 for i, stream := range streams {
835 var anyFileTokens bool
838 segments = segments[:0]
839 for i, token := range strings.Split(stream, " ") {
841 dirname = manifestUnescape(token)
844 if !strings.Contains(token, ":") {
846 return fmt.Errorf("line %d: bad file segment %q", lineno, token)
848 toks := strings.SplitN(token, "+", 3)
850 return fmt.Errorf("line %d: bad locator %q", lineno, token)
852 length, err := strconv.ParseInt(toks[1], 10, 32)
853 if err != nil || length < 0 {
854 return fmt.Errorf("line %d: bad locator %q", lineno, token)
856 segments = append(segments, storedSegment{
863 } else if len(segments) == 0 {
864 return fmt.Errorf("line %d: bad locator %q", lineno, token)
867 toks := strings.SplitN(token, ":", 3)
869 return fmt.Errorf("line %d: bad file segment %q", lineno, token)
873 offset, err := strconv.ParseInt(toks[0], 10, 64)
874 if err != nil || offset < 0 {
875 return fmt.Errorf("line %d: bad file segment %q", lineno, token)
877 length, err := strconv.ParseInt(toks[1], 10, 64)
878 if err != nil || length < 0 {
879 return fmt.Errorf("line %d: bad file segment %q", lineno, token)
881 name := dirname + "/" + manifestUnescape(toks[2])
882 fnode, err := dn.createFileAndParents(name)
884 return fmt.Errorf("line %d: cannot use path %q: %s", lineno, name, err)
886 // Map the stream offset/range coordinates to
887 // block/offset/range coordinates and add
888 // corresponding storedSegments to the filenode
890 // Can't continue where we left off.
891 // TODO: binary search instead of
892 // rewinding all the way (but this
893 // situation might be rare anyway)
896 for next := int64(0); segIdx < len(segments); segIdx++ {
897 seg := segments[segIdx]
898 next = pos + int64(seg.Len())
899 if next <= offset || seg.Len() == 0 {
903 if pos >= offset+length {
908 blkOff = int(offset - pos)
910 blkLen := seg.Len() - blkOff
911 if pos+int64(blkOff+blkLen) > offset+length {
912 blkLen = int(offset + length - pos - int64(blkOff))
914 fnode.appendSegment(storedSegment{
916 locator: seg.locator,
921 if next > offset+length {
927 if segIdx == len(segments) && pos < offset+length {
928 return fmt.Errorf("line %d: invalid segment in %d-byte stream: %q", lineno, pos, token)
932 return fmt.Errorf("line %d: no file segments", lineno)
933 } else if len(segments) == 0 {
934 return fmt.Errorf("line %d: no locators", lineno)
935 } else if dirname == "" {
936 return fmt.Errorf("line %d: no stream name", lineno)
942 // only safe to call from loadManifest -- no locking
943 func (dn *dirnode) createFileAndParents(path string) (fn *filenode, err error) {
944 names := strings.Split(path, "/")
945 basename := names[len(names)-1]
946 if basename == "" || basename == "." || basename == ".." {
947 err = fmt.Errorf("invalid filename")
950 for _, name := range names[:len(names)-1] {
956 switch node := dn.inodes[name].(type) {
958 dn = dn.newDirnode(name, 0755, dn.fileinfo.modTime)
967 switch node := dn.inodes[basename].(type) {
969 fn = dn.newFilenode(basename, 0755, dn.fileinfo.modTime)
978 func (dn *dirnode) mkdir(name string) (*filehandle, error) {
979 return dn.OpenFile(name, os.O_CREATE|os.O_EXCL, os.ModeDir|0755)
982 func (dn *dirnode) Mkdir(name string, perm os.FileMode) error {
983 f, err := dn.mkdir(name)
990 func (dn *dirnode) Remove(name string) error {
991 return dn.remove(strings.TrimRight(name, "/"), false)
994 func (dn *dirnode) RemoveAll(name string) error {
995 err := dn.remove(strings.TrimRight(name, "/"), true)
996 if os.IsNotExist(err) {
997 // "If the path does not exist, RemoveAll returns
998 // nil." (see "os" pkg)
1004 func (dn *dirnode) remove(name string, recursive bool) error {
1005 dirname, name := path.Split(name)
1006 if name == "" || name == "." || name == ".." {
1007 return ErrInvalidArgument
1009 dn, ok := dn.lookupPath(dirname).(*dirnode)
1011 return os.ErrNotExist
1015 switch node := dn.inodes[name].(type) {
1017 return os.ErrNotExist
1020 defer node.RUnlock()
1021 if !recursive && len(node.inodes) > 0 {
1022 return ErrDirectoryNotEmpty
1025 delete(dn.inodes, name)
1029 func (dn *dirnode) Rename(oldname, newname string) error {
1030 olddir, oldname := path.Split(oldname)
1031 if oldname == "" || oldname == "." || oldname == ".." {
1032 return ErrInvalidArgument
1034 olddirf, err := dn.OpenFile(olddir+".", os.O_RDONLY, 0)
1036 return fmt.Errorf("%q: %s", olddir, err)
1038 defer olddirf.Close()
1039 newdir, newname := path.Split(newname)
1040 if newname == "." || newname == ".." {
1041 return ErrInvalidArgument
1042 } else if newname == "" {
1043 // Rename("a/b", "c/") means Rename("a/b", "c/b")
1046 newdirf, err := dn.OpenFile(newdir+".", os.O_RDONLY, 0)
1048 return fmt.Errorf("%q: %s", newdir, err)
1050 defer newdirf.Close()
1052 // When acquiring locks on multiple nodes, all common
1053 // ancestors must be locked first in order to avoid
1054 // deadlock. This is assured by locking the path from root to
1055 // newdir, then locking the path from root to olddir, skipping
1056 // any already-locked nodes.
1057 needLock := []sync.Locker{}
1058 for _, f := range []*filehandle{olddirf, newdirf} {
1060 needLock = append(needLock, node)
1061 for node.Parent() != node {
1062 node = node.Parent()
1063 needLock = append(needLock, node)
1066 locked := map[sync.Locker]bool{}
1067 for i := len(needLock) - 1; i >= 0; i-- {
1068 if n := needLock[i]; !locked[n] {
1075 olddn := olddirf.inode.(*dirnode)
1076 newdn := newdirf.inode.(*dirnode)
1077 oldinode, ok := olddn.inodes[oldname]
1079 return os.ErrNotExist
1081 if locked[oldinode] {
1082 // oldinode cannot become a descendant of itself.
1083 return ErrInvalidArgument
1085 if existing, ok := newdn.inodes[newname]; ok {
1086 // overwriting an existing file or dir
1087 if dn, ok := existing.(*dirnode); ok {
1088 if !oldinode.Stat().IsDir() {
1089 return ErrIsDirectory
1093 if len(dn.inodes) > 0 {
1094 return ErrDirectoryNotEmpty
1098 if newdn.inodes == nil {
1099 newdn.inodes = make(map[string]inode)
1101 newdn.fileinfo.size++
1103 newdn.inodes[newname] = oldinode
1104 switch n := oldinode.(type) {
1110 panic(fmt.Sprintf("bad inode type %T", n))
1112 delete(olddn.inodes, oldname)
1113 olddn.fileinfo.size--
1117 func (dn *dirnode) Parent() inode {
1123 func (dn *dirnode) Readdir() (fi []os.FileInfo) {
1126 fi = make([]os.FileInfo, 0, len(dn.inodes))
1127 for _, inode := range dn.inodes {
1128 fi = append(fi, inode.Stat())
1133 func (dn *dirnode) Read(p []byte, ptr filenodePtr) (int, filenodePtr, error) {
1134 return 0, ptr, ErrInvalidOperation
1137 func (dn *dirnode) Write(p []byte, ptr filenodePtr) (int, filenodePtr, error) {
1138 return 0, ptr, ErrInvalidOperation
1141 func (dn *dirnode) Size() int64 {
1144 return dn.fileinfo.Size()
1147 func (dn *dirnode) Stat() os.FileInfo {
1153 func (dn *dirnode) Truncate(int64) error {
1154 return ErrInvalidOperation
1157 // lookupPath returns the inode for the file/directory with the given
1158 // name (which may contain "/" separators), along with its parent
1159 // node. If no such file/directory exists, the returned node is nil.
1160 func (dn *dirnode) lookupPath(path string) (node inode) {
1162 for _, name := range strings.Split(path, "/") {
1163 dn, ok := node.(*dirnode)
1167 if name == "." || name == "" {
1171 node = node.Parent()
1175 node = dn.inodes[name]
1181 func (dn *dirnode) newDirnode(name string, perm os.FileMode, modTime time.Time) *dirnode {
1188 mode: os.ModeDir | perm,
1192 if dn.inodes == nil {
1193 dn.inodes = make(map[string]inode)
1195 dn.inodes[name] = child
1200 func (dn *dirnode) newFilenode(name string, perm os.FileMode, modTime time.Time) *filenode {
1209 if dn.inodes == nil {
1210 dn.inodes = make(map[string]inode)
1212 dn.inodes[name] = child
1217 // OpenFile is analogous to os.OpenFile().
1218 func (dn *dirnode) OpenFile(name string, flag int, perm os.FileMode) (*filehandle, error) {
1219 if flag&os.O_SYNC != 0 {
1220 return nil, ErrSyncNotSupported
1222 dirname, name := path.Split(name)
1223 dn, ok := dn.lookupPath(dirname).(*dirnode)
1225 return nil, os.ErrNotExist
1227 var readable, writable bool
1228 switch flag & (os.O_RDWR | os.O_RDONLY | os.O_WRONLY) {
1237 return nil, fmt.Errorf("invalid flags 0x%x", flag)
1240 // A directory can be opened via "foo/", "foo/.", or
1244 return &filehandle{inode: dn}, nil
1246 return &filehandle{inode: dn.Parent()}, nil
1249 createMode := flag&os.O_CREATE != 0
1257 n, ok := dn.inodes[name]
1260 return nil, os.ErrNotExist
1263 n = dn.newDirnode(name, 0755, time.Now())
1265 n = dn.newFilenode(name, 0755, time.Now())
1267 } else if flag&os.O_EXCL != 0 {
1268 return nil, ErrFileExists
1269 } else if flag&os.O_TRUNC != 0 {
1271 return nil, fmt.Errorf("invalid flag O_TRUNC in read-only mode")
1272 } else if fn, ok := n.(*filenode); !ok {
1273 return nil, fmt.Errorf("invalid flag O_TRUNC when opening directory")
1280 append: flag&os.O_APPEND != 0,
1286 type segment interface {
1289 // Return a new segment with a subsection of the data from this
1290 // one. length<0 means length=Len()-off.
1291 Slice(off int, length int) segment
1294 type memSegment struct {
1298 func (me *memSegment) Len() int {
1302 func (me *memSegment) Slice(off, length int) segment {
1304 length = len(me.buf) - off
1306 buf := make([]byte, length)
1307 copy(buf, me.buf[off:])
1308 return &memSegment{buf: buf}
1311 func (me *memSegment) Truncate(n int) {
1312 if n > cap(me.buf) {
1315 newsize = newsize << 2
1317 newbuf := make([]byte, n, newsize)
1318 copy(newbuf, me.buf)
1321 // Zero unused part when shrinking, in case we grow
1322 // and start using it again later.
1323 for i := n; i < len(me.buf); i++ {
1330 func (me *memSegment) WriteAt(p []byte, off int) {
1331 if off+len(p) > len(me.buf) {
1332 panic("overflowed segment")
1334 copy(me.buf[off:], p)
1337 func (me *memSegment) ReadAt(p []byte, off int64) (n int, err error) {
1338 if off > int64(me.Len()) {
1342 n = copy(p, me.buf[int(off):])
1349 type storedSegment struct {
1352 size int // size of stored block (also encoded in locator)
1353 offset int // position of segment within the stored block
1354 length int // bytes in this segment (offset + length <= size)
1357 func (se storedSegment) Len() int {
1361 func (se storedSegment) Slice(n, size int) segment {
1364 if size >= 0 && se.length > size {
1370 func (se storedSegment) ReadAt(p []byte, off int64) (n int, err error) {
1371 if off > int64(se.length) {
1374 maxlen := se.length - int(off)
1375 if len(p) > maxlen {
1377 n, err = se.kc.ReadAt(se.locator, p, int(off)+se.offset)
1383 return se.kc.ReadAt(se.locator, p, int(off)+se.offset)
1386 func canonicalName(name string) string {
1387 name = path.Clean("/" + name)
1388 if name == "/" || name == "./" {
1390 } else if strings.HasPrefix(name, "/") {
1396 var manifestEscapeSeq = regexp.MustCompile(`\\([0-7]{3}|\\)`)
1398 func manifestUnescapeFunc(seq string) string {
1402 i, err := strconv.ParseUint(seq[1:], 8, 8)
1404 // Invalid escape sequence: can't unescape.
1407 return string([]byte{byte(i)})
1410 func manifestUnescape(s string) string {
1411 return manifestEscapeSeq.ReplaceAllStringFunc(s, manifestUnescapeFunc)
1414 var manifestEscapedChar = regexp.MustCompile(`[\000-\040:\s\\]`)
1416 func manifestEscapeFunc(seq string) string {
1417 return fmt.Sprintf("\\%03o", byte(seq[0]))
1420 func manifestEscape(s string) string {
1421 return manifestEscapedChar.ReplaceAllStringFunc(s, manifestEscapeFunc)