4
4
"context"
5
5
"encoding/binary"
6
6
"errors"
7
- "io/fs"
8
7
"log/slog"
9
8
"math/bits"
10
9
"runtime"
@@ -18,9 +17,9 @@ import (
18
17
type BlockDevice interface {
19
18
ReadBlocks (dst []byte , startBlock int64 ) (int , error )
20
19
WriteBlocks (data []byte , startBlock int64 ) (int , error )
21
- EraseSectors (startBlock , numBlocks int64 ) error
20
+ EraseBlocks (startBlock , numBlocks int64 ) error
22
21
// Mode returns 0 for no connection/prohibited access, 1 for read-only, 3 for read-write.
23
- Mode () accessmode
22
+ // Mode() accessmode
24
23
}
25
24
26
25
// sector index type.
@@ -60,7 +59,7 @@ type FS struct {
60
59
ffCodePage int
61
60
dbcTbl [10 ]byte
62
61
id uint16 // Filesystem mount ID. Serves to invalidate open files after mount.
63
- perm fs. FileMode
62
+ perm Mode
64
63
codepage []byte // unicode conversion table.
65
64
exCvt []byte // points to _tblCT* corresponding to codepage table.
66
65
log * slog.Logger
@@ -108,7 +107,7 @@ const (
108
107
sfnBufSize = 12
109
108
)
110
109
111
- type fileinfo struct {
110
+ type FileInfo struct {
112
111
fsize int64 // File Size.
113
112
fdate uint16
114
113
ftime uint16
@@ -208,6 +207,8 @@ func (fp *File) f_read(buff []byte) (br int, res fileResult) {
208
207
rbuff := buff
209
208
if fp .flag & faRead == 0 {
210
209
return 0 , frDenied
210
+ } else if fsys .perm & ModeRead == 0 {
211
+ return 0 , frDenied
211
212
}
212
213
remain := fp .obj .objsize - fp .fptr
213
214
btr := len (buff )
@@ -356,6 +357,8 @@ func (fp *File) f_write(buf []byte) (bw int, fr fileResult) {
356
357
return 0 , frWriteProtected
357
358
} else if fp .obj .fs .fstype == fstypeExFAT {
358
359
return 0 , frUnsupported
360
+ } else if fp .obj .fs .perm & ModeWrite == 0 {
361
+ return 0 , frWriteProtected
359
362
}
360
363
fs := fp .obj .fs
361
364
btw := len (buf )
@@ -500,6 +503,8 @@ func (fsys *FS) f_open(fp *File, name string, mode accessmode) fileResult {
500
503
return frInvalidObject
501
504
} else if fsys .fstype == fstypeExFAT {
502
505
return frUnsupported
506
+ } else if fsys .perm == 0 {
507
+ return frDenied
503
508
}
504
509
505
510
var dj dir
@@ -619,7 +624,7 @@ func (fsys *FS) f_open(fp *File, name string, mode accessmode) fileResult {
619
624
return res
620
625
}
621
626
622
- func (dp * dir ) f_readdir (fno * fileinfo ) fileResult {
627
+ func (dp * dir ) f_readdir (fno * FileInfo ) fileResult {
623
628
fsys := dp .obj .fs
624
629
fsys .trace ("dir:f_readdir" )
625
630
if fsys .fstype == fstypeExFAT {
@@ -764,12 +769,7 @@ func (fsys *FS) mount_volume(bd BlockDevice, ssize uint16, mode uint8) (fr fileR
764
769
// mutexes or file path handling. File path handling is left to
765
770
// the Go standard library which does a much better job than us.
766
771
// See `filepath` and `fs` standard library packages.
767
- devMode := bd .Mode ()
768
- if devMode == 0 {
769
- return frNotReady
770
- } else if mode & devMode != mode {
771
- return frDenied
772
- }
772
+
773
773
blk , err := makeBlockIndexer (int (ssize ))
774
774
if err != nil {
775
775
return frInvalidParameter
@@ -778,6 +778,7 @@ func (fsys *FS) mount_volume(bd BlockDevice, ssize uint16, mode uint8) (fr fileR
778
778
fsys .id ++ // Invalidate open files.
779
779
fsys .blk = blk
780
780
fsys .ssize = ssize
781
+ fsys .perm = Mode (mode )
781
782
fmt := fsys .find_volume (0 )
782
783
783
784
if fmt == bootsectorstatusDiskError {
@@ -1167,6 +1168,9 @@ func (fsys *FS) st_clust(dir []byte, cl uint32) {
1167
1168
}
1168
1169
1169
1170
func (fsys * FS ) disk_write (buf []byte , sector lba , numsectors int ) diskresult {
1171
+ if fsys .perm & ModeWrite == 0 {
1172
+ return drWriteProtected
1173
+ }
1170
1174
fsys .trace ("fs:disk_write" , slog .Uint64 ("start" , uint64 (sector )), slog .Int ("numsectors" , numsectors ))
1171
1175
if fsys .blk .off (int64 (len (buf ))) != 0 || fsys .blk ._divideBlockSize (int64 (len (buf ))) != int64 (numsectors ) {
1172
1176
fsys .logerror ("disk_write:unaligned" )
@@ -1196,7 +1200,7 @@ func (fsys *FS) disk_read(dst []byte, sector lba, numsectors int) diskresult {
1196
1200
}
1197
1201
func (fsys * FS ) disk_erase (startSector lba , numSectors int ) diskresult {
1198
1202
fsys .trace ("fs:disk_erase" , slog .Uint64 ("start" , uint64 (startSector )), slog .Int ("numsectors" , numSectors ))
1199
- err := fsys .device .EraseSectors (int64 (startSector ), int64 (numSectors ))
1203
+ err := fsys .device .EraseBlocks (int64 (startSector ), int64 (numSectors ))
1200
1204
if err != nil {
1201
1205
fsys .logerror ("disk_erase" , slog .String ("err" , err .Error ()))
1202
1206
return drError
@@ -1871,7 +1875,7 @@ func (dp *dir) sdi(ofs uint32) fileResult {
1871
1875
return frOK
1872
1876
}
1873
1877
1874
- func (dp * dir ) get_fileinfo (fno * fileinfo ) {
1878
+ func (dp * dir ) get_fileinfo (fno * FileInfo ) {
1875
1879
fsys := dp .obj .fs
1876
1880
1877
1881
fno .fname [0 ] = 0 // Invalidate.
@@ -2348,13 +2352,6 @@ func (fsys *FS) gen_numname(dst, src []byte, lfn []uint16, seq uint32) {
2348
2352
}
2349
2353
}
2350
2354
2351
- func (finfo * fileinfo ) AlternateName () string {
2352
- return str (finfo .altname [:])
2353
- }
2354
- func (finfo * fileinfo ) Name () string {
2355
- return str (finfo .fname [:])
2356
- }
2357
-
2358
2355
func str (s []byte ) string {
2359
2356
if len (s ) == 0 {
2360
2357
return ""
0 commit comments