Esempio n. 1
0
/**
 * librdf_new_serializer:
 * @world: redland world object
 * @name: the serializer factory name (or NULL or empty string if don't care)
 * @mime_type: the MIME type of the syntax (NULL if not used)
 * @type_uri: URI of syntax (NULL if not used)
 *
 * Constructor - create a new #librdf_serializer object.
 * 
 * Return value: new #librdf_serializer object or NULL
 **/
librdf_serializer*
librdf_new_serializer(librdf_world *world, 
                      const char *name, const char *mime_type,
                      librdf_uri *type_uri)
{
  librdf_serializer_factory* factory;

  librdf_world_open(world);

  factory = librdf_get_serializer_factory(world, name, mime_type, type_uri);
  if(!factory) {
    if(name)
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER, NULL,
                 "serializer '%s' not found", name);
    else if(mime_type)
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER, NULL,
                 "serializer for mime_type '%s' not found", mime_type);
    else if(type_uri)
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER, NULL,
                 "serializer for type URI '%s' not found", 
                 librdf_uri_as_string(type_uri));
    else
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER, NULL,
                 "default serializer not found");
    return NULL;
  }

  return librdf_new_serializer_from_factory(world, factory);
}
Esempio n. 2
0
static void
librdf_stream_from_node_iterator_finished(void* context)
{
  librdf_stream_from_node_iterator_stream_context* scontext=(librdf_stream_from_node_iterator_stream_context*)context;
  
  if(scontext->iterator)
    librdf_free_iterator(scontext->iterator);

  if(scontext->current) {
    switch(scontext->field) {
      case LIBRDF_STATEMENT_SUBJECT:
        librdf_statement_set_subject(scontext->current, NULL);
        break;
      case LIBRDF_STATEMENT_PREDICATE:
        librdf_statement_set_predicate(scontext->current, NULL);
        break;
      case LIBRDF_STATEMENT_OBJECT:
        librdf_statement_set_object(scontext->current, NULL);
        break;

      case LIBRDF_STATEMENT_ALL:
      default:
        librdf_log(scontext->iterator->world,
                   0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STREAM, NULL, 
                   "Illegal statement field %d seen", scontext->field);
    }
    librdf_free_statement(scontext->current);
  }

  LIBRDF_FREE(librdf_stream_from_node_iterator_stream_context, scontext);
}
Esempio n. 3
0
/**
 * librdf_query_results_to_file2:
 * @query_results: #librdf_query_results object
 * @name: filename to write to
 * @mime_type: mime type (or NULL)
 * @format_uri: URI of syntax to format to (or NULL)
 * @base_uri: Base URI of output formatted syntax (or NULL)
 *
 * Write a query results to a file.
 * 
 * A query results format can be named, have a mime type, or
 * identified by a URI, all of which are optional.  The default
 * query results format will be used if @name, @mime_type and @format_uri
 * are all NULL.
 *
 * librdf_query_results_formats_enumerate() returns information on
 * the known query results names, labels and URIs.
 *
 * The @base_uri may be used for as the base URI the generated
 * syntax, depending on the format.
 *
 * Return value: non 0 on failure
 **/
int
librdf_query_results_to_file2(librdf_query_results *query_results, 
                             const char *name,
                             const char *mime_type,
                             librdf_uri *format_uri,
                             librdf_uri *base_uri)
{
  FILE* fh;
  int status;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, query_results, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(name, string, 1);

  fh = fopen(name, "w+");
  if(!fh) {
    librdf_log(query_results->query->world, 0, LIBRDF_LOG_ERROR, 
               LIBRDF_FROM_QUERY, NULL, 
               "failed to open file '%s' for writing - %s",
               name, strerror(errno));
    return 1;
  }

  status = librdf_query_results_to_file_handle2(query_results, fh, 
                                                name, mime_type,
                                                format_uri, base_uri);
  fclose(fh);
  return status;
}
Esempio n. 4
0
/**
 * librdf_serializer_serialize_model_to_file:
 * @serializer: the serializer
 * @name: filename to serialize to
 * @base_uri: the base URI to use (or NULL)
 * @model: the #librdf_model model to use
 *
 * Write a serialized #librdf_model to a file.
 * 
 * Return value: non 0 on failure
 **/
