static int cmyth_storagegroup_update_fileinfo(cmyth_conn_t control, cmyth_storagegroup_file_t file) { char msg[256]; int count; int err = 0; int consumed; /* = profiles;*/ char tmp_str[2048]; if (!control) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n", __FUNCTION__); return -1; } if (!file) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: no file specified\n", __FUNCTION__); return -1; } snprintf(msg, sizeof(msg), "QUERY_SG_FILEQUERY[]:[]%s[]:[]%s[]:[]%s", file->hostname , file->storagegroup, file->filename); err = cmyth_send_message(control, msg); if (err < 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_send_message() failed (%d)\n", __FUNCTION__, err); return -1; } count = cmyth_rcv_length(control); if (count < 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_length() failed (%d)\n", __FUNCTION__, count); return -1; } consumed = cmyth_rcv_string(control, &err, tmp_str, sizeof(tmp_str) - 1, count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_string() failed (%d)\n", __FUNCTION__, count); return -1; } else if (count == 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: QUERY_SG_FILEQUERY failed(%s)\n", __FUNCTION__, tmp_str); return -1; } consumed = cmyth_rcv_ulong(control, &err, &(file->lastmodified), count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_ulong() failed (%d)\n", __FUNCTION__, count); return -1; } consumed = cmyth_rcv_ulong(control, &err, &(file->size), count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_ulong_long() failed (%d)\n", __FUNCTION__, count); return -1; } return 0; }
/* * cmyth_conn_check_block(cmyth_conn_t conn, unsigned long size) * * Scope: PUBLIC * * Description * * Check whether a block has finished transfering from a backend * server. This non-blocking check looks for a response from the * server indicating that a block has been entirely sent to on a data * socket. * * Return Value: * * Success: 0 for not complete, 1 for complete * * Failure: -(errno) */ int cmyth_conn_check_block(cmyth_conn_t conn, unsigned long size) { fd_set check; struct timeval timeout; int length; int err = 0; unsigned long sent; if (!conn) { return -EINVAL; } timeout.tv_sec = timeout.tv_usec = 0; FD_ZERO(&check); FD_SET(conn->conn_fd, &check); if (select((int)conn->conn_fd + 1, &check, NULL, NULL, &timeout) < 0) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: select failed (%d)\n", __FUNCTION__, errno); return -(errno); } if (FD_ISSET(conn->conn_fd, &check)) { /* * We have a bite, reel it in. */ length = cmyth_rcv_length(conn); if (length < 0) { return length; } cmyth_rcv_ulong(conn, &err, &sent, length); if (err) { return -err; } if (sent == size) { /* * This block has been sent, return TRUE. */ cmyth_dbg(CMYTH_DBG_DEBUG, "%s: block finished (%d bytes)\n", __FUNCTION__, sent); return 1; } else { cmyth_dbg(CMYTH_DBG_ERROR, "%s: block finished short (%d bytes)\n", __FUNCTION__, sent); return -ECANCELED; } } return 0; }
cmyth_storagegroup_file_t cmyth_storagegroup_get_fileinfo(cmyth_conn_t control, char *storagegroup, char *hostname, char *filename) { char msg[256]; int count = 0; int err = 0; cmyth_storagegroup_file_t ret = NULL; int consumed = 0; char tmp_str[2048]; if (!control) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n", __FUNCTION__); return 0; } pthread_mutex_lock(&mutex); snprintf(msg, sizeof(msg), "QUERY_SG_FILEQUERY[]:[]%s[]:[]%s[]:[]%s", hostname, storagegroup, filename); err = cmyth_send_message(control, msg); if (err < 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_send_message() failed (%d)\n", __FUNCTION__, err); goto out; } count = cmyth_rcv_length(control); if (count < 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_length() failed (%d)\n", __FUNCTION__, count); goto out; } consumed = cmyth_rcv_string(control, &err, tmp_str, sizeof(tmp_str) - 1, count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_string() failed (%d)\n", __FUNCTION__, count); ret = NULL; goto out; } else if (count == 0) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: QUERY_SG_FILEQUERY failed(%s)\n", __FUNCTION__, tmp_str); ret = NULL; goto out; } ret = cmyth_storagegroup_file_create(); if (!ret) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: alloc() failed for file\n", __FUNCTION__); ref_release(ret); ret = NULL; goto out; } ret->filename = ref_strdup(tmp_str); consumed = cmyth_rcv_ulong(control, &err, &(ret->lastmodified), count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_ulong() failed (%d)\n", __FUNCTION__, count); ref_release(ret); ret = NULL; goto out; } consumed = cmyth_rcv_ulong(control, &err, &(ret->size), count); count -= consumed; if (err) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_rcv_ulong_long() failed (%d)\n", __FUNCTION__, count); ref_release(ret); ret = NULL; goto out; } cmyth_dbg(CMYTH_DBG_DEBUG, "%s: filename: %s\n", __FUNCTION__, ret->filename); out: pthread_mutex_unlock(&mutex); return ret; }
/* * cmyth_ringbuf_read (cmyth_recorder_t rec, char *buf, unsigned long len) * * Scope: PUBLIC * * Description * * Request and read a block of data from backend * * Return Value: * * Sucess: number of bytes transfered * * Failure: an int containing -errno */ int cmyth_ringbuf_read(cmyth_recorder_t rec, char *buf, unsigned long len) { int err, count; int ret, req, nfds; char *end, *cur; char msg[256]; struct timeval tv; fd_set fds; if (!rec) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: no connection\n", __FUNCTION__); return -EINVAL; } pthread_mutex_lock (&mutex); snprintf(msg, sizeof(msg), "QUERY_RECORDER %u[]:[]REQUEST_BLOCK_RINGBUF[]:[]%ld", rec->rec_id, len); if ( (err = cmyth_send_message (rec->rec_conn, msg) ) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: cmyth_send_message() failed (%d)\n", __FUNCTION__, err); ret = err; goto out; } nfds = 0; req = 1; cur = buf; end = buf+len; while (cur < end || req) { tv.tv_sec = 20; tv.tv_usec = 0; FD_ZERO (&fds); if(req) { if((int)rec->rec_conn->conn_fd > nfds) nfds = (int)rec->rec_conn->conn_fd; FD_SET (rec->rec_conn->conn_fd, &fds); } if((int)rec->rec_ring->conn_data->conn_fd > nfds) nfds = (int)rec->rec_ring->conn_data->conn_fd; FD_SET (rec->rec_ring->conn_data->conn_fd, &fds); if ((ret = select (nfds+1, &fds, NULL, NULL,&tv)) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: select(() failed (%d)\n", __FUNCTION__, ret); goto out; } if (ret == 0) { rec->rec_ring->conn_data->conn_hang = 1; rec->rec_conn->conn_hang = 1; ret = -ETIMEDOUT; goto out; } /* check control connection */ if (FD_ISSET(rec->rec_conn->conn_fd, &fds) ) { if ((count = cmyth_rcv_length (rec->rec_conn)) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: cmyth_rcv_length() failed (%d)\n", __FUNCTION__, count); ret = count; goto out; } if ((ret = cmyth_rcv_ulong (rec->rec_conn, &err, &len, count))< 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: cmyth_rcv_long() failed (%d)\n", __FUNCTION__, ret); ret = err; goto out; } rec->rec_ring->file_pos += len; req = 0; end = buf+len; } /* check data connection */ if (FD_ISSET(rec->rec_ring->conn_data->conn_fd, &fds)) { if ((ret = recv (rec->rec_ring->conn_data->conn_fd, cur, end-cur, 0)) < 0) { cmyth_dbg (CMYTH_DBG_ERROR, "%s: recv() failed (%d)\n", __FUNCTION__, ret); goto out; } cur += ret; } } ret = end - buf; out: pthread_mutex_unlock (&mutex); return ret; }