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 31 of file pgcheckdir.c.

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

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

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