1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/mkinitrd/rules.c

318 lines
7.6 KiB
C
Raw Normal View History

/*
2016-08-09 19:29:04 -04:00
* Copyright (c) 2013, 2015, 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* rules.c
* Determines whether a given path is included in the filesystem.
*/
#include <ctype.h>
2016-08-09 19:29:04 -04:00
#include <err.h>
#include <errno.h>
2016-02-07 16:20:12 -05:00
#include <libgen.h>
#include <stdarg.h>
2016-02-28 17:03:16 -05:00
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rules.h"
2016-02-28 17:03:16 -05:00
static struct InclusionRule** rules;
static size_t num_rules;
static size_t num_rules_allocated;
static bool default_inclusion = true;
static bool default_inclusion_determined;
static char** manifest;
static size_t manifest_used;
static size_t manifest_length;
static const char* SkipCharacters(const char* str, char c)
{
while ( *str == c)
str++;
return str;
}
// /usr/bin/foobar match /usr = true
// /usr/bin/foobar match usr = false
// ///usr////bin//foobar match //usr// = true
// ///usr////bin//foobar match //usr//./evince = false
// TODO: Should this support . and .. too?
static bool PathMatchesPattern(const char* path, const char* pattern)
{
bool last_was_slash = false;
while ( true )
{
if ( !*pattern )
return !*path || last_was_slash;
if ( (last_was_slash = *pattern == '/') )
{
if ( *path == '/' )
{
path = SkipCharacters(path, '/');
pattern = SkipCharacters(pattern, '/');
continue;
}
return false;
}
if ( *pattern++ != *path++ )
return false;
}
}
2015-12-27 20:04:01 -05:00
static int search_path(const void* a_ptr, const void* b_ptr)
{
const char* key = (const char*) a_ptr;
char* path = *(char**) b_ptr;
return strcmp(key, path);
}
2016-02-28 17:03:16 -05:00
bool IncludesPath(const char* path)
{
bool determined = false;
bool included = false;
for ( size_t i = 0; i < num_rules; i++ )
{
2016-02-28 17:03:16 -05:00
struct InclusionRule* rule = rules[i];
if ( !PathMatchesPattern(path, rule->pattern) )
continue;
switch ( rules[i]->rule )
{
case RULE_INCLUDE:
included = true;
determined = true;
break;
case RULE_EXCLUDE:
included = false;
determined = true;
break;
}
}
if ( !determined )
included = default_inclusion;
2015-12-27 20:04:01 -05:00
if ( !included )
return false;
if ( manifest_used &&
!bsearch(path, manifest, manifest_used, sizeof(char*), search_path) )
return false;
return true;
}
2016-02-28 17:03:16 -05:00
bool ChangeRulesAmount(size_t new_length)
{
size_t new_num_rules = new_length < num_rules ? new_length : num_rules;
for ( size_t i = new_num_rules; i < num_rules; i++ )
2016-02-28 17:03:16 -05:00
{
free(rules[i]->pattern);
free(rules[i]);
}
num_rules = new_num_rules;
2016-02-28 17:03:16 -05:00
struct InclusionRule** new_rules = (struct InclusionRule**)
malloc(sizeof(struct InclusionRule*) * new_length);
for ( size_t i = 0; i < new_length && i < num_rules; i++ )
new_rules[i] = rules[i];
2016-02-28 17:03:16 -05:00
free(rules); rules = new_rules;
num_rules_allocated = new_length;
return true;
}
2016-02-28 17:03:16 -05:00
bool AddRule(struct InclusionRule* rule)
{
if ( num_rules == num_rules_allocated )
{
size_t new_length = num_rules_allocated ? 2 * num_rules_allocated : 32;
if ( !ChangeRulesAmount(new_length) )
return false;
}
rules[num_rules++] = rule;
return true;
}
static const char* SkipWhitespace(const char* line)
{
while ( *line && isspace((unsigned char) *line) )
line++;
return line;
}
static bool IsLineComment(const char* line)
{
return !*line || *line == '#';
}
static const char* IsLineCommand(const char* line, const char* command)
{
while ( *line && isspace((unsigned char) *line) )
line++;
size_t cmdlen = strlen(command);
if ( strncmp(line, command, cmdlen) != 0 )
return NULL;
if ( line[cmdlen] && !isspace((unsigned char) line[cmdlen]) )
return NULL;
while ( line[cmdlen] && isspace((unsigned char) line[cmdlen]) )
cmdlen++;
return line + cmdlen;
}
2016-02-28 17:03:16 -05:00
bool AddRulesFromFile(FILE* fp, const char* fpname)
{
size_t rules_at_start = num_rules;
size_t line_size;
size_t line_num = 0;
char* mem = NULL;
ssize_t line_len;
2016-05-15 10:32:38 -04:00
while ( 0 < (line_len = getline(&mem, &line_size, fp)) )
{
char* line = mem;
line_num++;
2016-05-15 10:32:38 -04:00
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
2016-02-28 17:03:16 -05:00
line = (char*) SkipWhitespace((char*) line);
if ( IsLineComment(line) )
continue;
const char* parameter;
if ( (parameter = IsLineCommand(line, "default")) )
{
bool value;
if ( !strcmp(parameter, "true") )
value = true;
else if ( !strcmp(parameter, "false") )
value = false;
else
{
2016-08-09 19:29:04 -04:00
warnx("%s:%zu: not a boolean '%s'", fpname, line_num, parameter);
goto error_out;
}
if ( !default_inclusion_determined )
default_inclusion = value,
default_inclusion_determined = true;
else
default_inclusion = default_inclusion || value;
}
else if ( (parameter = IsLineCommand(line, "exclude")) ||
(parameter = IsLineCommand(line, "include")) )
{
if ( !*parameter )
{
2016-08-09 19:29:04 -04:00
warnx("%s:%zu: no parameter given", fpname, line_num);
goto error_out;
}
const char* pattern = parameter;
2016-02-28 17:03:16 -05:00
enum InclusionRuleType type = line[0] == 'e' ? RULE_EXCLUDE : RULE_INCLUDE;
struct InclusionRule* rule =
(struct InclusionRule*) malloc(sizeof(struct InclusionRule));
rule->pattern = strdup(pattern);
rule->rule = type;
if ( !AddRule(rule) )
goto error_out_errno;
}
else
{
2016-08-09 19:29:04 -04:00
warnx("%s:%zu: line not understood: '%s'", fpname, line_num, line);
goto error_out;
}
}
if ( ferror(fp) )
{
error_out_errno:
2016-08-09 19:29:04 -04:00
warn("%s", fpname);
error_out:
free(mem);
ChangeRulesAmount(rules_at_start);
return false;
}
free(mem);
return true;
}
2015-12-27 20:04:01 -05:00
int compare_path(const void* a_ptr, const void* b_ptr)
{
const char* a = *(const char* const*) a_ptr;
const char* b = *(const char* const*) b_ptr;
return strcmp(a, b);
}
2016-02-28 17:03:16 -05:00
bool AddManifestPath(const char* path)
2016-02-07 16:20:12 -05:00
{
if ( manifest_used == manifest_length )
{
size_t new_length = 2 * manifest_length;
if ( new_length == 0 )
new_length = 64;
size_t new_size = new_length * sizeof(char*);
char** new_manifest = (char**) realloc(manifest, new_size);
if ( !new_manifest )
{
2016-08-09 19:29:04 -04:00
warn("malloc");
2016-02-07 16:20:12 -05:00
return false;
}
manifest = new_manifest;
manifest_length = new_length;
}
char* copy = strdup(path);
if ( !copy )
{
2016-08-09 19:29:04 -04:00
warn("malloc");
2016-02-07 16:20:12 -05:00
return false;
}
manifest[manifest_used++] = copy;
return true;
}
2016-02-28 17:03:16 -05:00
bool AddManifestFromFile(FILE* fp, const char* fpname)
2015-12-27 20:04:01 -05:00
{
char* line = NULL;
size_t line_size = 0;
ssize_t line_len;
2016-05-15 10:32:38 -04:00
while ( 0 < (line_len = getline(&line, &line_size, fp)) )
2015-12-27 20:04:01 -05:00
{
2016-05-15 10:32:38 -04:00
if ( line[line_len-1] == '\n' )
line[--line_len] = '\0';
2016-02-28 17:03:16 -05:00
if ( !AddManifestPath(line) )
2016-02-07 16:20:12 -05:00
return false;
2015-12-27 20:04:01 -05:00
}
free(line);
if ( ferror(fp) )
{
2016-08-09 19:29:04 -04:00
warn("%s", fpname);
2015-12-27 20:04:01 -05:00
return false;
}
2016-02-28 17:03:16 -05:00
if ( !AddManifestPath("/") ||
!AddManifestPath("/tix") ||
!AddManifestPath("/tix/manifest") )
2016-02-07 16:20:12 -05:00
return false;
char* fpname_copy = strdup(fpname);
if ( !fpname_copy )
{
2016-08-09 19:29:04 -04:00
warn("malloc");
2016-02-07 16:20:12 -05:00
return false;
}
const char* fpname_basename = basename(fpname_copy);
char* manifest_path;
if ( asprintf(&manifest_path, "/tix/manifest/%s", fpname_basename) < 0 )
{
free(fpname_copy);
2016-08-09 19:29:04 -04:00
warn("malloc");
2016-02-07 16:20:12 -05:00
return false;
}
free(fpname_copy);
2016-02-28 17:03:16 -05:00
if ( !AddManifestPath(manifest_path) )
2016-02-07 16:20:12 -05:00
return free(manifest_path), false;
free(manifest_path);
2015-12-27 20:04:01 -05:00
qsort(manifest, manifest_used, sizeof(char*), compare_path);
return true;
}