PostgreSQL Source Code  git master
filter.c File Reference
#include "postgres_fe.h"
#include "common/fe_memutils.h"
#include "common/logging.h"
#include "common/string.h"
#include "filter.h"
#include "lib/stringinfo.h"
#include "pqexpbuffer.h"
Include dependency graph for filter.c:

Go to the source code of this file.

Macros

#define is_keyword_str(cstr, str, bytes)    ((strlen(cstr) == (bytes)) && (pg_strncasecmp((cstr), (str), (bytes)) == 0))
 

Functions

void filter_init (FilterStateData *fstate, const char *filename, exit_function f_exit)
 
void filter_free (FilterStateData *fstate)
 
const char * filter_object_type_name (FilterObjectType fot)
 
static bool get_object_type (const char *keyword, int size, FilterObjectType *objtype)
 
void pg_log_filter_error (FilterStateData *fstate, const char *fmt,...)
 
static const char * filter_get_keyword (const char **line, int *size)
 
static const char * read_quoted_string (FilterStateData *fstate, const char *str, PQExpBuffer pattern)
 
static const char * read_pattern (FilterStateData *fstate, const char *str, PQExpBuffer pattern)
 
bool filter_read_item (FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
 

Macro Definition Documentation

◆ is_keyword_str

#define is_keyword_str (   cstr,
  str,
  bytes 
)     ((strlen(cstr) == (bytes)) && (pg_strncasecmp((cstr), (str), (bytes)) == 0))

Definition at line 23 of file filter.c.

Function Documentation

◆ filter_free()

void filter_free ( FilterStateData fstate)

Definition at line 61 of file filter.c.

62 {
63  if (!fstate)
64  return;
65 
66  free(fstate->linebuff.data);
67  fstate->linebuff.data = NULL;
68 
69  if (fstate->fp && fstate->fp != stdin)
70  {
71  if (fclose(fstate->fp) != 0)
72  pg_log_error("could not close filter file \"%s\": %m", fstate->filename);
73 
74  fstate->fp = NULL;
75  }
76 }
#define free(a)
Definition: header.h:65
#define pg_log_error(...)
Definition: logging.h:106
StringInfoData linebuff
Definition: filter.h:31
const char * filename
Definition: filter.h:28
FILE * fp
Definition: filter.h:27

References StringInfoData::data, FilterStateData::filename, FilterStateData::fp, free, FilterStateData::linebuff, and pg_log_error.

Referenced by read_dump_filters(), read_dumpall_filters(), and read_restore_filters().

◆ filter_get_keyword()

static const char* filter_get_keyword ( const char **  line,
int *  size 
)
static

Definition at line 179 of file filter.c.

180 {
181  const char *ptr = *line;
182  const char *result = NULL;
183 
184  /* Set returned length preemptively in case no keyword is found */
185  *size = 0;
186 
187  /* Skip initial whitespace */
188  while (isspace((unsigned char) *ptr))
189  ptr++;
190 
191  if (isalpha((unsigned char) *ptr))
192  {
193  result = ptr++;
194 
195  while (isalpha((unsigned char) *ptr) || *ptr == '_')
196  ptr++;
197 
198  *size = ptr - result;
199  }
200 
201  *line = ptr;
202 
203  return result;
204 }
static pg_noinline void Size size
Definition: slab.c:607

References size.

Referenced by filter_read_item().

◆ filter_init()

void filter_init ( FilterStateData fstate,
const char *  filename,
exit_function  f_exit 
)

Definition at line 37 of file filter.c.

38 {
39  fstate->filename = filename;
40  fstate->lineno = 0;
41  fstate->exit_nicely = f_exit;
42  initStringInfo(&fstate->linebuff);
43 
44  if (strcmp(filename, "-") != 0)
45  {
46  fstate->fp = fopen(filename, "r");
47  if (!fstate->fp)
48  {
49  pg_log_error("could not open filter file \"%s\": %m", filename);
50  fstate->exit_nicely(1);
51  }
52  }
53  else
54  fstate->fp = stdin;
55 }
static char * filename
Definition: pg_dumpall.c:119
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
exit_function exit_nicely
Definition: filter.h:29

References FilterStateData::exit_nicely, FilterStateData::filename, filename, FilterStateData::fp, initStringInfo(), FilterStateData::linebuff, FilterStateData::lineno, and pg_log_error.

Referenced by read_dump_filters(), read_dumpall_filters(), and read_restore_filters().

◆ filter_object_type_name()

const char* filter_object_type_name ( FilterObjectType  fot)

