Skip to content

Commit

Permalink
Merge pull request #13 from EmbeddedCat/12-simplify-the-sorter-process
Browse files Browse the repository at this point in the history
12 simplify the sorter process
  • Loading branch information
constarg authored Feb 21, 2023
2 parents 48bcad8 + 8f89b3d commit 2842c0b
Show file tree
Hide file tree
Showing 9 changed files with 586 additions and 85 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ project(File_Sorter_Core C)

set(CMAKE_C_STANDARD 17)

set(SOURCE_FILES main.c ./src/config.c ./src/sorter.c ./src/logger.c)
set(HEADER_FILES ./include/config.h ./include/sorter.h ./include/logger.h)
set(SOURCE_FILES main.c ./src/config.c ./src/sorter.c ./src/logger.c ./src/tool/actions.c)
set(HEADER_FILES ./include/config.h ./include/sorter.h ./include/logger.h ./include/tool/actions.h)
set(TO_INCLUDE ./include/)

add_executable(file-sorter ${SOURCE_FILES} ${HEADER_FILES})
Expand Down Expand Up @@ -41,7 +41,6 @@ endif ()
file(WRITE config/config.conf
[basic_config]\n
check_interval\ 3000\n
parse_interval\ 5000\n
debug_log\ 0\n
default_dir_path\ /home/$ENV{USER}/default_sorter_path/\n
enable_default_path\ 1\n
Expand All @@ -63,7 +62,7 @@ file(WRITE service/file-sorter.service
\n
[Service]\n
Type=simple\n
ExecStart=/bin/bash\ -c\ /usr/bin/file-sorter\n
ExecStart=/bin/bash\ -c\ "/home/$ENV{USER}/.local/bin/file-sorter\ --start-sorter"\n
Restart=on-failure\n
Group=$ENV{USER}\n
\n
Expand All @@ -82,4 +81,4 @@ file(WRITE autostart/file-sorter.desktop
install(FILES config/config.conf PERMISSIONS OWNER_WRITE OWNER_READ GROUP_WRITE GROUP_READ WORLD_WRITE WORLD_READ DESTINATION /home/$ENV{USER}/.local/share/file_sorter/config/)
install(FILES service/file-sorter.service PERMISSIONS OWNER_EXECUTE OWNER_READ WORLD_EXECUTE WORLD_READ DESTINATION /home/$ENV{USER}/.config/systemd/user/)
install(FILES autostart/file-sorter.desktop PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE GROUP_EXECUTE GROUP_READ GROUP_WRITE WORLD_EXECUTE WORLD_READ WORLD_WRITE DESTINATION /home/$ENV{USER}/.config/autostart/)
install(FILES build/file-sorter PERMISSIONS OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE DESTINATION /usr/bin/)
install(FILES build/file-sorter PERMISSIONS OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE DESTINATION ~/.local/bin/)
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ The information is as follows.<br>
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.
Expand Down Expand Up @@ -118,18 +117,24 @@ locations `/home/username/ Documents/py/` and `/home/username/Documents/cpp/`.
In the example above the first part consists of the extensions `.py` and `.c` and the second part of the
locations `/home/username/Documents/py/` and `*`.

# Tool
# Commands

Command | Description
---------|--------------
`file-sorter --start-sorter` | Start the sorter service
`file-sorter --help` | Displays all available commands.
`file-sorter --set-check-interval [value]` | Changes the value of the check interval field to the value `[value]`.
`file-sorter --set-default-dir-path [path]` | Changes the default location to `[path]`.
`file-sorter --set-enable-default-dir [value]` | 0:1 Enable the to transfer files in default dir.
`file-sorter --set-debug-log [value]` | Changes the logs status based on the value `[value]`. It can get the values `1` `(debug mode)` and `0` `(normal mode)`.
`file-sorter --set-mv-without-ext [value]` | 0:1 Enable the program to move files without extention.
`file-sorter --add-check [path]` | Adds `[path]` to the list of locations that the program looks at.
`file-sorter --add-target [ext] [path]` | Adds to the list the information that the program must send the file with extension `[ext]` to the location `[path]` if such a file is found.
`file-sorter --remove-check [row number]` | Deletes the item in the line `[row_number]`, the line number appears with the command `--list-checks` or `--list-targets`.
`file-sorter --list-checks` | Displays all locations that the program looks at.
`file-sorter --list-targets` | Displays all the elements that describe what the program does for a file.
`file-sorter --list-options` | Displays the program options.

Due to the nature of this program and this config i made a tool that helps the user to do the following:<br>

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

The tool described above can be found here:<br>
https://github.com/EmbeddedCat/file-sorter-terminal-tool

# Service

Expand Down
1 change: 0 additions & 1 deletion config/config.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[basic_config]
check_interval 3000
parse_interval 5000
debug_log 0
default_dir_path /home/embeddedcat/default_sorter_path/
enable_default_path 1
Expand Down
24 changes: 23 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@

#define FAILED_TO_PARSE -0x1

/**
* *******************
* OPTIONS
* *******************
*/
#define CHECK_INT "check_interval"
#define PARSE_INT "parse_interval"
#define DEBUG_LOG "debug_log"
#define DEFAULT_DIR "default_dir_path"
#define EN_DEFAULT "enable_default_path"
#define WITHOUT_EXT "move_files_without_ext"

/**
* ******************
* LISTS
* ******************
*/
#define CHECK_LISTID "[check]"
#define TARGET_LISTID "[targets]"
#define EXCLUDE_LISTID "[exclude]"


// 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.
Expand Down Expand Up @@ -54,4 +74,6 @@ extern void reparse_config(struct config *dst);

extern void destroy_config(struct config *src);

extern int update_config(const struct config *src);

#endif
101 changes: 101 additions & 0 deletions include/tool/actions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef ACTIONS_H
#define ACTIONS_H

// option manipulation commands.
/**
* Change the value of the check interval option
* in config file.
*/
extern int set_check_interval(const char *n_value);

/**
* Enable or disable the debug log
* feature. This works by changing
* the value of the corresponded value
* in config file.
*/
extern int set_debug_log(const char *n_state);

/**
* Change the default dir path, where the
* files sent when no rule has been set.
*/
extern int set_default_dir_path(const char *n_path);

/**
* Enable or disable the default dir
* feature. This works by changing
* the value of the corresponded value
* in config file.
*/
extern int set_enable_default_dir(const char *n_state);

/**
* Enable or disable the feature to
* move the file without extention.
* @param n_state The new state 0 or 1.
*/
extern int set_mv_without_ext(const char *n_state);


// list manipulation commands.
/**
* Add a new path on the check list that the core
* will scan for files.
*/
extern int add_check(const char *path);

/**
* Add a new rule on the target list that the core
* checks in order to organize the files.
* @param args The information that the builder pass to the callback.
*/
extern int add_target(const char *ext, const char *path);

/**
* Add a new rule on the exlude list that the core
* checks in order to exclude files.
*/
extern int add_exclude(const char *ext, const char *path);

/**
* Remove a path from the check list that the core
* will scan for files.
*/
extern int remove_check(const char *row);

/**
* Remove a rule from the target list that the core
* checks in order to organize the files.
*/
extern int remove_target(const char *row);

/**
* Remove a rule from the exlude list that the core
* checks in order to exclude files.
*/
extern int remove_exclude(const char *row);

// Show commands.
/**
* Show the available options.
*/
extern int list_options();

/**
* Show the list of path's in check list.
* @param args The information that the builder pass to the callback.
*/
extern int list_checks();

/**
* Show the list of rules in target list.
*/
extern int list_targets();

/**
* Show the list of rules in exclude list.
*/
extern int list_excludes();

#endif
137 changes: 124 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,140 @@

#include <malloc.h>

#include <config.h>
#include <sorter.h>
#include "config.h"
#include "sorter.h"
#include "tool/actions.h"

#else
// TODO - include logger.
#include <stdio.h>
#endif

const char usage[] = "Usage:\n \tfile-sorter [OPTION] ...\n\n"
"\t--start-sorter. Start the sorter service\n"
"\t--set-check-interval [value] Change the value of check interval.\n"
"\t--set-default-dir-path [path] Change the default directory path.\n"
"\t--set-enable-default-dir [value] 0:1 Enable the to transfer files in default dir.\n"
"\t--set-debug-log [value] 0:1 Change the log to debug mode (1).\n"
"\t--set-mv-without-ext [value] 0:1 Enable the program to move files without extention.\n"
"\t--add-check [path] Add new check.\n"
"\t--add-target [ext] [path] Add new target.\n"
"\t--remove-check [row number] remove check.\n"
"\t--remove-target [row number] remove target.\n"
"\t--list-checks list checks.\n"
"\t--list-targets list targets.\n"
"\t--list-options list options.\n\n\n"
"Each of the above commands will print an OK message on success.\n"
"Check https://github.com/EmbeddedCat/file-sorter-core for more information's about the project!.\n";

static inline int help()
{
return printf("%s\n", usage);
}


int main(int argc, char *argv[])
{
#ifdef __unix__
struct config config_p;
// initialzize the config.
init_config(&config_p);

// parse config.
parse_config(&config_p);

// start procces
start_sorter(&config_p);
if (argv[1] == NULL) {
return help();
} else if (!strcmp(argv[1], "--start-sorter")) {
struct config config_p;
// initialzize the config.
init_config(&config_p);
// parse config.
parse_config(&config_p);
// start procces
start_sorter(&config_p);
destroy_config(&config_p);
} else if (!strcmp(argv[1], "--set-check-interval")) {
if (argv[2] == NULL) return help();
if (set_check_interval(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--set-default-dir-path")) {
if (argv[2] == NULL) return help();
set_default_dir_path(argv[2]);
} else if (!strcmp(argv[1], "--set-enable-default-dir")) {
if (argv[2] == NULL) return help();
if (set_enable_default_dir(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--set-debug-log")) {
if (argv[2] == NULL) return help();
if (set_debug_log(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--set-mv-without-ext")) {
if (argv[2] == NULL) return help();
if (set_mv_without_ext(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--add-check")) {
if (argv[2] == NULL) return help();
if (add_check(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--add-target")) {
if (argv[2] == NULL) return help();
if (argv[3] == NULL) return help();
if (add_target(argv[2], argv[3]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--add-exclude")) {
if (argv[2] == NULL) return help();
if (argv[3] == NULL) return help();
if (add_exclude(argv[2], argv[3]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--remove-check")) {
if (argv[2] == NULL) return help();
if (remove_check(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--remove-target")) {
if (argv[2] == NULL) return help();
if (remove_target(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--remove-exclude")) {
if (argv[2] == NULL) return help();
if (remove_exclude(argv[2]) == -1) {
printf("Error\n");
} else {
printf("OK\n");
}
} else if (!strcmp(argv[1], "--list-options")) {
list_options();
} else if (!strcmp(argv[1], "--list-checks")) {
list_checks();
} else if (!strcmp(argv[1], "--list-targets")) {
list_targets();
} else if (!strcmp(argv[1], "--list-excludes")) {
list_excludes();
} else {
return help();
}

destroy_config(&config_p);
#else
// TODO - call logger and write that the oparating system is not compatiple.
printf("The oparation system does not support this binary.\n");
#endif
}
Loading

0 comments on commit 2842c0b

Please sign in to comment.