zpmod  b19981f
High-performance Zsh module for script optimization and filesystem helpers
fs.c File Reference

Core filesystem routines used by builtins and subcommands. More...

#include "zpmod.mdh"
#include "zpmod.pro"
#include "zpmod_vendor_shims.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "zpmod_fs.h"
Include dependency graph for fs.c:

Go to the source code of this file.

Data Structures

struct  zp_fs_cache_entry
 

Macros

#define GET_ZPMOD_CONFIG(K)   getsparam("ZPMOD_" #K)
 
#define ZP_FS_CACHE_MAX   64
 

Functions

int zp_pathstat_core (char *nam, char *outname, char *inname, int follow, char *fields)
 See zpmod_fs.h for contract. More...
 
static int zp_fs_cache_enabled (void)
 
static int zp_fs_cache_lookup (const char *dir, struct stat *st, int *out_idx)
 
static int zp_fs_cache_insert_dir (const char *dir, const struct stat *st, char *serialized)
 
int zp_path_warmup_core (const char *nam, int quiet, int prune_missing, int dry_run)
 Implements path-warmup functionality for executable discovery and path pruning. More...
 
int zp_dirlist_core (char *nam, char *outname, char *dir, int inc_all, int only_dirs, int only_files)
 See zpmod_fs.h for contract. More...
 
int zp_readfile_core (char *nam, char *outname, char *path, int use_mmap, int split, int delim)
 See zpmod_fs.h for contract. More...
 

Variables

static zp_fs_cache_entry zp_fs_cache [ZP_FS_CACHE_MAX]
 
static int zp_fs_cache_count = 0
 

Detailed Description

Core filesystem routines used by builtins and subcommands.

Definition in file fs.c.

Macro Definition Documentation

◆ GET_ZPMOD_CONFIG

#define GET_ZPMOD_CONFIG (   K)    getsparam("ZPMOD_" #K)

Definition at line 30 of file fs.c.

◆ ZP_FS_CACHE_MAX

#define ZP_FS_CACHE_MAX   64

Definition at line 158 of file fs.c.

Function Documentation

◆ zp_dirlist_core()

int zp_dirlist_core ( char *  nam,
char *  outname,
char *  dir,
int  inc_all,
int  only_dirs,
int  only_files 
)

See zpmod_fs.h for contract.

List directory entries into an output array with filters.

Definition at line 382 of file fs.c.

References metafy(), PATH_MAX, setaparam(), setsparam(), unsetparam(), zalloc(), zfree(), zp_unmetafy_zalloc(), and zwarnnam().

Referenced by bin_zpdirlist(), and cmd_dirlist().

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

◆ zp_fs_cache_enabled()

static int zp_fs_cache_enabled ( void  )
static

Definition at line 144 of file fs.c.

References GET_ZPMOD_CONFIG.

◆ zp_fs_cache_insert_dir()

static int zp_fs_cache_insert_dir ( const char *  dir,
const struct stat *  st,
char *  serialized 
)
static

Definition at line 178 of file fs.c.

References zp_fs_cache_entry::dev, zp_fs_cache_entry::dir_entries, zp_fs_cache_entry::ino, zp_fs_cache_entry::is_dir, zp_fs_cache_entry::mtime, zp_fs_cache_entry::size, zp_fs_cache, zp_fs_cache_count, ZP_FS_CACHE_MAX, and zsfree().

Here is the call graph for this function:

◆ zp_fs_cache_lookup()

static int zp_fs_cache_lookup ( const char *  dir,
struct stat *  st,
int *  out_idx 
)
static

◆ zp_path_warmup_core()

int zp_path_warmup_core ( const char *  nam,
int  quiet,
int  prune_missing,
int  dry_run 
)

Implements path-warmup functionality for executable discovery and path pruning.

Scan $PATH directories to warm filesystem caches and prepare command lookup.

This function provides two main features:

  1. Executable Discovery: Scans all directories in $PATH and touches executable files to warm filesystem caches and improve command lookup performance.
  2. Path Pruning: Safely removes non-existent directories from $PATH to reduce lookup overhead and eliminate stale entries.

Memory Safety Design

The prune functionality was redesigned to avoid memory corruption issues that occurred when mixing getaparam("path") results with setaparam("path") calls. The original implementation caused double-free errors due to zsh's parameter management internals.

Solution: Use a two-pass approach with separate array construction:

  • Pass 1: Count valid directories without modifying the original array
  • Pass 2: Build a new array using ztrdup() for proper string ownership
  • Safe update: Use setaparam("path", new_array) without dependency on getaparam() result
Parameters
namBuiltin name for error reporting
quietSuppress progress messages if true
prune_missingRemove non-existent directories from path if true
dry_runReport what would be pruned without making changes if true
Returns
Number of executable files discovered, or negative on error
Note
This function follows zpmod's memory management patterns using zsh allocators (zalloc, ztrdup, zfree) to ensure compatibility with zsh's garbage collection.

Definition at line 254 of file fs.c.

References arrlen(), getaparam(), PATH_MAX, setaparam(), zalloc(), and ztrdup().

Referenced by bin_zpmod().

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

◆ zp_pathstat_core()

int zp_pathstat_core ( char *  nam,
char *  outname,
char *  inname,
int  follow,
char *  fields 
)

See zpmod_fs.h for contract.

Batch stat entries from an input array into an output array.

Definition at line 36 of file fs.c.

References getaparam(), metafy(), setaparam(), setsparam(), unsetparam(), zalloc(), zfree(), zp_unmetafy_zalloc(), and zwarnnam().

Referenced by bin_zppathstat(), and cmd_pathstat().

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

◆ zp_readfile_core()

int zp_readfile_core ( char *  nam,
char *  outname,
char *  path,
int  use_mmap,
int  split,
int  delim 
)

See zpmod_fs.h for contract.

Read a file into a scalar or split into an array by delimiter.

Definition at line 444 of file fs.c.

References metafy(), path, setaparam(), setsparam(), unsetparam(), zalloc(), zfree(), zp_unmetafy_zalloc(), zrealloc(), and zwarnnam().

Referenced by bin_zpreadfile(), and cmd_readfile().

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

Variable Documentation

◆ zp_fs_cache

zp_fs_cache_entry zp_fs_cache[ZP_FS_CACHE_MAX]
static

Definition at line 159 of file fs.c.

Referenced by zp_fs_cache_insert_dir(), and zp_fs_cache_lookup().

◆ zp_fs_cache_count

int zp_fs_cache_count = 0
static

Definition at line 160 of file fs.c.

Referenced by zp_fs_cache_insert_dir(), and zp_fs_cache_lookup().