Skip to content

Commit 873f1bc

Browse files
committed
Add lcd and lls commands
1 parent 3075b72 commit 873f1bc

2 files changed

Lines changed: 159 additions & 6 deletions

File tree

examples/sftpclient/sftpclient.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ static void ShowCommands(void)
358358
printf("\tcd <string> change directory\n");
359359
printf("\tchmod <mode> <path> change mode\n");
360360
printf("\tget <remote file> <local file> pulls file(s) from server\n");
361+
printf("\tlcd <path> change local directory\n");
362+
printf("\tlls list local directory\n");
361363
printf("\tls list current directory\n");
362364
printf("\tmkdir <dir name> creates new directory on server\n");
363365
printf("\tput <local file> <remote file> push file(s) to server\n");
@@ -731,6 +733,28 @@ static int doCmds(func_args* args)
731733
continue;
732734
}
733735

736+
/* lcd must be checked before cd so that WSTRNSTR
737+
* does not match "cd" inside "lcd" */
738+
if ((pt = WSTRNSTR(msg, "lcd", MAX_CMD_SZ)) != NULL) {
739+
int sz;
740+
741+
pt += sizeof("lcd");
742+
sz = (int)WSTRLEN(pt);
743+
744+
if (sz > 0 && pt[sz - 1] == '\n') {
745+
pt[sz - 1] = '\0';
746+
}
747+
748+
if (WCHDIR(ssh->fs, pt) != 0) {
749+
if (SFTP_FPUTS(args,
750+
"Error changing local directory\n") < 0) {
751+
err_msg("fputs error");
752+
return -1;
753+
}
754+
}
755+
continue;
756+
}
757+
734758
if ((pt = WSTRNSTR(msg, "cd", MAX_CMD_SZ)) != NULL) {
735759
WS_SFTP_FILEATRB atrb;
736760
int sz;
@@ -1071,6 +1095,68 @@ static int doCmds(func_args* args)
10711095

10721096
}
10731097

1098+
#ifndef NO_WOLFSSH_DIR
1099+
/* lls must be checked before ls so that WSTRNSTR
1100+
* does not match "ls" inside "lls" */
1101+
if (WSTRNSTR(msg, "lls", MAX_CMD_SZ) != NULL) {
1102+
WDIR dir;
1103+
char cwd[WOLFSSH_MAX_FILENAME];
1104+
int llsErr = 0;
1105+
1106+
if (WGETCWD(ssh->fs, cwd, sizeof(cwd)) == NULL) {
1107+
if (SFTP_FPUTS(args,
1108+
"Error getting local directory\n") < 0) {
1109+
err_msg("fputs error");
1110+
return -1;
1111+
}
1112+
continue;
1113+
}
1114+
1115+
if (WOPENDIR(ssh->fs, NULL, &dir, cwd) != 0) {
1116+
if (SFTP_FPUTS(args,
1117+
"Error opening local directory\n") < 0) {
1118+
err_msg("fputs error");
1119+
return -1;
1120+
}
1121+
continue;
1122+
}
1123+
1124+
#ifdef WOLFSSH_ZEPHYR
1125+
{
1126+
struct fs_dirent dp;
1127+
1128+
while (fs_readdir(&dir, &dp) == 0 &&
1129+
dp.name[0] != '\0') {
1130+
if (SFTP_FPUTS(args, dp.name) < 0 ||
1131+
SFTP_FPUTS(args, "\n") < 0) {
1132+
err_msg("fputs error");
1133+
llsErr = 1;
1134+
break;
1135+
}
1136+
}
1137+
}
1138+
#else
1139+
{
1140+
struct dirent* dp;
1141+
1142+
while ((dp = WREADDIR(ssh->fs, &dir)) != NULL) {
1143+
if (SFTP_FPUTS(args, dp->d_name) < 0 ||
1144+
SFTP_FPUTS(args, "\n") < 0) {
1145+
err_msg("fputs error");
1146+
llsErr = 1;
1147+
break;
1148+
}
1149+
}
1150+
}
1151+
#endif
1152+
WCLOSEDIR(ssh->fs, &dir);
1153+
if (llsErr) {
1154+
return -1;
1155+
}
1156+
continue;
1157+
}
1158+
#endif /* NO_WOLFSSH_DIR */
1159+
10741160
if (WSTRNSTR(msg, "ls", MAX_CMD_SZ) != NULL) {
10751161
WS_SFTPNAME* tmp;
10761162
WS_SFTPNAME* current;

tests/sftp.c

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,23 @@ static const char* cmds[] = {
6161
#endif
6262
"rm configure.ac",
6363
"cd ../",
64+
"pwd",
6465
"ls",
6566
"rename test-get test-get-2",
6667
"rmdir a",
6768
"ls",
6869
"chmod 600 test-get-2",
6970
"rm test-get-2",
7071
"ls -s",
72+
"cd /nonexistent_path_xyz",
73+
"cd",
74+
#ifndef NO_WOLFSSH_DIR
75+
"lls",
76+
"lcd /tmp",
77+
"lls",
78+
"lcd /nonexistent_path_xyz",
79+
"lcd",
80+
#endif
7181
"exit"
7282
};
7383
static int commandIdx = 0;
@@ -129,14 +139,71 @@ static int Expected(int command)
129139
return 0;
130140
}
131141

