From 573ca55c50e2d3294a2aae3e4cc2b834482c9bf2 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 6 Feb 2023 00:29:33 -0800 Subject: [PATCH] add stat call --- pkg/cirfile/cirfile.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/cirfile/cirfile.go b/pkg/cirfile/cirfile.go index 51c8cbe7..797b09bd 100644 --- a/pkg/cirfile/cirfile.go +++ b/pkg/cirfile/cirfile.go @@ -32,6 +32,14 @@ type File struct { FlockStatus int } +type Stat struct { + Location string + Version byte + MaxSize int64 + FileOffset int64 + DataSize int64 +} + func (f *File) flock(ctx context.Context, lockType int) error { err := syscall.Flock(int(f.OSFile.Fd()), lockType|syscall.LOCK_NB) if err == nil { @@ -98,6 +106,25 @@ func OpenCirFile(fileName string) (*File, error) { return rtn, nil } +func StatCirFile(ctx context.Context, fileName string) (*Stat, error) { + file, err := OpenCirFile(fileName) + if err != nil { + return nil, err + } + defer file.Close() + fileOffset, dataSize, err := file.GetStartOffsetAndSize(ctx) + if err != nil { + return nil, err + } + return &Stat{ + Location: fileName, + Version: file.Version, + MaxSize: file.MaxSize, + FileOffset: fileOffset, + DataSize: dataSize, + }, nil +} + // if the file already exists, it is an error. // there is a race condition if two goroutines try to create the same file between Stat() and Create(), so // they both might get no error, but only one file will be valid. if this is a concern, this call