<commit_before>/*
 * libcsync -- a library to sync a directory with another
 *
 * Copyright (c) 2006-2008 by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * vim: ts=2 sw=2 et cindent
 */

#define _GNU_SOURCE /* asprintf */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "c_lib.h"
#include "csync_private.h"
#include "csync_config.h"
#include "csync_lock.h"
#include "csync_exclude.h"
#include "csync_journal.h"

#include "csync_update.h"

#include "vio/csync_vio.h"

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

int csync_create(CSYNC **csync, const char *local, const char *remote) {
  CSYNC *ctx;
  size_t len = 0;

  ctx = c_malloc(sizeof(CSYNC));
  if (ctx == NULL) {
    return -1;
  }

  /* remove trailing slashes */
  len = strlen(local);
  while(len > 0 && local[len - 1] == '/') --len;

  ctx->local.uri = c_strndup(local, len);
  if (ctx->local.uri == NULL) {
    return -1;
  }

  /* remove trailing slashes */
  len = strlen(remote);
  while(len > 0 && remote[len - 1] == '/') --len;

  ctx->remote.uri = c_strndup(remote, len);
  if (ctx->remote.uri == NULL) {
    SAFE_FREE(ctx->remote.uri);
    return -1;
  }

  ctx->options.max_depth = MAX_DEPTH;
  ctx->options.max_time_difference = MAX_TIME_DIFFERENCE;

  if (asprintf(&ctx->options.config_dir, "%s/%s", getenv("HOME"), CSYNC_CONF_DIR) < 0) {
    SAFE_FREE(ctx->local.uri);
    SAFE_FREE(ctx->remote.uri);
    SAFE_FREE(ctx);
    errno = ENOMEM;
    return -1;
  }

  *csync = ctx;
  return 0;
}

int csync_init(CSYNC *ctx) {
  int rc;
  char *log = NULL;
  char *exclude = NULL;
  char *journal = NULL;
  char *lock = NULL;
  char *config = NULL;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  /* Do not initialize twice */
  if (ctx->initialized) {
    return 1;
  }

  /* load log file */
  if (csync_log_init() < 0) {
    fprintf(stderr, "csync_init: logger init failed\n");
    return -1;
  }

  /* create dir if it doesn't exist */
  if (! c_isdir(ctx->options.config_dir)) {
    c_mkdirs(ctx->options.config_dir, 0700);
  }

  if (asprintf(&log, "%s/%s", ctx->options.config_dir, CSYNC_LOG_FILE) < 0) {
    rc = -1;
    goto out;
  }

  /* load log if it exists */
  if (c_isfile(log)) {
    csync_log_load(log);
  } else {
    if (c_copy(SYSCONFDIR "/csync/" CSYNC_LOG_FILE, log, 0644) == 0) {
      csync_log_load(log);
    }
  }

  /* create lock file */
  if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_lock(lock) < 0) {
    rc = -1;
    goto out;
  }

  /* load config file */
  if (asprintf(&config, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_config_load(ctx, config) < 0) {
    rc = -1;
    goto out;
  }

  /* load global exclude list */
  if (asprintf(&exclude, "%s/csync/%s", SYSCONFDIR, CSYNC_EXCLUDE_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_exclude_load(ctx, exclude) < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load %s - %s", exclude, strerror(errno));
  }
  SAFE_FREE(exclude);

  /* load exclude list */
  if (asprintf(&exclude, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_exclude_load(ctx, exclude) < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load %s - %s", exclude, strerror(errno));
  }

  /* create/load journal */
  if (asprintf(&journal, "%s/%s", ctx->options.config_dir, CSYNC_JOURNAL_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_journal_load(ctx, journal) < 0) {
    rc = -1;
    goto out;
  }

  /* TODO: load plugins */
  if (csync_vio_init(ctx, "smb", "") < 0) {
    rc = -1;
    goto out;
  }

  ctx->initialized = 1;

  rc = 0;

out:
  SAFE_FREE(log);
  SAFE_FREE(lock);
  SAFE_FREE(exclude);
  SAFE_FREE(journal);
  SAFE_FREE(config);
  return rc;
}

int csync_update(CSYNC *ctx) {
  time_t start, finish;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  time(&start);
  ctx->replica = LOCAL_REPLICA;
  if (csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH) < 0) {
    return -1;
  }
  time(&finish);
  CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
            "Update detection for local replica took %.2f seconds",
            difftime(finish, start));

#if 0
  time(&start);
  ctx->replica = REMOTE_REPLICA;
  if (csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH) < 0) {
    return -1;
  }
  time(&finish);
  CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
            "Update detection for remote replica took %.2f seconds",
            difftime(finish, start));
