aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c57
1 files changed, 44 insertions, 13 deletions
diff --git a/src/main.c b/src/main.c
index ea54701..34895c6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,3 +1,5 @@
+/* For strcasestr */
+#define _GNU_SOURCE
#include "buffer.h"
#include "fileutils.h"
#include "transmission.h"
@@ -76,11 +78,13 @@ static void usage_downloaded(void) {
}
static void usage_cleanup(void) {
- fprintf(stderr, "usage: automedia cleanup <days>\n");
+ fprintf(stderr, "usage: automedia cleanup [-d <days>] [search_term]\n");
fprintf(stderr, "OPTIONS\n");
- fprintf(stderr, " days Media that haven't received any updates in the specified amount of days are removed\n");
+ fprintf(stderr, " -d <days> Media that haven't received any updates in the specified amount of days will be shown. If not specified then all media will be included\n");
+ fprintf(stderr, " search_term The name of the media to find. If not inclued then all media (within -d days) will be included. Note this is case insensitive\n");
fprintf(stderr, "EXAMPLES\n");
- fprintf(stderr, " automedia cleanup 100\n");
+ fprintf(stderr, " automedia cleanup -d 100\n");
+ fprintf(stderr, " automedia cleanup baki\n");
exit(1);
}
@@ -207,7 +211,7 @@ typedef struct {
time_t updated_time;
} TrackedItemData;
-static void get_tracked_items(const char *tracked_dir, time_t age_sec, Buffer /*TrackedItemData*/ *tracked_items_buffer) {
+static void get_tracked_items(const char *tracked_dir, time_t age_sec, const char *search_term, Buffer /*TrackedItemData*/ *tracked_items_buffer) {
struct dirent *dir;
DIR *d = opendir(tracked_dir);
if(!d) {
@@ -224,13 +228,18 @@ static void get_tracked_items(const char *tracked_dir, time_t age_sec, Buffer /*
char *file_data;
long file_size;
+ if(!search_term)
+ search_term = "";
+
while((dir = readdir(d)) != NULL) {
int filename_len = strlen(dir->d_name);
if((filename_len == 1 && dir->d_name[0] == '.') || (filename_len == 2 && dir->d_name[0] == '.' && dir->d_name[1] == '.'))
continue;
- strcpy(data_filepath + data_filepath_length, dir->d_name);
+ if(search_term[0] != '\0' && !strcasestr(dir->d_name, search_term))
+ continue;
+ strcpy(data_filepath + data_filepath_length, dir->d_name);
strcpy(data_filepath + data_filepath_length + filename_len, "/updated");
if(file_get_content(data_filepath, &file_data, &file_size) != 0) {
fprintf(stderr, "Failed to read the content of file %s\n", data_filepath);
@@ -722,12 +731,31 @@ static int compare_tracked_item(const void *a, const void *b) {
static void command_cleanup(int argc, char **argv, const char *rss_config_dir, const char *html_config_dir) {
time_t days = 0;
- if(argc >= 1) {
- errno = 0;
- days = strtol(argv[0], NULL, 0);
- if(errno || days < 0) {
- fprintf(stderr, "Invalid value for days argument %s, expected a positive number\n", argv[0]);
+ const char *search_term = NULL;
+
+ for(int i = 0; i < argc; ++i) {
+ if(strcmp(argv[i], "-d") == 0) {
+ if(i == argc - 1) {
+ fprintf(stderr, "Error: missing argument after -d\n");
+ usage_cleanup();
+ }
+
+ errno = 0;
+ days = strtol(argv[i + 1], NULL, 0);
+ if(errno || days < 0) {
+ fprintf(stderr, "Error: invalid value for days argument %s, expected a positive number\n", argv[0]);
+ usage_cleanup();
+ }
+
+ ++i;
+ } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
usage_cleanup();
+ } else {
+ if(search_term) {
+ fprintf(stderr, "Error: search term specified multiple times. First as \"%s\" then as \"%s\"\n", search_term, argv[i]);
+ usage_cleanup();
+ }
+ search_term = argv[i];
}
}
@@ -744,9 +772,9 @@ static void command_cleanup(int argc, char **argv, const char *rss_config_dir, c
int num_rss_items = 0;
const time_t age_sec = 60 * 60 * 24 * days;
- get_tracked_items(rss_tracked_dir, age_sec, &tracked_items);
+ get_tracked_items(rss_tracked_dir, age_sec, search_term, &tracked_items);
num_rss_items = buffer_get_size(&tracked_items, sizeof(TrackedItemData));
- get_tracked_items(html_tracked_dir, age_sec, &tracked_items);
+ get_tracked_items(html_tracked_dir, age_sec, search_term, &tracked_items);
int num_tracked_items = buffer_get_size(&tracked_items, sizeof(TrackedItemData));
qsort(tracked_items.data, num_rss_items, sizeof(TrackedItemData), compare_tracked_item);
@@ -770,7 +798,10 @@ static void command_cleanup(int argc, char **argv, const char *rss_config_dir, c
buffer_init(&track_remove_ranges);
if(tracked_items.size == 0) {
- fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s\n", days, days == 1 ? "day" : "days");
+ if(search_term)
+ fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s and matches the search term \"%s\"\n", days, days == 1 ? "day" : "days", search_term);
+ else
+ fprintf(stderr, "There is no media that hasn't been updated in the last %ld %s\n", days, days == 1 ? "day" : "days");
goto cleanup;
}