PostgreSQL Source Code git master
version.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GET_PG_MAJORVERSION_NUM(v)   ((v) / 10000)
 

Functions

uint32 get_pg_version (const char *datadir, char **version_str)
 

Macro Definition Documentation

◆ GET_PG_MAJORVERSION_NUM

#define GET_PG_MAJORVERSION_NUM (   v)    ((v) / 10000)

Definition at line 19 of file version.h.

Function Documentation

◆ get_pg_version()

uint32 get_pg_version ( const char *  datadir,
char **  version_str 
)

Definition at line 44 of file version.c.

45{
46 FILE *version_fd;
47 char ver_filename[MAXPGPATH];
49 int v1 = 0,
50 v2 = 0;
51 struct stat st;
52
53 snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
54 datadir);
55
56 if ((version_fd = fopen(ver_filename, "r")) == NULL)
57 pg_fatal("could not open version file \"%s\": %m", ver_filename);
58
59 if (fstat(fileno(version_fd), &st) != 0)
60 pg_fatal("could not stat file \"%s\": %m", ver_filename);
61 if (st.st_size > PG_VERSION_MAX_SIZE)
62 pg_fatal("file \"%s\" is too large", ver_filename);
63
64 if (fscanf(version_fd, "%63s", buf) == 0 ||
65 sscanf(buf, "%d.%d", &v1, &v2) < 1)
66 pg_fatal("could not parse version file \"%s\"", ver_filename);
67
68 fclose(version_fd);
69
70 if (version_str)
71 {
72 *version_str = pg_malloc(PG_VERSION_MAX_SIZE);
73 memcpy(*version_str, buf, st.st_size);
74 }
75
76 if (v1 < 10)
77 {
78 /* pre-v10 style, e.g. 9.6.1 */
79 return v1 * 10000 + v2 * 100;
80 }
81 else
82 {
83 /* post-v10 style, e.g. 10.1 */
84 return v1 * 10000;
85 }
86}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define PG_VERSION_MAX_SIZE
Definition: version.c:27
#define pg_fatal(...)
#define MAXPGPATH
char * datadir
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:260
#define fstat
Definition: win32_port.h:273

References buf, datadir, fstat, MAXPGPATH, pg_fatal, pg_malloc(), PG_VERSION_MAX_SIZE, snprintf, and stat::st_size.

Referenced by check_data_dir(), check_data_directory(), CheckDataVersion(), and main().