Skip to content

Commit

Permalink
rput implemented. mkdir, lmkdir, lcd re-implemented for more robustness.
Browse files Browse the repository at this point in the history
  • Loading branch information
rovinbhandari committed Mar 12, 2012
1 parent c61328f commit 4de9635
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
11 changes: 5 additions & 6 deletions client_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ int main(int argc, char* argv[])
fprintf(stderr, "No path given.\n");
break;
case LCD:
if((x = chdir(*cmd->paths)) == -1)
fprintf(stderr, "Wrong path.\n");
if(cmd->npaths)
command_lcd(*cmd->paths);
else
fprintf(stderr, "No path given.\n");
break;
case PWD:
command_pwd(chp, data, sfd_client);
Expand Down Expand Up @@ -112,10 +114,7 @@ int main(int argc, char* argv[])
break;
case LMKDIR:
if(cmd->npaths)
{
if((x = mkdir(*cmd->paths, 0777)) == -1)
fprintf(stderr, "Error in creating directory.\n");
}
command_lmkdir(*cmd->paths);
else
fprintf(stderr, "No path to directory given.\n");
break;
Expand Down
2 changes: 2 additions & 0 deletions client_ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ void command_mgetwild(struct packet*, struct packet*, int);
void command_mputwild(struct packet*, struct packet*, int, char*);
void command_rput(struct packet*, struct packet*, int);
void command_mkdir(struct packet*, struct packet*, int, char*);
void command_lmkdir(char*);
void command_lcd(char*);

43 changes: 35 additions & 8 deletions client_ftp_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ void command_rput(struct packet* chp, struct packet* data, int sfd_client)
append_path(cmd, e->d_name);
else if(e->d_type == 4 && strcmp(e->d_name, ".") && strcmp(e->d_name, ".."))
{
// send packet for making directory on the server side.

if((x = chdir(e->d_name)) == -1)
er("chdir()", x);
command_mkdir(chp, data, sfd_client, e->d_name);

command_cd(chp, data, sfd_client, e->d_name);
command_lcd(e->d_name);

command_rput(chp, data, sfd_client);
if((x = chdir("..")) == -1)
er("chdir()", x);

command_cd(chp, data, sfd_client, "..");
command_lcd("..");
}
closedir(d);
command_mput(chp, data, sfd_client, cmd->npaths, cmd->paths);
Expand All @@ -365,9 +367,34 @@ void command_mkdir(struct packet* chp, struct packet* data, int sfd_client, char
if((x = recv(sfd_client, data, size_packet, 0)) <= 0)
er("recv()", x);
chp = ntohp(data);
if(chp->type == INFO && chp->comid == MKDIR && !strcmp(chp->buffer, "success"))
;
if(chp->type == INFO && chp->comid == MKDIR)
{
if(!strcmp(chp->buffer, "success"))
printf("\tCreated directory on server.\n");
else if(!strcmp(chp->buffer, "already exists"))
printf("\tDirectory already exitst on server.\n");
}
else
fprintf(stderr, "\tError executing command on the server.\n");
}

void command_lmkdir(char* dirname)
{
DIR* d = opendir(dirname);
if(d)
{
printf("\tDirectory already exists.\n");
closedir(d);
}
else if(mkdir(dirname, 0777) == -1)
fprintf(stderr, "Error in creating directory.\n");
else
printf("\tCreated directory.\n");
}

void command_lcd(char* path)
{
if(chdir(path) == -1)
fprintf(stderr, "Wrong path.\n");
}

4 changes: 1 addition & 3 deletions server_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ void* serve_client(void* info)
command_cd(shp, data, sfd_client, x == -1 ? "fail" : "success");
break;
case MKDIR:
if((x = mkdir(shp->buffer, 0777)) == -1)
fprintf(stderr, "Wrong path.\n");
command_mkdir(shp, data, sfd_client, x == -1 ? "fail" : "success");
command_mkdir(shp, data, sfd_client);
break;
case LS:
if(!getcwd(lpwd, sizeof lpwd))
Expand Down
2 changes: 1 addition & 1 deletion server_ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ void command_cd(struct packet*, struct packet*, int, char*);
void command_ls(struct packet*, struct packet*, int, char*);
void command_get(struct packet*, struct packet*, int);
void command_put(struct packet*, struct packet*, int);
void command_mkdir(struct packet*, struct packet*, int, char*);
void command_mkdir(struct packet*, struct packet*, int);

16 changes: 15 additions & 1 deletion server_ftp_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,22 @@ void command_put(struct packet* shp, struct packet* data, int sfd_client)
}
}

void command_mkdir(struct packet* shp, struct packet* data, int sfd_client, char* message)
void command_mkdir(struct packet* shp, struct packet* data, int sfd_client)
{
char message[LENBUFFER];
DIR* d = opendir(shp->buffer);
if(d)
{
strcpy(message, "already exists");
closedir(d);
}
else if(mkdir(shp->buffer, 0777) == -1)
{
fprintf(stderr, "Wrong path.\n");
strcpy(message, "fail");
}
else
strcpy(message, "success");
int x;
shp->type = INFO;
strcpy(shp->buffer, message);
Expand Down

0 comments on commit 4de9635

Please sign in to comment.