int csync_create(CSYNC **csync, const char *local, const char *remote) { CSYNC *ctx; size_t len = 0; char *home; int rc; ctx = c_malloc(sizeof(CSYNC)); if (ctx == NULL) { return -1; } ctx->status_code = CSYNC_STATUS_OK; /* 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) { ctx->status_code = CSYNC_STATUS_MEMORY_ERROR; free(ctx); 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) { ctx->status_code = CSYNC_STATUS_MEMORY_ERROR; free(ctx); return -1; } ctx->status_code = CSYNC_STATUS_OK; ctx->options.local_only_mode = false; ctx->pwd.uid = getuid(); ctx->pwd.euid = geteuid(); home = csync_get_user_home_dir(); if (home == NULL) { SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx); errno = ENOMEM; ctx->status_code = CSYNC_STATUS_MEMORY_ERROR; return -1; } rc = asprintf(&ctx->options.config_dir, "%s/%s", home, CSYNC_CONF_DIR); SAFE_FREE(home); if (rc < 0) { SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx); errno = ENOMEM; ctx->status_code = CSYNC_STATUS_MEMORY_ERROR; return -1; } ctx->local.list = 0; ctx->remote.list = 0; ctx->current_fs = NULL; ctx->abort = false; *csync = ctx; return 0; }
int csync_config_parse_file(CSYNC *ctx, const char *config) { unsigned int count = 0; char line[1024] = {0}; char *s; FILE *f; /* copy default config, if no config exists */ if (! c_isfile(config)) { /* check if there is still one csync.conf left over in $HOME/.csync * and copy it over (migration path) */ char *home = NULL; char *home_config = NULL; char *config_file = NULL; /* there is no statedb at the expected place. */ home = csync_get_user_home_dir(); if( !c_streq(home, ctx->options.config_dir) ) { int rc = -1; config_file = c_basename(config); if( config_file ) { rc = asprintf(&home_config, "%s/%s/%s", home, CSYNC_CONF_DIR, config_file); SAFE_FREE(config_file); } if (rc >= 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "config file %s not found, checking %s", config, home_config); /* check the home file and copy to new statedb if existing. */ if(c_isfile(home_config)) { if (c_copy(home_config, config, 0644) < 0) { /* copy failed, but that is not reason to die. */ CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "Could not copy config %s => %s", home_config, config); } else { CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "Copied %s => %s", home_config, config); } } } SAFE_FREE(home_config); } SAFE_FREE(home); /* Still install the default one if nothing is there. */ if( ! c_isfile(config)) { if (_csync_config_copy_default(config) < 0) { return -1; } } } f = fopen(config, "r"); if (f == NULL) { return 0; } CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Reading configuration data from %s", config); s = fgets(line, sizeof(line), f); while (s != NULL) { int rc; count++; rc = csync_config_parse_line(ctx, line, count); if (rc < 0) { fclose(f); return -1; } s = fgets(line, sizeof(line), f); } fclose(f); return 0; }
int csync_create(CSYNC **csync, const char *local, const char *remote) { CSYNC *ctx; size_t len = 0; char *home; int rc; ctx = c_malloc(sizeof(CSYNC)); if (ctx == NULL) { return -1; } ctx->error_code = CSYNC_ERR_NONE; /* 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) { ctx->error_code = CSYNC_ERR_MEM; 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); ctx->error_code = CSYNC_ERR_MEM; return -1; } ctx->options.max_depth = MAX_DEPTH; ctx->options.max_time_difference = MAX_TIME_DIFFERENCE; ctx->options.unix_extensions = 0; ctx->options.with_conflict_copys=false; ctx->options.local_only_mode = false; ctx->pwd.uid = getuid(); ctx->pwd.euid = geteuid(); home = csync_get_user_home_dir(); if (home == NULL) { SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx); errno = ENOMEM; ctx->error_code = CSYNC_ERR_MEM; return -1; } rc = asprintf(&ctx->options.config_dir, "%s/%s", home, CSYNC_CONF_DIR); SAFE_FREE(home); if (rc < 0) { SAFE_FREE(ctx->local.uri); SAFE_FREE(ctx->remote.uri); SAFE_FREE(ctx); errno = ENOMEM; ctx->error_code = CSYNC_ERR_MEM; return -1; } *csync = ctx; return 0; }