GSList* parse_acl_conf_file(const gchar* file_path, GError **error) { /* TODO: open file and read it line by line */ int fd; ssize_t r; guint8 buff[256]; GByteArray* data = NULL; GSList* result = NULL; fd = metautils_syscall_open(file_path, O_RDONLY, 0); if (fd == -1) { GSETERROR(error, "Failed to open file [%s] : %s", file_path, strerror(errno)); return NULL; } data = g_byte_array_new(); while ((r = metautils_syscall_read(fd, buff, sizeof(buff))) > 0) { data = g_byte_array_append(data, buff, r); } metautils_pclose(&fd); if (r < 0) { GSETERROR(error, "Failed to read data from file [%s] : %s", file_path, strerror(errno)); g_byte_array_free(data, TRUE); return NULL; } result = _parse_acl_bytes(data); return result; }
static void fd_consume_input(int fd) { guint8 data[256]; (void) metautils_syscall_read(fd, data, sizeof(data)); }
static GError * _client_manage_event_in_buffer(struct gridd_client_s *client, guint8 *d, gsize ds) { ssize_t rc; switch (client->step) { case CONNECTING: g_assert(client->fd >= 0); client->step = client->request ? REQ_SENDING : CONNECTED; return NULL; case REQ_SENDING: client->step = REQ_SENDING; g_get_current_time(&(client->tv_step)); if (!client->request) return NULL; _client_reset_reply(client); /* Continue to send the request */ rc = metautils_syscall_write(client->fd, client->request->data + client->sent_bytes, client->request->len - client->sent_bytes); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "write error (%s)", strerror(errno)); if (rc > 0) client->sent_bytes += rc; if (client->sent_bytes < client->request->len) return NULL; client->step = REP_READING_SIZE; case REP_READING_SIZE: client->step = REP_READING_SIZE; g_get_current_time(&(client->tv_step)); if (!client->reply) client->reply = g_byte_array_new(); if (client->reply->len < 4) { /* Continue reading the size */ rc = metautils_syscall_read(client->fd, d, ds); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "read error (%s)", strerror(errno)); if (rc > 0) g_byte_array_append(client->reply, d, rc); if (client->reply->len < 4) { if (!rc) return NEWERROR(errno, "EOF!"); return NULL; } } client->size = l4v_get_size(client->reply->data); case REP_READING_DATA: client->step = REP_READING_DATA; g_get_current_time(&(client->tv_step)); rc = 0; if (client->reply->len < client->size+4) { rc = metautils_syscall_read(client->fd, d, ds); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "read error (%s)", strerror(errno)); if (rc > 0) g_byte_array_append(client->reply, d, rc); } if (client->reply->len >= client->size+4) { GError *err = _client_manage_reply_data(client); if (err) { client->step = STATUS_FAILED; return err; } else { if (client->step != CONNECTING && client->step != STATUS_FAILED && client->step != STATUS_OK) { client->reply = g_byte_array_remove_range(client->reply, 0, client->size+4); client->step = REP_READING_SIZE; client->size = 0; } } } else if (!rc) return NEWERROR(errno, "EOF!"); return NULL; default: g_assert_not_reached(); return NEWERROR(0, "Invalid state"); } g_assert_not_reached(); return NEWERROR(0, "BUG unreachable code"); }
static GError * _client_manage_event_in_buffer(struct gridd_client_s *client, guint8 *d, gsize ds) { guint32 s32; ssize_t rc; switch (client->step) { case CONNECTING: EXTRA_ASSERT(client->fd >= 0); EXTRA_ASSERT(client->request != NULL); client->step = REQ_SENDING; return NULL; case REQ_SENDING: client->step = REQ_SENDING; if (!client->request) return NULL; _client_reset_reply(client); /* Continue to send the request */ rc = metautils_syscall_write(client->fd, client->request->data + client->sent_bytes, client->request->len - client->sent_bytes); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "write error (%s)", strerror(errno)); if (rc > 0) client->sent_bytes += rc; if (client->sent_bytes < client->request->len) return NULL; client->step = REP_READING_SIZE; case REP_READING_SIZE: client->step = REP_READING_SIZE; if (!client->reply) client->reply = g_byte_array_new(); if (client->reply->len < 4) { /* Continue reading the size */ rc = metautils_syscall_read(client->fd, d, (4 - client->reply->len)); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "read error (%s)", strerror(errno)); if (rc > 0) g_byte_array_append(client->reply, d, rc); if (client->reply->len < 4) { if (!rc) return NEWERROR(errno, "EOF!"); return NULL; } } EXTRA_ASSERT (client->reply->len == 4); s32 = *((guint32*)(client->reply->data)); client->size = g_ntohl(s32); case REP_READING_DATA: client->step = REP_READING_DATA; rc = 0; EXTRA_ASSERT (client->reply->len <= client->size + 4); if (client->reply->len < client->size + 4) { gsize remaiming = client->size + 4 - client->reply->len; gsize dmax = ds; if (dmax > remaiming) dmax = remaiming; rc = metautils_syscall_read(client->fd, d, dmax); if (rc < 0) return (errno == EINTR || errno == EAGAIN) ? NULL : NEWERROR(errno, "read error (%s)", strerror(errno)); if (rc > 0) g_byte_array_append(client->reply, d, rc); } EXTRA_ASSERT (client->reply->len <= client->size + 4); if (client->reply->len == client->size + 4) { GError *err = _client_manage_reply_data(client); if (err) { client->step = STATUS_FAILED; return err; } else { if (client->step != CONNECTING && client->step != STATUS_FAILED && client->step != STATUS_OK) { client->reply = g_byte_array_set_size(client->reply, 0); client->step = REP_READING_SIZE; client->size = 0; } } } else if (!rc) return NEWERROR(errno, "EOF!"); return NULL; default: g_assert_not_reached(); return NEWERROR(0, "Invalid state"); } g_assert_not_reached(); return NEWERROR(0, "BUG unreachable code"); }