Skip to content

Commit 1d53705

Browse files
remittormanojampalam
authored andcommitted
win32: Fix enumerate root dir content on SFTP-server (#148)
PowerShell/Win32-OpenSSH#539
1 parent 4879602 commit 1d53705

File tree

4 files changed

+156
-8
lines changed

4 files changed

+156
-8
lines changed

contrib/win32/win32compat/fileio.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,21 @@ fileio_stat(const char *path, struct _stat64 *buf)
745745
wchar_t* wpath = NULL;
746746
WIN32_FILE_ATTRIBUTE_DATA attributes = { 0 };
747747
int ret = -1, len = 0;
748+
749+
memset(buf, 0, sizeof(struct _stat64));
750+
751+
/* Detect root dir */
752+
if (path && strcmp(path, "/") == 0) {
753+
buf->st_mode = _S_IFDIR | _S_IREAD | 0xFF;
754+
buf->st_dev = USHRT_MAX; // rootdir flag
755+
return 0;
756+
}
757+
748758
if ((wpath = utf8_to_utf16(path)) == NULL) {
749759
errno = errno_from_Win32LastError();
750760
debug3("utf8_to_utf16 failed for file:%s error:%d", path, GetLastError());
751761
return -1;
752762
}
753-
memset(buf, 0, sizeof(struct _stat64));
754763

755764
if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &attributes) == FALSE) {
756765
errno = errno_from_Win32LastError();

contrib/win32/win32compat/misc.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,16 @@ convertToBackslash(char *str)
851851
}
852852
}
853853

854+
void
855+
convertToBackslashW(wchar_t *str)
856+
{
857+
while (*str) {
858+
if (*str == L'/')
859+
*str = L'\\';
860+
str++;
861+
}
862+
}
863+
854864
/* convert back slash to forward slash */
855865
void
856866
convertToForwardslash(char *str)
@@ -874,9 +884,9 @@ realpath(const char *path, char resolved[PATH_MAX])
874884
char tempPath[PATH_MAX];
875885

876886
if ((path[0] == '/') && path[1] && (path[2] == ':'))
877-
strncpy(resolved, path + 1, strlen(path)); /* skip the first '/' */
887+
strncpy(resolved, path + 1, PATH_MAX); /* skip the first '/' */
878888
else
879-
strncpy(resolved, path, strlen(path) + 1);
889+
strncpy(resolved, path, PATH_MAX);
880890

881891
if ((resolved[0]) && (resolved[1] == ':') && (resolved[2] == '\0')) { /* make "x:" as "x:\\" */
882892
resolved[2] = '\\';
@@ -889,7 +899,7 @@ realpath(const char *path, char resolved[PATH_MAX])
889899
convertToForwardslash(tempPath);
890900

891901
resolved[0] = '/'; /* will be our first slash in /x:/users/test1 format */
892-
strncpy(resolved + 1, tempPath, sizeof(tempPath) - 1);
902+
strncpy(resolved + 1, tempPath, PATH_MAX - 1);
893903
return resolved;
894904
}
895905

contrib/win32/win32compat/misc_internal.h

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#define SSH_ASYNC_STDOUT "SSH_ASYNC_STDOUT"
55
#define SSH_ASYNC_STDERR "SSH_ASYNC_STDERR"
66

7+
#define GOTO_CLEANUP_IF(_cond_,_err_) do { \
8+
if ((_cond_)) { \
9+
hr = _err_; \
10+
goto cleanup; \
11+
} \
12+
} while(0)
713
#define NULL_DEVICE "/dev/null"
814

915
#define IS_INVALID_HANDLE(h) ( ((NULL == h) || (INVALID_HANDLE_VALUE == h)) ? 1 : 0 )
@@ -17,6 +23,7 @@ void w32posix_done();
1723
char* w32_programdir();
1824

1925
void convertToBackslash(char *str);
26+
void convertToBackslashW(wchar_t *str);
2027
void convertToForwardslash(char *str);
2128

2229
void unix_time_to_file_time(ULONG, LPFILETIME);

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)