Skip to content

Commit

Permalink
FIX: reading from virtual /proc files on Linux using read/part
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Dec 8, 2023
1 parent bf49996 commit b0f6840
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/p-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,11 @@ REBINT Mode_Syms[] = {
len += cnt;
SET_FLAG(file->modes, RFM_RESEEK);
}
if (cnt > len) return (REBCNT)len;
// Originaly, when the requested part was larger then the reported file's size,
// then the reported file size was used instead. But on Posix there are virtual files,
// where the size is reported as 0. In this case we keep the user's requested length.
// See: https://github.com/Oldes/Rebol-issues/issues/2303
if (cnt > len && len > 0) return (REBCNT)len;
return (REBCNT)cnt;
}

Expand Down
21 changes: 21 additions & 0 deletions src/tests/units/port-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,27 @@ if system/platform = 'Windows [
--assert all [error? e: try [append/only p "aa"] e/id = 'bad-refines]
try [delete %issue-1894]

if exists? %/proc/cpuinfo [
--test-- "Reading from /proc files on Linux"
;@@ https://github.com/Oldes/Rebol-issues/issues/2303
--assert all [
not error? info: try [read/string %/proc/cpuinfo]
empty? info ;; empty, because to read this type of file, the size must be specified!
]
--assert all [
not error? info: try [read/string/part %/proc/cpuinfo 10000]
print info
0 < length? info
]
]
--test-- "Reading an empty file"
--assert all [
file? write %empty ""
0 = length? read %empty
0 = length? read/part %empty 1000
port? delete %empty
]

===end-group===

if system/platform = 'Windows [
Expand Down

0 comments on commit b0f6840

Please sign in to comment.