Code:
// returns the size of file in bytes
int GetFSize( char filename[MAXFILENAME] ) {
struct stat st;
stat( filename, &st );
return st.st_size;
}
/*===================================================================*/
// sends one file specified in the header to the socket_file
int MySend( struct tINFO header, FILE *socket_file ) {
FILE *fr;
if ( (fr = fopen( header.file, "r" )) == NULL )
return -4;
header.action = GET_IT;
header.filesize = GetFSize( header.file ); // determines size of file
if ( fwrite( &header, sizeof(header), 1, socket_file ) != 1 ) // sends the header information
return -2;
char buffer[BLEN] = "";
for (;;) { // sending buffers
int c;
if ((c = fread(&buffer, 1, BLEN, fr)) < 1) // reading from local file
if (!feof(fr))
return -1;
#ifdef TESTING
cout << "buffer length: " << c << endl;
#endif
if (fwrite(buffer, c, 1, socket_file) != 1) // writing to socket
return -2;
if (fflush(socket_file) != 0) // flushing socket
return -2;
if ( feof(fr) )
break;
}
if ( fclose( fr ) != 0 )
return -1;
return 1;
}
/*===================================================================*/
// getting file, until the socket_file is clean
int MyGet( FILE *socket_file ) {
for (;;) {
struct tINFO header;
if ( fread( &header, sizeof(header), 1, socket_file ) != 1)
if ( feof(socket_file) )
break;
else
return -2;
if ( header.action == GET_IT ) { // if receiving regular file
#ifdef TESTING
cout << header.file << endl;
#endif
FILE *fw;
if ( (fw = fopen( header.file, "w" )) == NULL ) // creating file
return -1;
char buffer[BLEN] = "";
int max = (int)ceilf((double)header.filesize / (BLEN));
#ifdef TESTING
cout << "filesize: " << header.filesize << " fsize/BLEN: " << max << " ###" << endl;
#endif
for ( int i = 0; i < max; i++ ) { // receiving buffers
int c;
if ( (c = fread( buffer, 1, (header.filesize-i*BLEN) > BLEN ? BLEN : (header.filesize-i*BLEN), socket_file )) < 1 ) { // reading from socket
if ( feof(socket_file) )
break;
else
return -2;
}
#ifdef TESTING
cout << "recieved buffer: " << c << endl;
#endif
if ( fwrite( buffer, c, 1, fw ) != 1 ) // writing to local file
return -1;
}
if ( fclose( fw ) != 0 )
return -4;
} else if ( header.action == DIRECTORY ) { // if receiving directory
if ( mkdir( header.file, 00777 ) != 0 ) // creating directory
if ( errno != 17 ) // file(dir) exists
return -1;
continue;
}
} // unfinite cycle
return 1;
}
/*===================================================================*/
// Recursively sends files in a directory
int MyRecursiveSend( char filename[MAXFILENAME], FILE *socket_file ) {
struct stat st;
stat( filename, &st );
if ( S_ISDIR(st.st_mode) ) { // IS direcotry
struct dirent *dir_entity = (struct dirent *)malloc( sizeof(struct dirent) );
DIR *dir;
if ( (dir = opendir( filename )) == NULL )
return -1;
struct tINFO header;
header.action = DIRECTORY;
strncpy( header.file, filename, MAXFILENAME );
if ( fwrite( &header, sizeof(header), 1, socket_file ) != 1 )
return -2;
while ( (dir_entity = readdir(dir)) != NULL ) { // browsing direcotry
if ( strcmp( dir_entity->d_name, "." ) != 0 && strcmp( dir_entity->d_name, ".." ) != 0 ) {
char filename_new[MAXFILENAME] = "";
strcat( filename_new, filename );
strcat( filename_new, "/" );
strcat( filename_new, dir_entity->d_name );
MyRecursiveSend( filename_new, socket_file ); // recursive calling for all elements of directory
}
}
free(dir_entity); // frees the memory allocated for dirent
if ( closedir( dir ) != 0)
return -1;
} else { // IS regular file
#ifdef TESTING
cout << filename << endl;
#endif
struct tINFO header;
strncpy( header.file, filename, MAXFILENAME );
MySend( header, socket_file ); // sending the file
}
return 1;
}
and whole aplication is on
Code:
http://rapidshare.com/files/111152607/ftpserver.rar.html