Skip to content

Commit 6301972

Browse files
committed
Add multiple ../../.. support in sftp-server for changing directories or get/put
Before one could provide only one .. ; now it can be any numbers. cd ../../.. or get ../backup/myfile.c --- all these formations now work.
1 parent 39c00bf commit 6301972

File tree

1 file changed

+42
-78
lines changed

1 file changed

+42
-78
lines changed

openbsd-compat/realpath.c

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -203,101 +203,65 @@ realpath(const char *path, char resolved[PATH_MAX])
203203

204204
#else
205205

206-
char *realpathWin32(const char *path, char resolved[PATH_MAX])
206+
void backslashconvert(char *str)
207207
{
208-
size_t path_len;
209-
unsigned int lastSlash;
210-
char realpath[PATH_MAX];
211-
char * pch;
208+
while (*str) {
209+
if (*str == '/')
210+
*str = '\\'; // convert forward slash to back slash
211+
str++;
212+
}
212213

213-
path_len = strlcpy(realpath, path+1, sizeof(realpath));
214+
}
214215

215-
char * pchMac;
216-
pchMac = strstr (realpath, "._");
217-
if (pchMac != NULL)
218-
{
219-
pchMac[0] = '\0';
220-
pchMac++;
221-
pchMac++;
222-
strcat(realpath, pchMac);
223-
}
216+
// convert back slash to forward slash
217+
void slashconvert(char *str)
218+
{
219+
while (*str) {
220+
if (*str == '\\')
221+
*str = '/'; // convert back slash to forward slash
222+
str++;
223+
}
224+
}
224225

225-
pch = strrchr(realpath, '/');
226-
lastSlash = pch - realpath + 1;
227-
if(path_len == lastSlash)
228-
{
229-
realpath[lastSlash-1] = '\0';
230-
}
226+
char *realpathWin32(const char *path, char resolved[PATH_MAX])
227+
{
228+
char realpath[PATH_MAX];
231229

232-
pch = strrchr(realpath,'.');
233-
if(pch != NULL)
234-
{
235-
if (realpath[pch-realpath - 1] == '.')
236-
{
237-
realpath[pch - realpath - 2] = '\0';
238-
pch = strrchr(realpath, '/');
239-
if(pch != NULL)
240-
realpath[pch - realpath] = '\0';
241-
}
242-
}
230+
strlcpy(resolved, path + 1, sizeof(realpath));
231+
backslashconvert(resolved);
232+
PathCanonicalizeA(realpath, resolved);
233+
slashconvert(realpath);
243234

244-
/*
245-
* Store terminating slash in 'X:/' on Windows.
246-
*/
247-
248-
if (realpath[1] == ':' && realpath[2] == 0)
249-
{
250-
realpath[2] = '/';
251-
realpath[3] = 0;
252-
}
253-
254-
resolved[0] = *path; // will be our first slash in /x:/users/test1 format
255-
strncpy (resolved+1, realpath, sizeof(realpath));
256-
return resolved;
235+
/*
236+
* Store terminating slash in 'X:/' on Windows.
237+
*/
238+
239+
if (realpath[1] == ':' && realpath[2] == 0)
240+
{
241+
realpath[2] = '/';
242+
realpath[3] = 0;
243+
}
244+
245+
resolved[0] = *path; // will be our first slash in /x:/users/test1 format
246+
strncpy(resolved + 1, realpath, sizeof(realpath));
247+
return resolved;
257248
}
258249

250+
// like realpathWin32() but takes out the first slash so that windows systems can work on the actual file or directory
259251
char *realpathWin32i(const char *path, char resolved[PATH_MAX])
260252
{
261-
size_t path_len;
262-
unsigned int lastSlash;
263253
char realpath[PATH_MAX];
264-
char * pch;
265254

266255
if (path[0] != '/') {
267256
// absolute form x:/abc/def given, no first slash to take out
268-
path_len = strlcpy(realpath, path, sizeof(realpath));
257+
strlcpy(resolved, path, sizeof(realpath));
269258
}
270259
else
271-
path_len = strlcpy(realpath, path + 1, sizeof(realpath));
272-
273-
char * pchMac;
274-
pchMac = strstr(realpath, "._");
275-
if (pchMac != NULL)
276-
{
277-
pchMac[0] = '\0';
278-
pchMac++;
279-
pchMac++;
280-
strcat(realpath, pchMac);
281-
}
282-
283-
pch = strrchr(realpath, '/');
284-
lastSlash = pch - realpath + 1;
285-
if (path_len == lastSlash)
286-
{
287-
realpath[lastSlash - 1] = '\0';
288-
}
260+
strlcpy(resolved, path + 1, sizeof(realpath));
289261

290-
pch = strrchr(realpath, '.');
291-
if (pch != NULL)
292-
{
293-
if (realpath[pch - realpath - 1] == '.')
294-
{
295-
realpath[pch - realpath - 2] = '\0';
296-
pch = strrchr(realpath, '/');
297-
if (pch != NULL)
298-
realpath[pch - realpath] = '\0';
299-
}
300-
}
262+
backslashconvert(resolved);
263+
PathCanonicalizeA(realpath, resolved);
264+
slashconvert(realpath);
301265

302266
/*
303267
* Store terminating slash in 'X:/' on Windows.

0 commit comments

Comments
 (0)