From 2f23dd3348032973e713da247b873b19ae1154d6 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 13 Dec 2021 09:56:34 -0800 Subject: [PATCH] Restrict to one open file of each type at a time --- blefs/error.go | 2 ++ blefs/file.go | 17 +++++++++++++++-- blefs/fs.go | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/blefs/error.go b/blefs/error.go index 27e03b4..8441929 100644 --- a/blefs/error.go +++ b/blefs/error.go @@ -11,6 +11,8 @@ var ( ErrFileWriteOnly = errors.New("file is write only") ErrInvalidOffset = errors.New("invalid file offset") ErrOffsetChanged = errors.New("offset has already been changed") + ErrReadOpen = errors.New("only one file can be opened for reading at a time") + ErrWriteOpen = errors.New("only one file can be opened for writing at a time") ) // FSError represents an error returned by BLE FS diff --git a/blefs/file.go b/blefs/file.go index b1938d0..4a4ee4e 100644 --- a/blefs/file.go +++ b/blefs/file.go @@ -24,6 +24,10 @@ type File struct { // Open opens a file and returns it as an fs.File to // satisfy the fs.FS interface func (blefs *FS) Open(path string) (*File, error) { + if blefs.readOpen { + return nil, ErrReadOpen + } + blefs.readOpen = true // Make a read file request. This opens the file for reading. err := blefs.request( FSCmdReadFile, @@ -49,6 +53,10 @@ func (blefs *FS) Open(path string) (*File, error) { // Create makes a new file on the BLE file system and returns it. func (blefs *FS) Create(path string, size uint32) (*File, error) { + if blefs.writeOpen { + return nil, ErrWriteOpen + } + blefs.writeOpen = true // Make a write file request. This will create and open a file for writing. err := blefs.request( FSCmdWriteFile, @@ -316,9 +324,14 @@ func (fl *File) Seek(offset int64, whence int) (int64, error) { return int64(newOffset), nil } -// Close implements the fs.File interface. -// It just returns nil. +// Close must be called before opening another file func (fl *File) Close() error { + if fl.isReadOnly { + fl.fs.readOpen = false + } else if fl.isWriteOnly { + fl.fs.writeOpen = false + } + return nil } diff --git a/blefs/fs.go b/blefs/fs.go index 8e20de9..6d9d06f 100644 --- a/blefs/fs.go +++ b/blefs/fs.go @@ -50,6 +50,8 @@ var btOptsCmd = map[string]interface{}{"type": "command"} type FS struct { transferChar *gatt.GattCharacteristic1 transferRespCh <-chan *bluez.PropertyChanged + readOpen bool + writeOpen bool } // New creates a new fs given the transfer characteristic