From ea9241a52a30f369311450b5debb815354e87d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Pollak?= Date: Thu, 27 Apr 2023 17:00:16 -0400 Subject: [PATCH] Add 'match-dirname' option --- lib/cfg.h | 1 + lib/cmdline.c | 1 + lib/file.c | 7 +++++++ lib/file.h | 4 ++++ lib/preprocess.c | 5 +++++ 5 files changed, 18 insertions(+) diff --git a/lib/cfg.h b/lib/cfg.h index 4ba42ed24..074f85c09 100644 --- a/lib/cfg.h +++ b/lib/cfg.h @@ -75,6 +75,7 @@ typedef struct RmCfg { gboolean limits_specified; gboolean filter_mtime; gboolean match_basename; + gboolean match_dirname; gboolean unmatched_basenames; gboolean match_with_extension; gboolean match_without_extension; diff --git a/lib/cmdline.c b/lib/cmdline.c index 56141d941..580977c8a 100644 --- a/lib/cmdline.c +++ b/lib/cmdline.c @@ -1428,6 +1428,7 @@ bool rm_cmd_parse_args(int argc, char **argv, RmSession *session) { {"must-match-tagged" , 'm' , 0 , G_OPTION_ARG_NONE , &cfg->must_match_tagged , _("Must have twin in tagged dir") , NULL} , {"must-match-untagged" , 'M' , 0 , G_OPTION_ARG_NONE , &cfg->must_match_untagged , _("Must have twin in untagged dir") , NULL} , {"match-basename" , 'b' , 0 , G_OPTION_ARG_NONE , &cfg->match_basename , _("Only find twins with same basename") , NULL} , + {"match-dirname" , 'B' , 0 , G_OPTION_ARG_NONE , &cfg->match_dirname , _("Only find twins with same dirname") , NULL} , {"match-extension" , 'e' , 0 , G_OPTION_ARG_NONE , &cfg->match_with_extension , _("Only find twins with same extension") , NULL} , {"match-without-extension" , 'i' , 0 , G_OPTION_ARG_NONE , &cfg->match_without_extension , _("Only find twins with same basename minus extension") , NULL} , {"merge-directories" , 'D' , EMPTY , G_OPTION_ARG_CALLBACK , FUNC(merge_directories) , _("Find duplicate directories") , NULL} , diff --git a/lib/file.c b/lib/file.c index 3e2c835f9..27e317897 100644 --- a/lib/file.c +++ b/lib/file.c @@ -180,6 +180,13 @@ gint rm_file_basenames_cmp(const RmFile *file_a, const RmFile *file_b) { return g_ascii_strcasecmp(file_a->folder->basename, file_b->folder->basename); } +gint rm_file_dirnames_cmp(const RmFile *file_a, const RmFile *file_b) { + if (!file_a->folder->parent || !file_b->folder->parent) + return 0; + + return g_ascii_strcasecmp(file_a->folder->parent->basename, file_b->folder->parent->basename); +} + void rm_file_hardlink_add(RmFile *head, RmFile *link) { if(!head->hardlinks) { head->hardlinks = g_queue_new(); diff --git a/lib/file.h b/lib/file.h index 3246af2fb..28acbc4d6 100644 --- a/lib/file.h +++ b/lib/file.h @@ -377,4 +377,8 @@ RmFile *rm_file_copy(RmFile *file); */ gint rm_file_basenames_cmp(const RmFile *file_a, const RmFile *file_b); + +gint rm_file_dirnames_cmp(const RmFile *file_a, const RmFile *file_b); + + #endif /* end of include guard */ diff --git a/lib/preprocess.c b/lib/preprocess.c index e57eb3aaf..d1b836a97 100644 --- a/lib/preprocess.c +++ b/lib/preprocess.c @@ -70,6 +70,11 @@ gint rm_file_cmp(const RmFile *file_a, const RmFile *file_b) { RmCfg *cfg = file_a->session->cfg; + if(cfg->match_dirname) { + result = rm_file_dirnames_cmp(file_a, file_b); + RETURN_IF_NONZERO(result); + } + if(cfg->match_basename) { result = rm_file_basenames_cmp(file_a, file_b); RETURN_IF_NONZERO(result);