| 80 | } |
| 81 | |
| 82 | func OpenBlocksFile(filename string, options *BlockFileOptions) (*BlocksFile, error) { |
| 83 | // TODO 考虑是否使用flock锁定,防止多进程写冲突 |
| 84 | fp, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666) |
| 85 | if err != nil { |
| 86 | if os.IsNotExist(err) { |
| 87 | var dir = filepath.Dir(filename) |
| 88 | _ = os.MkdirAll(dir, 0777) |
| 89 | |
| 90 | // try again |
| 91 | fp, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666) |
| 92 | } |
| 93 | |
| 94 | if err != nil { |
| 95 | return nil, fmt.Errorf("open blocks file failed: %w", err) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return NewBlocksFileWithRawFile(fp, options) |
| 100 | } |
| 101 | |
| 102 | func (this *BlocksFile) Filename() string { |
| 103 | return this.fp.Name() |