1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| /** *********************************************************************************************************************************************************************************************************************************
* \fn t_boolean file_copy(const char *src, const char *dst)
***********************************************************************************************************************************************************************************************************************************
* \brief Copies a file
***********************************************************************************************************************************************************************************************************************************
* \param src[const char*] → Path to the source file
* \param dst[const char*] → Path to the destination file
* \return copy_status[t_boolean] → Status of the function after its execution
*
**********************************************************************************************************************************************************************************************************************************/
t_boolean file_copy( const char *src, const char *dst )
{
t_boolean copy_status = FALSE;
if ( src && dst )
{
t_uint64 f_src_size = file_length( src );
if ( file_exist( src ) == TRUE && file_exist( dst ) == FALSE )
{
FILE *f_src = NULL; FILE *f_dst = NULL;
if ( ( f_src = file_open( src, "rb" ) ) && ( f_dst = file_open( dst,"wb" ) ) )
{
double t0 = GetTickCount();
t_file_unit *file_unit = NULL;
if ( ( file_unit = file_copy_get_size_unit( src ) ) )
{
pthread_t t;
t_uint64 read_byte, written_byte, total_written_byte=0;
char *buffer = NULL;
if ( ( buffer = allocate_1D_c( MBYTE ) ) )
{
t_file_copy_thread *fct = file_copy_thread_init( dst, file_unit, f_src_size, src, t0, 0 );
while ( 0 < ( read_byte = fread ( buffer, BYTE, MBYTE, f_src ) ) )
{
written_byte = fwrite( buffer, read_byte, 1, f_dst );
total_written_byte += written_byte*read_byte;
file_copy_thread_update( fct, total_written_byte );
pthread_create (&t, NULL, file_copy_thread, (t_file_copy_thread*)fct );
pthread_join( t, NULL );
}
file_copy_thread_update( fct, total_written_byte );
pthread_create( &t, NULL, file_copy_thread_end, (t_file_copy_thread*)fct) ;
pthread_join( t, NULL );
pthread_exit(t);
deallocate_1D_c( buffer );
}
else fprintf(logger, "\nfile.h::file_copy --> allocate_1D_c returned NULL");
}
else fprintf(logger,"\nfile.h::file_copy --> file_copy_get_size_unit returned NULL");
file_close( f_src );
file_close( f_dst );
}
else
{
if ( !f_src )
fprintf(logger,"\nfile.h::file_copy --> f_src=file_open returned NULL");
if ( !f_dst )
fprintf(logger,"\nfile.h::file_copy --> f_dst=file_open returned NULL");
}
}
else fprintf(logger,"\nfile.h::file_copy --> \"%s\" doesn't exist or \"%s\" already exists", src, dst);
t_uint64 f_dst_size = file_length( dst );
if ( f_dst_size == f_src_size )
copy_status=TRUE;
}
else
{
if( !src )
fprintf(logger,"\nfile.h::file_copy --> src is NULL");
if( !dst )
fprintf(logger,"\nfile.h::file_copy --> dst is NULL");
}
return copy_status;
} |
Partager