Advertisement
3axap_010

FilesConcatenation.c

May 12th, 2020
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <glob.h>
  5. #include <pthread.h>
  6. #include <limits.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <semaphore.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include <dlfcn.h>
  13.  
  14. #define BUF_SIZE 1024
  15.  
  16. sem_t read_sem;
  17. sem_t write_sem;
  18.  
  19. int (*async_read)(int file, char* buffer, size_t size, off_t offset);
  20. int (*async_write)(int file, char* buffer, size_t size, off_t offset);
  21. void (*print)();
  22.  
  23. char buffer[BUF_SIZE];
  24.  
  25. int errfunc(const char* epath, int errno)
  26. {
  27.     fprintf(stderr, "Glob error: %s, %d\n", epath, errno);
  28.     return 0;
  29. }
  30.  
  31. void* writer_thread_routine(void* args)
  32. {
  33.     off_t offset = 0, bytes_written = 0;
  34.     size_t bytes_numb_to_write = 0;
  35.  
  36.     while (1)
  37.     {
  38.         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  39.  
  40.         sem_wait(&read_sem);
  41.  
  42.         if (strlen(buffer) >= BUF_SIZE)
  43.         {
  44.             bytes_numb_to_write = BUF_SIZE;
  45.         }
  46.         else
  47.         {
  48.             bytes_numb_to_write = strlen(buffer);
  49.         }
  50.  
  51.         bytes_written = async_write(*((int*)args), buffer, bytes_numb_to_write, offset);
  52.         offset += bytes_written;
  53.        
  54.         sem_post(&write_sem);
  55.  
  56.         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  57.     }
  58.  
  59.     pthread_exit(NULL);
  60. }
  61.  
  62. int main(int argc, char** argv)
  63. {
  64.     if (argc != 3)
  65.     {
  66.         printf("Invalid input!\n");
  67.         return 1;
  68.     }
  69.  
  70.     void* handle = dlopen("/home/zakhar/Documents/C/FilesConcat/dll.so", RTLD_LAZY);
  71.     if (!handle)
  72.     {
  73.         fprintf(stderr, "Can't lopad dll\n");
  74.         return 1;
  75.     }
  76.  
  77.     *(void**)(&async_read) = dlsym(handle, "async_read");
  78.     *(void**)(&async_write) = dlsym(handle, "async_write");
  79.  
  80.     if (dlerror())
  81.     {
  82.         fprintf(stderr, "Can't load dll function\n");
  83.         dlclose(handle);
  84.         return 1;
  85.     }
  86.  
  87.     char cur_dir[PATH_MAX];
  88.     getcwd(cur_dir, PATH_MAX);
  89.  
  90.     int output_file = open(argv[2], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  91.  
  92.     if (chdir(argv[1]) == -1)
  93.     {
  94.         fprintf(stderr, "Can't open required directory!\n");
  95.         return 1;
  96.     }
  97.  
  98.     glob_t gl;
  99.     glob("*", GLOB_ERR, errfunc, &gl);
  100.  
  101.     sem_init(&write_sem, 0, 1);
  102.     sem_init(&read_sem, 0, 0);
  103.  
  104.     pthread_t thread;
  105.     pthread_create(&thread, NULL, writer_thread_routine, (void*)&output_file); 
  106.  
  107.     off_t offset = 0;
  108.     off_t bytes_read = 0;
  109.  
  110.     for (int i = 0; i < gl.gl_pathc; i++)
  111.     {
  112.         int source;
  113.  
  114.         if ((source = open(gl.gl_pathv[i], O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
  115.         {
  116.             fprintf(stderr, "Can't open file %s\n", gl.gl_pathv[i]);
  117.             break;
  118.         }
  119.  
  120.         do
  121.         {
  122.             sem_wait(&write_sem);
  123.  
  124.             bytes_read = async_read(source, buffer, BUF_SIZE, offset);
  125.             offset += bytes_read;
  126.  
  127.             if (bytes_read < BUF_SIZE)
  128.             {
  129.                 buffer[bytes_read] = '\0';
  130.             }
  131.            
  132.             sem_post(&read_sem);
  133.         } while (bytes_read == BUF_SIZE);
  134.  
  135.         offset = 0;
  136.         bytes_read = 0;
  137.  
  138.         close(source);
  139.     }
  140.  
  141.     sem_wait(&write_sem);
  142.     pthread_cancel(thread);
  143.  
  144.     close(output_file);
  145.     chdir(cur_dir);
  146.     globfree(&gl);
  147.     sem_close(&write_sem);
  148.     sem_close(&read_sem);
  149.     dlclose(handle);
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement