XAgent-Smith

Thread_for_copyfiles

Nov 24th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #include <errno.h>
  2. #include <sys/fcntl.h>
  3. #include <pthread.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <stdtool.c>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define SIZE PIPE_BUF
  11.  
  12. #define PERM (S_IRUSR |S_IWUSR |S_IRGRP|S_IROTH )
  13.  
  14. #define O_RWC (O_RDONLY | O_CREAT | O_WRONLY | O_TRUNC )
  15.  
  16. #define READ O_RDONLY
  17.  
  18. void *fcopy( void *arg )
  19. {
  20.  
  21. int fd_to    = *((int*)arg)   ;
  22.  
  23.  
  24. int fd_from  = *((int*)arg+1) ;
  25. char buf[SIZE]     ;
  26.  
  27. int bytes_reades = 0 ;
  28. int bytes_writen = 0 ;
  29.  
  30. *((int*)arg+2) = 0 ;
  31.  
  32. for(;;)
  33. {
  34.  
  35. if((bytes_reades = read(fd_from , buf , SIZE) ) <0&&errno == EINTR )
  36. {
  37.     continue;
  38. }
  39. if(bytes_reades == 0 )
  40. {
  41.  
  42. break ;
  43. }
  44.  
  45. if(bytes_reades == -1  )
  46. {
  47. perror("unable to read file  " ) ;
  48.      *((int*)arg+2) = -1         ;
  49.      pthread_exit(NULL)          ;
  50. }
  51. if( ((bytes_writen = write(fd_to , buf , SIZE ) )<0) && errno==EINTR )
  52. {
  53.     continue ;
  54. }
  55.  
  56. if(bytes_writen == -1){
  57. perror("unable to read file " ) ;
  58.      *((int*)arg+2) = -1 ;
  59.      pthread_exit(NULL) ;
  60. }
  61.  
  62. if(bytes_writen == 0) break;
  63.  
  64. *((int*)arg+2) = *((int*)arg+2) + bytes_writen ;
  65.  
  66. }
  67.  
  68. pthread_exit(NULL) ;
  69. }
  70.  
  71. int file_copy(char *to ,char *from)
  72.  
  73. {
  74.   pthread_t thread1    ;
  75.   pthread_attr_t attr1 ;
  76.  
  77.   int *arg     ;
  78.   int fd1_to   ;
  79.   int fd2_from ;
  80.  
  81. if( ( fd1_to = open(to, O_RWC ,PERM) ) < 0 )
  82. {
  83.     fprintf(stderr,"%s[at line (%d) ] Error unable to open file [%s]",__FILE__,__LINE__,to) ;
  84.  
  85.       exit(-1) ;
  86. }
  87.  
  88. if((fd2_from = open(from,READ ))<0)
  89. {
  90.     fprintf(stderr,"%s[at line (%d) ] Error unable to open file [%s]",__FILE__,__LINE__,from) ;
  91.        close(fd1_to);
  92.       exit(-1) ;
  93. }
  94. if( (arg = (int*)malloc( sizeof( int )*(3)) ) == NULL )
  95. {
  96. fprintf(stderr,"\n%s[at line (%d) ] Error unable to allocat memory \n ",__FILE__,__LINE__)              ;
  97.  
  98.       close(fd1_to)   ;
  99.       close(fd2_from) ;
  100.       exit(-1)        ;
  101. }
  102.  
  103. *arg = fd1_to          ;
  104.  
  105. *(arg + 1 ) = fd2_from ;
  106.  
  107.  
  108. if(pthread_attr_init(&attr1))
  109. {
  110.   perror("Unable to initiallize thread atrribute object");
  111.       close(fd1_to)   ;
  112.       close(fd2_from) ;
  113.      safe_free(arg)   ;
  114.      exit(-1)         ;
  115. }
  116.  
  117. if(pthread_attr_setdetachstate( &attr1 , PTHREAD_CREATE_JOINABLE ))
  118. {
  119. perror("unable to getdetachstate");
  120. }
  121.  
  122. if(pthread_attr_setscope(&attr1,PTHREAD_SCOPE_SYSTEM))
  123. {
  124.     fprintf(stderr, "Error failed to get system scope for thread") ;
  125. }
  126.  
  127. if(pthread_create(&thread1, &attr1 ,(void * (*)( void *) )fcopy, (void *)arg))
  128. {
  129.  
  130. fprintf(stderr,"%s[at line (%d) ] Error unable to create thread ",__FILE__,__LINE__) ;
  131.       close(fd1_to)   ;
  132.       close(fd2_from) ;
  133.       safe_free( arg ) ;
  134.       exit(-1) ;
  135.  
  136. }
  137.  
  138. if(pthread_join(thread1 , NULL ) )
  139. {
  140.     perror("Unable to join thread") ;
  141.      close(fd1_to)   ;
  142.       close(fd2_from) ;
  143.      safe_free(arg) ;
  144.      exit(-1) ;
  145. }
  146.  
  147.  
  148. if(*(arg+2) == -1)
  149. {
  150.     return -1 ;
  151. }
  152.  
  153. printf("\nFile Copied : Bytes copied = [%u]bytes\n",*(arg+2)) ;
  154.  
  155.     close(fd1_to)   ;
  156.     close(fd2_from) ;
  157.      safe_free(arg) ;
  158.  
  159.   return 0 ;
  160. }
  161.  
  162. int main(int argc ,char **argv )
  163. {
  164.  
  165. if(argc < 2)
  166.   {
  167.     fprintf(stderr,"%s Error : usage file to file ",__FILE__) ;
  168.      exit(-1) ;
  169.   }
  170.  
  171. if(file_copy(*(argv+1) , *(argv+2)))
  172. {
  173.  
  174. perror("unable to copy data" ) ;
  175.  
  176. exit(-1) ;
  177.  
  178. }
  179.  
  180. return 0;
  181.  
  182. }
Add Comment
Please, Sign In to add comment