Skip to content

Commit 8775144

Browse files
committed
FEAT: don't allow clear on not opened port, allow size? on port
related to: Oldes/Rebol-issues#812
1 parent 8483bc3 commit 8775144

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/core/p-file.c

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ REBINT Mode_Syms[] = {
638638
DECIDE(file->file.index > file->file.size);
639639

640640
case A_CLEAR:
641+
if (!IS_OPEN(file)) Trap1(RE_NOT_OPEN, path);
641642
// !! check for write enabled?
642643
SET_FLAG(file->modes, RFM_RESEEK);
643644
SET_FLAG(file->modes, RFM_TRUNCATE);

src/mezz/base-files.reb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exists?: func [
2828

2929
size?: func [
3030
{Returns the size of a file or vector (bits per value).}
31-
target [file! url! vector!]
31+
target [file! url! port! vector!]
3232
][
3333
query/mode target 'size
3434
]

src/tests/units/port-test.r3

+24-8
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,31 @@ if system/platform = 'Windows [
353353
]
354354
try [delete %file-552]
355355

356-
--test-- "clear file port"
356+
--test-- "CLEAR file port"
357357
;@@ https://github.com/Oldes/Rebol-issues/issues/812
358-
--assert file? write %file-812 to-binary "Hello World!"
359-
--assert port? f: open %file-812
360-
--assert "Hello World!" = to-string read f
361-
--assert port? clear f
362-
--assert 0 = length? f
363-
--assert port? close f
364-
try [delete %file-812]
358+
--assert all [
359+
file? write %file-812 "Hello World!"
360+
port? f: open %file-812
361+
"Hello World!" = read/string f
362+
port? clear f ; this actually does not clear the file as we are at the end of the stream
363+
0 = length? f
364+
"Hello" = read/seek/string/part f 0 5
365+
7 = length? f
366+
port? clear f ; this should truncate the file
367+
0 = length? f
368+
port? close f
369+
5 = length? f ; because there is still "Hello" left
370+
all [
371+
error? e: try [clear f]
372+
e/id = 'not-open
373+
]
374+
port? f: open %file-812
375+
port? clear f ; this should clear the file completely, because the position is at its head
376+
port? close f
377+
0 = size? %file-812
378+
not error? try [delete %file-812]
379+
]
380+
365381

366382
--test-- "RENAME file"
367383
;@@ https://github.com/Oldes/Rebol-issues/issues/446

0 commit comments

Comments
 (0)