Plan 9 from Bell Labs’s /usr/web/sources/contrib/de0u/root/sys/src/cmd/divergefs/namerule.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <String.h>
#include "common.h"
#include "string.h"
#include "debug.h"
#include "utils.h"
#include "filepath.h"
#include "rule.h"
#include "filedirrule.h"
#include "namerule.h"

enum 
{ 
  DEBUG_NAMERULE = false,
  DEBUG_EXTENSIONRULE = true
};

typedef struct NameRule
{
  FileDirRule;
  char *data;
} NameRule;

static Rule *namerule_new(char *data, char *root, 
  RuleOperations *ops, FileDirRuleOperations *fdops, char *name)
{
  NameRule *result;
  char buf[RULE_MAXNAMELEN];
  assert_valid(data);
  assert_valid(root);
  assert_valid(ops);
  assert_valid(fdops);

  result = (NameRule *)emalloc_fs(sizeof(*result));
  result->ops = ops;
  result->root = estrdup_fs(root);
  result->fdops = fdops;
  result->data = string_convert_escaped(estrdup_fs(data));
  snprint(buf, sizeof(buf), "%s<%s>", name, result->data);
  result->name = estrdup_fs(buf);
  return result;
}

static void namerule_free(Rule *rule)
{
  NameRule *self = (NameRule *)rule;

  free(self->data);
  self->data = nil;
  free(self->name);
  self->name = nil;
  free(self);
}


static bool extensionrule_contains_file(FileDirRule *rule, char *name);

static RuleOperations extensionops =
{
  .free       = namerule_free,
  .issatisfy  = filedirrule_file_issatisfy,
  .contains   = filedirrule_contains
};

static FileDirRuleOperations extensionfdops =
{
  .contains_file  = extensionrule_contains_file,
  .contains_dir   = filedirrule_contains_true
};

/**
 * @todo check if this is compatible with utf rune
 */
static bool extensionrule_contains_file(FileDirRule *rule, char *name)
{
  bool result;
  char *extension;
  NameRule *self = (NameRule *)rule;

  NOISE(DEBUG_EXTENSIONRULE, 
    "entering extensionrule_contains_file with name: %s", name);
  extension = string_after_last(name, '.');
  if(extension == nil)
  {
    NOISE(DEBUG_EXTENSIONRULE, "extensionrule_contains_file does not have dot");
    return false;
  }

  result = strcmp(self->data, extension) == 0;
  NOISE(DEBUG_EXTENSIONRULE, 
    "extensionrule_contains_file compare %s with %s result: %d",
    self->data, extension, result);
  return result;
}

Rule *extensionrule_parse(char *rule, char *setting, char *path)
{
  assert_valid(rule);
  assert_valid(setting);
  assert_valid(path);
  if(strcmp(rule, "ext") != 0)
  {
    return nil;
  }

  INFO(DEBUG_EXTENSIONRULE, "parsed ext rule with path: %s", path);
  return namerule_new(setting, path, &extensionops, &extensionfdops, "ext");
}



Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.