Definition at line 83 of file filter.c.

84 {
85  switch (fot)
86  {
88  return "comment or empty line";
90  return "table data";
92  return "table data and children";
94  return "database";
96  return "extension";
98  return "foreign data";
100  return "function";
102  return "index";
104  return "schema";
106  return "table";
108  return "table and children";
110  return "trigger";
111  }
112 
113  /* should never get here */
114  pg_unreachable();
115 }
#define pg_unreachable()
Definition: c.h:296
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
Definition: filter.h:51
@ FILTER_OBJECT_TYPE_SCHEMA
Definition: filter.h:57
@ FILTER_OBJECT_TYPE_INDEX
Definition: filter.h:56
@ FILTER_OBJECT_TYPE_TRIGGER
Definition: filter.h:60
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
Definition: filter.h:54
@ FILTER_OBJECT_TYPE_DATABASE
Definition: filter.h:52
@ FILTER_OBJECT_TYPE_FUNCTION
Definition: filter.h:55
@ FILTER_OBJECT_TYPE_TABLE_DATA
Definition: filter.h:50
@ FILTER_OBJECT_TYPE_NONE
Definition: filter.h:49
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
Definition: filter.h:59
@ FILTER_OBJECT_TYPE_EXTENSION
Definition: filter.h:53
@ FILTER_OBJECT_TYPE_TABLE
Definition: filter.h:58

References FILTER_OBJECT_TYPE_DATABASE, FILTER_OBJECT_TYPE_EXTENSION, FILTER_OBJECT_TYPE_FOREIGN_DATA, FILTER_OBJECT_TYPE_FUNCTION, FILTER_OBJECT_TYPE_INDEX, FILTER_OBJECT_TYPE_NONE, FILTER_OBJECT_TYPE_SCHEMA, FILTER_OBJECT_TYPE_TABLE, FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN, FILTER_OBJECT_TYPE_TABLE_DATA, FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN, FILTER_OBJECT_TYPE_TRIGGER, and pg_unreachable.

Referenced by read_dump_filters(), read_dumpall_filters(), and read_restore_filters().

◆ filter_read_item()

bool filter_read_item ( FilterStateData fstate,
char **  objname,
FilterCommandType comtype,
FilterObjectType objtype 
)

Definition at line 388 of file filter.c.

392 {
393  if (pg_get_line_buf(fstate->fp, &fstate->linebuff))
394  {
395  const char *str = fstate->linebuff.data;
396  const char *keyword;
397  int size;
398  PQExpBufferData pattern;
399 
400  fstate->lineno++;
401 
402  /* Skip initial white spaces */
403  while (isspace((unsigned char) *str))
404  str++;
405 
406  /*
407  * Skip empty lines or lines where the first non-whitespace character
408  * is a hash indicating a comment.
409  */
410  if (*str != '\0' && *str != '#')
411  {
412  /*
413  * First we expect sequence of two keywords, {include|exclude}
414  * followed by the object type to operate on.
415  */
416  keyword = filter_get_keyword(&str, &size);
417  if (!keyword)
418  {
419  pg_log_filter_error(fstate,
420  _("no filter command found (expected \"include\" or \"exclude\")"));
421  fstate->exit_nicely(1);
422  }
423 
424  if (is_keyword_str("include", keyword, size))
425  *comtype = FILTER_COMMAND_TYPE_INCLUDE;
426  else if (is_keyword_str("exclude", keyword, size))
427  *comtype = FILTER_COMMAND_TYPE_EXCLUDE;
428  else
429  {
430  pg_log_filter_error(fstate,
431  _("invalid filter command (expected \"include\" or \"exclude\")"));
432  fstate->exit_nicely(1);
433  }
434 
435  keyword = filter_get_keyword(&str, &size);
436  if (!keyword)
437  {
438  pg_log_filter_error(fstate, _("missing filter object type"));
439  fstate->exit_nicely(1);
440  }
441 
442  if (!get_object_type(keyword, size, objtype))
443  {
444  pg_log_filter_error(fstate,
445  _("unsupported filter object type: \"%.*s\""), size, keyword);
446  fstate->exit_nicely(1);
447  }
448 
449  initPQExpBuffer(&pattern);
450 
451  str = read_pattern(fstate, str, &pattern);
452  *objname = pattern.data;
453  }
454  else
455  {
456  *objname = NULL;
457  *comtype = FILTER_COMMAND_TYPE_NONE;
458  *objtype = FILTER_OBJECT_TYPE_NONE;
459  }
460 
461  return true;
462  }
463 
464  if (ferror(fstate->fp))
465  {
466  pg_log_error("could not read from filter file \"%s\": %m", fstate->filename);
467  fstate->exit_nicely(1);
468  }
469 
470  return false;
471 }
#define _(x)
Definition: elog.c:90
static bool get_object_type(const char *keyword, int size, FilterObjectType *objtype)
Definition: filter.c:123
#define is_keyword_str(cstr, str, bytes)
Definition: filter.c:23
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
Definition: filter.c:155
static const char * filter_get_keyword(const char **line, int *size)
Definition: filter.c:179
static const char * read_pattern(FilterStateData *fstate, const char *str, PQExpBuffer pattern)
Definition: filter.c:298
@ FILTER_COMMAND_TYPE_NONE
Definition: filter.h:39
@ FILTER_COMMAND_TYPE_EXCLUDE
Definition: filter.h:41
@ FILTER_COMMAND_TYPE_INCLUDE
Definition: filter.h:40
const char * str
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90

