Skip to content

Commit 02fc287

Browse files
committed
win32: Fix enumerate root dir content on SFTP-server
Enum logical drives via GetLogicalDriveStringsW.
1 parent c5f4fa5 commit 02fc287

File tree

3 files changed

+156
-10
lines changed

3 files changed

+156
-10
lines changed

contrib/win32/win32compat/fileio.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,21 @@ fileio_stat(const char *path, struct _stat64 *buf)
738738
wchar_t* wpath = NULL;
739739
WIN32_FILE_ATTRIBUTE_DATA attributes = { 0 };
740740
int ret = -1, len = 0;
741+
742+
memset(buf, 0, sizeof(struct _stat64));
743+
744+
/* Detect root dir */
745+
if (path && strcmp(path, "/") == 0) {
746+
buf->st_mode = _S_IFDIR | _S_IREAD | 0xFF;
747+
buf->st_dev = USHRT_MAX; // rootdir flag
748+
return 0;
749+
}
750+
741751
if ((wpath = utf8_to_utf16(path)) == NULL) {
742752
errno = errno_from_Win32LastError();
743753
debug3("utf8_to_utf16 failed for file:%s error:%d", path, GetLastError());
744754
return -1;
745755
}
746-
memset(buf, 0, sizeof(struct _stat64));
747756

748757
if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &attributes) == FALSE) {
749758
errno = errno_from_Win32LastError();

contrib/win32/win32compat/misc.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -866,27 +866,42 @@ convertToForwardslash(char *str)
866866
char *
867867
realpath(const char *path, char resolved[PATH_MAX])
868868
{
869+
char tempPath[PATH_MAX];
870+
869871
if (!path || !resolved) return NULL;
870872

871-
char tempPath[PATH_MAX];
873+
*resolved = 0;
874+
if (path[0] == 0)
875+
return NULL;
876+
877+
/* Detect root dir */
878+
if (strcmp(path, "/") == 0 || strcmp(path, "/..") == 0 ||
879+
(path[0] == '/' && isalpha(path[1]) && strcmp(path + 2, ":/..") == 0)) {
880+
strncpy(resolved, "/", sizeof(resolved));
881+
return resolved;
882+
}
883+
if (path[0] == '/' && isalpha(path[1]) && strcmp(path + 2, ":") == 0) {
884+
strncpy(resolved, path, sizeof(resolved));
885+
return resolved;
886+
}
872887

873888
if ((path[0] == '/') && path[1] && (path[2] == ':'))
874-
strncpy(resolved, path + 1, strlen(path)); /* skip the first '/' */
889+
strncpy(resolved, path + 1, sizeof(resolved)); /* skip the first '/' */
875890
else
876-
strncpy(resolved, path, strlen(path) + 1);
891+
strncpy(resolved, path, sizeof(resolved));
877892

878893
if ((resolved[0]) && (resolved[1] == ':') && (resolved[2] == '\0')) { /* make "x:" as "x:\\" */
879894
resolved[2] = '\\';
880895
resolved[3] = '\0';
881896
}
882897

883-
if (_fullpath(tempPath, resolved, PATH_MAX) == NULL)
898+
if (_fullpath(tempPath, resolved, sizeof(tempPath)) == NULL)
884899
return NULL;
885900

886901
convertToForwardslash(tempPath);
887902

888903
resolved[0] = '/'; /* will be our first slash in /x:/users/test1 format */
889-
strncpy(resolved + 1, tempPath, sizeof(tempPath) - 1);
904+
strncpy(resolved + 1, tempPath, sizeof(resolved) - 1);
890905
return resolved;
891906
}
892907

contrib/win32/win32compat/win32_dirent.c

+126-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,54 @@ struct DIR_ {
4242
intptr_t hFile;
4343
struct _wfinddata_t c_file;
4444
int first;
45+
wchar_t * nextdisk;
4546
};
4647

48+
#define ATTR_ROOTDIR UINT_MAX
49+
50+
/* Enumerate all devices which have drive name.
51+
Return a DIR stream on the root directory, or NULL if it could not be enumerated. */
52+
DIR *
53+
openrootdir(const char *name)
54+
{
55+
int hr = 0;
56+
DWORD dw;
57+
DIR * pdir;
58+
struct _wfinddata_t c_file = {0};
59+
wchar_t * p;
60+
61+
dw = GetLogicalDriveStringsW(_countof(c_file.name) - 2, c_file.name);
62+
if (!dw) {
63+
errno = ENODEV;
64+
return NULL;
65+
}
66+
c_file.attrib = ATTR_ROOTDIR;
67+
c_file.size = 0;
68+
p = c_file.name;
69+
while (*p) {
70+
size_t len = wcslen(p);
71+
if (len == 0)
72+
break;
73+
p += len + 1;
74+
c_file.size++;
75+
}
76+
if (c_file.size == 0) {
77+
errno = ENODEV;
78+
return NULL;
79+
}
80+
pdir = malloc(sizeof(DIR));
81+
if (!pdir) {
82+
errno = ENOMEM;
83+
return NULL;
84+
}
85+
memset(pdir, 0, sizeof(DIR));
86+
pdir->hFile = 0;
87+
memcpy(&pdir->c_file, &c_file, sizeof(c_file));
88+
pdir->first = 1;
89+
90+
return pdir;
91+
}
92+
4793
/* Open a directory stream on NAME.
4894
Return a DIR stream on the directory, or NULL if it could not be opened. */
4995
DIR *
@@ -55,14 +101,31 @@ opendir(const char *name)
55101
wchar_t searchstr[PATH_MAX];
56102
wchar_t* wname = NULL;
57103
int needed;
104+
size_t len;
105+
106+
/* Detect root dir */
107+
if (name && strcmp(name, "/") == 0)
108+
return openrootdir(name);
58109

59110
if ((wname = utf8_to_utf16(sanitized_path(name))) == NULL) {
60111
errno = ENOMEM;
61112
return NULL;
62113
}
63114

115+
convertToBackslashW(wname);
116+
len = wcslen(wname);
117+
if (len && wname[len-1] == L'\\') {
118+
len--;
119+
wname[len] = 0;
120+
}
121+
if (len >= PATH_MAX) {
122+
free(wname);
123+
errno = ENAMETOOLONG;
124+
return NULL;
125+
}
126+
64127
/* add *.* for Windows _findfirst() search pattern */
65-
swprintf_s(searchstr, PATH_MAX, L"%s\\*.*", wname);
128+
swprintf_s(searchstr, _countof(searchstr) - 1, L"%s\\*.*", wname);
66129
free(wname);
67130

68131
if ((hFile = _wfindfirst(searchstr, &c_file)) == -1L)
@@ -92,13 +155,69 @@ closedir(DIR *dirp)
92155

93156
if (dirp && (dirp->hFile)) {
94157
_findclose(dirp->hFile);
95-
dirp->hFile = 0;
96-
free(dirp);
97158
}
159+
free(dirp);
98160

99161
return 0;
100162
}
101163

