Ejemplo n.º 1
0
int executeServerCommand(int msfd, struct url *url, struct config *cfg) {
	/* In base al comando inserito, viene chiamata la rispettiva funzione. */

	// Si controlla l'esistenza dell'identificativo e si cambia directory.
	int returnCode = checkIdentifier(url->identifier, cfg->rootdir);

	if (returnCode != BAD_IDENTIFIER) {
		char* identifierPath = createFullPath(cfg->rootdir, url->identifier);
		char* command = url->command;

		if (strcmp(command, "mkdir") == 0) {
			returnCode = makeDir(identifierPath, url->argument);
		} else if (strcmp(command, "put") == 0) {
			returnCode = putf(msfd, identifierPath, url->argument, url->optionalArgument, "wb");
		} else if (strcmp(command, "puta") == 0) {
			returnCode = putf(msfd, identifierPath, url->argument, url->optionalArgument, "ab");
		} else if (strcmp(command, "move") == 0) {
			returnCode = movefd(identifierPath, url->argument, url->optionalArgument);
		} else if (strcmp(command, "dele") == 0) {
			returnCode = deletefd(identifierPath, url->argument);
		} else if (strcmp(command, "get") == 0) {
			returnCode = getf(msfd, identifierPath, url->argument);
		} else {
			log_err("Unknown_command");
			returnCode = MALFORMED_COMMAND;
		}

		free(identifierPath);
	} else {
		if (strcmp(url->command, "get") == 0) {
			/* Se l'identificativo non è valido, bisogna comunque
			avvertire il client che non sta per ricevere alcun file. */
			sendEntireFile(msfd, NULL);
		} else if (strcmp(url->command, "put") == 0 || strcmp(url->command, "puta") == 0) {
			/* E allo stesso modo devo avvertire il client, nel caso in cui
			lui voglia effettuare un put ma l'identificativo è errato. */
			sendReturnCode(msfd, returnCode);
		}
	}

	log_info("Return code: %d", returnCode);
	return returnCode;
}
void cmd_Up(char str[10],char strname[14])
{
	int l,i,itemp;
	char *a;
	l=strlen(strname);
	if(l>=14)
	{
		printf("文件名过长!\n");
		return;
	}
	if(strcmp(str,"cd")==0)
	{
		if(Enterdir(strname)>=0)
		{

			l=strlen(cmdhead);

		//	printf("进入目录成功!\n");
			if(strcmp(strname,"..")==0)
			{
					if(strcmp(cmdhead,"root")!=0)
					{
						i=0;
						while(cmdhead[i]!='\0')
						{

							if(cmdhead[i]=='\\')
								{
									itemp=i;

								}
							i++;
						}
						cmdhead[itemp]='\0';

					}

			}
			else if(strcmp(strname,".")!=0)
			{

				strcat(cmdhead,"\\");
				strcat(cmdhead,strname);
			}
		}
		else
		{
			printf("进入目录失败!\n");
		}

	}
	else if(strcmp(str,"create")==0)
	{
		if(create(strname))
			{
			//	printf("文件创建成功!\n");
			}
			else
			{
				printf("文件创建失败!\n");
			}

	}
	else if(strcmp(str,"read")==0)
	{
		char * buf;
		buf=ReadFile(strname);
		if(buf==NULL)
		{
			printf("读取失败!\n");
		}
		else
		{
			printf("所读文件内容:\n%s\n",buf);
		}

		free(buf);
	}
	else if(strcmp(str,"cdir")==0)
	{
		if(cdir(strname))
		{
		//	printf("文件夹创建成功!\n");
		}
		else
		{
			printf("文件夹创建失败!\n");
		}
	}
	else if(strcmp(str,"adduser")==0)
	{
		if(adduser(strname))
		{
		//	printf("文件夹创建成功!\n");
		}
		else
		{
			printf("用户创建失败!\n");
		}
	}
	else if(strcmp(str,"del")==0)
	{
		if(deletefd(strname))
		{
		//	printf("%s成功删除!\n",strname);
		}
		else
		{
			printf("%s删除失败!\n",strname);
		}


	}
	else if(strcmp(str,"write")==0)
	{
		if(mywrite(strname))
		{
				printf("%s保存成功!\n",strname);
		}
		else
		{
				printf("%s保存失败!\n",strname);
		}

	}
	
	else
	{
		printf("错误命令!\n");
	}
}