src/filemanager/{achown,chattr,chmod,chown}.c: get rid of code duplication.

* (panel_find_marked_file):
  * (panel_get_marked_file): new WPanel APIs.
  * Use these APIs in src/filemanager/{achown,chattr,chmod,chown}.c.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2024-10-19 20:45:22 +03:00
parent 2d9383fd54
commit b232229b46
6 changed files with 61 additions and 100 deletions

View File

@ -831,17 +831,6 @@ advanced_chown_done (gboolean need_update)
/* --------------------------------------------------------------------------------------------- */
static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;
return panel->dir.list[current_file].fname;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
try_advanced_chown (const vfs_path_t *p, mode_t m, uid_t u, gid_t g)
{
@ -957,7 +946,7 @@ apply_advanced_chowns (WPanel *panel, vfs_path_t *vpath, struct stat *sf)
{
const GString *fname;
fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, sf) == 0);
@ -1017,20 +1006,10 @@ advanced_chown_cmd (WPanel *panel)
need_update = FALSE;
end_chown = FALSE;
if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
{
/* single file */
const file_entry_t *fe;
fe = panel_current_entry (panel);
if (fe == NULL)
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;
fname = fe->fname;
}
vpath = vfs_path_from_str (fname->str);
if (mc_stat (vpath, &sf_stat) != 0)

View File

@ -1087,17 +1087,6 @@ chattr_done (gboolean need_update)
/* --------------------------------------------------------------------------------------------- */
static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;
return panel->dir.list[current_file].fname;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
try_chattr (const vfs_path_t *p, unsigned long m)
{
@ -1173,7 +1162,7 @@ chattr_apply_mask (WPanel *panel, vfs_path_t *vpath, unsigned long m)
{
const GString *fname;
fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_fgetflags (vpath, &m) == 0);
@ -1224,20 +1213,10 @@ chattr_cmd (WPanel *panel)
need_update = FALSE;
end_chattr = FALSE;
if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
{
/* single file */
const file_entry_t *fe;
fe = panel_current_entry (panel);
if (fe == NULL)
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;
fname = fe->fname;
}
vpath = vfs_path_from_str (fname->str);
if (mc_fgetflags (vpath, &flags) != 0)

View File

@ -410,17 +410,6 @@ chmod_done (gboolean need_update)
/* --------------------------------------------------------------------------------------------- */
static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;
return panel->dir.list[current_file].fname;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
try_chmod (const vfs_path_t *p, mode_t m)
{
@ -496,7 +485,7 @@ apply_mask (WPanel *panel, vfs_path_t *vpath, struct stat *sf)
{
const GString *fname;
fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, sf) == 0);
@ -549,20 +538,10 @@ chmod_cmd (WPanel *panel)
need_update = FALSE;
end_chmod = FALSE;
if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
{
/* single file */
const file_entry_t *fe;
fe = panel_current_entry (panel);
if (fe == NULL)
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;
fname = fe->fname;
}
vpath = vfs_path_from_str (fname->str);
if (mc_stat (vpath, &sf_stat) != 0)

View File

@ -282,17 +282,6 @@ chown_done (gboolean need_update)
/* --------------------------------------------------------------------------------------------- */
static const GString *
next_file (const WPanel *panel)
{
while (panel->dir.list[current_file].f.marked == 0)
current_file++;
return panel->dir.list[current_file].fname;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
try_chown (const vfs_path_t *p, uid_t u, gid_t g)
{
@ -366,7 +355,7 @@ apply_chowns (WPanel *panel, vfs_path_t *vpath, uid_t u, gid_t g)
const GString *fname;
struct stat sf;
fname = next_file (panel);
fname = panel_find_marked_file (panel, &current_file);
vpath = vfs_path_from_str (fname->str);
ok = (mc_stat (vpath, &sf) == 0);
@ -418,20 +407,10 @@ chown_cmd (WPanel *panel)
need_update = FALSE;
end_chown = FALSE;
if (panel->marked != 0)
fname = next_file (panel); /* next marked file */
else
{
/* single file */
const file_entry_t *fe;
fe = panel_current_entry (panel);
if (fe == NULL)
fname = panel_get_marked_file (panel, &current_file);
if (fname == NULL)
break;
fname = fe->fname;
}
vpath = vfs_path_from_str (fname->str);
if (mc_stat (vpath, &sf_stat) != 0)

View File

@ -4959,6 +4959,49 @@ file_mark (WPanel *panel, int lc_index, int val)
}
}
/* --------------------------------------------------------------------------------------------- */
/**
* Find marked file starting from the given position.
*
* @param panel WPanel object
* @param curent_file a staring position to get current or search next marked file
*
* @return pointer to the name of find file or NULL if no file found or @current_file is out of range
*/
const GString *
panel_find_marked_file (const WPanel *panel, int *current_file)
{
while (panel->dir.list[*current_file].f.marked == 0 && *current_file < panel->dir.len)
(*current_file)++;
return (*current_file >= panel->dir.len ? NULL : panel->dir.list[*current_file].fname);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get marked file clsest to the given position.
*
* @param panel WPanel object
* @param curent_file a staring position to get current or closest next marked file. If there are
* no marked files in @panel, @panel->current is used.
*
* @return pointer to the name of find file or NULL if no file found or @current_file is out of range.
*/
const GString *
panel_get_marked_file (const WPanel *panel, int *current_file)
{
const file_entry_t *fe;
if (panel->marked != 0)
return panel_find_marked_file (panel, current_file);
fe = panel_current_entry (panel);
return (fe == NULL ? NULL : fe->fname);
}
/* --------------------------------------------------------------------------------------------- */
void

View File

@ -180,6 +180,8 @@ void select_item (WPanel * panel);
void recalculate_panel_summary (WPanel * panel);
void file_mark (WPanel * panel, int idx, int val);
void do_file_mark (WPanel * panel, int idx, int val);
const GString *panel_find_marked_file (const WPanel *panel, int *current_file);
const GString *panel_get_marked_file (const WPanel *panel, int *current_file);
gboolean panel_do_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type);
MC_MOCKABLE gboolean panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath,