This is the part of the ftp cmd "LIST" :
Code:
if(recv(newsockd, buffer, 5, 0) < 0){
perror("Errore nella ricezione comando LIST");
onexit(newsockd, sockd, 0, 2);
}
other = strtok(buffer, "\n");
if(strcmp(other, "LIST") == 0){
printf("Ricevuta richiesta LIST\n");
} else onexit(newsockd, sockd, 0, 2);
count = file_list("./", &files);
if((fp_list = fopen("listfiles.txt", "w")) == NULL){
perror("Impossibile aprire il file per la scrittura LIST");
onexit(newsockd, sockd, 0, 2);
}
for(i=0; i < count; i++){
if(strcmp(files[i], "DIR ..") == 0 || strcmp(files[i], "DIR .") == 0) continue;
else{
fprintf(fp_list, "%s\n", files[i]);
}
}
fclose(fp_list);
if((fpl = open("listfiles.txt", O_RDONLY)) < 0){
perror("open file with open");
onexit(newsockd, sockd, 0, 2);
exit(1);
}
if(fstat(fpl, &fileStat) < 0){
perror("Errore fstat");
onexit(newsockd, sockd, fpl, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
perror("Errore durante l'invio grande file list");
onexit(newsockd, sockd, fpl, 3);
}
rc_list = sendfile(newsockd, fpl, &offset_list, fileStat.st_size);
if(rc_list == -1){
perror("Invio file list non riuscito");
onexit(newsockd, sockd, fpl, 3);
}
if((uint32_t)rc_list != fsize){
fprintf(stderr, "Trasferimento incompleto: %d di %d bytes inviati\n", rc_list, (int)fileStat.st_size);
onexit(newsockd, sockd, fpl, 3);
}
close(fpl);
if(remove( "listfiles.txt" ) == -1 ){
perror("errore cancellazione file");
onexit(newsockd, sockd, 0, 2);
}
memset(buffer, 0, sizeof(buffer));
this is the function file_list:
Code:
#include "prototypes.h"
uint32_t file_list(char *path, char ***ls){
DIR *dp;
struct stat fileStat;
struct dirent *ep = NULL;
uint32_t len, count = 0;
int file = 0;
*ls = NULL;
dp = opendir (path);
if(dp == NULL){
fprintf(stderr, "Non esiste la directory: %s\n", path);
exit(1);
}
ep = readdir(dp);
while(NULL != ep){
count++;
ep = readdir(dp);
}
rewinddir(dp);
*ls = calloc(count, sizeof(char *));
count = 0;
ep = readdir(dp);
while(ep != NULL){
if((file = open(ep->d_name, O_RDONLY)) < 0){
perror("apertura file");
exit(1);
}
if(fstat(file, &fileStat) != 0){
perror("filestat");
free(*ls);
close(file);
exit(EXIT_FAILURE);
}
close(file);
if(S_ISDIR(fileStat.st_mode)){
len = strlen(ep->d_name);
(*ls)[count] = malloc(len+5); /* lunghezza stringa + "DIR \n" */
strcpy((*ls)[count], "DIR "); /* copio DIR */
strcat((*ls)[count++], ep->d_name); /* concateno la stringa DIR con il nome della dir */
ep = readdir(dp);
}
else{
(*ls)[count++] = strdup(ep->d_name);
ep = readdir(dp);
}
}
(void)closedir(dp);
return count;
}
What is the problem?
When i call the LIST cmd the 1st time it is all ok but when i call it the 2nd time the files is partially sent and i don't know why O.o