int
librdf_serializer_serialize_model_to_file(librdf_serializer* serializer,
                                          const char *name, 
                                          librdf_uri* base_uri,
                                          librdf_model* model) 
{
  FILE* fh;
  int status;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(serializer, librdf_serializer, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(name, string, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);

  fh=fopen(name, "w+");
  if(!fh) {
    librdf_log(serializer->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER,
               NULL, "failed to open file '%s' for writing - %s",
               name, strerror(errno));
    return 1;
  }
  
  status=librdf_serializer_serialize_model_to_file_handle(serializer, fh, 
                                                          base_uri, model);
  fclose(fh);
  return status;
}
Esempio n. 5
0
static void*
librdf_stream_from_node_iterator_get_statement(void* context, int flags)
{
  librdf_stream_from_node_iterator_stream_context* scontext=(librdf_stream_from_node_iterator_stream_context*)context;
  librdf_node* node;
  
  switch(flags) {
    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:

      if(!(node=(librdf_node*)librdf_iterator_get_object(scontext->iterator)))
        return NULL;

      /* The node object above is shared, no need to free it before
       * assigning to the statement, which is also shared, and
       * return to the user.
       */
      switch(scontext->field) {
        case LIBRDF_STATEMENT_SUBJECT:
          librdf_statement_set_subject(scontext->current, node);
          break;
        case LIBRDF_STATEMENT_PREDICATE:
          librdf_statement_set_predicate(scontext->current, node);
          break;
        case LIBRDF_STATEMENT_OBJECT:
          librdf_statement_set_object(scontext->current, node);
          break;

        case LIBRDF_STATEMENT_ALL:
        default:
          librdf_log(scontext->iterator->world,
                     0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STREAM, NULL,
                     "Illegal statement field %d seen", scontext->field);
          return NULL;
      }
      
      return scontext->current;

    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
      return librdf_iterator_get_context(scontext->iterator);
    default:
      librdf_log(scontext->iterator->world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STREAM, NULL,
                 "Unknown iterator method flag %d", flags);
      return NULL;
  }

}
static void*
cassandra_results_stream_get_statement(void* context, int flags)
{

    cassandra_results_stream* scontext;
    const char* s;
    size_t s_len;
    const char* p;
    size_t p_len;
    const char* o;
    size_t o_len;
    const CassRow* row;
	
    scontext = (cassandra_results_stream*)context;

    switch(flags) {

    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:

	row = cass_iterator_get_row(scontext->iter);

	cass_value_get_string(cass_row_get_column(row, 0), &s, &s_len);
	cass_value_get_string(cass_row_get_column(row, 1), &p, &p_len);
	cass_value_get_string(cass_row_get_column(row, 2), &o, &o_len);

	if (scontext->statement) {
	    librdf_free_statement(scontext->statement);
	    scontext->statement = 0;
	}

	librdf_node* sn, * pn, * on;
	sn = node_constructor_helper(scontext->storage->world, s, s_len);
	pn = node_constructor_helper(scontext->storage->world, p, p_len);
	on = node_constructor_helper(scontext->storage->world, o, o_len);

	if (sn == 0 || pn == 0 || on == 0) {
	    if (sn) librdf_free_node(sn);
	    if (pn) librdf_free_node(pn);
	    if (on) librdf_free_node(on);
	    return 0;
	}

	scontext->statement =
	    librdf_new_statement_from_nodes(scontext->storage->world,
					    sn, pn, on);

	return scontext->statement;

    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
	return scontext->context;

    default:
	librdf_log(scontext->storage->world,
		   0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
		   "Unknown iterator method flag %d", flags);
	return NULL;
    }
    
}
Esempio n. 7
0
static void*
librdf_list_iterator_get_method(void* iterator, int flags) 
{
  librdf_list_iterator_context* context=(librdf_list_iterator_context*)iterator;
  librdf_list_node *node=context->current;
  
  if(!node)
    return NULL;

  if(flags == LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT)
    return node->data;

  librdf_log(context->list->world,
             0, LIBRDF_LOG_ERROR, LIBRDF_FROM_LIST, NULL,
             "Unsupported iterator method flag %d", flags);
  return NULL;
}
Esempio n. 8
0
static void*
librdf_query_rasqal_query_results_get_statement(void* context, int flags)
{
  librdf_query_rasqal_stream_context* scontext=(librdf_query_rasqal_stream_context*)context;

  switch(flags) {
    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:
      return scontext->statement;

    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
      return NULL;
      
    default:
      librdf_log(scontext->query->world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
                 "Unknown iterator method flag %d", flags);
      return NULL;
  }

}
Esempio n. 9
0
static void*
librdf_storage_tstore_find_get_statement(void* context, int flags)
{
  librdf_storage_tstore_find_stream_context* scontext=(librdf_storage_tstore_find_stream_context*)context;

  switch(flags) {
    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:
      {
        librdf_statement* statement=librdf_storage_tstore_statement_from_rs_triple(scontext->storage->world, scontext->triple);
        return statement;
      }
    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
      return NULL;
    default:
      librdf_log(scontext->storage->world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                 "Unknown iterator method flag %d", flags);
      return NULL;
  }
}
Esempio n. 10
0
/* functions implementing storage api */
static int
librdf_storage_file_init(librdf_storage* storage, const char *name,
                         librdf_hash* options)
{
  char *name_copy;
  char *contexts;
  int rc = 1;
  int is_uri = !strcmp(storage->factory->name, "uri");
  const char *format_name = (is_uri ? "guess" : "rdfxml");
  librdf_storage_file_instance* context;

  context = LIBRDF_CALLOC(librdf_storage_file_instance*, 1, sizeof(*context));
  if(!context)
    goto done;

  librdf_storage_set_instance(storage, context);

  /* Cannot save contexts in a file; pass everything else on */
  contexts = librdf_hash_get_del(options, "contexts");
  if(contexts)
    LIBRDF_FREE(char*, contexts);

  context->format_name = librdf_hash_get_del(options, "format");
  if(context->format_name) {
    /* for 'file' and 'uri' storage, check this is a valid parser
     * for 'file' storage, also check this is a valid serializer 
     */
    if(!librdf_parser_check_name(storage->world, context->format_name) ||
       (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) {
      librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL,
                 "Ignoring storage %s format option '%s' - using default format '%s'",
                 storage->factory->name, context->format_name, format_name);
      LIBRDF_FREE(char*, context->format_name);
      context->format_name = NULL;
    }

    if(context->format_name)
      format_name = context->format_name;
  }
Esempio n. 11
0
static void*
librdf_node_static_iterator_get_method(void* iterator, int flags) 
{
  librdf_node_static_iterator_context* context;

  context = (librdf_node_static_iterator_context*)iterator;
  
  if(context->current > context->size-1)
    return NULL;

  switch(flags) {
    case LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT:
       return (void*)context->nodes[context->current];

    case LIBRDF_ITERATOR_GET_METHOD_GET_CONTEXT:
      return NULL;

    default:
      librdf_log(context->world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_NODE, NULL,
                 "Unknown iterator method flag %d", flags);
      return NULL;
  }
}
Esempio n. 12
0
/*
 * librdf_parser_raptor_new_statement_handler - helper callback function for raptor RDF when a new triple is asserted
 * @context: context for callback
 * @statement: raptor_statement
 *
 * Adds the statement to the list of statements.
 */
static void
librdf_parser_raptor_new_statement_handler(void *context,
                                           raptor_statement *rstatement)
{
  librdf_parser_raptor_stream_context* scontext=(librdf_parser_raptor_stream_context*)context;
  librdf_node* node;
  librdf_statement* statement;
  librdf_world* world=scontext->pcontext->parser->world;
  int rc;

  statement=librdf_new_statement(world);
  if(!statement)
    return;

  if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, (const unsigned char*)rstatement->subject->value.blank.string);
  } else if (rstatement->subject->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->subject->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor subject identifier type %d",
               rstatement->subject->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create subject node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_subject(statement, node);


  if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->predicate->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor predicate identifier type %d",
               rstatement->predicate->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create predicate node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_predicate(statement, node);

  if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) {
    node = librdf_new_node_from_typed_literal(world,
                                              rstatement->object->value.literal.string,
                                              (const char *)rstatement->object->value.literal.language,
                                              (librdf_uri*)rstatement->object->value.literal.datatype);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->object->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor object identifier type %d",
               rstatement->object->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create object node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_object(statement, node);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  if(1) {
    raptor_iostream *iostr;
    iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stderr);
    librdf_statement_write(statement, iostr);
    raptor_free_iostream(iostr);
  }
#endif

  if(scontext->model) {
    rc=librdf_model_add_statement(scontext->model, statement);
    librdf_free_statement(statement);
  } else {
    rc=librdf_list_add(scontext->statements, statement);
    if(rc)
      librdf_free_statement(statement);
  }
  if(rc) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot add statement to model");
  }
}
Esempio n. 13
0
static int
librdf_query_rasqal_query_results_update_statement(void* context)
{
  librdf_query_rasqal_stream_context* scontext=(librdf_query_rasqal_stream_context*)context;
  librdf_world* world=scontext->query->world;
  librdf_node* node;
  
  raptor_statement *rstatement=rasqal_query_results_get_triple(scontext->qcontext->results);
  if(!rstatement)
    return 1;
  
  scontext->statement=librdf_new_statement(world);
  if(!scontext->statement)
    return 1;

  /* subject */
  
  if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->subject->value.blank.string);
  } else if(rstatement->subject->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->subject->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Unknown Raptor subject identifier type %d",
               rstatement->subject->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create subject node");
    goto fail;
  }
  
  librdf_statement_set_subject(scontext->statement, node);

  /* predicate */

  if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->predicate->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Unknown Raptor predicate identifier type %d",
               rstatement->predicate->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create predicate node");
    goto fail;
  }
  
  librdf_statement_set_predicate(scontext->statement, node);
  
  /* object */

  if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) {
    node = librdf_new_node_from_typed_literal(world,
                                              rstatement->object->value.literal.string,
                                              (const char*)rstatement->object->value.literal.language,
                                              (librdf_uri*)rstatement->object->value.literal.datatype);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->object->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor object identifier type %d",
               rstatement->object->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create object node");
    goto fail;
  }

  librdf_statement_set_object(scontext->statement, node);

  return 0; /* success */

  fail:
  librdf_free_statement(scontext->statement);
  scontext->statement=NULL;
  return 1;
}
Esempio n. 14
0
/* functions implementing storage api */
static int
librdf_storage_file_init(librdf_storage* storage, const char *name,
                         librdf_hash* options)
{
    char *name_copy;
    char *contexts;
    int rc = 1;
    int is_uri = !strcmp(storage->factory->name, "uri");
    const char *format_name = (is_uri ? "guess" : "rdfxml");
    librdf_storage_file_instance* context;

    context = (librdf_storage_file_instance*)LIBRDF_CALLOC(librdf_storage_file_instance, 1, sizeof(librdf_storage_file_instance));
    if(!context)
        goto done;

    librdf_storage_set_instance(storage, context);

    /* Cannot save contexts in a file; pass everything else on */
    contexts = librdf_hash_get_del(options, "contexts");
    if(contexts)
        LIBRDF_FREE(cstring, contexts);

    context->format_name = librdf_hash_get_del(options, "format");
    if(context->format_name) {
        /* for 'file' and 'uri' storage, check this is a valid parser
         * for 'file' storage, also check this is a valid serializer
         */
        if(!librdf_parser_check_name(storage->world, context->format_name) ||
                (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) {
            librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL,
                       "Ignoring storage %s format option '%s' - using default format '%s'",
                       storage->factory->name, context->format_name, format_name);
            LIBRDF_FREE(cstring, context->format_name);
            context->format_name = NULL;
        }

        if(context->format_name)
            format_name = context->format_name;
    }


    if(is_uri)
        context->uri = librdf_new_uri(storage->world, (const unsigned char*)name);
    else {
        context->name_len = strlen(name);
        name_copy = (char*)LIBRDF_MALLOC(cstring, context->name_len+1);
        if(!name_copy)
            goto done;
        strcpy(name_copy,name);
        context->name = name_copy;
        context->uri = librdf_new_uri_from_filename(storage->world, context->name);
    }

    context->storage = librdf_new_storage_with_options(storage->world,
                       NULL, NULL,
                       options);
    if(!context->storage)
        goto done;

    context->model = librdf_new_model(storage->world, context->storage, NULL);
    if(!context->model)
        goto done;

    if(is_uri || !access((const char*)context->name, F_OK)) {
        librdf_parser *parser;

        parser = librdf_new_parser(storage->world, format_name, NULL, NULL);
        if(!parser) {
            rc = 1;
            goto done;
        }
        librdf_parser_parse_into_model(parser, context->uri, NULL, context->model);
        librdf_free_parser(parser);
    }

    context->changed = 0;

    rc = 0;

done:

    /* no more options, might as well free them now */
    if(options)
        librdf_free_hash(options);

    return rc;
}
Esempio n. 15
0
static int
librdf_storage_file_sync(librdf_storage *storage)
{
    librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance;
    char *backup_name;
    char *new_name;
    librdf_serializer* serializer;
    FILE *fh;
    int rc=0;

    if(!context->changed)
        return 0;

    if(!context->name) {
        /* FIXME - URI cannot be written */
        context->changed=0;
        return 0;
    }

    backup_name=NULL;

    if(!access((const char*)context->name, F_OK)) {
        /* name"~\0" */
        backup_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+2);
        if(!backup_name)
            return 1;
        strcpy(backup_name, (const char*)context->name);
        backup_name[context->name_len]='~';
        backup_name[context->name_len+1]='\0';

        if(rename(context->name, backup_name) < 0) {
            librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                       "rename of '%s' to '%s' failed - %s",
                       context->name, backup_name, strerror(errno));
            LIBRDF_FREE(cstring, backup_name);
            return 1;
        }
    }

    /* name".new\0" */
    new_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+5);
    if(!new_name)
        return 1;
    strcpy(new_name, (const char*)context->name);
    strcpy(new_name+context->name_len, ".new");

    serializer = librdf_new_serializer(storage->world, context->format_name,
                                       NULL, NULL);
    if(!serializer) {
        LIBRDF_FREE(cstring, new_name);
        if(backup_name)
            LIBRDF_FREE(cstring, backup_name);
        return 1;
    }

    fh=fopen(new_name, "w+");
    if(!fh) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "failed to open file '%s' for writing - %s",
                   new_name, strerror(errno));
        rc=1;
    } else {
        librdf_serializer_serialize_model_to_file_handle(serializer, fh,
                context->uri,
                context->model);
        fclose(fh);
    }
    librdf_free_serializer(serializer);

    if(fh && rename(new_name, context->name) < 0) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "rename of '%s' to '%s' failed - %s (%d)",
                   new_name, context->name, strerror(errno), errno);
        fh=NULL;
        rc=1;
    }

    LIBRDF_FREE(cstring, new_name);

    /* restore backup on failure (fh=NULL) */
    if(!fh && backup_name && rename(backup_name, context->name) < 0) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "rename of '%s' to '%s' failed - %s",
                   backup_name, context->name, strerror(errno));
        rc=1;
    }

    if(backup_name)
        LIBRDF_FREE(cstring, backup_name);

    context->changed=0;

    return rc;
}
Esempio n. 16
0
/**
 * librdf_statement_decode2:
 * @world: redland world
 * @statement: the statement to deserialise into
 * @context_node: pointer to #librdf_node context_node to deserialise into
 * @buffer: the buffer to use
 * @length: buffer size
 *
 * Decodes a statement + context node from a buffer.
 * 
 * Decodes the serialised statement (as created by librdf_statement_encode() )
 * from the given buffer.  If a context node is found and context_node is
 * not NULL, a pointer to the new #librdf_node is stored in *context_node.
 * 
 * Return value: number of bytes used or 0 on failure (bad encoding, allocation failure)
 **/