#endif

  return 0;
}

int csync_destroy(CSYNC *ctx) {
  char *lock = NULL;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  csync_vio_shutdown(ctx);

  /* TODO: write journal */

  if (ctx->journal) {
    sqlite3_close(ctx->journal);
    /* TODO if we successfully synchronized, overwrite the original journal */
  }

  csync_exclude_destroy(ctx);

  if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) {
    csync_lock_remove(lock);
  }

  /* TODO: destroy the rbtree */

  csync_log_fini();

  c_rbtree_free(ctx->local.tree);
  c_rbtree_free(ctx->remote.tree);
  SAFE_FREE(ctx->local.uri);
  SAFE_FREE(ctx->remote.uri);
  SAFE_FREE(ctx->options.config_dir);

  SAFE_FREE(ctx);

  SAFE_FREE(lock);

  return 0;
}

const char *csync_version(void) {
  return CSYNC_VERSION_STRING;
}

<commit_msg>Check which module we have to load.<commit_after>/*
 * libcsync -- a library to sync a directory with another
 *
 * Copyright (c) 2006-2008 by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * vim: ts=2 sw=2 et cindent
 */

#define _GNU_SOURCE /* asprintf */

#include <errno.h>
#include <fnmatch.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "c_lib.h"
#include "csync_private.h"
#include "csync_config.h"
#include "csync_lock.h"
#include "csync_exclude.h"
#include "csync_journal.h"

#include "csync_update.h"

#include "vio/csync_vio.h"

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

int csync_create(CSYNC **csync, const char *local, const char *remote) {
  CSYNC *ctx;
  size_t len = 0;

  ctx = c_malloc(sizeof(CSYNC));
  if (ctx == NULL) {
    return -1;
  }

  /* remove trailing slashes */
  len = strlen(local);
  while(len > 0 && local[len - 1] == '/') --len;

  ctx->local.uri = c_strndup(local, len);
  if (ctx->local.uri == NULL) {
    return -1;
  }

  /* remove trailing slashes */
  len = strlen(remote);
  while(len > 0 && remote[len - 1] == '/') --len;

  ctx->remote.uri = c_strndup(remote, len);
  if (ctx->remote.uri == NULL) {
    SAFE_FREE(ctx->remote.uri);
    return -1;
  }

  ctx->options.max_depth = MAX_DEPTH;
  ctx->options.max_time_difference = MAX_TIME_DIFFERENCE;

  if (asprintf(&ctx->options.config_dir, "%s/%s", getenv("HOME"), CSYNC_CONF_DIR) < 0) {
    SAFE_FREE(ctx->local.uri);
    SAFE_FREE(ctx->remote.uri);
    SAFE_FREE(ctx);
    errno = ENOMEM;
    return -1;
  }

  *csync = ctx;
  return 0;
}

