Beispiel #1
0
void
persist_state_alloc_string(PersistState *self, const gchar *persist_name, const gchar *value, gssize len)
{
  PersistEntryHandle handle;
  SerializeArchive *sa;
  GString *buf;
  gboolean success;
  gpointer block;

  if (len < 0)
    len = strlen(value);

  buf = g_string_sized_new(len + 5);
  sa = serialize_string_archive_new(buf);

  success = serialize_write_cstring(sa, value, len);
  g_assert(success == TRUE);

  serialize_archive_free(sa);

  handle = persist_state_alloc_entry(self, persist_name, buf->len);
  block = persist_state_map_entry(self, handle);
  memcpy(block, buf->str, buf->len);
  persist_state_unmap_entry(self, handle);
  g_string_free(buf, TRUE);
}
Beispiel #2
0
static void
_alloc_state(JournalReader *self)
{
    self->persist_handle = persist_state_alloc_entry(self->persist_state, self->persist_name, sizeof(JournalReaderState));
    JournalReaderState *state = persist_state_map_entry(self->persist_state, self->persist_handle);

    state->header.version = 0;
    state->header.big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);

    persist_state_unmap_entry(self->persist_state, self->persist_handle);
}
void
write_test_file_for_test_in_use_handle(gboolean in_use_handle)
{
    PersistState *state = clean_and_create_persist_state_for_test("test_in_use.persist");
    PersistEntryHandle handle = persist_state_alloc_entry(state, "alma", sizeof(TestState));
    TestState *test_state = (TestState*) persist_state_map_entry(state, handle);
    test_state->value = 0xDEADBEEF;
    persist_state_unmap_entry(state, handle);

    if (!in_use_handle)
        persist_state_remove_entry(state, "alma");

    commit_and_free_persist_state(state);
}
void
test_persist_state_foreach_entry(void)
{
    PersistState *state = clean_and_create_persist_state_for_test("test_persist_foreach.persist");

    PersistEntryHandle handle = persist_state_alloc_entry(state, "test", sizeof(TestState));
    TestState *test_state = persist_state_map_entry(state, handle);
    test_state->value = 3;
    persist_state_unmap_entry(state, handle);

    persist_state_foreach_entry(state, _foreach_callback_assertions, "test_userdata");

    cancel_and_destroy_persist_state(state);
}
Beispiel #5
0
int main(int argc, char** argv) {
	struct arguments args;

	set_default_args(&args);
	if (parse_args(argc, argv, &args)) {
		return 1;
	}

	PersistEntryHandle handle;
	struct journald_state* journald_state = NULL;

	msg_init(TRUE);
	PersistState* state = persist_state_new(args.filename);

	gsize state_size;
	guint8 persist_version;

	persist_state_start_dump(state);
	switch (args.action) {
	case LIST:
		persist_state_foreach_entry(state, &handle_entry, NULL);
	break;
	case SHOW_JOURNALD_CURSOR:
		handle = persist_state_lookup_entry(state, systemd_journal, &state_size, &persist_version);
		if (handle == 0)
			break;
		journald_state = persist_state_map_entry(state, handle);
		printf("journald cursor: %s\n", journald_state->cursor);
		persist_state_unmap_entry(state, handle);
	break;
	case SAVE_JOURNALD_CURSOR:
		handle = persist_state_alloc_entry(state, systemd_journal, sizeof(struct journald_state));
		if (handle == 0)
			break;
		journald_state = persist_state_map_entry(state, handle);
		journald_state->header.version = 0;
		journald_state->header.big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);
		strncpy(journald_state->cursor, args.action_arg.journald_cursor, MAX_CURSOR_LENGTH);
		persist_state_unmap_entry(state, handle);
		persist_state_commit(state);
	break;
	default:;
	}

	persist_state_free(state);
	msg_deinit();

	return 0;
}
Beispiel #6
0
static void
create_persist_file_with_hostid(const gchar *persist_file, guint32 hostid)
{
  PersistState *state;
  PersistEntryHandle handle;
  HostIdState *host_id_state;

  unlink(persist_file);
  state = create_persist_state(persist_file);
  handle = persist_state_alloc_entry(state, HOST_ID_PERSIST_KEY, sizeof(HostIdState));
  host_id_state = persist_state_map_entry(state, handle);
  host_id_state->host_id = hostid;
  persist_state_unmap_entry(state, handle);
  persist_state_commit(state);
  persist_state_free(state);
}
static PersistEntryHandle
log_proto_buffered_server_alloc_state(LogProtoBufferedServer *self, PersistState *persist_state, const gchar *persist_name)
{
  LogProtoBufferedServerState *state;
  PersistEntryHandle handle;

  handle = persist_state_alloc_entry(persist_state, persist_name, sizeof(LogProtoBufferedServerState));
  if (handle)
    {
      state = persist_state_map_entry(persist_state, handle);

      state->header.version = 0;
      state->header.big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);

      persist_state_unmap_entry(persist_state, handle);

    }
  return handle;
}
void
test_persist_state_remove_entry(void)
{
    guint8 version;
    gsize size;

    PersistState *state = clean_and_create_persist_state_for_test("test_persist_state_remove_entry.persist");

    PersistEntryHandle handle = persist_state_alloc_entry(state, "test", sizeof(TestState));

    handle = persist_state_lookup_entry(state, "test", &size, &version);
    assert_true(handle != 0, "lookup failed before removing entry");

    persist_state_remove_entry(state, "test");

    state = restart_persist_state(state);

    handle = persist_state_lookup_entry(state, "test", &size, &version);
    assert_true(handle == 0, "lookup succeeded after removing entry");

    cancel_and_destroy_persist_state(state);
}
void
test_values(void)
{
    PersistState *state;
    gint i, j;
    gchar *data;

    state = clean_and_create_persist_state_for_test("test_values.persist");

    for (i = 0; i < 1000; i++)
    {
        gchar buf[16];
        PersistEntryHandle handle;

        g_snprintf(buf, sizeof(buf), "testkey%d", i);
        if (!(handle = persist_state_alloc_entry(state, buf, 128)))
        {
            fprintf(stderr, "Error allocating value in the persist file: %s\n", buf);
            exit(1);
        }
        data = persist_state_map_entry(state, handle);
        for (j = 0; j < 128; j++)
        {
            data[j] = (i % 26) + 'A';
        }
        persist_state_unmap_entry(state, handle);
    }
    for (i = 0; i < 1000; i++)
    {
        gchar buf[16];
        PersistEntryHandle handle;
        gsize size;
        guint8 version;

        g_snprintf(buf, sizeof(buf), "testkey%d", i);
        if (!(handle = persist_state_lookup_entry(state, buf, &size, &version)))
        {
            fprintf(stderr, "Error retrieving value from the persist file: %s\n", buf);
            exit(1);
        }
        data = persist_state_map_entry(state, handle);
        for (j = 0; j < 128; j++)
        {
            if (data[j] != (i % 26) + 'A')
            {
                fprintf(stderr, "Invalid data in persistent entry\n");
                exit(1);
            }
        }
        persist_state_unmap_entry(state, handle);
    }

    state = restart_persist_state(state);

    for (i = 0; i < 1000; i++)
    {
        gchar buf[16];
        PersistEntryHandle handle;
        gsize size;
        guint8 version;

        g_snprintf(buf, sizeof(buf), "testkey%d", i);
        if (!(handle = persist_state_lookup_entry(state, buf, &size, &version)))
        {
            fprintf(stderr, "Error retrieving value from the persist file: %s\n", buf);
            exit(1);
        }
        if (size != 128 || version != 4)
        {
            fprintf(stderr, "Error retrieving value from the persist file: %s, invalid size (%d) or version (%d)\n", buf, (gint) size, version);
            exit(1);
        }
        data = persist_state_map_entry(state, handle);
        for (j = 0; j < 128; j++)
        {
            if (data[j] != (i % 26) + 'A')
            {
                fprintf(stderr, "Invalid data in persistent entry\n");
                exit(1);
            }
        }
        persist_state_unmap_entry(state, handle);
    }
    cancel_and_destroy_persist_state(state);
}