Skip to content

Commit e72a375

Browse files
committed
Refactor code checking for file existence
jit.c and dfgr.c had a copy of the same code to check if a file exists or not, with a twist: jit.c did not check for EACCES when failing the stat() call for the path whose existence is tested. This refactored routine will be used by an upcoming patch. Reviewed-by: Ashutosh Bapat Discussion: https://postgr.es/m/ZTiV8tn_MIb_H2rE@paquier.xyz
1 parent 08c3ad2 commit e72a375

File tree

4 files changed

+29
-40
lines changed

4 files changed

+29
-40
lines changed

src/backend/jit/jit.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static bool provider_failed_loading = false;
4545

4646

4747
static bool provider_init(void);
48-
static bool file_exists(const char *name);
4948

5049

5150
/*
@@ -89,7 +88,7 @@ provider_init(void)
8988
*/
9089
snprintf(path, MAXPGPATH, "%s/%s%s", pkglib_path, jit_provider, DLSUFFIX);
9190
elog(DEBUG1, "probing availability of JIT provider at %s", path);
92-
if (!file_exists(path))
91+
if (!pg_file_exists(path))
9392
{
9493
elog(DEBUG1,
9594
"provider not available, disabling JIT for current session");
@@ -188,20 +187,3 @@ InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
188187
INSTR_TIME_ADD(dst->optimization_counter, add->optimization_counter);
189188
INSTR_TIME_ADD(dst->emission_counter, add->emission_counter);
190189
}
191-
192-
static bool
193-
file_exists(const char *name)
194-
{
195-
struct stat st;
196-
197-
Assert(name != NULL);
198-
199-
if (stat(name, &st) == 0)
200-
return !S_ISDIR(st.st_mode);
201-
else if (!(errno == ENOENT || errno == ENOTDIR))
202-
ereport(ERROR,
203-
(errcode_for_file_access(),
204-
errmsg("could not access file \"%s\": %m", name)));
205-
206-
return false;
207-
}

src/backend/storage/file/fd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,29 @@ pg_fdatasync(int fd)
493493
return rc;
494494
}
495495

496+
/*
497+
* pg_file_exists -- check that a file exists.
498+
*
499+
* This requires an absolute path to the file. Returns true if the file is
500+
* not a directory, false otherwise.
501+
*/
502+
bool
503+
pg_file_exists(const char *name)
504+
{
505+
struct stat st;
506+
507+
Assert(name != NULL);
508+
509+
if (stat(name, &st) == 0)
510+
return !S_ISDIR(st.st_mode);
511+
else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
512+
ereport(ERROR,
513+
(errcode_for_file_access(),
514+
errmsg("could not access file \"%s\": %m", name)));
515+
516+
return false;
517+
}
518+
496519
/*
497520
* pg_flush_data --- advise OS that the described dirty data should be flushed
498521
*

src/backend/utils/fmgr/dfmgr.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "fmgr.h"
3434
#include "lib/stringinfo.h"
3535
#include "miscadmin.h"
36+
#include "storage/fd.h"
3637
#include "storage/shmem.h"
3738
#include "utils/hsearch.h"
3839

@@ -78,7 +79,6 @@ char *Dynamic_library_path;
7879
static void *internal_load_library(const char *libname);
7980
static void incompatible_module_error(const char *libname,
8081
const Pg_magic_struct *module_magic_data) pg_attribute_noreturn();
81-
static bool file_exists(const char *name);
8282
static char *expand_dynamic_library_name(const char *name);
8383
static void check_restricted_library_name(const char *name);
8484
static char *substitute_libpath_macro(const char *name);
@@ -400,23 +400,6 @@ incompatible_module_error(const char *libname,
400400
errdetail_internal("%s", details.data)));
401401
}
402402

403-
static bool
404-
file_exists(const char *name)
405-
{
406-
struct stat st;
407-
408-
Assert(name != NULL);
409-
410-
if (stat(name, &st) == 0)
411-
return !S_ISDIR(st.st_mode);
412-
else if (!(errno == ENOENT || errno == ENOTDIR || errno == EACCES))
413-
ereport(ERROR,
414-
(errcode_for_file_access(),
415-
errmsg("could not access file \"%s\": %m", name)));
416-
417-
return false;
418-
}
419-
420403

421404
/*
422405
* If name contains a slash, check if the file exists, if so return
@@ -447,7 +430,7 @@ expand_dynamic_library_name(const char *name)
447430
else
448431
{
449432
full = substitute_libpath_macro(name);
450-
if (file_exists(full))
433+
if (pg_file_exists(full))
451434
return full;
452435
pfree(full);
453436
}
@@ -465,7 +448,7 @@ expand_dynamic_library_name(const char *name)
465448
{
466449
full = substitute_libpath_macro(new);
467450
pfree(new);
468-
if (file_exists(full))
451+
if (pg_file_exists(full))
469452
return full;
470453
pfree(full);
471454
}
@@ -582,7 +565,7 @@ find_in_dynamic_libpath(const char *basename)
582565

583566
elog(DEBUG3, "find_in_dynamic_libpath: trying \"%s\"", full);
584567

585-
if (file_exists(full))
568+
if (pg_file_exists(full))
586569
return full;
587570

588571
pfree(full);

src/include/storage/fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ extern int pg_fsync(int fd);
182182
extern int pg_fsync_no_writethrough(int fd);
183183
extern int pg_fsync_writethrough(int fd);
184184
extern int pg_fdatasync(int fd);
185+
extern bool pg_file_exists(const char *fname);
185186
extern void pg_flush_data(int fd, off_t offset, off_t nbytes);
186187
extern int pg_truncate(const char *path, off_t length);
187188
extern void fsync_fname(const char *fname, bool isdir);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy