Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2cdaf0

Browse files
committed
Read until EOF vice stat-reported size in read_binary_file
read_binary_file(), used by SQL functions pg_read_file() and friends,uses stat to determine file length to read, when not passed an explicitlength as an argument. This is problematic, for example, if the filebeing read is a virtual file with a stat-reported length of zero.Arrange to read until EOF, or StringInfo data string lenth limit, isreached instead.Original complaint and patch by me, with significant review, corrections,advice, and code optimizations by Tom Lane. Backpatched to v11. Prior tothat only paths relative to the data and log dirs were allowed for files,so no "zero length" files were reachable anyway.Reviewed-By: Tom LaneDiscussion:https://postgr.es/m/flat/969b8d82-5bb2-5fa8-4eb1-f0e685c5d736%40joeconway.comBackpatch-through: 11
1 parentef799bd commitc2cdaf0

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

‎contrib/adminpack/expected/adminpack.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ SELECT pg_file_rename('test_file1', 'test_file2');
6464
(1 row)
6565

6666
SELECT pg_read_file('test_file1'); -- not there
67-
ERROR: could notstat file "test_file1": No such file or directory
67+
ERROR: could notopen file "test_file1" for reading: No such file or directory
6868
SELECT pg_read_file('test_file2');
6969
pg_read_file
7070
--------------
@@ -93,7 +93,7 @@ SELECT pg_file_rename('test_file2', 'test_file3', 'test_file3_archive');
9393
(1 row)
9494

9595
SELECT pg_read_file('test_file2'); -- not there
96-
ERROR: could notstat file "test_file2": No such file or directory
96+
ERROR: could notopen file "test_file2" for reading: No such file or directory
9797
SELECT pg_read_file('test_file3');
9898
pg_read_file
9999
--------------

‎src/backend/utils/adt/genfile.c

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,11 @@ read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
103103
boolmissing_ok)
104104
{
105105
bytea*buf;
106-
size_tnbytes;
106+
size_tnbytes=0;
107107
FILE*file;
108108

109-
if (bytes_to_read<0)
110-
{
111-
if (seek_offset<0)
112-
bytes_to_read=-seek_offset;
113-
else
114-
{
115-
structstatfst;
116-
117-
if (stat(filename,&fst)<0)
118-
{
119-
if (missing_ok&&errno==ENOENT)
120-
returnNULL;
121-
else
122-
ereport(ERROR,
123-
(errcode_for_file_access(),
124-
errmsg("could not stat file \"%s\": %m",filename)));
125-
}
126-
127-
bytes_to_read=fst.st_size-seek_offset;
128-
}
129-
}
130-
131-
/* not sure why anyone thought that int64 length was a good idea */
132-
if (bytes_to_read> (MaxAllocSize-VARHDRSZ))
109+
/* clamp request size to what we can actually deliver */
110+
if (bytes_to_read> (int64) (MaxAllocSize-VARHDRSZ))
133111
ereport(ERROR,
134112
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
135113
errmsg("requested length too large")));
@@ -151,9 +129,68 @@ read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read,
151129
(errcode_for_file_access(),
152130
errmsg("could not seek in file \"%s\": %m",filename)));
153131

154-
buf= (bytea*)palloc((Size)bytes_to_read+VARHDRSZ);
132+
if (bytes_to_read >=0)
133+
{
134+
/* If passed explicit read size just do it */
135+
buf= (bytea*)palloc((Size)bytes_to_read+VARHDRSZ);
136+
137+
nbytes=fread(VARDATA(buf),1, (size_t)bytes_to_read,file);
138+
}
139+
else
140+
{
141+
/* Negative read size, read rest of file */
142+
StringInfoDatasbuf;
143+
144+
initStringInfo(&sbuf);
145+
/* Leave room in the buffer for the varlena length word */
146+
sbuf.len+=VARHDRSZ;
147+
Assert(sbuf.len<sbuf.maxlen);
148+
149+
while (!(feof(file)||ferror(file)))
150+
{
151+
size_trbytes;
152+
153+
/* Minimum amount to read at a time */
154+
#defineMIN_READ_SIZE 4096
155+
156+
/*
157+
* If not at end of file, and sbuf.len is equal to
158+
* MaxAllocSize - 1, then either the file is too large, or
159+
* there is nothing left to read. Attempt to read one more
160+
* byte to see if the end of file has been reached. If not,
161+
* the file is too large; we'd rather give the error message
162+
* for that ourselves.
163+
*/
164+
if (sbuf.len==MaxAllocSize-1)
165+
{
166+
charrbuf[1];
155167

156-
nbytes=fread(VARDATA(buf),1, (size_t)bytes_to_read,file);
168+
fread(rbuf,1,1,file);
169+
if (!feof(file))
170+
ereport(ERROR,
171+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
172+
errmsg("file length too large")));
173+
else
174+
break;
175+
}
176+
177+
/* OK, ensure that we can read at least MIN_READ_SIZE */
178+
enlargeStringInfo(&sbuf,MIN_READ_SIZE);
179+
180+
/*
181+
* stringinfo.c likes to allocate in powers of 2, so it's likely
182+
* that much more space is available than we asked for. Use all
183+
* of it, rather than making more fread calls than necessary.
184+
*/
185+
rbytes=fread(sbuf.data+sbuf.len,1,
186+
(size_t) (sbuf.maxlen-sbuf.len-1),file);
187+
sbuf.len+=rbytes;
188+
nbytes+=rbytes;
189+
}
190+
191+
/* Now we can commandeer the stringinfo's buffer as the result */
192+
buf= (bytea*)sbuf.data;
193+
}
157194

158195
if (ferror(file))
159196
ereport(ERROR,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp