Skip to content

Commit 956c0f0

Browse files
committed
fix file read bugs; remove need for null termination on API boundary
1 parent 7fe07be commit 956c0f0

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

Diff for: fat.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (fp *File) f_read(buff []byte) (br int, res fileResult) {
224224
br += rcnt
225225
rbuff = rbuff[rcnt:]
226226
fp.fptr += int64(rcnt)
227-
if btr < 0 {
227+
if btr <= 0 {
228228
break
229229
}
230230
if fp.fptr%ss == 0 {
@@ -278,14 +278,12 @@ func (fp *File) f_read(buff []byte) (br int, res fileResult) {
278278
}
279279
fp.sect = sect
280280
}
281-
rcnt = int(ss) - int(fp.fptr%ss)
281+
modfptr := int(fp.fptr % ss)
282+
rcnt = int(ss) - modfptr
282283
if rcnt > btr {
283284
rcnt = btr
284285
}
285-
if fsys.move_window(fp.sect) != frOK {
286-
return br, fp.abort(frDiskErr)
287-
}
288-
copy(rbuff[:rcnt], fp.buf[fp.fptr%ss:])
286+
copy(rbuff[:rcnt], fp.buf[modfptr:])
289287
}
290288
return br, frOK
291289
}
@@ -460,6 +458,8 @@ func (fs *FS) f_opendir(dp *dir, path string) (fr fileResult) {
460458
} else if fs.fstype == fstypeExFAT {
461459
return frUnsupported
462460
}
461+
path += "\x00" // TODO(soypat): change internal algorithms to non-null terminated strings.
462+
463463
dp.obj.fs = fs
464464

465465
fr = dp.follow_path(path)
@@ -495,6 +495,7 @@ func (fs *FS) f_opendir(dp *dir, path string) (fr fileResult) {
495495
// f_open opens or creates a file.
496496
func (fsys *FS) f_open(fp *File, name string, mode accessmode) fileResult {
497497
fsys.trace("f_open", slog.String("name", name), slog.Uint64("mode", uint64(mode)))
498+
name += "\x00" // TODO(soypat): change internal algorithms to non-null terminated strings.
498499
if fp == nil {
499500
return frInvalidObject
500501
} else if fsys.fstype == fstypeExFAT {

Diff for: fat_test.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ import (
1010
"testing"
1111
)
1212

13-
func TestOpenRootFile(t *testing.T) {
13+
func TestRead(t *testing.T) {
1414
fs, _ := initTestFAT()
15-
var fp File
16-
fr := fs.f_open(&fp, "rootfile\x00", faRead)
17-
if fr != frOK {
18-
t.Fatal(fr.Error())
15+
var rootFile File
16+
fr := fs.f_open(&rootFile, "rootfile", faRead)
17+
mustBeOK(t, fr)
18+
19+
buf := make([]byte, 512)
20+
n, fr := rootFile.f_read(buf)
21+
mustBeOK(t, fr)
22+
23+
got := string(buf[:n])
24+
if got != rootFileContents {
25+
t.Errorf("mismatched rootfile contents:\n%q\n%q\n", got, rootFileContents)
26+
}
27+
28+
var dirfile File
29+
fr = fs.f_open(&dirfile, "rootdir/dirfile", faRead)
30+
mustBeOK(t, fr)
31+
n, fr = dirfile.f_read(buf)
32+
mustBeOK(t, fr)
33+
got = string(buf[:n])
34+
if got != dirFileContents {
35+
t.Errorf("mismatched dirfile contents:\n%q\n%q\n", got, dirFileContents)
1936
}
2037
}
2138

2239
func TestFileInfo(t *testing.T) {
2340
fs, _ := initTestFAT()
2441
var dir dir
25-
fr := fs.f_opendir(&dir, "rootdir\x00")
26-
if fr != frOK {
27-
t.Fatal(fr.Error())
28-
}
42+
fr := fs.f_opendir(&dir, "rootdir")
43+
mustBeOK(t, fr)
2944
var finfo fileinfo
3045
fr = dir.f_readdir(&finfo)
3146
if fr != frOK {
@@ -43,7 +58,7 @@ func TestFileInfo(t *testing.T) {
4358

4459
func ExampleRead() {
4560
const (
46-
filename = "test.txt\x00"
61+
filename = "test.txt"
4762
data = "abc123"
4863
)
4964
var fs FS
@@ -120,6 +135,12 @@ func DefaultFATByteBlocks(numBlocks int) *BytesBlocks {
120135
buf: buf,
121136
}
122137
}
138+
func mustBeOK(t *testing.T, fr fileResult) {
139+
t.Helper()
140+
if fr != frOK {
141+
t.Fatal(fr.Error())
142+
}
143+
}
123144

124145
type BytesBlocks struct {
125146
blk blkIdxer
@@ -312,3 +333,6 @@ var fatInit = map[int64][512]byte{
312333
0x35, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x73, 0x2e, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // |5 lines.........|
313334
},
314335
}
336+
337+
const rootFileContents = "this is\nthe root file\n"
338+
const dirFileContents = "this is not\nnot the root\nnot the root file\nnope. \nThis file has 5 lines.\n"

0 commit comments

Comments
 (0)