Пример #1
0
dpl_status_t 
cb_metadata_list(dpl_dict_var_t *var,
                 void *cb_arg)
{
  struct metadata_list_arg *arg = (struct metadata_list_arg *) cb_arg;
  dpl_status_t ret, ret2;

  if (arg->metadatum_func)
    {
      dpl_value_t val;
      
      val.type = DPL_VALUE_STRING;
      val.string = var->val->string;
      ret2 = arg->metadatum_func(arg->cb_arg, var->key, &val);
      if (DPL_SUCCESS != ret2)
        {
          ret = ret2;
          goto end;
        }
    }
  
  ret2 = dpl_dict_add_value(arg->metadata, var->key, var->val, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  ret = DPL_SUCCESS;

 end:

  return ret;
}
Пример #2
0
static dpl_status_t
convert_obj_to_value(dpl_ctx_t *ctx,
                     struct json_object *obj,
                     int level,
                     dpl_value_t **valp)
{
  int ret, ret2;
  dpl_value_t *val = NULL;
  char *key; 
  struct lh_entry *entry;
  json_object *child;
  dpl_dict_t *subdict = NULL;
  dpl_vec_t *vector = NULL;

  DPRINTF("convert_obj_to_value level=%d type=%d\n", level, json_object_get_type(obj));

  val = malloc(sizeof (*val));
  if (NULL == val)
    {
      ret = DPL_ENOMEM;
      goto end;
    }
  memset(val, 0, sizeof (*val));

  switch (json_object_get_type(obj))
    {
    case json_type_null:
      return DPL_ENOTSUPP;
    case json_type_array:
      {
        int n_items = json_object_array_length(obj);
        int i;

        vector = dpl_vec_new(2, 2);
        if (NULL == vector)
          {
            ret = DPL_ENOMEM;
            goto end;
          }

        for (i = 0;i < n_items;i++)
          {
            child = json_object_array_get_idx(obj, i);
            dpl_value_t *subval;

            ret2 = convert_obj_to_value(ctx, child, level+1, &subval);
            if (DPL_SUCCESS != ret2)
              {
                ret = ret2;
                goto end;
              }
            
            ret2 = dpl_vec_add_value(vector, subval);

            dpl_value_free(subval);
            
            if (DPL_SUCCESS != ret2)
              {
                ret = ret2;
                goto end;
              }
          }

        val->type = DPL_VALUE_VECTOR;
        val->vector = vector;
        vector = NULL;
        break ;
      }
    case json_type_object:
      {
        subdict = dpl_dict_new(13);
        if (NULL == subdict)
          {
            ret = DPL_ENOMEM;
            goto end;
          }
        
        for (entry = json_object_get_object(obj)->head; (entry ? (key = (char*)entry->k, child = (struct json_object*)entry->v, entry) : 0); entry = entry->next)
          {
            dpl_value_t *subval;

            DPRINTF("key='%s'\n", key);

            ret2 = convert_obj_to_value(ctx, child, level+1, &subval);
            if (DPL_SUCCESS != ret2)
              {
                ret = ret2;
                goto end;
              }
            
            ret2 = dpl_dict_add_value(subdict, key, subval, 0);

            dpl_value_free(subval);

            if (DPL_SUCCESS != ret2)
              {
                ret = ret2;
                goto end;
              }
          }

        val->type = DPL_VALUE_SUBDICT;
        val->subdict = subdict;
        subdict = NULL;
        break ;
      }
    case json_type_boolean:
    case json_type_double:
    case json_type_int:
    case json_type_string:
      {
        pthread_mutex_lock(&ctx->lock); //lock for objects other than string
        val->string = strdup((char *) json_object_get_string(obj));
        pthread_mutex_unlock(&ctx->lock);

        if (NULL == val->string)
          {
            ret = DPL_ENOMEM;
            goto end;
          }
        val->type = DPL_VALUE_STRING;
        break ;
      }
    }

  if (NULL != valp)
    {
      *valp = val;
      val = NULL;
    }

  ret = DPL_SUCCESS;
  
 end:

  if (NULL != vector)
    dpl_vec_free(vector);

  if (NULL != subdict)
    dpl_dict_free(subdict);

  if (NULL != val)
    dpl_value_free(val);

  DPRINTF("level=%d ret=%d\n", level, ret);

  return ret;
}
Пример #3
0
dpl_status_t
dpl_posix_head_raw(dpl_ctx_t *ctx,
                   const char *bucket,
                   const char *resource,
                   const char *subresource,
                   const dpl_option_t *option, 
                   dpl_ftype_t object_type,
                   const dpl_condition_t *condition,
                   dpl_dict_t **metadatap,
                   char **locationp)
{
  dpl_status_t ret, ret2;
  char path[MAXPATHLEN];
  int iret;
  struct stat st;
  char buf[256];
  dpl_dict_t *metadata = NULL;
  dpl_dict_t *subdict = NULL;
  dpl_value_t value;

  DPL_TRACE(ctx, DPL_TRACE_BACKEND, "");

  snprintf(path, sizeof (path), "/%s/%s",
           ctx->base_path ? ctx->base_path : "",
           resource ? resource : "");

  iret = stat(path, &st);
  if (-1 == iret)
    {
      ret = dpl_posix_map_errno();
      goto end;
    }

  metadata = dpl_dict_new(13);
  if (NULL == metadata)
    {
      ret = DPL_ENOMEM;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%ld", st.st_dev);
  ret2 = dpl_dict_add(metadata, "dev", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lX", st.st_ino);
  ret2 = dpl_dict_add(metadata, "ino", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }
  
  snprintf(buf, sizeof (buf), "%u", st.st_mode);
  ret2 = dpl_dict_add(metadata, "mode", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%ld", st.st_nlink);
  ret2 = dpl_dict_add(metadata, "nlink", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%u", st.st_uid);
  ret2 = dpl_dict_add(metadata, "uid", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%u", st.st_gid);
  ret2 = dpl_dict_add(metadata, "gid", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_rdev);
  ret2 = dpl_dict_add(metadata, "rdev", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_size);
  ret2 = dpl_dict_add(metadata, "size", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_blksize);
  ret2 = dpl_dict_add(metadata, "blksize", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_blocks);
  ret2 = dpl_dict_add(metadata, "blocks", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_atime);
  ret2 = dpl_dict_add(metadata, "atime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_mtime);
  ret2 = dpl_dict_add(metadata, "mtime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_ctime);
  ret2 = dpl_dict_add(metadata, "ctime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  subdict = dpl_dict_new(13);
  if (NULL == subdict)
    {
      ret = DPL_ENOMEM;
      goto end;
    }

  ret2 = dpl_get_xattrs(path,
                        subdict,
                        DPL_POSIX_XATTR_PREFIX,
                        XATTRS_NO_ENCODING);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  value.type = DPL_VALUE_SUBDICT;
  value.subdict = subdict;
  // dpl_dict_add_value dups the value, so don't prevent freeing subdict
  ret2 = dpl_dict_add_value(metadata, "xattr", &value, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  if (NULL != metadatap)
    {
      *metadatap = metadata;
      metadata = NULL;
    }

  ret = DPL_SUCCESS;

 end:

  if (NULL != subdict)
    dpl_dict_free(subdict);

  if (NULL != metadata)
    dpl_dict_free(metadata);

  DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ret=%d", ret);

  return ret;
}
Пример #4
0
static dpl_status_t
cb_posix_get_metadatum_from_xattr_value(dpl_dict_var_t *var, void *cb_arg)
{
  struct metadata_conven *mc = (struct metadata_conven *) cb_arg;
  return dpl_dict_add_value(mc->metadata, var->key, var->val, 0);
}
Пример #5
0
dpl_status_t
dpl_posix_get_metadatum_from_value(const char *key,
                                   dpl_value_t *val,
                                   dpl_metadatum_func_t metadatum_func,
                                   void *cb_arg,
                                   dpl_dict_t *metadata,
                                   dpl_sysmd_t *sysmdp)
{
  dpl_status_t ret, ret2;
  int iret;
  char buf[256];

  if (sysmdp)
    {
      if (!strcmp(key, "atime"))
        {
          assert(val->type == DPL_VALUE_STRING);
          sysmdp->atime = strtoul(val->string, NULL, 0);
          sysmdp->mask |= DPL_SYSMD_MASK_ATIME;
        }
      else if (!strcmp(key, "mtime"))
        {
          assert(val->type == DPL_VALUE_STRING);
          sysmdp->mtime = strtoul(val->string, NULL, 0);
          sysmdp->mask |= DPL_SYSMD_MASK_MTIME;
        }
      else if (!strcmp(key, "ctime"))
        {
          assert(val->type == DPL_VALUE_STRING);
          sysmdp->ctime = strtoul(val->string, NULL, 0);
          sysmdp->mask |= DPL_SYSMD_MASK_CTIME;
        }
      else if (!strcmp(key, "size"))
        {
          assert(val->type == DPL_VALUE_STRING);
          sysmdp->size = strtoul(val->string, NULL, 0);
          sysmdp->mask |= DPL_SYSMD_MASK_SIZE;
        }
      else if (!strcmp(key, "uid"))
        {
          uid_t uid;
          struct passwd pwd, *pwdp;

          assert(val->type == DPL_VALUE_STRING);
          uid = atoi(val->string);
          iret = getpwuid_r(uid, &pwd, buf, sizeof (buf), &pwdp);
          if (iret == -1)
            {
              perror("getpwuid");
              ret = DPL_FAILURE;
              goto end;
            }
          snprintf(sysmdp->owner, sizeof (sysmdp->owner), "%s", pwdp->pw_name);
          sysmdp->mask |= DPL_SYSMD_MASK_OWNER;
        }
      else if (!strcmp(key, "gid"))
        {
          gid_t gid;
          struct group grp, *grpp;

          assert(val->type == DPL_VALUE_STRING);
          gid = atoi(val->string);
          iret = getgrgid_r(gid, &grp, buf, sizeof (buf), &grpp);
          if (iret == -1)
            {
              perror("getgrgid");
              ret = DPL_FAILURE;
              goto end;
            }
          snprintf(sysmdp->group, sizeof (sysmdp->group), "%s", grpp->gr_name);
          sysmdp->mask |= DPL_SYSMD_MASK_GROUP;
        }
      else if (!strcmp(key, "ino"))
        {
          assert(val->type == DPL_VALUE_STRING);
          snprintf(sysmdp->id, sizeof (sysmdp->id), "%s", val->string);
          sysmdp->mask |= DPL_SYSMD_MASK_ID;
        }
    }

  if (!strcmp(key, "xattr"))
    {
      //this is the metadata object
      if (DPL_VALUE_SUBDICT != val->type)
        {
          ret = DPL_EINVAL;
          goto end;
        }

      if (metadata)
        {
          if (metadatum_func)
            {
              ret2 = metadatum_func(cb_arg, key, val);
              if (DPL_SUCCESS != ret2)
                {
                  ret = ret2;
                  goto end;
                }
            }

          //add md into metadata
          ret2 = dpl_dict_add_value(metadata, key, val, 0);
          if (DPL_SUCCESS != ret2)
            {
              ret = ret2;
              goto end;
            }
        }
    }
  
  ret = DPL_SUCCESS;
  
 end:

  return ret;
}
Пример #6
0
dpl_status_t
dpl_posix_head_raw(dpl_ctx_t *ctx,
                   const char *bucket,
                   const char *resource,
                   const char *subresource,
                   const dpl_option_t *option, 
                   dpl_ftype_t object_type,
                   const dpl_condition_t *condition,
                   dpl_dict_t **metadatap,
                   char **locationp)
{
  dpl_status_t ret, ret2;
  char path[MAXPATHLEN];
  int iret;
  struct stat st;
  char buf[256];
  dpl_dict_t *metadata = NULL;
  char xattr[64*1024];
  ssize_t ssize_ret, off;
  dpl_dict_t *subdict = NULL;
  dpl_value_t value;

  DPL_TRACE(ctx, DPL_TRACE_BACKEND, "");

  snprintf(path, sizeof (path), "/%s/%s", ctx->base_path ? ctx->base_path : "", resource);

  iret = stat(path, &st);
  if (-1 == iret)
    {
      ret = DPL_FAILURE;
      goto end;
    }

  metadata = dpl_dict_new(13);
  if (NULL == metadata)
    {
      ret = DPL_ENOMEM;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%ld", st.st_dev);
  ret2 = dpl_dict_add(metadata, "dev", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lX", st.st_ino);
  ret2 = dpl_dict_add(metadata, "ino", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }
  
  snprintf(buf, sizeof (buf), "%u", st.st_mode);
  ret2 = dpl_dict_add(metadata, "mode", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%ld", st.st_nlink);
  ret2 = dpl_dict_add(metadata, "nlink", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%u", st.st_uid);
  ret2 = dpl_dict_add(metadata, "uid", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%u", st.st_gid);
  ret2 = dpl_dict_add(metadata, "gid", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_rdev);
  ret2 = dpl_dict_add(metadata, "rdev", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_size);
  ret2 = dpl_dict_add(metadata, "size", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_blksize);
  ret2 = dpl_dict_add(metadata, "blksize", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_blocks);
  ret2 = dpl_dict_add(metadata, "blocks", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_atime);
  ret2 = dpl_dict_add(metadata, "atime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_mtime);
  ret2 = dpl_dict_add(metadata, "mtime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  snprintf(buf, sizeof (buf), "%lu", st.st_ctime);
  ret2 = dpl_dict_add(metadata, "ctime", buf, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  subdict = dpl_dict_new(13);
  if (NULL == subdict)
    {
      ret = DPL_ENOMEM;
      goto end;
    }

  ssize_ret = llistxattr(path, xattr, sizeof (xattr));
  if (-1 == ssize_ret)
    {
      ret = DPL_FAILURE;
      goto end;
    }

  off = 0;
  while (off < ssize_ret)
    {
      char *key;
      int key_len;
      ssize_t val_len;
      
      key = (xattr + off);
      key_len = strlen(key);

      val_len = lgetxattr(path, key, buf, sizeof (buf) - 1);
      if (val_len == -1)
        {
          ret = DPL_FAILURE;
          goto end;
        }

      if (strncmp(key, XATTR_PREFIX, strlen(XATTR_PREFIX)))
        {
          ret = DPL_EINVAL;
          goto end;
        }

      buf[val_len] = 0;
      ret2 = dpl_dict_add(subdict, key + strlen(XATTR_PREFIX), buf, 0);
      if (DPL_SUCCESS != ret2)
        {
          ret = ret2;
          goto end;
        }

      off += key_len + 1;
    }

  value.type = DPL_VALUE_SUBDICT;
  value.subdict = subdict;
  subdict = NULL;
  ret2 = dpl_dict_add_value(metadata, "xattr", &value, 0);
  if (DPL_SUCCESS != ret2)
    {
      ret = ret2;
      goto end;
    }

  if (NULL != metadatap)
    {
      *metadatap = metadata;
      metadata = NULL;
    }

  ret = DPL_SUCCESS;

 end:

  if (NULL != subdict)
    dpl_dict_free(subdict);

  if (NULL != metadata)
    dpl_dict_free(metadata);

  DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ret=%d", ret);

  return ret;
}