From db0b67e8a0892807e8791eef55990d0c850f8480 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Fri, 17 Jun 2022 08:32:55 +0800 Subject: [PATCH] Add FileReader and FileBufReader utilities The FileReader interface is the wrapper of io.Reader. It provides the fs.FileInfo as well. The FileBufReader struct is the wrapper of bufio.Reader, it also provides fs.FileInfo. Signed-off-by: Benjamin Wang --- client/pkg/fileutil/filereader.go | 60 ++++++++++++++++++++++++++ client/pkg/fileutil/filereader_test.go | 44 +++++++++++++++++++ client/pkg/go.mod | 1 + client/pkg/go.sum | 1 + 4 files changed, 106 insertions(+) create mode 100644 client/pkg/fileutil/filereader.go create mode 100644 client/pkg/fileutil/filereader_test.go diff --git a/client/pkg/fileutil/filereader.go b/client/pkg/fileutil/filereader.go new file mode 100644 index 000000000..55248888c --- /dev/null +++ b/client/pkg/fileutil/filereader.go @@ -0,0 +1,60 @@ +// Copyright 2022 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fileutil + +import ( + "bufio" + "io" + "io/fs" + "os" +) + +// FileReader is a wrapper of io.Reader. It also provides file info. +type FileReader interface { + io.Reader + FileInfo() (fs.FileInfo, error) +} + +type fileReader struct { + *os.File +} + +func NewFileReader(f *os.File) FileReader { + return &fileReader{f} +} + +func (fr *fileReader) FileInfo() (fs.FileInfo, error) { + return fr.Stat() +} + +// FileBufReader is a wrapper of bufio.Reader. It also provides file info. +type FileBufReader struct { + *bufio.Reader + fi fs.FileInfo +} + +func NewFileBufReader(fr FileReader) *FileBufReader { + bufReader := bufio.NewReader(fr) + fi, err := fr.FileInfo() + if err != nil { + // This should never happen. + panic(err) + } + return &FileBufReader{bufReader, fi} +} + +func (fbr *FileBufReader) FileInfo() fs.FileInfo { + return fbr.fi +} diff --git a/client/pkg/fileutil/filereader_test.go b/client/pkg/fileutil/filereader_test.go new file mode 100644 index 000000000..2f863cdce --- /dev/null +++ b/client/pkg/fileutil/filereader_test.go @@ -0,0 +1,44 @@ +// Copyright 2022 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fileutil + +import ( + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFileBufReader(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "wal") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + fi, err := f.Stat() + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + fbr := NewFileBufReader(NewFileReader(f)) + + if !strings.HasPrefix(fbr.FileInfo().Name(), "wal") { + t.Errorf("Unexpected file name: %s", fbr.FileInfo().Name()) + } + assert.Equal(t, fi.Size(), fbr.FileInfo().Size()) + assert.Equal(t, fi.IsDir(), fbr.FileInfo().IsDir()) + assert.Equal(t, fi.Mode(), fbr.FileInfo().Mode()) + assert.Equal(t, fi.ModTime(), fbr.FileInfo().ModTime()) +} diff --git a/client/pkg/go.mod b/client/pkg/go.mod index 425d437e0..f30aef331 100644 --- a/client/pkg/go.mod +++ b/client/pkg/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/coreos/go-systemd/v22 v22.3.2 + github.com/stretchr/testify v1.7.0 go.uber.org/zap v1.17.0 golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 ) diff --git a/client/pkg/go.sum b/client/pkg/go.sum index a271115ad..8bc5e2f04 100644 --- a/client/pkg/go.sum +++ b/client/pkg/go.sum @@ -20,6 +20,7 @@ go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=