diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index dd84ea7..3205926 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -12,6 +12,7 @@ A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behavior: + 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' @@ -24,15 +25,17 @@ A clear and concise description of what you expected to happen. If applicable, add screenshots to help explain your problem. **Desktop (please complete the following information):** - - OS: [e.g. iOS] - - Browser [e.g. chrome, safari] - - Version [e.g. 22] + +- OS: [e.g. iOS] +- Browser [e.g. chrome, safari] +- Version [e.g. 22] **Smartphone (please complete the following information):** - - Device: [e.g. iPhone6] - - OS: [e.g. iOS8.1] - - Browser [e.g. stock browser, safari] - - Version [e.g. 22] + +- Device: [e.g. iPhone6] +- OS: [e.g. iOS8.1] +- Browser [e.g. stock browser, safari] +- Version [e.g. 22] **Additional context** Add any other context about the problem here. diff --git a/.github/workflows/File-Sorter-Core Build.yml b/.github/workflows/File-Sorter-Core Build.yml index 1dd7eff..5380599 100644 --- a/.github/workflows/File-Sorter-Core Build.yml +++ b/.github/workflows/File-Sorter-Core Build.yml @@ -19,20 +19,20 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Configure CMake - # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. - # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - - name: Build - # Build your program with the given configuration - run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} + - name: Build + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - - name: Test - working-directory: ${{github.workspace}}/build - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C ${{env.BUILD_TYPE}} + - name: Test + working-directory: ${{github.workspace}}/build + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C ${{env.BUILD_TYPE}} diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml index eaecfee..89e2060 100644 --- a/.github/workflows/greetings.yml +++ b/.github/workflows/greetings.yml @@ -1,6 +1,6 @@ name: Greetings -on: [pull_request, issues] +on: [ pull_request, issues ] jobs: greeting: @@ -9,8 +9,8 @@ jobs: issues: write pull-requests: write steps: - - uses: actions/first-interaction@v1 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - issue-message: 'Thanks for the issue! i will see it as soon as possible and i will try to give you an answer or make a solution.' - pr-message: 'Thank you for your contribution to this project! i will see and check your request as soon as possible, until then, make your coffee and keep coding! ' + - uses: actions/first-interaction@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + issue-message: 'Thanks for the issue! i will see it as soon as possible and i will try to give you an answer or make a solution.' + pr-message: 'Thank you for your contribution to this project! i will see and check your request as soon as possible, until then, make your coffee and keep coding! ' diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d8e197..4588fda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,7 @@ file(WRITE config/config.conf debug_log\ 0\n default_dir_path\ /home/$ENV{USER}/default_sorter_path/\n enable_default_path\ 1\n - move_files_without_ext\ 0\n + move_files_without_ext\ 0\n \n [check]\n \n @@ -54,8 +54,8 @@ file(WRITE config/config.conf [targets]\n \n [done]\n - [exclude]\n - [done]) + [exclude]\n + [done]) file(WRITE service/file-sorter.service [Unit]\n diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 0fa9d9c..cc8b1cf 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -106,7 +106,7 @@ Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community -standards, including sustained inappropriate behavior, harassment of an +standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within diff --git a/README.md b/README.md index 7a8b950..7cd69b7 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,13 @@ # Introduction -Automatically transfer files, which are scattered outside of their folders, to the folders to which they belong using a simple config file and a tool. +Automatically transfer files, which are scattered outside of their folders, to the folders to which they belong using a +simple config file and a tool. # Installation requirements + The following packages must be installed.
+ ``` sudo apt-get install build-essential cmake git gcc ``` @@ -62,22 +65,23 @@ move_files_without_ext 0 [done] ``` + The config is stored in the following location:
`~/.local/share/file_sorter/config/config.conf` The information is as follows.
-Field | Description ----------|-------------- -`check_interval` | The time ( seconds ) between checks (for new files etc). -`parse_interval` | The time ( seconds ) to read the config file again for any changes. -`debug_log` | For debugging. To enter debug mode, the value of the debugLog field must be changed to 1. -`default_dir_path` | If a file is found outside of a folder and no specific location has been specified to which it should be moved, then it will go to default_dir_path. -`enable_default_path` | If this option is enabled, then any file that do not have a specific location to which they should be sent will be sent to the default. -`move_files_without_ext` | If this option is enabled, then the sorter will move files that has no extension, otherwise those files will be ignored. -`[check]` | This field includes all locations where the program will look for files. Each location that enters this field must be entered before `[done_check]`. -`[targets]` | This field contains all the file extensions and all the locations that these files should be sent to. Each line in this field consists of two elements that are separated by a space. The first element is the extension of the file and the second is the location where this file should be sent. Also each new line must be entered before `[done_targets]`. -`[exclude]` | This field contains all the file extensions and all the locations that these files should be ignored. Each line in this field consists of two elements that are separated by a space. The first element is the extension of the file and the second is the location where it must ignore a file. If a file has to be ignored in any location, then * must be placed instead of location. Also each new line must be entered before `[done]`. + Field | Description +--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + `check_interval` | The time ( seconds ) between checks (for new files etc). + `parse_interval` | The time ( seconds ) to read the config file again for any changes. + `debug_log` | For debugging. To enter debug mode, the value of the debugLog field must be changed to 1. + `default_dir_path` | If a file is found outside of a folder and no specific location has been specified to which it should be moved, then it will go to default_dir_path. + `enable_default_path` | If this option is enabled, then any file that do not have a specific location to which they should be sent will be sent to the default. + `move_files_without_ext` | If this option is enabled, then the sorter will move files that has no extension, otherwise those files will be ignored. + `[check]` | This field includes all locations where the program will look for files. Each location that enters this field must be entered before `[done_check]`. + `[targets]` | This field contains all the file extensions and all the locations that these files should be sent to. Each line in this field consists of two elements that are separated by a space. The first element is the extension of the file and the second is the location where this file should be sent. Also each new line must be entered before `[done_targets]`. + `[exclude]` | This field contains all the file extensions and all the locations that these files should be ignored. Each line in this field consists of two elements that are separated by a space. The first element is the extension of the file and the second is the location where it must ignore a file. If a file has to be ignored in any location, then * must be placed instead of location. Also each new line must be entered before `[done]`. An example of `[check]`:
@@ -114,12 +118,14 @@ In the example above the first part consists of the extensions `.py` and `.c` an locations `/home/username/Documents/py/` and `*`. # Tool + Due to the nature of this program and this config i made a tool that helps the user to do the following:
- - Add locations to the config check field.
- - Be able to add targets.
- - To be able to delete check and targets.
- - To be able to see the check and targets.
- - To be able to change the program settings.

+ +- Add locations to the config check field.
+- Be able to add targets.
+- To be able to delete check and targets.
+- To be able to see the check and targets.
+- To be able to change the program settings.

The tool described above can be found here:
https://github.com/EmbeddedCat/file-sorter-terminal-tool @@ -130,11 +136,13 @@ The program can be run using `systemd`. To start the program from `systemd` the ``` systemctl --user start file-sorter``` # Compatible operating systems + - [X] Linux - [ ] Windows - [ ] Mac # Contributors + diff --git a/config/config.conf b/config/config.conf index c3a76d8..628dba9 100644 --- a/config/config.conf +++ b/config/config.conf @@ -1,10 +1,10 @@ [basic_config] -check_interval 3 -parse_interval 5 +check_interval 3000 +parse_interval 5000 debug_log 0 -default_dir_path /home/username/default_sorter_path/ +default_dir_path /home/embeddedcat/default_sorter_path/ enable_default_path 1 -move_files_without_ext 1 +move_files_without_ext 0 [check] @@ -14,4 +14,4 @@ move_files_without_ext 1 [done] [exclude] -[done] +[done] \ No newline at end of file diff --git a/include/config.h b/include/config.h index 27cd817..b2fca9b 100644 --- a/include/config.h +++ b/include/config.h @@ -4,33 +4,33 @@ #include -#define FAILED_TO_PARSE -0x1 +#define FAILED_TO_PARSE -0x1 // config options. struct options { - unsigned int o_check_interval; // check interval option. - unsigned int o_parse_interval; // parse interval option. - unsigned int o_debug_log:1; // debug log option. - char *o_default_path; // default path option. - unsigned int o_enable_default:1; // enable default path option. - unsigned int o_move_no_ext:1; // move files that has no extention. + unsigned int o_check_interval; // check interval option. + unsigned int o_parse_interval; // parse interval option. + unsigned int o_debug_log: 1; // debug log option. + char *o_default_path; // default path option. + unsigned int o_enable_default: 1; // enable default path option. + unsigned int o_move_no_ext: 1; // move files that has no extention. }; // config lists. struct lists { - char *(*l_check_list); // the check list of the config. - char *(*l_target_list); // the targests of the config. - char *(*l_exclude_list); // the excludes of the config. + char *(*l_check_list); // the check list of the config. + char *(*l_target_list); // the targests of the config. + char *(*l_exclude_list); // the excludes of the config. }; // the config file. struct config { - struct options c_options; // the options of the config. - struct lists c_lists; // the lists of the config. + struct options c_options; // the options of the config. + struct lists c_lists; // the lists of the config. }; @@ -40,7 +40,7 @@ struct config **/ static void inline init_config(struct config *config) { - memset(config, 0x0, sizeof(struct config)); + memset(config, 0x0, sizeof(struct config)); } /** diff --git a/include/logger.h b/include/logger.h index 5061894..190a439 100644 --- a/include/logger.h +++ b/include/logger.h @@ -14,6 +14,4 @@ extern void logger(const char *msg, int type); - - #endif diff --git a/main.c b/main.c index 7d1462a..f4ab075 100644 --- a/main.c +++ b/main.c @@ -10,23 +10,21 @@ #endif - - int main(int argc, char *argv[]) { #ifdef __unix__ - struct config config_p; - // initialzize the config. - init_config(&config_p); + struct config config_p; + // initialzize the config. + init_config(&config_p); - // parse config. - parse_config(&config_p); + // parse config. + parse_config(&config_p); - // start procces - start_sorter(&config_p); + // start procces + start_sorter(&config_p); - destroy_config(&config_p); + destroy_config(&config_p); #else - // TODO - call logger and write that the oparating system is not compatiple. + // TODO - call logger and write that the oparating system is not compatiple. #endif } diff --git a/src/config.c b/src/config.c index 3e68e75..6f77734 100644 --- a/src/config.c +++ b/src/config.c @@ -12,164 +12,163 @@ #define CONF_PATH "/.local/share/file_sorter/config/config.conf" - static char *get_config_buff() { - // determine the absolute path of the config. - char *username = getlogin(); - char *absolute = (char *) malloc((strlen(CONF_PATH) + strlen(username) + 7) - * sizeof(char)); - - if (absolute == NULL) { - logger("Failed to allocate mamory.", ERROR); - return NULL; - } - strcpy(absolute, "/home/"); - strcat(absolute, username); - strcat(absolute, CONF_PATH); - - // read the size of the config. - struct stat conf_stat; - if (lstat(absolute, &conf_stat) != 0) return NULL; // TODO: Call the logger here. - - char *conf_buff = (char *) malloc((conf_stat.st_size + 1) * sizeof(char)); - if (conf_buff == NULL) return NULL; - - int conf_fd = open(absolute, O_RDONLY); - if (conf_fd == -1) { - logger("Failed to allocate mamory.", ERROR); - return NULL; - } - - if (read(conf_fd, conf_buff, conf_stat.st_size) == -1) return NULL; // TODO: Call the logger here. - close(conf_fd); - conf_buff[conf_stat.st_size] = '\0'; - - free(absolute); - return conf_buff; + // determine the absolute path of the config. + char *username = getlogin(); + char *absolute = (char *) malloc((strlen(CONF_PATH) + strlen(username) + 7) + * sizeof(char)); + + if (absolute == NULL) { + logger("Failed to allocate mamory.", ERROR); + return NULL; + } + strcpy(absolute, "/home/"); + strcat(absolute, username); + strcat(absolute, CONF_PATH); + + // read the size of the config. + struct stat conf_stat; + if (lstat(absolute, &conf_stat) != 0) return NULL; // TODO: Call the logger here. + + char *conf_buff = (char *) malloc((conf_stat.st_size + 1) * sizeof(char)); + if (conf_buff == NULL) return NULL; + + int conf_fd = open(absolute, O_RDONLY); + if (conf_fd == -1) { + logger("Failed to allocate mamory.", ERROR); + return NULL; + } + + if (read(conf_fd, conf_buff, conf_stat.st_size) == -1) return NULL; // TODO: Call the logger here. + close(conf_fd); + conf_buff[conf_stat.st_size] = '\0'; + + free(absolute); + return conf_buff; } static char *isolate_opt(const char *conf_buff, const char *opt) { - // don't change the original buffer. - char *tmp = strdup(conf_buff); - char *opt_c = strstr(tmp, opt); - opt_c = strtok(opt_c, " "); // separete the option and the label. - opt_c = strtok(NULL, "\n"); // get the option. - opt_c = strdup(opt_c); - - free(tmp); - return opt_c; // return value must be freed. + // don't change the original buffer. + char *tmp = strdup(conf_buff); + char *opt_c = strstr(tmp, opt); + opt_c = strtok(opt_c, " "); // separete the option and the label. + opt_c = strtok(NULL, "\n"); // get the option. + opt_c = strdup(opt_c); + + free(tmp); + return opt_c; // return value must be freed. } static inline unsigned int parse_int_opt(const char *conf_buff, const char *opt) { - char *opt_a = isolate_opt(conf_buff, opt); - unsigned int opt_r = (opt_a == NULL)? 0:atoi(opt_a); - if (opt_r == 0) { - logger("An option has value of zero.", WAR); - } - free(opt_a); - return opt_r; // get the int value. + char *opt_a = isolate_opt(conf_buff, opt); + unsigned int opt_r = (opt_a == NULL) ? 0 : atoi(opt_a); + if (opt_r == 0) { + logger("An option has value of zero.", WAR); + } + free(opt_a); + return opt_r; // get the int value. } static inline char *parse_str_opt(const char *conf_buff, const char *opt) { - // [!] TODO: Verify path before return. - return isolate_opt(conf_buff, opt); // get the str value. + // [!] TODO: Verify path before return. + return isolate_opt(conf_buff, opt); // get the str value. } static char **parse_list(const char *conf_buff, const char *list) { - // locate the list. - char *tmp = strdup(conf_buff); - char *loc = strstr(tmp, list); - if (loc == NULL) { - logger("List is missing", WAR); - return NULL; - } - - char *curr_el = strtok(loc, "\n"); - unsigned int list_l = 10; - unsigned int real_l = 0; - char *(*list_r) = (char **) malloc(list_l * sizeof(char *)); - if (list_r == NULL) { - logger("Failed to allocate mamory for list.", WAR); - return NULL; - } - - for (int c = 0; curr_el; c++) { - curr_el = strtok(NULL, "\n"); - if (curr_el == NULL) break; - if (!strcmp(curr_el, "[done]")) break; - real_l = c + 1; - - if (real_l == list_l) { - list_l += 10; - list_r = (char **) realloc(list_r, list_l * sizeof(char *)); - if (list_r == NULL) { - logger("Failed to reallocate mamory for list.", WAR); - return NULL; - } - } - list_r[c] = strdup(curr_el); - } - - if (real_l == 0) { - free(list_r); - free(tmp); - return NULL; - } - - // null terminate the array. - list_r[real_l] = NULL; - free(tmp); - return (char **) realloc(list_r, (real_l + 1) * sizeof(char *)); // adjust the size of the list. + // locate the list. + char *tmp = strdup(conf_buff); + char *loc = strstr(tmp, list); + if (loc == NULL) { + logger("List is missing", WAR); + return NULL; + } + + char *curr_el = strtok(loc, "\n"); + unsigned int list_l = 10; + unsigned int real_l = 0; + char *(*list_r) = (char **) malloc(list_l * sizeof(char *)); + if (list_r == NULL) { + logger("Failed to allocate mamory for list.", WAR); + return NULL; + } + + for (int c = 0; curr_el; c++) { + curr_el = strtok(NULL, "\n"); + if (curr_el == NULL) break; + if (!strcmp(curr_el, "[done]")) break; + real_l = c + 1; + + if (real_l == list_l) { + list_l += 10; + list_r = (char **) realloc(list_r, list_l * sizeof(char *)); + if (list_r == NULL) { + logger("Failed to reallocate mamory for list.", WAR); + return NULL; + } + } + list_r[c] = strdup(curr_el); + } + + if (real_l == 0) { + free(list_r); + free(tmp); + return NULL; + } + + // null terminate the array. + list_r[real_l] = NULL; + free(tmp); + return (char **) realloc(list_r, (real_l + 1) * sizeof(char *)); // adjust the size of the list. } static inline void free_list(char *(**list)) { - if ((*list) == NULL) return; - for (int i = 0; (*list)[i]; i++) { - free((*list)[i]); - } - free(*list); - *list = NULL; + if ((*list) == NULL) return; + for (int i = 0; (*list)[i]; i++) { + free((*list)[i]); + } + free(*list); + *list = NULL; } void parse_config(struct config *dst) { - char *conf_buff = get_config_buff(); - if (!conf_buff) { - logger("Failed to read config file.", ERROR); - return; - } - - dst->c_options.o_check_interval = parse_int_opt(conf_buff, "check_interval"); - dst->c_options.o_parse_interval = parse_int_opt(conf_buff, "parse_interval"); - dst->c_options.o_debug_log = parse_int_opt(conf_buff, "debug_log") & 0x1; - dst->c_options.o_default_path = parse_str_opt(conf_buff, "default_dir_path"); - dst->c_options.o_enable_default = parse_int_opt(conf_buff, "enable_default_path") & 0x1; - dst->c_options.o_move_no_ext = parse_int_opt(conf_buff, "move_files_without_extention") & 0x1; - dst->c_lists.l_check_list = parse_list(conf_buff, "[check]"); - dst->c_lists.l_target_list = parse_list(conf_buff, "[targets]"); - dst->c_lists.l_exclude_list = parse_list(conf_buff, "[exclude]"); - free(conf_buff); + char *conf_buff = get_config_buff(); + if (!conf_buff) { + logger("Failed to read config file.", ERROR); + return; + } + + dst->c_options.o_check_interval = parse_int_opt(conf_buff, "check_interval"); + dst->c_options.o_parse_interval = parse_int_opt(conf_buff, "parse_interval"); + dst->c_options.o_debug_log = parse_int_opt(conf_buff, "debug_log") & 0x1; + dst->c_options.o_default_path = parse_str_opt(conf_buff, "default_dir_path"); + dst->c_options.o_enable_default = parse_int_opt(conf_buff, "enable_default_path") & 0x1; + dst->c_options.o_move_no_ext = parse_int_opt(conf_buff, "move_files_without_extention") & 0x1; + dst->c_lists.l_check_list = parse_list(conf_buff, "[check]"); + dst->c_lists.l_target_list = parse_list(conf_buff, "[targets]"); + dst->c_lists.l_exclude_list = parse_list(conf_buff, "[exclude]"); + free(conf_buff); } void destroy_config(struct config *src) { - // free the allocated space where it should. - free(src->c_options.o_default_path); - src->c_options.o_default_path = NULL; - free_list(&src->c_lists.l_check_list); - free_list(&src->c_lists.l_target_list); - free_list(&src->c_lists.l_exclude_list); + // free the allocated space where it should. + free(src->c_options.o_default_path); + src->c_options.o_default_path = NULL; + free_list(&src->c_lists.l_check_list); + free_list(&src->c_lists.l_target_list); + free_list(&src->c_lists.l_exclude_list); } void reparse_config(struct config *dst) { - destroy_config(dst); - // reparse. - parse_config(dst); + destroy_config(dst); + // reparse. + parse_config(dst); } diff --git a/src/logger.c b/src/logger.c index 7125f2e..a66fb0c 100644 --- a/src/logger.c +++ b/src/logger.c @@ -3,18 +3,16 @@ #include - void logger(const char *msg, int type) { - switch (type) - { - case ERROR: - printf("[ERROR] %s\n", msg); - break; - case WAR: - printf("[WARNING] %s\n", msg); - break; - case LOG: - printf("[LOG] %s\n", msg); - } + switch (type) { + case ERROR: + printf("[ERROR] %s\n", msg); + break; + case WAR: + printf("[WARNING] %s\n", msg); + break; + case LOG: + printf("[LOG] %s\n", msg); + } } diff --git a/src/sorter.c b/src/sorter.c index 0707d89..fc23a40 100644 --- a/src/sorter.c +++ b/src/sorter.c @@ -37,7 +37,7 @@ static inline char *extract_ext(const char *file_path) while ((tmp = strstr(++tmp, ".")) != NULL) { dot_loc = tmp; } - + return dot_loc; } @@ -45,18 +45,18 @@ static inline int is_excluded(const char *path, const char *ext) { char *(*exclude_l) = config_file->c_lists.l_exclude_list; if (exclude_l == NULL) return 0; - for (int exl = 0; exclude_l[exl]; exl++) { + for (int exl = 0; exclude_l[exl]; exl++) { if ( - strstr(exclude_l[exl], path) - && - strstr(exclude_l[exl], ext) - ) { - return 1; + strstr(exclude_l[exl], path) + && + strstr(exclude_l[exl], ext) + ) { + return 1; } else if ( - strstr(exclude_l[exl], "*") - && - strstr(exclude_l[exl], ext) - ) { + strstr(exclude_l[exl], "*") + && + strstr(exclude_l[exl], ext) + ) { return 1; } } @@ -72,7 +72,7 @@ static inline char *get_target_rule(const char *ext) strstr(target_list[tr], ext) || strstr(target_list[tr], "*") - ) { + ) { return strstr(target_list[tr], "/"); } } @@ -83,38 +83,38 @@ static void move_file(const char *path, const char *file_name) { char *ext = extract_ext(file_name); if (ext == NULL) return; - if (!strcmp(ext, NO_EXTENTION) && + if (!strcmp(ext, NO_EXTENTION) && !config_file->c_options.o_move_no_ext) { return; } if (is_excluded(path, ext)) return; char *old_path = NULL; - char *send_to = NULL; + char *send_to = NULL; char *target_path = get_target_rule(ext); - old_path = (char *) malloc(sizeof(char) * (strlen(path) - + strlen(file_name) + 1)); + old_path = (char *) malloc(sizeof(char) * (strlen(path) + + strlen(file_name) + 1)); if (old_path == NULL) { if (config_file->c_options.o_debug_log) { logger("Failed to allocate space for old path", WAR); } return; - } + } strcpy(old_path, path); strcat(old_path, file_name); if (!target_path && config_file->c_options.o_enable_default) { char *default_path = config_file->c_options.o_default_path; send_to = (char *) malloc(sizeof(char) * (strlen(default_path) - + strlen(file_name) + 1)); + + strlen(file_name) + 1)); if (send_to == NULL) return; strcpy(send_to, default_path); strcat(send_to, file_name); if (rename(old_path, send_to) == -1) { - if (config_file->c_options.o_debug_log) { - logger("Failed to move file to default_path", WAR); + if (config_file->c_options.o_debug_log) { + logger("Failed to move file to default_path", WAR); } } logger("Successfully move a file", LOG); @@ -122,14 +122,14 @@ static void move_file(const char *path, const char *file_name) free(send_to); old_path = NULL; send_to = NULL; - } + } if (!target_path) { free(old_path); return; - } + } send_to = (char *) malloc(sizeof(char) * (strlen(target_path) - + strlen(file_name) + 1)); - if (send_to == NULL) { + + strlen(file_name) + 1)); + if (send_to == NULL) { if (config_file->c_options.o_debug_log) { logger("Failed to allocate space to build the path to send.", WAR); } @@ -139,7 +139,7 @@ static void move_file(const char *path, const char *file_name) strcat(send_to, file_name); if (rename(old_path, send_to) == -1) { - if (config_file->c_options.o_debug_log) { + if (config_file->c_options.o_debug_log) { logger("Failed to move file to default_path", WAR); } } @@ -156,11 +156,11 @@ static void search_files(const char *path) dir = opendir(path); if (dir == NULL) { logger("Failed to open directory.", WAR); - return; - } + return; + } while ((file = readdir(dir)) != NULL) { // if the current element of the directory is a file. - if (file->d_type == DT_REG && file->d_name[0] != '.') { + if (file->d_type == DT_REG && file->d_name[0] != '.') { move_file(path, file->d_name); } } @@ -175,16 +175,15 @@ static void *organize_files(void *arg) sleep(config_file->c_options.o_check_interval); pthread_mutex_lock(&config_lock); // read check list. - if (config_file->c_lists.l_check_list == NULL) continue; + if (config_file->c_lists.l_check_list == NULL) continue; for (int i = 0; config_file->c_lists.l_check_list[i]; i++) { search_files(config_file->c_lists.l_check_list[i]); } - pthread_mutex_unlock(&config_lock); + pthread_mutex_unlock(&config_lock); } } - void start_sorter(struct config *src) { config_file = src; @@ -198,10 +197,10 @@ void start_sorter(struct config *src) pthread_create(&refresh_t, NULL, &refresh_config, NULL) != 0 || pthread_create(&organize_t, NULL, &organize_files, NULL) != 0 - ) { - logger("Can't create threads, shuting down..", ERROR); - pthread_mutex_destroy(&config_lock); - return; + ) { + logger("Can't create threads, shuting down..", ERROR); + pthread_mutex_destroy(&config_lock); + return; } pthread_join(refresh_t, NULL);