PostgreSQL Source Code  git master
pgcheckdir.c File Reference
#include "c.h"
#include <dirent.h>
Include dependency graph for pgcheckdir.c:

Go to the source code of this file.

Functions

int pg_check_dir (const char *dir)
 

Function Documentation

◆ pg_check_dir()

int pg_check_dir ( const char *  dir)

Definition at line 33 of file pgcheckdir.c.

34 {
35  int result = 1;
36  DIR *chkdir;
37  struct dirent *file;
38  bool dot_found = false;
39  bool mount_found = false;
40  int readdir_errno;
41 
42  chkdir = opendir(dir);
43  if (chkdir == NULL)
44  return (errno == ENOENT) ? 0 : -1;
45 
46  while (errno = 0, (file = readdir(chkdir)) != NULL)
47  {
48  if (strcmp(".", file->d_name) == 0 ||
49  strcmp("..", file->d_name) == 0)
50  {
51  /* skip this and parent directory */
52  continue;
53  }
54 #ifndef WIN32
55  /* file starts with "." */
56  else if (file->d_name[0] == '.')
57  {
58  dot_found = true;
59  }
60  /* lost+found directory */
61  else if (strcmp("lost+found", file->d_name) == 0)
62  {
63  mount_found = true;
64  }
65 #endif
66  else
67  {
68  result = 4; /* not empty */
69  break;
70  }
71  }
72 
73  if (errno)
74  result = -1; /* some kind of I/O error? */
75 
76  /* Close chkdir and avoid overwriting the readdir errno on success */
77  readdir_errno = errno;
78  if (closedir(chkdir))
79  result = -1; /* error executing closedir */
80  else
81  errno = readdir_errno;
82 
83  /* We report on mount point if we find a lost+found directory */
84  if (result == 1 && mount_found)
85  result = 3;
86 
87  /* We report on dot-files if we _only_ find dot files */
88  if (result == 1 && dot_found)
89  result = 2;
90 
91  return result;
92 }
int closedir(DIR *)
Definition: dirent.c:123
struct dirent * readdir(DIR *)
Definition: dirent.c:78
DIR * opendir(const char *)
Definition: dirent.c:33
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References closedir(), dirent::d_name, opendir(), and readdir().

Referenced by bbsink_server_new(), cleanup_output_dirs(), create_data_directory(), create_xlog_or_symlink(), main(), and verify_dir_is_empty_or_create().