()
| 49 | } |
| 50 | |
| 51 | func (this *MetaFile) load() error { |
| 52 | AckReadThread() |
| 53 | _, err := this.fp.Seek(0, io.SeekStart) |
| 54 | ReleaseReadThread() |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | // TODO 检查文件是否完整 |
| 60 | |
| 61 | var buf = make([]byte, 4<<10) |
| 62 | var blockBytes []byte |
| 63 | for { |
| 64 | AckReadThread() |
| 65 | n, readErr := this.fp.Read(buf) |
| 66 | ReleaseReadThread() |
| 67 | if n > 0 { |
| 68 | blockBytes = append(blockBytes, buf[:n]...) |
| 69 | for len(blockBytes) > 4 { |
| 70 | var l = int(binary.BigEndian.Uint32(blockBytes[:4])) + 4 /* Len **/ |
| 71 | if len(blockBytes) < l { |
| 72 | break |
| 73 | } |
| 74 | |
| 75 | action, hash, data, decodeErr := DecodeMetaBlock(blockBytes[:l]) |
| 76 | if decodeErr != nil { |
| 77 | return decodeErr |
| 78 | } |
| 79 | |
| 80 | switch action { |
| 81 | case MetaActionNew: |
| 82 | this.headerMap[hash] = NewLazyFileHeaderFromData(data) |
| 83 | case MetaActionRemove: |
| 84 | delete(this.headerMap, hash) |
| 85 | } |
| 86 | |
| 87 | blockBytes = blockBytes[l:] |
| 88 | } |
| 89 | } |
| 90 | if readErr != nil { |
| 91 | if readErr == io.EOF { |
| 92 | break |
| 93 | } |
| 94 | return readErr |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | func (this *MetaFile) WriteMeta(hash string, status int, expiresAt int64, expectedFileSize int64) error { |
| 102 |
no test coverage detected