Restrict to one open file of each type at a time

This commit is contained in:
Elara 2021-12-13 09:56:34 -08:00
parent 382b1d9556
commit 9250d26fdc
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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