Implementation of commands MKD and RMD

This commit is contained in:
Ugo Cirmignani
2017-11-19 16:35:33 +01:00
parent 532b98183f
commit d0593cbdbb
9 changed files with 180 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -425,6 +425,87 @@ int parseCommandRest(clientDataType *theClientData)
return 1; return 1;
} }
int parseCommandMkd(clientDataType *theClientData)
{
int returnStatus = 0;
char *theDirectoryFilename;
dynamicStringDataType theResponse;
dynamicStringDataType mkdFileName;
theDirectoryFilename = getFtpCommandArg("MKD", theClientData->theCommandReceived);
cleanDynamicStringDataType(&theResponse, 1);
cleanDynamicStringDataType(&mkdFileName, 1);
if (theDirectoryFilename[0] == '/')
{
setDynamicStringDataType(&mkdFileName, theDirectoryFilename, strlen(theDirectoryFilename));
}
else
{
setDynamicStringDataType(&mkdFileName, theClientData->login.absolutePath.text, theClientData->login.absolutePath.textLen);
appendToDynamicStringDataType(&mkdFileName, "/", 1);
appendToDynamicStringDataType(&mkdFileName, theDirectoryFilename, strlen(theDirectoryFilename));
}
if (strlen(theDirectoryFilename) > 0)
{
printf("\nThe directory to make is: %s", mkdFileName.text);
returnStatus = mkdir(mkdFileName.text, S_IRWXU | S_IRWXG | S_IRWXO);
}
setDynamicStringDataType(&theResponse, "257 \"", strlen("257 \""));
appendToDynamicStringDataType(&theResponse, theDirectoryFilename, strlen(theDirectoryFilename));
appendToDynamicStringDataType(&theResponse, "\" : The directory was successfully created\r\n", strlen("\" : The directory was successfully created\r\n"));
write(theClientData->socketDescriptor, theResponse.text, theResponse.textLen);
cleanDynamicStringDataType(&theResponse, 0);
cleanDynamicStringDataType(&mkdFileName, 0);
return 1;
}
int parseCommandRmd(clientDataType *theClientData)
{
int returnStatus = 0;
char *theDirectoryFilename;
dynamicStringDataType theResponse;
dynamicStringDataType mkdFileName;
theDirectoryFilename = getFtpCommandArg("RMD", theClientData->theCommandReceived);
cleanDynamicStringDataType(&theResponse, 1);
cleanDynamicStringDataType(&mkdFileName, 1);
if (theDirectoryFilename[0] == '/')
{
setDynamicStringDataType(&mkdFileName, theDirectoryFilename, strlen(theDirectoryFilename));
}
else
{
setDynamicStringDataType(&mkdFileName, theClientData->login.absolutePath.text, theClientData->login.absolutePath.textLen);
appendToDynamicStringDataType(&mkdFileName, "/", 1);
appendToDynamicStringDataType(&mkdFileName, theDirectoryFilename, strlen(theDirectoryFilename));
}
if (strlen(theDirectoryFilename) > 0)
{
printf("\nThe directory to make is: %s", mkdFileName.text);
returnStatus = rmdir(mkdFileName.text);
}
setDynamicStringDataType(&theResponse, "250 The directory was successfully removed\r\n", strlen("250 The directory was successfully removed\r\n"));
write(theClientData->socketDescriptor, theResponse.text, theResponse.textLen);
cleanDynamicStringDataType(&theResponse, 0);
cleanDynamicStringDataType(&mkdFileName, 0);
return 1;
}
int parseCommandCdup(clientDataType *theClientData) int parseCommandCdup(clientDataType *theClientData)
{ {
int i; int i;
@ -500,5 +581,18 @@ int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom
printf("\n Bytes written: %d", toReturn); printf("\n Bytes written: %d", toReturn);
fclose(retrFP); fclose(retrFP);
return toReturn;
}
char *getFtpCommandArg(char * theCommand, char *theCommandString)
{
char *toReturn = theCommandString + strlen(theCommand);
while (toReturn[0] == ' ')
{
toReturn += 1;
}
return toReturn; return toReturn;
} }

View File

