@@ -278,81 +278,50 @@ pg_read_file(PG_FUNCTION_ARGS)
278
278
*
279
279
* No superuser check done here- instead privileges are handled by the
280
280
* GRANT system.
281
+ *
282
+ * If read_to_eof is true, bytes_to_read must be -1, otherwise negative values
283
+ * are not allowed for bytes_to_read.
281
284
*/
282
- Datum
283
- pg_read_file_v2 (PG_FUNCTION_ARGS )
285
+ static text *
286
+ pg_read_file_common (text * filename_t , int64 seek_offset , int64 bytes_to_read ,
287
+ bool read_to_eof , bool missing_ok )
284
288
{
285
- text * filename_t = PG_GETARG_TEXT_PP (0 );
286
- int64 seek_offset = 0 ;
287
- int64 bytes_to_read = -1 ;
288
- bool missing_ok = false;
289
- char * filename ;
290
- text * result ;
291
-
292
- /* handle optional arguments */
293
- if (PG_NARGS () >= 3 )
294
- {
295
- seek_offset = PG_GETARG_INT64 (1 );
296
- bytes_to_read = PG_GETARG_INT64 (2 );
297
-
298
- if (bytes_to_read < 0 )
299
- ereport (ERROR ,
300
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
301
- errmsg ("requested length cannot be negative" )));
302
- }
303
- if (PG_NARGS () >= 4 )
304
- missing_ok = PG_GETARG_BOOL (3 );
305
-
306
- filename = convert_and_check_filename (filename_t );
289
+ if (read_to_eof )
290
+ Assert (bytes_to_read == -1 );
291
+ else if (bytes_to_read < 0 )
292
+ ereport (ERROR ,
293
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
294
+ errmsg ("requested length cannot be negative" )));
307
295
308
- result = read_text_file (filename , seek_offset , bytes_to_read , missing_ok );
309
- if (result )
310
- PG_RETURN_TEXT_P (result );
311
- else
312
- PG_RETURN_NULL ();
296
+ return read_text_file (convert_and_check_filename (filename_t ),
297
+ seek_offset , bytes_to_read , missing_ok );
313
298
}
314
299
315
300
/*
316
301
* Read a section of a file, returning it as bytea
302
+ *
303
+ * Parameters are interpreted the same as pg_read_file_common().
317
304
*/
318
- Datum
319
- pg_read_binary_file (PG_FUNCTION_ARGS )
305
+ static bytea *
306
+ pg_read_binary_file_common (text * filename_t ,
307
+ int64 seek_offset , int64 bytes_to_read ,
308
+ bool read_to_eof , bool missing_ok )
320
309
{
321
- text * filename_t = PG_GETARG_TEXT_PP (0 );
322
- int64 seek_offset = 0 ;
323
- int64 bytes_to_read = -1 ;
324
- bool missing_ok = false;
325
- char * filename ;
326
- bytea * result ;
327
-
328
- /* handle optional arguments */
329
- if (PG_NARGS () >= 3 )
330
- {
331
- seek_offset = PG_GETARG_INT64 (1 );
332
- bytes_to_read = PG_GETARG_INT64 (2 );
333
-
334
- if (bytes_to_read < 0 )
335
- ereport (ERROR ,
336
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
337
- errmsg ("requested length cannot be negative" )));
338
- }
339
- if (PG_NARGS () >= 4 )
340
- missing_ok = PG_GETARG_BOOL (3 );
341
-
342
- filename = convert_and_check_filename (filename_t );
310
+ if (read_to_eof )
311
+ Assert (bytes_to_read == -1 );
312
+ else if (bytes_to_read < 0 )
313
+ ereport (ERROR ,
314
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
315
+ errmsg ("requested length cannot be negative" )));
343
316
344
- result = read_binary_file (filename , seek_offset ,
345
- bytes_to_read , missing_ok );
346
- if (result )
347
- PG_RETURN_BYTEA_P (result );
348
- else
349
- PG_RETURN_NULL ();
317
+ return read_binary_file (convert_and_check_filename (filename_t ),
318
+ seek_offset , bytes_to_read , missing_ok );
350
319
}
351
320
352
321
353
322
/*
354
- * Wrapper functions for the 1 and 3 argument variants of pg_read_file_v2()
355
- * and pg_read_binary_file().
323
+ * Wrapper functions for the variants of SQL functions pg_read_file() and
324
+ * pg_read_binary_file().
356
325
*
357
326
* These are necessary to pass the sanity check in opr_sanity, which checks
358
327
* that all built-in functions that share the implementing C function take
@@ -361,25 +330,126 @@ pg_read_binary_file(PG_FUNCTION_ARGS)
361
330
Datum
362
331
pg_read_file_off_len (PG_FUNCTION_ARGS )
363
332
{
364
- return pg_read_file_v2 (fcinfo );
333
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
334
+ int64 seek_offset = PG_GETARG_INT64 (1 );
335
+ int64 bytes_to_read = PG_GETARG_INT64 (2 );
336
+ text * ret ;
337
+
338
+ ret = pg_read_file_common (filename_t , seek_offset , bytes_to_read ,
339
+ false, false);
340
+ if (!ret )
341
+ PG_RETURN_NULL ();
342
+
343
+ PG_RETURN_TEXT_P (ret );
344
+ }
345
+
346
+ Datum
347
+ pg_read_file_off_len_missing (PG_FUNCTION_ARGS )
348
+ {
349
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
350
+ int64 seek_offset = PG_GETARG_INT64 (1 );
351
+ int64 bytes_to_read = PG_GETARG_INT64 (2 );
352
+ bool missing_ok = PG_GETARG_BOOL (3 );
353
+ text * ret ;
354
+
355
+ ret = pg_read_file_common (filename_t , seek_offset , bytes_to_read ,
356
+ false, missing_ok );
357
+
358
+ if (!ret )
359
+ PG_RETURN_NULL ();
360
+
361
+ PG_RETURN_TEXT_P (ret );
365
362
}
366
363
367
364
Datum
368
365
pg_read_file_all (PG_FUNCTION_ARGS )
369
366
{
370
- return pg_read_file_v2 (fcinfo );
367
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
368
+ text * ret ;
369
+
370
+ ret = pg_read_file_common (filename_t , 0 , -1 , true, false);
371
+
372
+ if (!ret )
373
+ PG_RETURN_NULL ();
374
+
375
+ PG_RETURN_TEXT_P (ret );
376
+ }
377
+
378
+ Datum
379
+ pg_read_file_all_missing (PG_FUNCTION_ARGS )
380
+ {
381
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
382
+ bool missing_ok = PG_GETARG_BOOL (1 );
383
+ text * ret ;
384
+
385
+ ret = pg_read_file_common (filename_t , 0 , -1 , true, missing_ok );
386
+
387
+ if (!ret )
388
+ PG_RETURN_NULL ();
389
+
390
+ PG_RETURN_TEXT_P (ret );
371
391
}
372
392
373
393
Datum
374
394
pg_read_binary_file_off_len (PG_FUNCTION_ARGS )
375
395
{
376
- return pg_read_binary_file (fcinfo );
396
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
397
+ int64 seek_offset = PG_GETARG_INT64 (1 );
398
+ int64 bytes_to_read = PG_GETARG_INT64 (2 );
399
+ text * ret ;
400
+
401
+ ret = pg_read_binary_file_common (filename_t , seek_offset , bytes_to_read ,
402
+ false, false);
403
+ if (!ret )
404
+ PG_RETURN_NULL ();
405
+
406
+ PG_RETURN_BYTEA_P (ret );
407
+ }
408
+
409
+ Datum
410
+ pg_read_binary_file_off_len_missing (PG_FUNCTION_ARGS )
411
+ {
412
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
413
+ int64 seek_offset = PG_GETARG_INT64 (1 );
414
+ int64 bytes_to_read = PG_GETARG_INT64 (2 );
415
+ bool missing_ok = PG_GETARG_BOOL (3 );
416
+ text * ret ;
417
+
418
+ ret = pg_read_binary_file_common (filename_t , seek_offset , bytes_to_read ,
419
+ false, missing_ok );
420
+ if (!ret )
421
+ PG_RETURN_NULL ();
422
+
423
+ PG_RETURN_BYTEA_P (ret );
377
424
}
378
425
379
426
Datum
380
427
pg_read_binary_file_all (PG_FUNCTION_ARGS )
381
428
{
382
- return pg_read_binary_file (fcinfo );
429
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
430
+ text * ret ;
431
+
432
+ ret = pg_read_binary_file_common (filename_t , 0 , -1 , true, false);
433
+
434
+ if (!ret )
435
+ PG_RETURN_NULL ();
436
+
437
+ PG_RETURN_BYTEA_P (ret );
438
+ }
439
+
440
+ Datum
441
+ pg_read_binary_file_all_missing (PG_FUNCTION_ARGS )
442
+ {
443
+ text * filename_t = PG_GETARG_TEXT_PP (0 );
444
+ bool missing_ok = PG_GETARG_BOOL (1 );
445
+ text * ret ;
446
+
447
+ ret = pg_read_binary_file_common (filename_t , 0 , -1 , true, missing_ok );
448
+
449
+ if (!ret )
450
+ PG_RETURN_NULL ();
451
+
452
+ PG_RETURN_BYTEA_P (ret );
383
453
}
384
454
385
455
/*
0 commit comments