Skip to content

Commit 0ea3379

Browse files
committed
Fix interfaces and remove shortcircuiting bug from readfiles and fix file permissions for readwrite mode
1 parent c9a5c24 commit 0ea3379

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

basis-library/mpl/file.sig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sig
2222

2323
val readChars: t -> int -> char ArraySlice.slice -> unit
2424
val readWord8s: t -> int -> Word8.word ArraySlice.slice -> unit
25-
val writeChar : {file: t, file_offset: int, array_slice_offset: int} -> char -> unit
26-
val writeWord8s : {file: t, file_offset: int, array_slice_offset: int} -> Word8.word ArraySlice.slice -> unit
25+
val writeChar : {file: t, file_offset: int} -> char -> unit
26+
val writeWord8s : {file: t, file_offset: int} -> Word8.word ArraySlice.slice -> unit
2727

2828
end

basis-library/mpl/file.sml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct
2828
fun openFile path =
2929
let
3030
open Posix.FileSys
31-
val file = openf (path, O_RDONLY, O.fromWord 0w0)
31+
val file = openf (path, O_RDWR, O.fromWord 0w0)
3232
val size = Position.toInt (ST.size (fstat file))
3333
val fd = C_Int.fromInt (SysWord.toInt (fdToWord file))
3434
val ptr = mmapFileReadable (fd, C_Size.fromInt size)
@@ -64,15 +64,15 @@ struct
6464
Char.chr (Word8.toInt (MLton.Pointer.getWord8 (ptr, i)))
6565

6666
fun readChar (ptr, size, stillOpen) (i: int) =
67-
if !stillOpen = OpenRead orelse !stillOpen = OpenReadWrite andalso i >= 0 andalso i < size then
67+
if (!stillOpen = OpenRead orelse !stillOpen = OpenReadWrite) andalso i >= 0 andalso i < size then
6868
unsafeReadChar (ptr, size, stillOpen) i
6969
else if i < 0 orelse i >= size then
7070
raise Subscript
7171
else
7272
raise Closed
7373

7474
fun readWord8 (ptr, size, stillOpen) (i: int) =
75-
if !stillOpen = OpenRead orelse !stillOpen = OpenReadWrite andalso i >= 0 andalso i < size then
75+
if (!stillOpen = OpenRead orelse !stillOpen = OpenReadWrite) andalso i >= 0 andalso i < size then
7676
unsafeReadWord8 (ptr, size, stillOpen) i
7777
else if i < 0 orelse i >= size then
7878
raise Subscript
@@ -84,7 +84,7 @@ struct
8484
val (arr, j, n) = ArraySlice.base slice
8585
val start = MLtonPointer.add (ptr, Word.fromInt i)
8686
in
87-
if !stillOpen = OpenRead orelse !stillOpen = OpenReadWrite andalso i >= 0 andalso i+n <= size then
87+
if (!stillOpen = OpenRead orelse !stillOpen = OpenReadWrite) andalso i >= 0 andalso i+n <= size then
8888
copyCharsToBuffer (start, arr, C_Size.fromInt j, C_Size.fromInt n)
8989
else if i < 0 orelse i+n > size then
9090
raise Subscript
@@ -97,37 +97,37 @@ struct
9797
val (arr, j, n) = ArraySlice.base slice
9898
val start = MLtonPointer.add (ptr, Word.fromInt i)
9999
in
100-
if !stillOpen = OpenRead orelse !stillOpen = OpenReadWrite andalso i >= 0 andalso i+n <= size then
100+
if (!stillOpen = OpenRead orelse !stillOpen = OpenReadWrite) andalso i >= 0 andalso i+n <= size then
101101
copyWord8sToBuffer (start, arr, C_Size.fromInt j, C_Size.fromInt n)
102102
else if i < 0 orelse i+n > size then
103103
raise Subscript
104104
else
105105
raise Closed
106106
end
107107

108-
fun writeChar {file = (ptr, size, stillOpen), file_offset = file_offset, array_slice_offset = i} c =
109-
if !stillOpen = OpenReadWrite andalso i >= 0 andalso i < size then
110-
MLton.Pointer.setWord8 (ptr, i + file_offset, Primitive.Char8.idToWord8 c)
111-
else if i < 0 orelse i >= size then
108+
fun writeChar {file = (ptr, size, stillOpen), file_offset = file_offset} c =
109+
if !stillOpen = OpenReadWrite andalso file_offset >= 0 andalso file_offset < size then
110+
MLton.Pointer.setWord8 (ptr, file_offset, Primitive.Char8.idToWord8 c)
111+
else if file_offset < 0 orelse file_offset >= size then
112112
raise Subscript
113113
else if !stillOpen = OpenRead then
114114
raise WrongFilePermission
115115
else
116116
raise Closed
117117

118-
fun writeWord8s {file = (ptr, size, stillOpen), file_offset = file_offset, array_slice_offset = i} slice =
119-
let
120-
val (arr, j, n) = ArraySlice.base slice
121-
val start = MLtonPointer.add (ptr, Word.fromInt file_offset)
122-
in
123-
if !stillOpen = OpenReadWrite andalso i >= 0 andalso file_offset + (n - i) <= size then
124-
copyWord8sFromBuffer (start, arr, C_Size.fromInt (i + j), C_Size.fromInt (n - i))
125-
else if i < 0 orelse i + n > size then
126-
raise Subscript
127-
else if !stillOpen = OpenRead then
128-
raise WrongFilePermission
129-
else
130-
raise Closed
131-
end
118+
fun writeWord8s {file = (ptr, size, stillOpen), file_offset} slice =
119+
let
120+
val (arr, j, n) = ArraySlice.base slice
121+
val start = MLtonPointer.add (ptr, Word.fromInt file_offset)
122+
in
123+
if !stillOpen = OpenReadWrite andalso file_offset >= 0 andalso file_offset + n <= size then
124+
copyWord8sFromBuffer (start, arr, C_Size.fromInt j, C_Size.fromInt n)
125+
else if file_offset < 0 orelse file_offset + n > size then
126+
raise Subscript
127+
else if !stillOpen = OpenRead then
128+
raise WrongFilePermission
129+
else
130+
raise Closed
131+
end
132132

133133
end

examples/lib/WriteFile.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct
1616
val lo = i*k
1717
val hi = Int.min ((i+1)*k, n)
1818
in
19-
MPL.File.writeWord8s { file = file , file_offset = oldSize + lo, array_slice_offset = 0} (Seq.subseq content (lo, hi-lo))
19+
MPL.File.writeWord8s { file = file , file_offset = oldSize + lo} (Seq.subseq content (lo, hi-lo))
2020
end
2121
);
2222
MPL.File.closeFile file

0 commit comments

Comments
 (0)