Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <dlfcn.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <aio.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <errno.h>
- #define BUFFER_SIZE 1024
- void makeFileList(char*);
- void* reader(void*);
- void* writer(void*);
- void clearBuffer();
- void createThread( pthread_t* , void*(*function)(void*) , void* );
- void* openLibrary(char* libPath);
- int validateArguments(int argCount, char* appName);
- void createMutexes();
- void initReadCb(int fid);
- void initWriteCb(int fid);
- // Extern functions from .so library
- int (*ARead)(struct aiocb*);
- int (*AWrite)(struct aiocb*);
- pthread_mutex_t r_mutex, w_mutex;
- std::vector<std::string> FileList;
- char buffer[BUFFER_SIZE];
- int isReadingEnded = 0;
- struct aiocb read_cb, write_cb;
- int read_len = 0, write_len = 0;
- int main(int argc, char** argv)
- {
- if (validateArguments(argc, argv[0]) != 1)
- {
- return(EXIT_FAILURE);
- }
- void* library = openLibrary(argv[1]);
- createMutexes();
- makeFileList(argv[2]);
- pthread_mutex_lock(&w_mutex);
- pthread_t pwriter, preader;
- createThread( &pwriter, &writer, (void*)argv[3] );
- createThread( &preader, &reader, (void*)argv[2] );
- pthread_join (preader, 0);
- pthread_join (pwriter, 0);
- pthread_mutex_destroy(&r_mutex);
- pthread_mutex_destroy(&w_mutex);
- dlclose(library);
- puts(" - END - ");
- return 0;
- }
- int validateArguments(int argCount, char* appName)
- {
- if ( argCount != 4 )
- {
- printf("\n %s [library_path] [input_folder_path] [output_file_path]\n", appName );
- return 0;
- }
- return 1;
- }
- void createMutexes()
- {
- pthread_mutex_init(&r_mutex, NULL);
- pthread_mutex_init(&w_mutex, NULL);
- }
- void createThread( pthread_t* id, void*(*function)(void*) , void* args )
- {
- int result = pthread_create(id ,NULL, function, args);
- }
- void* openLibrary(char* libPath)
- {
- void *lid = dlopen(libPath, RTLD_LAZY);
- ARead = dlsym(lid, "Read");
- AWrite = dlsym(lid, "Write");
- return lid;
- }
- void makeFileList(char* inputDir)
- {
- DIR *dp;
- struct dirent *dirp;
- if((dp = opendir(inputDir)) == NULL)
- {
- std::cout << "Error(" << strerror(errno) << ") opening " << inputDir << std::endl;
- return errno;
- }
- while ((dirp = readdir(dp)) != NULL)
- {
- FileList.push_back( std::string(inputDir) + std::string(dirp->d_name));
- if ( opendir(FileList.back().c_str()) != NULL )
- {
- FileList.pop_back();
- }
- }
- closedir(dp);
- }
- void* reader(void *pp)
- {
- char* currentFilePath = NULL;
- for( int i = 0, file_count = FileList.size(); i < file_count; ++i)
- {
- currentFilePath = FileList[i].c_str();
- int fid = open(currentFilePath, O_RDONLY);
- if(fid == -1)
- {
- continue;
- }
- clearBuffer();
- initReadCb(fid);
- do
- {
- pthread_mutex_lock(&r_mutex);
- printf("Reading %s\n", currentFilePath);
- read_len = ARead(&read_cb);
- read_cb.aio_offset += read_len;
- pthread_mutex_unlock(&w_mutex);
- }while(read_len == BUFFER_SIZE);
- close(fid);
- }
- isReadingEnded = 1;
- pthread_mutex_unlock(&r_mutex);
- }
- void* writer(void* arg)
- {
- char* outputFile = (char*)arg;
- int fid = open(outputFile, O_WRONLY | O_CREAT | O_APPEND | O_TRUNC);
- if(fid == -1)
- {
- return;
- }
- initWriteCb(fid);
- while(!isReadingEnded)
- {
- pthread_mutex_lock(&w_mutex);
- write_cb.aio_nbytes = read_len;
- printf("%ld bytes writed\n", AWrite(&write_cb));
- pthread_mutex_unlock(&r_mutex);
- }
- close(fid);
- }
- void clearBuffer()
- {
- bzero(buffer, BUFFER_SIZE-1);
- }
- void initReadCb(int fid)
- {
- read_cb.aio_fildes = fid;
- read_cb.aio_offset = 0;
- read_cb.aio_buf = &buffer;
- read_cb.aio_nbytes = BUFFER_SIZE;
- read_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
- }
- void initWriteCb(int fid)
- {
- write_cb.aio_fildes = fid;
- write_cb.aio_offset = 0;
- write_cb.aio_buf = &buffer;
- write_cb.aio_sigevent.sigev_notify = SIGEV_NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement