]> granicus.if.org Git - postgresql/blob - src/port/dirent.c
Fix the fastpath rule for jsonb_concat with an empty operand.
[postgresql] / src / port / dirent.c
1 /*-------------------------------------------------------------------------
2  *
3  * dirent.c
4  *        opendir/readdir/closedir for win32/msvc
5  *
6  * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/dirent.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
21
22 #include <dirent.h>
23
24
25 struct DIR
26 {
27         char       *dirname;
28         struct dirent ret;                      /* Used to return to caller */
29         HANDLE          handle;
30 };
31
32 DIR *
33 opendir(const char *dirname)
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
68                                                                                  * there */
69         strcat(d->dirname, "*");        /* Search for entries named anything */
70         d->handle = INVALID_HANDLE_VALUE;
71         d->ret.d_ino = 0;                       /* no inodes on win32 */
72         d->ret.d_reclen = 0;            /* not used on win32 */
73
74         return d;
75 }
76
77 struct dirent *
78 readdir(DIR *d)
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                         errno = ENOENT;
88                         return NULL;
89                 }
90         }
91         else
92         {
93                 if (!FindNextFile(d->handle, &fd))
94                 {
95                         if (GetLastError() == ERROR_NO_MORE_FILES)
96                         {
97                                 /* No more files, force errno=0 (unlike mingw) */
98                                 errno = 0;
99                                 return NULL;
100                         }
101                         _dosmaperr(GetLastError());
102                         return NULL;
103                 }
104         }
105         strcpy(d->ret.d_name, fd.cFileName);            /* Both strings are MAX_PATH
106                                                                                                  * long */
107         d->ret.d_namlen = strlen(d->ret.d_name);
108
109         return &d->ret;
110 }
111
112 int
113 closedir(DIR *d)
114 {
115         int                     ret = 0;
116
117         if (d->handle != INVALID_HANDLE_VALUE)
118                 ret = !FindClose(d->handle);
119         free(d->dirname);
120         free(d);
121
122         return ret;
123 }