int csync_init(CSYNC *ctx) {
  int rc;
  char *log = NULL;
  char *exclude = NULL;
  char *journal = NULL;
  char *lock = NULL;
  char *config = NULL;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  /* Do not initialize twice */
  if (ctx->initialized) {
    return 1;
  }

  /* load log file */
  if (csync_log_init() < 0) {
    fprintf(stderr, "csync_init: logger init failed\n");
    return -1;
  }

  /* create dir if it doesn't exist */
  if (! c_isdir(ctx->options.config_dir)) {
    c_mkdirs(ctx->options.config_dir, 0700);
  }

  if (asprintf(&log, "%s/%s", ctx->options.config_dir, CSYNC_LOG_FILE) < 0) {
    rc = -1;
    goto out;
  }

  /* load log if it exists */
  if (c_isfile(log)) {
    csync_log_load(log);
  } else {
    if (c_copy(SYSCONFDIR "/csync/" CSYNC_LOG_FILE, log, 0644) == 0) {
      csync_log_load(log);
    }
  }

  /* create lock file */
  if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_lock(lock) < 0) {
    rc = -1;
    goto out;
  }

  /* load config file */
  if (asprintf(&config, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_config_load(ctx, config) < 0) {
    rc = -1;
    goto out;
  }

  /* load global exclude list */
  if (asprintf(&exclude, "%s/csync/%s", SYSCONFDIR, CSYNC_EXCLUDE_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_exclude_load(ctx, exclude) < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load %s - %s", exclude, strerror(errno));
  }
  SAFE_FREE(exclude);

  /* load exclude list */
  if (asprintf(&exclude, "%s/%s", ctx->options.config_dir, CSYNC_CONF_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_exclude_load(ctx, exclude) < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not load %s - %s", exclude, strerror(errno));
  }

  /* create/load journal */
  if (asprintf(&journal, "%s/%s", ctx->options.config_dir, CSYNC_JOURNAL_FILE) < 0) {
    rc = -1;
    goto out;
  }

  if (csync_journal_load(ctx, journal) < 0) {
    rc = -1;
    goto out;
  }

  /* check for uri */
  if (fnmatch("*://*", ctx->remote.uri, 0) == 0) {
    size_t len;
    len = strstr(ctx->remote.uri, "://") - ctx->remote.uri;
    /* get protocol */
    if (len > 0) {
      char *module = NULL;
      /* module name */
      module = c_strndup(ctx->remote.uri, len);
      if (module == NULL) {
        rc = -1;
        goto out;
      }
      /* load module */
      if (csync_vio_init(ctx, module, NULL) < 0) {
        rc = -1;
        goto out;
      }
    }
  }

  ctx->initialized = 1;

  rc = 0;

out:
  SAFE_FREE(log);
  SAFE_FREE(lock);
  SAFE_FREE(exclude);
  SAFE_FREE(journal);
  SAFE_FREE(config);
  SAFE_FREE(module);
  return rc;
}

int csync_update(CSYNC *ctx) {
  time_t start, finish;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  time(&start);
  ctx->replica = LOCAL_REPLICA;
  if (csync_ftw(ctx, ctx->local.uri, csync_walker, MAX_DEPTH) < 0) {
    return -1;
  }
  time(&finish);
  CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
            "Update detection for local replica took %.2f seconds",
            difftime(finish, start));

#if 0
  time(&start);
  ctx->replica = REMOTE_REPLICA;
  if (csync_ftw(ctx, ctx->remote.uri, csync_walker, MAX_DEPTH) < 0) {
    return -1;
  }
  time(&finish);
  CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
            "Update detection for remote replica took %.2f seconds",
            difftime(finish, start));
#endif

  return 0;
}

int csync_destroy(CSYNC *ctx) {
  char *lock = NULL;

  if (ctx == NULL) {
    errno = EBADF;
    return -1;
  }

  csync_vio_shutdown(ctx);

  /* TODO: write journal */

  if (ctx->journal) {
    sqlite3_close(ctx->journal);
    /* TODO if we successfully synchronized, overwrite the original journal */
  }

  csync_exclude_destroy(ctx);

  if (asprintf(&lock, "%s/%s", ctx->options.config_dir, CSYNC_LOCK_FILE) > 0) {
    csync_lock_remove(lock);
  }

  /* TODO: destroy the rbtree */

  csync_log_fini();

  c_rbtree_free(ctx->local.tree);
  c_rbtree_free(ctx->remote.tree);
  SAFE_FREE(ctx->local.uri);
  SAFE_FREE(ctx->remote.uri);
  SAFE_FREE(ctx->options.config_dir);

  SAFE_FREE(ctx);

  SAFE_FREE(lock);

  return 0;
}

const char *csync_version(void) {
  return CSYNC_VERSION_STRING;
}


{'fraction_non_alphanumeric': 0.14994495989935525, 'fraction_numerical': 0.013052366724327724, 'mean_word_length': 2.9871473354231974, 'pattern_counts': {'":': 1, '<': 50, '<?xml version=': 0, '>': 91, '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': '7165685', 'n_tokens_mistral': 5075, 'n_tokens_neox': 4870, 'n_words': 1216}