References _, StringInfoData::data, PQExpBufferData::data, FilterStateData::exit_nicely, FilterStateData::filename, FILTER_COMMAND_TYPE_EXCLUDE, FILTER_COMMAND_TYPE_INCLUDE, FILTER_COMMAND_TYPE_NONE, filter_get_keyword(), FILTER_OBJECT_TYPE_NONE, FilterStateData::fp, get_object_type(), initPQExpBuffer(), is_keyword_str, FilterStateData::linebuff, FilterStateData::lineno, pg_get_line_buf(), pg_log_error, pg_log_filter_error(), read_pattern(), size, and str.

Referenced by read_dump_filters(), read_dumpall_filters(), and read_restore_filters().

◆ get_object_type()

static bool get_object_type ( const char *  keyword,
int  size,
FilterObjectType objtype 
)
static

Definition at line 123 of file filter.c.

124 {
125  if (is_keyword_str("table_data", keyword, size))
127  else if (is_keyword_str("table_data_and_children", keyword, size))
129  else if (is_keyword_str("database", keyword, size))
130  *objtype = FILTER_OBJECT_TYPE_DATABASE;
131  else if (is_keyword_str("extension", keyword, size))
132  *objtype = FILTER_OBJECT_TYPE_EXTENSION;
133  else if (is_keyword_str("foreign_data", keyword, size))
135  else if (is_keyword_str("function", keyword, size))
136  *objtype = FILTER_OBJECT_TYPE_FUNCTION;
137  else if (is_keyword_str("index", keyword, size))
138  *objtype = FILTER_OBJECT_TYPE_INDEX;
139  else if (is_keyword_str("schema", keyword, size))
140  *objtype = FILTER_OBJECT_TYPE_SCHEMA;
141  else if (is_keyword_str("table", keyword, size))
142  *objtype = FILTER_OBJECT_TYPE_TABLE;
143  else if (is_keyword_str("table_and_children", keyword, size))
145  else if (is_keyword_str("trigger", keyword, size))
146  *objtype = FILTER_OBJECT_TYPE_TRIGGER;
147  else
148  return false;
149 
150  return true;
151 }

References FILTER_OBJECT_TYPE_DATABASE, FILTER_OBJECT_TYPE_EXTENSION, FILTER_OBJECT_TYPE_FOREIGN_DATA, FILTER_OBJECT_TYPE_FUNCTION, FILTER_OBJECT_TYPE_INDEX, FILTER_OBJECT_TYPE_SCHEMA, FILTER_OBJECT_TYPE_TABLE, FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN, FILTER_OBJECT_TYPE_TABLE_DATA, FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN, FILTER_OBJECT_TYPE_TRIGGER, is_keyword_str, and size.

Referenced by filter_read_item().

◆ pg_log_filter_error()

void pg_log_filter_error ( FilterStateData fstate,
const char *  fmt,
  ... 
)

Definition at line 155 of file filter.c.

156 {
157  va_list argp;
158  char buf[256];
159 
160  va_start(argp, fmt);
161  vsnprintf(buf, sizeof(buf), fmt, argp);
162  va_end(argp);
163 
164  pg_log_error("invalid format in filter read from \"%s\" on line %d: %s",
165  (fstate->fp == stdin ? "stdin" : fstate->filename),
166  fstate->lineno,
167  buf);
168 }
static void const char * fmt
va_end(args)
va_start(args, fmt)
static char * buf
Definition: pg_test_fsync.c:73
#define vsnprintf
Definition: port.h:237

