<commit_before>/*
 * libcsync -- a library to sync a directory with another
 *
 * Copyright (c) 2008      by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * vim: ts=2 sw=2 et cindent
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <unistd.h>

#include "c_lib.h"

#include "csync_private.h"
#include "csync_exclude.h"

#define CSYNC_LOG_CATEGORY_NAME "csync.exclude"
#include "csync_log.h"

static void _csync_exclude_add(CSYNC *ctx, const char *string) {
  if (ctx->excludes == NULL) {
    ctx->excludes = c_strlist_new(32);
  }

  if (ctx->excludes->count == ctx->excludes->size) {
    ctx->excludes = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size);
  }

  c_strlist_add(ctx->excludes, string);
}

int csync_exclude_load(CSYNC *ctx, const char *fname) {
  int fd = -1;
  int i = 0;
  int rc = -1;
  off_t size;
  char *buf = NULL;
  char *entry = NULL;

  fd = open(fname, O_RDONLY);
  if (fd < 0) {
    return -1;
  }

  size = lseek(fd, 0, SEEK_END);
  if (size < 0) {
    rc = -1;
    goto out;
  }
  lseek(fd, 0, SEEK_SET);
  if (size == 0) {
    rc = 0;
    goto out;
  }
  buf = c_malloc(size);
  if (read(fd, buf, size) != size) {
    rc = -1;
    goto out;
  }
  close(fd);

  /* FIXME: Don't add duplicates */
  entry = buf;
  for (i = 0; i < size; i++) {
    if (buf[i] == '\n') {
      if (entry != buf + i) {
        buf[i] = '\0';
        if (*entry != '#' || *entry == '\n') {
          CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Adding entry: %s", entry);
          _csync_exclude_add(ctx, entry);
        }
      }
      entry = buf + i + 1;
    }
  }
  SAFE_FREE(buf);

  rc = 0;
out:
  SAFE_FREE(buf);
  close(fd);
  return rc;
}

void csync_exclude_destroy(CSYNC *ctx) {
  c_strlist_destroy(ctx->excludes);
}

int csync_excluded(CSYNC *ctx, const char *path) {
  size_t i;
  const char *p;

  if (! ctx->options.unix_filesystem) {
    for (p = path; *p; p++) {
      switch (*p) {
        case '/':
        case '\\':
        case ':':
        case '?':
        case '*':
        case '"':
        case '>':
        case '<':
        case '|':
          return 1;
        default:
          break;
      }
    }
  }

  if (ctx->excludes == NULL) {
    return 0;
  }

  if (ctx->excludes->count) {
    for (i = 0; i < ctx->excludes->count; i++) {
      if (fnmatch(ctx->excludes->vector[i], path, 0) == 0) {
        return 1;
      }
    }
  }
  return 0;
}
<commit_msg>Don't exclude paths.<commit_after>/*
 * libcsync -- a library to sync a directory with another
 *
 * Copyright (c) 2008      by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * vim: ts=2 sw=2 et cindent
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <unistd.h>

#include "c_lib.h"

#include "csync_private.h"
#include "csync_exclude.h"

#define CSYNC_LOG_CATEGORY_NAME "csync.exclude"
#include "csync_log.h"

static void _csync_exclude_add(CSYNC *ctx, const char *string) {
  if (ctx->excludes == NULL) {
    ctx->excludes = c_strlist_new(32);
  }

  if (ctx->excludes->count == ctx->excludes->size) {
    ctx->excludes = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size);
  }

  c_strlist_add(ctx->excludes, string);
}

int csync_exclude_load(CSYNC *ctx, const char *fname) {
  int fd = -1;
  int i = 0;
  int rc = -1;
  off_t size;
  char *buf = NULL;
  char *entry = NULL;

  fd = open(fname, O_RDONLY);
  if (fd < 0) {
    return -1;
  }

  size = lseek(fd, 0, SEEK_END);
  if (size < 0) {
    rc = -1;
    goto out;
  }
  lseek(fd, 0, SEEK_SET);
  if (size == 0) {
    rc = 0;
    goto out;
  }
  buf = c_malloc(size);
  if (read(fd, buf, size) != size) {
    rc = -1;
    goto out;
  }
  close(fd);

  /* FIXME: Don't add duplicates */
  entry = buf;
  for (i = 0; i < size; i++) {
    if (buf[i] == '\n') {
      if (entry != buf + i) {
        buf[i] = '\0';
        if (*entry != '#' || *entry == '\n') {
          CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Adding entry: %s", entry);
          _csync_exclude_add(ctx, entry);
        }
      }
      entry = buf + i + 1;
    }
  }
  SAFE_FREE(buf);

  rc = 0;
out:
  SAFE_FREE(buf);
  close(fd);
  return rc;
}

void csync_exclude_destroy(CSYNC *ctx) {
  c_strlist_destroy(ctx->excludes);
}

int csync_excluded(CSYNC *ctx, const char *path) {
  size_t i;
  const char *p;

  if (! ctx->options.unix_filesystem) {
    for (p = path; *p; p++) {
      switch (*p) {
        case '\\':
        case ':':
        case '?':
        case '*':
        case '"':
        case '>':
        case '<':
        case '|':
          return 1;
        default:
          break;
      }
    }
  }

  if (ctx->excludes == NULL) {
    return 0;
  }

  if (ctx->excludes->count) {
    for (i = 0; i < ctx->excludes->count; i++) {
      if (fnmatch(ctx->excludes->vector[i], path, 0) == 0) {
        return 1;
      }
    }
  }
  return 0;
}

{'fraction_non_alphanumeric': 0.147982777866369, 'fraction_numerical': 0.01403284962525913, 'mean_word_length': 2.4863813229571985, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2182534', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2300, 'n_words': 687}