mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
add stat call
This commit is contained in:
@@ -32,6 +32,14 @@ type File struct {
|
|||||||
FlockStatus int
|
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 {
|
func (f *File) flock(ctx context.Context, lockType int) error {
|
||||||
err := syscall.Flock(int(f.OSFile.Fd()), lockType|syscall.LOCK_NB)
|
err := syscall.Flock(int(f.OSFile.Fd()), lockType|syscall.LOCK_NB)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -98,6 +106,25 @@ func OpenCirFile(fileName string) (*File, error) {
|
|||||||
return rtn, nil
|
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.
|
// 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
|
// 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
|
// they both might get no error, but only one file will be valid. if this is a concern, this call
|
||||||
|
|||||||
Reference in New Issue
Block a user