Restrict to one open file of each type at a time
This commit is contained in:
		| @@ -11,6 +11,8 @@ var ( | |||||||
| 	ErrFileWriteOnly = errors.New("file is write only") | 	ErrFileWriteOnly = errors.New("file is write only") | ||||||
| 	ErrInvalidOffset = errors.New("invalid file offset") | 	ErrInvalidOffset = errors.New("invalid file offset") | ||||||
| 	ErrOffsetChanged = errors.New("offset has already been changed") | 	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 | // FSError represents an error returned by BLE FS | ||||||
|   | |||||||
| @@ -24,6 +24,10 @@ type File struct { | |||||||
| // Open opens a file and returns it as an fs.File to | // Open opens a file and returns it as an fs.File to | ||||||
| // satisfy the fs.FS interface | // satisfy the fs.FS interface | ||||||
| func (blefs *FS) Open(path string) (*File, error) { | 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. | 	// Make a read file request. This opens the file for reading. | ||||||
| 	err := blefs.request( | 	err := blefs.request( | ||||||
| 		FSCmdReadFile, | 		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. | // Create makes a new file on the BLE file system and returns it. | ||||||
| func (blefs *FS) Create(path string, size uint32) (*File, error) { | 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. | 	// Make a write file request. This will create and open a file for writing. | ||||||
| 	err := blefs.request( | 	err := blefs.request( | ||||||
| 		FSCmdWriteFile, | 		FSCmdWriteFile, | ||||||
| @@ -316,9 +324,14 @@ func (fl *File) Seek(offset int64, whence int) (int64, error) { | |||||||
| 	return int64(newOffset), nil | 	return int64(newOffset), nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // Close implements the fs.File interface. | // Close must be called before opening another file | ||||||
| // It just returns nil. |  | ||||||
| func (fl *File) Close() error { | func (fl *File) Close() error { | ||||||
|  | 	if fl.isReadOnly { | ||||||
|  | 		fl.fs.readOpen = false | ||||||
|  | 	} else if fl.isWriteOnly { | ||||||
|  | 		fl.fs.writeOpen = false | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -50,6 +50,8 @@ var btOptsCmd = map[string]interface{}{"type": "command"} | |||||||
| type FS struct { | type FS struct { | ||||||
| 	transferChar   *gatt.GattCharacteristic1 | 	transferChar   *gatt.GattCharacteristic1 | ||||||
| 	transferRespCh <-chan *bluez.PropertyChanged | 	transferRespCh <-chan *bluez.PropertyChanged | ||||||
|  | 	readOpen       bool | ||||||
|  | 	writeOpen      bool | ||||||
| } | } | ||||||
|  |  | ||||||
| // New creates a new fs given the transfer characteristic | // New creates a new fs given the transfer characteristic | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user