示例#1
0
static int
es_faprovider_register(duk_context *ctx)
{
  es_context_t *ec = es_get(ctx);
  const char *name = duk_require_string(ctx, 0);

  es_fap_t *ef = es_resource_alloc(&es_resource_fap);

  ef->fap.fap_opaque = ef;
  if(es_prop_is_true(ctx, 1, "cachable"))
    ef->fap.fap_flags |= FAP_ALLOW_CACHE,

  ef->fap.fap_fini  = es_fap_fini;
  ef->fap.fap_open  = es_fap_open;
  ef->fap.fap_seek  = es_fap_seek;
  ef->fap.fap_read  = es_fap_read;
  ef->fap.fap_close = es_fap_close;
  ef->fap.fap_fsize = es_fap_fsize;
  ef->fap.fap_stat  = es_fap_stat;


  ef->name = strdup(name);
  ef->fap.fap_name = ef->name;

  es_resource_retain(&ef->super); // Refcount owned by FAP
  fileaccess_register_dynamic(&ef->fap);

  es_resource_link(&ef->super, ec, 1);
  es_root_register(ctx, 1, ef);
  es_resource_push(ctx, &ef->super);
  return 1;
}
示例#2
0
static int
set_timer(duk_context *duk, int repeat)
{
  es_context_t *ec = es_get(duk);

  es_timer_t *et = es_resource_create(ec, &es_resource_timer, 1);
  int val = duk_require_int(duk, 1);

  es_root_register(duk, 0, et);

  et->et_interval = val * repeat;

  int64_t now = arch_get_ts();
  et->et_expire = now + val * 1000LL;

  hts_mutex_lock(&timer_mutex);

  if(thread_running == 0) {
    thread_running = 1;
    hts_thread_create_detached("estimer", timer_thread, NULL,
                               THREAD_PRIO_MODEL);
  } else {
    hts_cond_signal(&timer_cond);
  }

  LIST_INSERT_SORTED(&timers, et, et_link, estimercmp, es_timer_t);

  hts_mutex_unlock(&timer_mutex);

  es_resource_push(duk, &et->super);
  return 1;
}
示例#3
0
static int
es_sqlite_create(duk_context *ctx)
{
    char path[PATH_MAX];
    char errbuf[512];
    es_context_t *ec = es_get(ctx);
    const char *name = duk_safe_to_string(ctx, 0);

    // Create the db-dir for this plugin

    snprintf(path, sizeof(path), "%s/databases", ec->ec_storage);

    if(fa_makedirs(path, errbuf, sizeof(errbuf)))
        duk_error(ctx, DUK_ERR_ERROR, "Unable to create directory %s -- %s",
                  path, errbuf);

    snprintf(path, sizeof(path), "%s/databases/%s", ec->ec_storage, name);

    sqlite3 *db = db_open(path, 0);
    if(db == NULL)
        duk_error(ctx, DUK_ERR_ERROR, "Unable to open database -- check logs");


    es_sqlite_t *es = es_resource_create(ec, &es_resource_sqlite, 0);

    es->es_db = db;
    es->es_name = strdup(name);

    es_resource_push(ctx, &es->super);
    return 1;
}
示例#4
0
文件: es_route.c 项目: Overx/showtime
static int
es_route_create(duk_context *ctx)
{
  const char *str = duk_safe_to_string(ctx, 0);

  if(str[0] != '^') {
    int l = strlen(str);
    char *s = alloca(l + 2);
    s[0] = '^';
    memcpy(s+1, str, l+1);
    str = s;
  }

  es_context_t *ec = es_get(ctx);

  hts_mutex_lock(&route_mutex);

  es_route_t *er;

  LIST_FOREACH(er, &routes, er_link)
    if(!strcmp(er->er_pattern, str))
      break;

  if(er != NULL) {
    hts_mutex_unlock(&route_mutex);
    duk_error(ctx, DUK_ERR_ERROR, "Route %s already exist", str);
  }

  er = es_resource_alloc(&es_resource_route);
  if(hts_regcomp(&er->er_regex, str)) {
    hts_mutex_unlock(&route_mutex);
    free(er);
    duk_error(ctx, DUK_ERR_ERROR, "Invalid regular expression for route %s",
              str);
  }

  er->er_pattern = strdup(str);
  er->er_prio = strcspn(str, "()[].*?+$") ?: INT32_MAX;

  LIST_INSERT_SORTED(&routes, er, er_link, er_cmp, es_route_t);

  es_resource_link(&er->super, ec, 1);

  hts_mutex_unlock(&route_mutex);

  es_root_register(ctx, 1, er);

  es_resource_push(ctx, &er->super);
  return 1;
}
示例#5
0
文件: es_fs.c 项目: Ezio-PS/movian
static int
es_file_open(duk_context *ctx)
{
  char errbuf[512];

  es_context_t *ec = es_get(ctx);

  const char *flagsstr = duk_to_string(ctx, 1);
  //  int mode = duk_to_int(ctx, 2);

  int flags;


  if(!strcmp(flagsstr, "r")) {
    flags = 0;
  } else if(!strcmp(flagsstr, "w")) {
    flags = FA_WRITE;
  } else if(!strcmp(flagsstr, "a")) {
    flags = FA_WRITE | FA_APPEND;
  } else {
    duk_error(ctx, DUK_ERR_ERROR, "Invalid flags '%s' to open", flagsstr);
  }

  const char *filename = get_filename(ctx, 0, ec, !!flags);

  fa_handle_t *fh = fa_open_ex(filename, errbuf, sizeof(errbuf), flags, NULL);
  if(fh == NULL)
    duk_error(ctx, DUK_ERR_ERROR, "Unable to open file '%s' -- %s",
              filename, errbuf);

  es_fd_t *efd = es_resource_create(ec, &es_resource_fd, 0);
  efd->efd_path = strdup(filename);
  efd->efd_fh = fh;

  es_resource_push(ctx, &efd->super);
  return 1;
}