164+
/* Read a root directory entry from DIRP.
165+
Return a pointer to a `struct dirent' describing the entry,
166+
or NULL for EOF or error. The storage returned may be overwritten
167+
by a later readdir call on the same DIR stream. */
168+
struct dirent *
169+
readrootdir(DIR * dirp)
170+
{
171+
wchar_t * p;
172+
size_t len = 0;
173+
struct dirent *pdirentry;
174+
UINT dt;
175+
ULARGE_INTEGER totalNumberOfBytes;
176+
BOOL x;
177+
178+
if (dirp->c_file.size <= 0) {
179+
errno = ENODATA;
180+
return NULL;
181+
}
182+
if (dirp->first) {
183+
dirp->first = 0;
184+
dirp->nextdisk = dirp->c_file.name;
185+
}
186+
187+
p = dirp->nextdisk;
188+
189+
for ( ; ; p += len + 1) {
190+
len = wcslen(p);
191+
if (len == 0) {
192+
dirp->nextdisk = p;
193+
errno = ENODATA;
194+
return NULL; /* end of multi-string */
195+
}
196+
197+
dt = GetDriveTypeW(p);
198+
if (dt == DRIVE_UNKNOWN || dt == DRIVE_NO_ROOT_DIR || dt == DRIVE_RAMDISK)
199+
continue;
200+
201+
x = GetDiskFreeSpaceExW(p, NULL, &totalNumberOfBytes, NULL);
202+
if (!x || totalNumberOfBytes.QuadPart == 0)
203+
continue;
204+
205+
break; // process filtered disk
206+
}
207+
dirp->nextdisk = p + len + 1;
208+
209+
if ((pdirentry = malloc(sizeof(struct dirent))) == NULL) {
210+
errno = ENOMEM;
211+
return NULL;
212+
}
213+
pdirentry->d_name[0] = (char)p[0];
214+
pdirentry->d_name[1] = ':';
215+
pdirentry->d_name[2] = 0;
216+
217+
pdirentry->d_ino = 1; // a fictious one like UNIX to say it is nonzero
218+
return pdirentry;
219+
}
220+
102221
/* Read a directory entry from DIRP.
103222
Return a pointer to a `struct dirent' describing the entry,
104223
or NULL for EOF or error. The storage returned may be overwritten
@@ -113,6 +232,9 @@ readdir(void *avp)
113232
DIR *dirp = (DIR *)avp;
114233
char *tmp = NULL;
115234

235+
if (dirp->hFile == 0 && dirp->c_file.attrib == ATTR_ROOTDIR)
236+
return readrootdir(dirp);
237+
116238
for (;;) {
117239
if (dirp->first) {
118240
memcpy(&c_file, &dirp->c_file, sizeof(c_file));
@@ -128,7 +250,7 @@ readdir(void *avp)
128250
return NULL;
129251
}
130252

131-
strncpy(pdirentry.d_name, tmp, strlen(tmp) + 1);
253+
strncpy(pdirentry.d_name, tmp, sizeof(pdirentry.d_name));
132254
free(tmp);
133255

134256
pdirentry.d_ino = 1; /* a fictious one like UNIX to say it is nonzero */

0 commit comments

Comments
 (0)