(fp *os.File, options *BlockFileOptions)
| 43 | } |
| 44 | |
| 45 | func NewBlocksFileWithRawFile(fp *os.File, options *BlockFileOptions) (*BlocksFile, error) { |
| 46 | options.EnsureDefaults() |
| 47 | |
| 48 | var bFilename = fp.Name() |
| 49 | if !strings.HasSuffix(bFilename, BFileExt) { |
| 50 | return nil, errors.New("filename '" + bFilename + "' must has a '" + BFileExt + "' extension") |
| 51 | } |
| 52 | |
| 53 | var mu = &sync.RWMutex{} |
| 54 | |
| 55 | var mFilename = strings.TrimSuffix(bFilename, BFileExt) + MFileExt |
| 56 | mFile, err := OpenMetaFile(mFilename, mu) |
| 57 | if err != nil { |
| 58 | _ = fp.Close() |
| 59 | return nil, fmt.Errorf("load '%s' failed: %w", mFilename, err) |
| 60 | } |
| 61 | |
| 62 | AckReadThread() |
| 63 | _, err = fp.Seek(0, io.SeekEnd) |
| 64 | ReleaseReadThread() |
| 65 | if err != nil { |
| 66 | _ = fp.Close() |
| 67 | _ = mFile.Close() |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | return &BlocksFile{ |
| 72 | fp: fp, |
| 73 | mFile: mFile, |
| 74 | mu: mu, |
| 75 | opt: options, |
| 76 | syncAt: time.Now(), |
| 77 | readerPool: make(chan *FileReader, 32), |
| 78 | writingFileMap: map[string]zero.Zero{}, |
| 79 | }, nil |
| 80 | } |
| 81 | |
| 82 | func OpenBlocksFile(filename string, options *BlockFileOptions) (*BlocksFile, error) { |
| 83 | // TODO 考虑是否使用flock锁定,防止多进程写冲突 |
no test coverage detected