ftwin 0.8.10
ft_ignore.c File Reference

Implementation of the hierarchical .gitignore-style pattern matching logic. More...

#include "ft_ignore.h"
#include "debug.h"
#include <apr_file_io.h>
#include <apr_strings.h>
#include <string.h>
#include <ctype.h>
Include dependency graph for ft_ignore.c:

Go to the source code of this file.

Functions

static char * ft_glob_to_pcre (const char *pattern, apr_pool_t *pool, unsigned int *flags)
 Convert Git glob pattern to PCRE regex Handles: *, **, ?, [abc], !, /, escapes.
 
ft_ignore_context_tft_ignore_context_create (apr_pool_t *pool, ft_ignore_context_t *parent, const char *base_dir)
 Creates a new ignore context.
 
apr_status_t ft_ignore_add_pattern_str (ft_ignore_context_t *ctx, const char *pattern_str)
 Adds a single pattern string to a context.
 
apr_status_t ft_ignore_load_file (ft_ignore_context_t *ctx, const char *filepath)
 Loads and parses an ignore file (like .gitignore) into a context.
 
ft_ignore_match_result_t ft_ignore_match (ft_ignore_context_t *ctx, const char *fullpath, int is_dir)
 Checks if a given path should be ignored based on the hierarchical context.
 

Variables

static const size_t MAX_PATTERN_LEN = 4096
 The maximum length of a pattern string.
 

Detailed Description

Implementation of the hierarchical .gitignore-style pattern matching logic.

Definition in file ft_ignore.c.

Function Documentation

◆ ft_ignore_add_pattern_str()

apr_status_t ft_ignore_add_pattern_str ( ft_ignore_context_t ctx,
const char *  pattern_str 
)

Adds a single pattern string to a context.

The pattern is parsed, compiled into a regex, and added to the context's list.

Parameters
[in]ctxThe context to add the pattern to.
[in]pattern_strThe raw pattern string to add.
Returns
APR_SUCCESS on success, or an error code on failure.

Definition at line 197 of file ft_ignore.c.

References DEBUG_ERR, ft_ignore_pattern_t::flags, ft_glob_to_pcre(), ft_ignore_pattern_t::pattern_str, ft_ignore_context_t::patterns, ft_ignore_context_t::pool, and ft_ignore_pattern_t::regex.

Referenced by ft_ignore_load_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_ignore_context_create()

ft_ignore_context_t * ft_ignore_context_create ( apr_pool_t *  pool,
ft_ignore_context_t parent,
const char *  base_dir 
)

Creates a new ignore context.

Parameters
[in]poolThe APR pool to allocate the new context from.
[in]parentThe parent context, or NULL to create a root context.
[in]base_dirThe absolute path to the directory this context represents.
Returns
A pointer to the newly created context.

Definition at line 184 of file ft_ignore.c.

References ft_ignore_context_t::base_dir, ft_ignore_context_t::base_dir_len, ft_ignore_context_t::parent, ft_ignore_context_t::patterns, and ft_ignore_context_t::pool.

◆ ft_ignore_load_file()

apr_status_t ft_ignore_load_file ( ft_ignore_context_t ctx,
const char *  filepath 
)

Loads and parses an ignore file (like .gitignore) into a context.

Parameters
[in]ctxThe context to load the patterns into.
[in]filepathThe path to the ignore file.
Returns
APR_SUCCESS on success, or an error code on failure.

Definition at line 239 of file ft_ignore.c.

References ft_ignore_add_pattern_str(), MAX_PATTERN_LEN, and ft_ignore_context_t::pool.

Here is the call graph for this function:

◆ ft_ignore_match()

ft_ignore_match_result_t ft_ignore_match ( ft_ignore_context_t ctx,
const char *  fullpath,
int  is_dir 
)

Checks if a given path should be ignored based on the hierarchical context.

It traverses the context hierarchy from the current context up to the root, applying rules from each level. The last matching pattern determines the outcome.

Parameters
[in]ctxThe starting context for matching (usually the one for the file's direct parent).
[in]fullpathThe absolute path of the file or directory to check.
[in]is_dirA flag indicating if the path is a directory.
Returns
The match result (ignored, whitelisted, or no match).

Definition at line 268 of file ft_ignore.c.

References ft_ignore_context_t::base_dir, ft_ignore_context_t::base_dir_len, ft_ignore_pattern_t::flags, FT_IGNORE_MATCH_IGNORED, FT_IGNORE_MATCH_NONE, FT_IGNORE_MATCH_WHITELISTED, ft_ignore_context_t::parent, ft_ignore_context_t::patterns, and ft_ignore_pattern_t::regex.

Variable Documentation

◆ MAX_PATTERN_LEN

const size_t MAX_PATTERN_LEN = 4096
static

The maximum length of a pattern string.

This constant defines the maximum buffer size for reading and processing a single pattern from a .gitignore file.

Definition at line 35 of file ft_ignore.c.

Referenced by ft_glob_to_pcre(), and ft_ignore_load_file().