Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --match-relative-path option and support #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct RmCfg {
gboolean limits_specified;
gboolean filter_mtime;
gboolean match_basename;
gboolean match_relative_path;
gboolean unmatched_basenames;
gboolean match_with_extension;
gboolean match_without_extension;
Expand Down
1 change: 1 addition & 0 deletions lib/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ bool rm_cmd_parse_args(int argc, char **argv, RmSession *session) {
{"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-extension" , 'e' , 0 , G_OPTION_ARG_NONE , &cfg->match_with_extension , _("Only find twins with same extension") , NULL} ,
{"match-relative-path" , 0 , 0 , G_OPTION_ARG_NONE , &cfg->match_relative_path , _("Only find twins with same relative path") , 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} ,
{"honour-dir-layout" , 'j' , EMPTY , G_OPTION_ARG_CALLBACK , FUNC(honour_dir_layout) , _("Only find directories with same file layout") , NULL} ,
Expand Down
19 changes: 19 additions & 0 deletions lib/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ 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_relpaths_cmp(const RmFile *file_a, const RmFile *file_b) {
gint diff = file_a->depth - file_b->depth;
RETURN_IF_NONZERO(diff);
RmNode* node_a = file_a->folder;
RmNode* node_b = file_b->folder;

for (gint16 depth = file_a->depth; depth > 0; --depth) {
g_assert(node_a);
g_assert(node_b);

diff = g_ascii_strcasecmp(node_a->basename, node_b->basename);
RETURN_IF_NONZERO(diff);

node_a = node_a->parent;
node_b = node_b->parent;
}
return 0;
}

void rm_file_hardlink_add(RmFile *head, RmFile *link) {
if(!head->hardlinks) {
head->hardlinks = g_queue_new();
Expand Down
6 changes: 6 additions & 0 deletions lib/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,10 @@ RmFile *rm_file_copy(RmFile *file);
*/
gint rm_file_basenames_cmp(const RmFile *file_a, const RmFile *file_b);

/**
* @brief Compare relative paths of two files
* @retval true if relative paths match.
*/
gint rm_file_relpaths_cmp(const RmFile *file_a, const RmFile *file_b);

#endif /* end of include guard */
5 changes: 5 additions & 0 deletions lib/preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ gint rm_file_cmp(const RmFile *file_a, const RmFile *file_b) {

if(cfg->match_without_extension) {
result = rm_file_cmp_without_extension(file_a, file_b);
RETURN_IF_NONZERO(result);
}

if (cfg->match_relative_path) {
result = rm_file_relpaths_cmp(file_a, file_b);
}

return result;
Expand Down