References buf, FilterStateData::filename, fmt, FilterStateData::fp, FilterStateData::lineno, pg_log_error, va_end(), va_start(), and vsnprintf.

Referenced by filter_read_item(), read_dump_filters(), read_dumpall_filters(), read_pattern(), read_quoted_string(), and read_restore_filters().

◆ read_pattern()

static const char* read_pattern ( FilterStateData fstate,
const char *  str,
PQExpBuffer  pattern 
)
static

Definition at line 298 of file filter.c.

299 {
300  bool skip_space = true;
301  bool found_space = false;
302 
303  /* Skip initial whitespace */
304  while (isspace((unsigned char) *str))
305  str++;
306 
307  if (*str == '\0')
308  {
309  pg_log_filter_error(fstate, _("missing object name pattern"));
310  fstate->exit_nicely(1);
311  }
312 
313  while (*str && *str != '#')
314  {
315  while (*str && !isspace((unsigned char) *str) && !strchr("#,.()\"", *str))
316  {
317  /*
318  * Append space only when it is allowed, and when it was found in
319  * original string.
320  */
321  if (!skip_space && found_space)
322  {
323  appendPQExpBufferChar(pattern, ' ');
324  skip_space = true;
325  }
326 
327  appendPQExpBufferChar(pattern, *str++);
328  }
329 
330  skip_space = false;
331 
332  if (*str == '"')
333  {
334  if (found_space)
335  appendPQExpBufferChar(pattern, ' ');
336 
337  str = read_quoted_string(fstate, str, pattern);
338  }
339  else if (*str == ',')
340  {
341  appendPQExpBufferStr(pattern, ", ");
342  skip_space = true;
343  str++;
344  }
345  else if (*str && strchr(".()", *str))
346  {
347  appendPQExpBufferChar(pattern, *str++);
348  skip_space = true;
349  }
350 
351  found_space = false;
352 
353  /* skip ending whitespaces */
354  while (isspace((unsigned char) *str))
355  {
356  found_space = true;
357  str++;
358  }
359  }
360 
361  return str;
362 }
static const char * read_quoted_string(FilterStateData *fstate, const char *str, PQExpBuffer pattern)
Definition: filter.c:213
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References _, appendPQExpBufferChar(), appendPQExpBufferStr(), FilterStateData::exit_nicely, pg_log_filter_error(), read_quoted_string(), and str.

Referenced by filter_read_item().

◆ read_quoted_string()

static const char* read_quoted_string ( FilterStateData fstate,
const char *  str,
PQExpBuffer  pattern 
)
static

Definition at line 213 of file filter.c.

216 {
217  appendPQExpBufferChar(pattern, '"');
218  str++;
219 
220  while (1)
221  {
222  /*
223  * We can ignore \r or \n chars because the string is read by
224  * pg_get_line_buf, so these chars should be just trailing chars.
225  */
226  if (*str == '\r' || *str == '\n')
227  {
228  str++;
229  continue;
230  }
231 
232  if (*str == '\0')
233  {
234  Assert(fstate->linebuff.data);
235 
236  if (!pg_get_line_buf(fstate->fp, &fstate->linebuff))
237  {
238  if (ferror(fstate->fp))
239  pg_log_error("could not read from filter file \"%s\": %m",
240  fstate->filename);
241  else
242  pg_log_filter_error(fstate, _("unexpected end of file"));
243 
244  fstate->exit_nicely(1);
245  }
246 
247  str = fstate->linebuff.data;
248 
249  appendPQExpBufferChar(pattern, '\n');
250  fstate->lineno++;
251  }
252 
253  if (*str == '"')
254  {
255  appendPQExpBufferChar(pattern, '"');
256  str++;
257 
258  if (*str == '"')
259  {
260  appendPQExpBufferChar(pattern, '"');
261  str++;
262  }
263  else
264  break;
265  }
266  else if (*str == '\\')
267  {
268  str++;
269  if (*str == 'n')
270  appendPQExpBufferChar(pattern, '\n');
271  else if (*str == '\\')
272  appendPQExpBufferChar(pattern, '\\');
273 
274  str++;
275  }
276  else
277  appendPQExpBufferChar(pattern, *str++);
278  }
279 
280  return str;
281 }
#define Assert(condition)
Definition: c.h:858

References _, appendPQExpBufferChar(), Assert, StringInfoData::data, FilterStateData::exit_nicely, FilterStateData::filename, FilterStateData::fp, FilterStateData::linebuff, FilterStateData::lineno, pg_get_line_buf(), pg_log_error, pg_log_filter_error(), and str.

Referenced by read_pattern().