@ -33,10 +33,14 @@ int parseCommandTypeI(clientDataType *theClientData);
int parseCommandPasv(ftpDataType * data, int socketId); int parseCommandPasv(ftpDataType * data, int socketId);
int parseCommandList(ftpDataType * data, int socketId); int parseCommandList(ftpDataType * data, int socketId);
int parseCommandRetr(ftpDataType * data, int socketId); int parseCommandRetr(ftpDataType * data, int socketId);
int parseCommandMkd(clientDataType *theClientData);
int parseCommandRmd(clientDataType *theClientData);
int parseCommandStor(ftpDataType * data, int socketId); int parseCommandStor(ftpDataType * data, int socketId);
int parseCommandCwd(clientDataType *theClientData); int parseCommandCwd(clientDataType *theClientData);
int parseCommandCdup(clientDataType *theClientData); int parseCommandCdup(clientDataType *theClientData);
int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom); int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom);
char *getFtpCommandArg(char * theCommand, char *theCommandString);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -65,7 +65,6 @@ void setDynamicStringDataType(dynamicStringDataType *dynamicString, char *theStr
} }
} }
void appendToDynamicStringDataType(dynamicStringDataType *dynamicString, char *theString, int stringLen) void appendToDynamicStringDataType(dynamicStringDataType *dynamicString, char *theString, int stringLen)
{ {
printf("\n Appending in %s --> %s", dynamicString->text, theString); printf("\n Appending in %s --> %s", dynamicString->text, theString);

View File

@ -329,7 +329,7 @@ void runFtpServer(void)
FD_ZERO(&rset); FD_ZERO(&rset);
FD_ZERO(&wset); FD_ZERO(&wset);
FD_ZERO(&eset); FD_ZERO(&eset);
FD_ZERO(&rsetTemp); FD_ZERO(&rsetTemp);
FD_ZERO(&wsetTemp); FD_ZERO(&wsetTemp);
FD_ZERO(&esetTemp); FD_ZERO(&esetTemp);
@ -668,7 +668,84 @@ static int processCommand(int processingElement)
printf("\nSTOR COMMAND RECEIVED"); printf("\nSTOR COMMAND RECEIVED");
toReturn = parseCommandStor(&ftpData, processingElement); toReturn = parseCommandStor(&ftpData, processingElement);
} }
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MKD", strlen("MKD")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "mkd", strlen("mkd")) == 0)
{
printf("\nMKD command received");
toReturn = parseCommandMkd(&ftpData.clients[processingElement]);
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "ABOR", strlen("ABOR")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "abor", strlen("abor")) == 0)
{
printf("\nABOR command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "dele", strlen("dele")) == 0)
{
printf("\nDELE command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "mtdm", strlen("mtdm")) == 0)
{
printf("\nMTDM command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "nlst", strlen("nlst")) == 0)
{
printf("\nNLST command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "PORT", strlen("PORT")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "port", strlen("port")) == 0)
{
printf("\nPORT command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "quit", strlen("quit")) == 0)
{
printf("\nQUIT command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RMD", strlen("RMD")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "rmd", strlen("rmd")) == 0)
{
printf("\nRMD command received");
toReturn = parseCommandRmd(&ftpData.clients[processingElement]);
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RNFR", strlen("RNFR")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "rnfr", strlen("rnfr")) == 0)
{
printf("\nRNFR command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "RNTO", strlen("RNTO")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "rnto", strlen("rnto")) == 0)
{
printf("\nRNTO command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SIZE", strlen("SIZE")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "size", strlen("size")) == 0)
{
printf("\nSIZE command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "APPE", strlen("APPE")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "appe", strlen("appe")) == 0)
{
printf("\nAPPE command received");
//To implement
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "NOOP", strlen("NOOP")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "noop", strlen("noop")) == 0)
{
printf("\nNOOP command received");
//To implement
}
//REST COMMAND //REST COMMAND
//REST 0 //REST 0
//350 Restarting at 0 //350 Restarting at 0

View File

@ -6,6 +6,8 @@
</data> </data>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/> <group>
<file>file:/home/ugo/NetBeansProjects/uFTP/ftpCommandsElaborate.h</file>
</group>
</open-files> </open-files>
</project-private> </project-private>