size_t
librdf_statement_decode2(librdf_world* world,
                         librdf_statement* statement, 
                         librdf_node** context_node,
                         unsigned char *buffer, size_t length)
{
  unsigned char *p;
  librdf_node* node;
  unsigned char type;
  size_t total_length=0;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 0);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
    LIBRDF_DEBUG2("Decoding buffer of %d bytes\n", length);
#endif


  /* absolute minimum - first byte is type */
  if(length < 1)
    return 0;

  p=buffer;
  if(*p++ != 'x')
    return 0;
  length--;
  total_length++;
  
  
  while(length>0) {
    size_t node_len;
    
    type=*p++;
    length--;
    total_length++;

    if(!length)
      return 0;
    
    if(!(node=librdf_node_decode(world, &node_len, p, length)))
      return 0;

    p += node_len;
    length -= node_len;
    total_length += node_len;
    
#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
    LIBRDF_DEBUG3("Found type %c (%d bytes)\n", type, node_len);
#endif
  
    switch(type) {
    case 's': /* subject */
      statement->subject=node;
      break;
      
    case 'p': /* predicate */
      statement->predicate=node;
      break;
      
    case 'o': /* object */
      statement->object=node;
      break;

    case 'c': /* context */
      if(context_node)
        *context_node=node;
      else
        librdf_free_node(node);
      break;

    default:
      librdf_log(world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STATEMENT, NULL,
                 "Illegal statement encoding '%c' seen", p[-1]);
      return 0;
    }
  }

  return total_length;
}
Esempio n. 17
0
/**
 * librdf_new_sql_config:
 * @world: librdf_world
 * @storage_name: SQL storage name
 * @layout: SQL schema variant
 * @config_dir: directory for configuration files
 * @predicate_uri_strings: configuration predicate URIs to look for
 * 
 * Constructor - Make a new SQL configuration for a layout from a file
 *
 * Uses SQL storage name @storage_name and with database schema
 * @layout to give a configuration that will contain an array of
 * string values in the #librdf_sql_config field values array.
 * 
 * Return value: configuration or NULL on failure
 **/
