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

Go to the source code of this file.

Data Structures

struct  DIR
 

Functions

DIRopendir (const char *dirname)
 
struct direntreaddir (DIR *d)
 
int closedir (DIR *d)
 

Function Documentation

◆ closedir()

int closedir ( DIR d)

Definition at line 127 of file dirent.c.

128 {
129  int ret = 0;
130 
131  if (d->handle != INVALID_HANDLE_VALUE)
132  ret = !FindClose(d->handle);
133  free(d->dirname);
134  free(d);
135 
136  return ret;
137 }
#define free(a)
Definition: header.h:65
HANDLE handle
Definition: dirent.c:29
char * dirname
Definition: dirent.c:27

References DIR::dirname, free, and DIR::handle.

Referenced by CleanupPriorWALFiles(), close_destination_dir(), FindEndOfXLOG(), FreeDesc(), FreeDir(), InitArchiveFmt_Directory(), KillExistingArchiveStatus(), KillExistingWALSummaries(), KillExistingXLOG(), pg_check_dir(), pgfnames(), process_directory_recursively(), recurse_dir(), scan_directory(), scan_for_existing_tablespaces(), search_directory(), verify_directory(), verify_plain_backup_directory(), and verify_tar_backup().

◆ opendir()

DIR* opendir ( const char *  dirname)

Definition at line 33 of file dirent.c.

34 {
35  DWORD attr;
36  DIR *d;
37 
38  /* Make sure it is a directory */
39  attr = GetFileAttributes(dirname);
40  if (attr == INVALID_FILE_ATTRIBUTES)
41  {
42  errno = ENOENT;
43  return NULL;
44  }
45  if ((attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
46  {
47  errno = ENOTDIR;
48  return NULL;
49  }
50 
51  d = malloc(sizeof(DIR));
52  if (!d)
53  {
54  errno = ENOMEM;
55  return NULL;
56  }
57  d->dirname = malloc(strlen(dirname) + 4);
58  if (!d->dirname)
59  {
60  errno = ENOMEM;
61  free(d);
62  return NULL;
63  }
64  strcpy(d->dirname, dirname);
65  if (d->dirname[strlen(d->dirname) - 1] != '/' &&
66  d->dirname[strlen(d->dirname) - 1] != '\\')
67  strcat(d->dirname, "\\"); /* Append backslash if not already there */
68  strcat(d->dirname, "*"); /* Search for entries named anything */
69  d->handle = INVALID_HANDLE_VALUE;
70  d->ret.d_ino = 0; /* no inodes on win32 */
71  d->ret.d_reclen = 0; /* not used on win32 */
72  d->ret.d_type = DT_UNKNOWN;
73 
74  return d;
75 }
#define DT_UNKNOWN
Definition: dirent.h:25
#define malloc(a)
Definition: header.h:50
Definition: dirent.c:26
struct dirent ret
Definition: dirent.c:28
unsigned short d_reclen
Definition: dirent.h:12
long d_ino
Definition: dirent.h:11
unsigned char d_type
Definition: dirent.h:13

References dirent::d_ino, dirent::d_reclen, dirent::d_type, DIR::dirname, DT_UNKNOWN, free, DIR::handle, malloc, and DIR::ret.

Referenced by AllocateDir(), CleanupPriorWALFiles(), FindEndOfXLOG(), get_destination_dir(), InitArchiveFmt_Directory(), KillExistingArchiveStatus(), KillExistingWALSummaries(), KillExistingXLOG(), main(), pg_check_dir(), pgfnames(), process_directory_recursively(), recurse_dir(), scan_directory(), scan_for_existing_tablespaces(), search_directory(), verify_directory(), and verify_plain_backup_directory().

◆ readdir()

struct dirent* readdir ( DIR d)

Definition at line 78 of file dirent.c.

79 {
80  WIN32_FIND_DATA fd;
81 
82  if (d->handle == INVALID_HANDLE_VALUE)
83  {
84  d->handle = FindFirstFile(d->dirname, &fd);
85  if (d->handle == INVALID_HANDLE_VALUE)
86  {
87  /* If there are no files, force errno=0 (unlike mingw) */
88  if (GetLastError() == ERROR_FILE_NOT_FOUND)
89  errno = 0;
90  else
91  _dosmaperr(GetLastError());
92  return NULL;
93  }
94  }
95  else
96  {
97  if (!FindNextFile(d->handle, &fd))
98  {
99  /* If there are no more files, force errno=0 (like mingw) */
100  if (GetLastError() == ERROR_NO_MORE_FILES)
101  errno = 0;
102  else
103  _dosmaperr(GetLastError());
104  return NULL;
105  }
106  }
107  strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
108  d->ret.d_namlen = strlen(d->ret.d_name);
109 
110  /*
111  * For reparse points dwReserved0 field will contain the ReparseTag. We
112  * check this first, because reparse points are also reported as
113  * directories.
114  */
115  if ((fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0 &&
116  (fd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
117  d->ret.d_type = DT_LNK;
118  else if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
119  d->ret.d_type = DT_DIR;
120  else
121  d->ret.d_type = DT_REG;
122 
123  return &d->ret;
124 }
#define DT_DIR
Definition: dirent.h:28
#define DT_REG
Definition: dirent.h:30
#define DT_LNK
Definition: dirent.h:31
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char d_name[MAX_PATH]
Definition: dirent.h:15
unsigned short d_namlen
Definition: dirent.h:14
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), dirent::d_name, dirent::d_namlen, dirent::d_type, DIR::dirname, DT_DIR, DT_LNK, DT_REG, fd(), DIR::handle, and DIR::ret.

Referenced by CleanupPriorWALFiles(), FindEndOfXLOG(), FindStreamingStart(), InitArchiveFmt_Directory(), KillExistingArchiveStatus(), KillExistingWALSummaries(), KillExistingXLOG(), pg_check_dir(), pgfnames(), process_directory_recursively(), ReadDirExtended(), recurse_dir(), rmtree(), scan_directory(), scan_for_existing_tablespaces(), search_directory(), verify_plain_backup_directory(), and verify_tar_backup().