Skip to content

Commit

Permalink
help: split out scanning code
Browse files Browse the repository at this point in the history
The part of directory scanning and filtering for .md files are moved
out of help.c into scan.c.
scan_dir will return a list of file path for .md files
  • Loading branch information
shadofren authored and flatcap committed Aug 14, 2019
1 parent e873c4c commit 31b022a
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Makefile.autosetup
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ ALLOBJS+= $(LIBNCRYPTOBJS)
# libhelp
@if USE_DEVEL_HELP
LIBHELP= libhelp.a
LIBHELPOBJS= help/help.o help/vector.o
LIBHELPOBJS= help/help.o help/scan.o help/vector.o
CLEANFILES+= $(LIBHELP) $(LIBHELPOBJS)
MUTTLIBS+= $(LIBHELP)
ALLOBJS+= $(LIBHELPOBJS)
Expand Down
51 changes: 10 additions & 41 deletions help/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "config.h"
#include <stddef.h>
#include <errno.h>
#include <ftw.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
Expand All @@ -36,15 +35,15 @@
#include <unistd.h>
#include "mutt/mutt.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "email/lib.h"
#include "help.h"
#include "core/lib.h"
#include "globals.h"
#include "mutt_header.h"
#include "mutt_thread.h"
#include "muttlib.h"
#include "mx.h"
#include "scan.h"
#include "vector.h"

struct stat;
Expand Down Expand Up @@ -646,27 +645,6 @@ static void help_doc_uplink(const struct Email *target, const struct Email *sour
mutt_list_insert_tail(&source->env->references, mutt_str_strdup(tgt_msgid));
}

/**
* help_add_to_list - Callback for nftw whenever a file is read
* @param fpath Filename
* @param sb Timestamp for the file
* @param tflag File type
* @param ftwbuf Private nftw data
*
* @sa https://linux.die.net/man/3/nftw
*
* @note Only act on file
*/
static int help_add_to_list(const char *fpath, const struct stat *sb, int tflag,
struct FTW *ftwbuf)
{
mutt_debug(1, "entering add_to_list: '%s'\n", fpath);
if (tflag == FTW_F)
help_doc_gather(&DocList, fpath);

return 0; /* To tell nftw() to continue */
}

/**
* help_read_dir - Read a directory and process its entries recursively using nftw to
* find and link all help documents
Expand All @@ -679,11 +657,12 @@ static int help_read_dir(const char *path)
{
mutt_debug(1, "entering help_read_dir: '%s'\n", path);

// Max of 20 open file handles, 0 flags
if (nftw(path, help_add_to_list, 20, 0) == -1)
struct Vector *file_paths = scan_dir(path);

for (size_t i = 0; i < file_paths->size; i++)
{
perror("nftw");
return 1;
char *path2 = vector_get(file_paths, i, NULL);
help_doc_gather(&DocList, path2);
}
/* Sort 'index.md' in list to the top */
vector_sort(DocList, help_doc_type_cmp);
Expand Down Expand Up @@ -790,10 +769,9 @@ static int help_doclist_parse(struct Mailbox *m)
}

/* mark all but the first email as read */
m->emails[0]->read = false;
for (size_t i = 1; i < m->msg_count; i++)
for (size_t i = 0; i < m->msg_count; i++)
{
m->emails[i]->read = true;
m->emails[i]->read = false;
}
m->msg_unread = m->msg_count;

Expand All @@ -816,17 +794,8 @@ struct Account *help_ac_find(struct Account *a, const char *path)
*/
int help_ac_add(struct Account *a, struct Mailbox *m)
{
if (!a || !m)
return -1;

if (m->magic != MUTT_HELP)
if (!a || !m || (m->magic != MUTT_HELP))
return -1;

m->account = a;

struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
np->mailbox = m;
STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
return 0;
}

Expand Down
10 changes: 4 additions & 6 deletions help/help.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
#ifndef MUTT_HELP_HELP_H
#define MUTT_HELP_HELP_H

#include <stddef.h>
#include <stdint.h>
#include "mx.h"
#include "vector.h"

extern struct MxOps MxHelpOps;

Expand All @@ -41,12 +39,12 @@ typedef uint8_t HelpDocFlags; ///< Types of Help Documents, e.g. #HELP_DOC_I
#define HELP_DOC_SECTION (1 << 4) ///< Document is treated as help section

/**
* struct helpfile_header - Describes the header of a help file
* struct HelpFileHeader - Describes the header of a help file
*/
struct HelpFileHeader
{
char *key;
char *val;
char *key; ///< Name of header
char *val; ///< Value of header
};

/**
Expand All @@ -60,6 +58,6 @@ struct HelpDocMeta
};

void help_doclist_free(void);
int help_doclist_init(void);
int help_doclist_init(void);

#endif /* MUTT_HELP_HELP_H */
75 changes: 75 additions & 0 deletions help/scan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file
* Scan a directory with nftw
*
* @authors
* Copyright (C) 2019 Tran Manh Tu <xxlaguna93@gmail.com>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <ftw.h>
#include <string.h>
#include <stdio.h>
#include "mutt/mutt.h"
#include "scan.h"
#include "vector.h"

struct stat;

static struct Vector *DocPaths; ///< All valid help documents within $help_doc_dir folder

/**
* add_file - Callback for nftw whenever a file is read
* @param fpath Filename
* @param sb Timestamp for the file
* @param tflag File type
* @param ftwbuf Private nftw data
*
* @sa https://linux.die.net/man/3/nftw
*
* @note Only act on file
*/
static int add_file(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
{
if (tflag == FTW_F)
{
char *ext = strrchr(fpath, '.');
char *path = mutt_str_strdup(fpath);
if (ext && !mutt_str_strcmp(ext, ".md"))
{
vector_new_append(&DocPaths, sizeof(char *), path);
}
}
return 0; /* To tell nftw() to continue */
}

/**
* scan_dir - Scan a directory recursively using nftw to
* find all paths to .md files
* @param path absolute path of a directory
*/
struct Vector *scan_dir(const char *path)
{
vector_free(&DocPaths, NULL);
DocPaths = vector_new(sizeof(char *));
// Max of 20 open file handles, 0 flags
if (nftw(path, add_file, 20, 0) == -1)
{
perror("nftw");
}

return DocPaths;
}
28 changes: 28 additions & 0 deletions help/scan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file
* Scan a directory with nftw
*
* @authors
* Copyright (C) 2019 Tran Manh Tu <xxlaguna93@gmail.com>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MUTT_HELP_SCAN_H
#define MUTT_HELP_SCAN_H

struct Vector *scan_dir(const char *path);

#endif /* MUTT_HELP_SCAN_H */
Loading

0 comments on commit 31b022a

Please sign in to comment.