132-
case 10:
133-
return (WSTRNSTR(inBuf, "test-get", sizeof(inBuf)) == NULL);
142+
case 10: /* pwd after cd ../ */
143+
for (i = 0; i < (int)sizeof(inBuf); i++) {
144+
if (inBuf[i] == '\n') {
145+
inBuf[i] = '\0';
146+
break;
147+
}
148+
}
149+
if (WSTRLEN(inBuf) >= 2 &&
150+
inBuf[WSTRLEN(inBuf) - 1] == 'a' &&
151+
inBuf[WSTRLEN(inBuf) - 2] == '/') {
152+
printf("pwd still in /a after cd ../: %s\n", inBuf);
153+
return -1;
154+
}
155+
break;
156+
157+
case 11: /* ls after cd ../ */
158+
return (WSTRNSTR(inBuf, "test-get",
159+
sizeof(inBuf)) == NULL);
160+
161+
case 14: /* ls after rmdir */
162+
return (WSTRNSTR(inBuf, "test-get-2",
163+
sizeof(inBuf)) == NULL);
134164

135-
case 13:
136-
return (WSTRNSTR(inBuf, "test-get-2", sizeof(inBuf)) == NULL);
165+
case 17: /* ls -s */
166+
return (WSTRNSTR(inBuf, "size in bytes",
167+
sizeof(inBuf)) == NULL);
137168

138-
case 16:
139-
return (WSTRNSTR(inBuf, "size in bytes", sizeof(inBuf)) == NULL);
169+
case 18: /* cd to nonexistent path */
170+
if (WSTRNSTR(inBuf, "Error changing directory",
171+
sizeof(inBuf)) == NULL) {
172+
fprintf(stderr,
173+
"cd: expected error not found in %s\n", inBuf);
174+
return 1;
175+
}
176+
return 0;
177+
178+
#ifndef NO_WOLFSSH_DIR
179+
case 20: /* lls from working directory */
180+
if (WSTRNSTR(inBuf, "configure.ac",
181+
sizeof(inBuf)) == NULL) {
182+
fprintf(stderr,
183+
"lls: configure.ac not found in %s\n", inBuf);
184+
return 1;
185+
}
186+
return 0;
187+
188+
case 23: /* lcd to nonexistent path */
189+
if (WSTRNSTR(inBuf, "Error changing local directory",
190+
sizeof(inBuf)) == NULL) {
191+
fprintf(stderr,
192+
"lcd: expected error not found in %s\n", inBuf);
193+
return 1;
194+
}
195+
return 0;
196+
197+
case 24: /* lcd with empty path */
198+
if (WSTRNSTR(inBuf, "Error changing local directory",
199+
sizeof(inBuf)) == NULL) {
200+
fprintf(stderr,
201+
"lcd: expected error on empty path in %s\n",
202+
inBuf);
203+
return 1;
204+
}
205+
return 0;
206+
#endif /* NO_WOLFSSH_DIR */
140207

141208
default:
142209
break;

0 commit comments

Comments
 (0)