Skip to content

Commit 2ac8c15

Browse files
committed
FIX: update file info after clearing file-port (posix)
related to: Oldes/Rebol-issues#812 and Oldes/Rebol-issues#552
1 parent f1395c8 commit 2ac8c15

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/os/posix/dev-file.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ static int Get_File_Info(REBREQ *file)
487487
{
488488
ssize_t num_bytes;
489489
struct stat info;
490-
490+
file->actual = 0;
491+
491492
if (!file->id) {
492493
file->error = -RFE_NO_HANDLE;
493494
return DR_ERROR;
@@ -505,17 +506,17 @@ static int Get_File_Info(REBREQ *file)
505506
if (ftruncate(file->id, file->file.index)) return DR_ERROR;
506507
}
507508

508-
if (file->length == 0) return DR_DONE;
509-
510-
num_bytes = write(file->id, file->data, file->length);
511-
if (num_bytes < 0) {
512-
if (errno == ENOSPC) file->error = -RFE_DISK_FULL;
513-
else file->error = -RFE_BAD_WRITE;
514-
return DR_ERROR;
515-
} else {
516-
//SET_FLAG(file->modes, RFM_RESEEK);
517-
file->actual = (u32)num_bytes;
509+
if (file->length > 0) {
510+
num_bytes = write(file->id, file->data, file->length);
511+
if (num_bytes < 0) {
512+
if (errno == ENOSPC) file->error = -RFE_DISK_FULL;
513+
else file->error = -RFE_BAD_WRITE;
514+
return DR_ERROR;
515+
} else {
516+
file->actual = (u32)num_bytes;
517+
}
518518
}
519+
// update new file info
519520
if (fstat(file->id, &info) == 0) {
520521
file->file.size = info.st_size;
521522
file->file.time.l = (i32)(info.st_mtime);

src/tests/units/port-test.r3

+2
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,12 @@ if system/platform = 'Windows [
365365
file? write %file-812 "Hello World!"
366366
port? f: open %file-812
367367
"Hello World!" = read/string f
368+
13 = index? f
368369
port? clear f ; this actually does not clear the file as we are at the end of the stream
369370
0 = length? f
370371
12 = size? f
371372
"Hello" = read/seek/string/part f 0 5
373+
6 = index? f
372374
7 = length? f
373375
12 = size? f
374376
port? clear f ; this should truncate the file

0 commit comments

Comments
 (0)