PostgreSQL Source Code  git master
file_utils.h File Reference
#include <dirent.h>
Include dependency graph for file_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum PGFileType PGFileType
 

Enumerations

enum  PGFileType {
  PGFILETYPE_ERROR, PGFILETYPE_UNKNOWN, PGFILETYPE_REG, PGFILETYPE_DIR,
  PGFILETYPE_LNK
}
 

Functions

PGFileType get_dirent_type (const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
 

Typedef Documentation

◆ PGFileType

typedef enum PGFileType PGFileType

Enumeration Type Documentation

◆ PGFileType

enum PGFileType
Enumerator
PGFILETYPE_ERROR 
PGFILETYPE_UNKNOWN 
PGFILETYPE_REG 
PGFILETYPE_DIR 
PGFILETYPE_LNK 

Definition at line 18 of file file_utils.h.

Function Documentation

◆ get_dirent_type()

PGFileType get_dirent_type ( const char *  path,
const struct dirent de,
bool  look_through_symlinks,
int  elevel 
)

Definition at line 410 of file file_utils.c.

References dirent::d_type, DT_DIR, DT_LNK, DT_REG, ereport, errcode_for_file_access(), errmsg(), lstat, pg_log_generic(), PGFILETYPE_DIR, PGFILETYPE_ERROR, PGFILETYPE_LNK, PGFILETYPE_REG, PGFILETYPE_UNKNOWN, S_ISDIR, S_ISREG, and stat.

Referenced by walkdir().

414 {
415  PGFileType result;
416 
417  /*
418  * Some systems tell us the type directly in the dirent struct, but that's
419  * a BSD and Linux extension not required by POSIX. Even when the
420  * interface is present, sometimes the type is unknown, depending on the
421  * filesystem.
422  */
423 #if defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
424  if (de->d_type == DT_REG)
425  result = PGFILETYPE_REG;
426  else if (de->d_type == DT_DIR)
427  result = PGFILETYPE_DIR;
428  else if (de->d_type == DT_LNK && !look_through_symlinks)
429  result = PGFILETYPE_LNK;
430  else
431  result = PGFILETYPE_UNKNOWN;
432 #else
433  result = PGFILETYPE_UNKNOWN;
434 #endif
435 
436  if (result == PGFILETYPE_UNKNOWN)
437  {
438  struct stat fst;
439  int sret;
440 
441 
442  if (look_through_symlinks)
443  sret = stat(path, &fst);
444  else
445  sret = lstat(path, &fst);
446 
447  if (sret < 0)
448  {
449  result = PGFILETYPE_ERROR;
450 #ifdef FRONTEND
451  pg_log_generic(elevel, "could not stat file \"%s\": %m", path);
452 #else
453  ereport(elevel,
455  errmsg("could not stat file \"%s\": %m", path)));
456 #endif
457  }
458  else if (S_ISREG(fst.st_mode))
459  result = PGFILETYPE_REG;
460  else if (S_ISDIR(fst.st_mode))
461  result = PGFILETYPE_DIR;
462 #ifdef S_ISLNK
463  else if (S_ISLNK(fst.st_mode))
464  result = PGFILETYPE_LNK;
465 #endif
466  }
467 
468  return result;
469 }
#define DT_DIR
Definition: dirent.h:28
PGFileType
Definition: file_utils.h:18
#define DT_LNK
Definition: dirent.h:31
int errcode_for_file_access(void)
Definition: elog.c:633
#define S_ISREG(m)
Definition: win32_port.h:299
#define stat(a, b)
Definition: win32_port.h:255
static int elevel
Definition: vacuumlazy.c:333
unsigned char d_type
Definition: dirent.h:13
#define ereport(elevel,...)
Definition: elog.h:144
void pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...)
Definition: logging.c:197
#define S_ISDIR(m)
Definition: win32_port.h:296
#define lstat(path, sb)
Definition: win32_port.h:244
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define DT_REG
Definition: dirent.h:30