vfs_dirent: add new member d_len to get rid of d_name length calculation.

* (vfs_dirent_assign): set d_len up.
  * (mc_readdir): sync with new vfs_dirent: use d_len member.
  * (filename_completion_function): likewise.
  * (handle_dirent): likewise.
  * (find_ignore_dir_search): add 2nd argument to use length of
    directory name if it's known.
  * (do_search): sync with modified vfs_dirent and find_ignore_dir_search().
  * (NLENGTH): remove.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2024-05-01 10:30:57 +03:00
parent f4183ed2f2
commit ab3e3339bb
7 changed files with 14 additions and 16 deletions

View File

@ -98,9 +98,6 @@
#define MC_MAXPATHLEN MAXPATHLEN
#endif
/* unistd.h defines _POSIX_VERSION on POSIX.1 systems. */
#define NLENGTH(dirent) (strlen ((dirent)->d_name))
/* DragonFlyBSD doesn't provide MAXNAMLEN macro */
#ifndef MAXNAMLEN
#define MAXNAMLEN NAME_MAX

View File

@ -481,7 +481,7 @@ mc_readdir (DIR * dirp)
#ifdef HAVE_CHARSET
str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
#else
g_string_assign (vfs_str_buffer, entry->d_name);
g_string_append_len (vfs_str_buffer, entry->d_name, entry->d_len);
#endif
vfs_dirent_assign (mc_readdir_result, vfs_str_buffer->str, entry->d_ino);
vfs_dirent_free (entry);

View File

@ -565,6 +565,7 @@ vfs_dirent_assign (struct vfs_dirent *d, const char *fname, ino_t ino)
{
g_string_assign (d->d_name_str, fname);
d->d_name = d->d_name_str->str;
d->d_len = d->d_name_str->len;
d->d_ino = ino;
}

View File

@ -214,6 +214,7 @@ struct vfs_dirent
/* public */
ino_t d_ino;
char *d_name; /* Alias of d_name_str->str */
size_t d_len; /* Alias of d_name_str->len */
};
/*** global variables defined in .c file *********************************************************/

View File

@ -212,9 +212,8 @@ filename_completion_function (const char *text, int state, input_complete_t flag
{
/* Otherwise, if these match up to the length of filename, then
it may be a match. */
if ((entry->d_name[0] != filename[0]) ||
((NLENGTH (entry)) < filename_len) ||
strncmp (filename, entry->d_name, filename_len) != 0)
if (entry->d_name[0] != filename[0] || entry->d_len < filename_len
|| strncmp (filename, entry->d_name, filename_len) != 0)
continue;
}
@ -291,7 +290,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
if (!IS_PATH_SEP (temp->str[temp->len - 1]))
g_string_append_c (temp, PATH_SEP);
}
g_string_append (temp, entry->d_name);
g_string_append_len (temp, entry->d_name, entry->d_len);
if (isdir)
g_string_append_c (temp, PATH_SEP);

View File

@ -159,7 +159,7 @@ handle_dirent (struct vfs_dirent *dp, const file_filter_t * filter, struct stat
return FALSE;
if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
return FALSE;
if (!panels_options.show_backups && dp->d_name[strlen (dp->d_name) - 1] == '~')
if (!panels_options.show_backups && dp->d_name[dp->d_len - 1] == '~')
return FALSE;
vpath = vfs_path_from_str (dp->d_name);
@ -186,7 +186,7 @@ handle_dirent (struct vfs_dirent *dp, const file_filter_t * filter, struct stat
gboolean files_only = (filter->flags & SELECT_FILES_ONLY) != 0;
ok = ((S_ISDIR (buf1->st_mode) || *link_to_dir) && files_only)
|| mc_search_run (filter->handler, dp->d_name, 0, strlen (dp->d_name), NULL);
|| mc_search_run (filter->handler, dp->d_name, 0, dp->d_len, NULL);
}
return ok;

View File

@ -1188,11 +1188,11 @@ search_content (WDialog * h, const char *directory, const char *filename)
If dir is relative, this means we're going to add dir to the directory stack.
**/
static gboolean
find_ignore_dir_search (const char *dir)
find_ignore_dir_search (const char *dir, size_t len)
{
if (find_ignore_dirs != NULL)
{
const size_t dlen = strlen (dir);
const size_t dlen = len == (size_t) (-1) ? strlen (dir) : len;
const unsigned char dabs = g_path_is_absolute (dir) ? 1 : 0;
char **ignore_dir;
@ -1337,7 +1337,7 @@ do_search (WDialog * h)
pop_start_dir = FALSE;
/* handle absolute ignore dirs here */
if (!find_ignore_dir_search (vfs_path_as_str (tmp_vpath)))
if (!find_ignore_dir_search (vfs_path_as_str (tmp_vpath), -1))
break;
vfs_path_free (tmp_vpath, TRUE);
@ -1380,7 +1380,7 @@ do_search (WDialog * h)
if (options.find_recurs && (directory != NULL))
{ /* Can directory be NULL ? */
/* handle relative ignore dirs here */
if (options.ignore_dirs_enable && find_ignore_dir_search (dp->d_name))
if (options.ignore_dirs_enable && find_ignore_dir_search (dp->d_name, dp->d_len))
ignore_count++;
else
{
@ -1401,8 +1401,8 @@ do_search (WDialog * h)
}
}
search_ok = mc_search_run (search_file_handle, dp->d_name,
0, strlen (dp->d_name), &bytes_found);
search_ok =
mc_search_run (search_file_handle, dp->d_name, 0, dp->d_len, &bytes_found);
if (search_ok)
{