代码拉取完成,页面将自动刷新
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fsblkstorage
import (
"os"
"github.com/pkg/errors"
)
//// WRITER ////
type blockfileWriter struct {
filePath string
file *os.File
}
func newBlockfileWriter(filePath string) (*blockfileWriter, error) {
writer := &blockfileWriter{filePath: filePath}
return writer, writer.open()
}
func (w *blockfileWriter) truncateFile(targetSize int) error {
fileStat, err := w.file.Stat()
if err != nil {
return err
}
if fileStat.Size() > int64(targetSize) {
w.file.Truncate(int64(targetSize))
}
return nil
}
func (w *blockfileWriter) append(b []byte, sync bool) error {
_, err := w.file.Write(b)
if err != nil {
return err
}
if sync {
return w.file.Sync()
}
return nil
}
func (w *blockfileWriter) open() error {
file, err := os.OpenFile(w.filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return errors.Wrapf(err, "error opening block file writer for file %s", w.filePath)
}
w.file = file
return nil
}
func (w *blockfileWriter) close() error {
return errors.WithStack(w.file.Close())
}
//// READER ////
type blockfileReader struct {
file *os.File
}
func newBlockfileReader(filePath string) (*blockfileReader, error) {
file, err := os.OpenFile(filePath, os.O_RDONLY, 0600)
if err != nil {
return nil, errors.Wrapf(err, "error opening block file reader for file %s", filePath)
}
reader := &blockfileReader{file}
return reader, nil
}
func (r *blockfileReader) read(offset int, length int) ([]byte, error) {
b := make([]byte, length)
_, err := r.file.ReadAt(b, int64(offset))
if err != nil {
return nil, errors.Wrapf(err, "error reading block file for offset %d and length %d", offset, length)
}
return b, nil
}
func (r *blockfileReader) close() error {
return errors.WithStack(r.file.Close())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。