fsautoproc
Basic file transformation automation management utility
|
File-specific system command execution mapping implementation. More...
#include "lcmd.h"
#include <assert.h>
#include <errno.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "cJSON/cJSON.h"
#include "fd.h"
#include "index.h"
#include "log.h"
#include "sl.h"
#include "tm.h"
Functions | |
static void | lcmdfree (struct lcmdset_s *cmd) |
Frees the memory allocated for a single command set entry struct. | |
void | lcmdfree_r (struct lcmdset_s **cs) |
Iterates and frees all memory allocated by the command set array. | |
static char * | fsreadstr (const char *fp) |
Reads the contents of the file described by filepath fp into a dynamically allocated buffer returned by the function. | |
static slist_t * | lcmdjsontosl (const cJSON *arr) |
Duplicates a cJSON array of strings into a slist_t. cJSON array entries that fail cJSON_IsString will be ignored and a warning printed. | |
static int | lcmdparseflags (const cJSON *item) |
Parses a cJSON array of strings into a set of file event bit flags. Non-string entries are ignored. Unrecognized string entries will log an error message. Accepted strings are "new", "mod", "del", and "nop" which map to LCTRIG_NEW , LCTRIG_MOD , LCTRIG_DEL , and LCTRIG_NOP respectively. | |
static int | lcmdparseone (const cJSON *obj, struct lcmdset_s *cmd, const int id) |
Populates a single command struct by parsing the fields of the provided cJSON object. | |
struct lcmdset_s ** | lcmdparse (const char *fp) |
Prses the provided file path and populates an array of command sets. The file must be a valid JSON file containing an array of objects. Each object must contain the following keys: | |
static bool | lcmdmatch (regex_t **fpatterns, const char *fp) |
Checks if the provided filepath matches any of the compiled regex patterns in the provided array. | |
bool | lcmdmatchany (struct lcmdset_s **cs, const char *fp) |
Checks if the provided file path matches any of the file patterns in the command set. | |
static int | lcmdinvoke (const char *cmd, const struct inode_s *node, const struct fdset_s *fds, const int flags, uint64_t *msspent) |
Invokes a string cmd as a system command using system(3) in a forked/child process. The file path of node is set as an environment variable for use in the command. File descriptor set fds is used to optionally redirect stdout and stderr of the child command processes. | |
int | lcmdexec (struct lcmdset_s **cs, const struct inode_s *node, const struct fdset_s *fds, int flags) |
Sequentially iterates the command set and executes the configured system commands on the provided file node if the trigger flags and file patterns match. | |
File-specific system command execution mapping implementation.
|
static |
Reads the contents of the file described by filepath fp
into a dynamically allocated buffer returned by the function.
fp | The filepath to read |
int lcmdexec | ( | struct lcmdset_s ** | cs, |
const struct inode_s * | node, | ||
const struct fdset_s * | fds, | ||
int | flags ) |
Sequentially iterates the command set and executes the configured system commands on the provided file node if the trigger flags and file patterns match.
cs | The command set array to filter and execute |
node | The file node to execute on |
fds | The file descriptor set to use for stdout/stderr redirection |
flags | The trigger flags to match, see LCTRIG_* . If LCTOPT_VERBOSE is set, the commands will be printed to stdout before execution. If LCTOPT_TRACE is set, the true/false match result for each command set will be printed to stdout. |
system(3)
is returned.
|
static |
Frees the memory allocated for a single command set entry struct.
cmd | Command set entry to free |
void lcmdfree_r | ( | struct lcmdset_s ** | cs | ) |
Iterates and frees all memory allocated by the command set array.
cs | The command set array to free |
|
static |
Invokes a string cmd
as a system command using system(3)
in a forked/child process. The file path of node
is set as an environment variable for use in the command. File descriptor set fds
is used to optionally redirect stdout and stderr of the child command processes.
cmd | The command string to execute |
node | The file node to use for the FILEPATH environment variable |
fds | The file descriptor set to use for stdout/stderr redirection |
flags | Bit flags for controlling command execution. If the LCTOPT_VERBOSE flag is set, the command will be printed to stdout before execution. If the LCTOPT_TRACE flag is set, debug information regarding the eligibility of any encountered file path will be printed to stdout. |
msspent | Optional pointer to a uint64_t value to which the time spent executing the command (in milliseconds) will be added. |
|
static |
Duplicates a cJSON array of strings into a slist_t. cJSON array entries that fail cJSON_IsString
will be ignored and a warning printed.
arr | cJSON array of strings |
slfree
.
|
static |
Checks if the provided filepath matches any of the compiled regex patterns in the provided array.
fpatterns | Array of compiled regex patterns |
fp | Filepath to match |
bool lcmdmatchany | ( | struct lcmdset_s ** | cs, |
const char * | fp ) |
Checks if the provided file path matches any of the file patterns in the command set.
cs | The command set array to filter |
fp | The file path to match |
struct lcmdset_s ** lcmdparse | ( | const char * | fp | ) |
Prses the provided file path and populates an array of command sets. The file must be a valid JSON file containing an array of objects. Each object must contain the following keys:
on
: An array of trigger flags to matchpatterns
: An array of file patterns to matchcommands
: An array of system commands to execute The on
array must contain one or more of the following strings:new
: Trigger on new filesmod
: Trigger on modified filesdel
: Trigger on deleted filesnop
: Trigger on no operation The patterns
array must contain one or more strings that are used to match the file path. The commands
array must contain one or more strings that are passed to system(3)
for execution. fp | The file path to parse |
|
static |
Parses a cJSON array of strings into a set of file event bit flags. Non-string entries are ignored. Unrecognized string entries will log an error message. Accepted strings are "new", "mod", "del", and "nop" which map to LCTRIG_NEW
, LCTRIG_MOD
, LCTRIG_DEL
, and LCTRIG_NOP
respectively.
item | cJSON array of strings |
|
static |
Populates a single command struct by parsing the fields of the provided cJSON object.
obj | cJSON object containing the command data |
cmd | Struct to populate with parsed command data |
id | Command set index for naming purposes |