librdf_sql_config*
librdf_new_sql_config(librdf_world* world,
                      const char* storage_name,
                      const char* layout,
                      const char* config_dir,
                      const char** predicate_uri_strings)
{
  raptor_parser* rdf_parser=NULL;
  unsigned char *uri_string=NULL;
  raptor_uri *base_uri;
  raptor_uri *uri;
  librdf_sql_config* config;
  size_t len;
  int i;
  
  librdf_world_open(world);

  config=(librdf_sql_config*)LIBRDF_MALLOC(librdf_sql_config,
                                           sizeof(librdf_sql_config));

  len=strlen(config_dir) + 1 + strlen(storage_name) + 4 + 1;
  if(layout)
    len+= strlen(layout) + 1;
  config->filename=(char*)LIBRDF_MALLOC(cstring, len);
  if(layout)
    sprintf(config->filename, "%s/%s-%s.ttl", config_dir, storage_name, layout);
  else
    sprintf(config->filename, "%s/%s.ttl", config_dir, storage_name);

  config->predicate_uri_strings=predicate_uri_strings;
  for(i=0; config->predicate_uri_strings[i]; i++)
    ;
  config->predicates_count=i;
  config->values=(char**)LIBRDF_CALLOC(cstring, sizeof(char*), 
                                       config->predicates_count);
  
  LIBRDF_DEBUG4("Attempting to open %s layout %s storage config file %s\n", 
                storage_name, (layout ? layout: "(default)"), config->filename);
  
  if(access((const char*)config->filename, R_OK)) {
    librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
               "Failed to open configuration file %s for storage %s layout %s - %s",
               config->filename, storage_name, (layout ? layout: "(default)"),
               strerror(errno));
    librdf_free_sql_config(config);
    return NULL;
  }
  
  uri_string=raptor_uri_filename_to_uri_string(config->filename);
  uri=raptor_new_uri(uri_string);
  base_uri=raptor_uri_copy(uri);
  
  rdf_parser=raptor_new_parser("turtle");
  raptor_set_statement_handler(rdf_parser, config,
                               librdf_sql_config_store_triple);
  raptor_parse_file(rdf_parser, uri, base_uri);
  raptor_free_parser(rdf_parser);
  
  raptor_free_uri(base_uri);
  raptor_free_memory(uri_string);
  raptor_free_uri(uri);

  /* Check all values are given */
  for(i=0; i < config->predicates_count; i++) {
    if(!config->values[i]) {
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                 "Configuration %s missing for storage %s",
                 config->predicate_uri_strings[i], storage_name);
      librdf_free_sql_config(config);
      return NULL;
    }
  }
  
  return config;
}