/*
- * write input from fd to dynamically allocated char array,
- * but maximal max_size byte. Return size.
+ * write input from fd to dynamically allocated buffer,
+ * but maximal max_size byte.
*/
-static int fd2buf(int fd, char **buf, int max_size)
+static int fd2buf(int fd, unsigned max_size, struct osl_object *obj)
{
const size_t chunk_size = 1024;
- size_t size = 2048;
- char *p;
+ size_t size = 2048, received = 0;
int ret;
+ char *buf = para_malloc(size);
- *buf = para_malloc(size * sizeof(char));
- p = *buf;
- while ((ret = read(fd, p, chunk_size)) > 0) {
- p += ret;
- if ((p - *buf) + chunk_size >= size) {
- char *tmp;
-
+ for (;;) {
+ ret = recv_bin_buffer(fd, buf + received, chunk_size);
+ if (ret <= 0)
+ break;
+ received += ret;
+ if (received + chunk_size >= size) {
size *= 2;
- if (size > max_size) {
- ret = -E_INPUT_TOO_LARGE;
- goto out;
- }
- tmp = para_realloc(*buf, size);
- p = (p - *buf) + tmp;
- *buf = tmp;
+ ret = -E_INPUT_TOO_LARGE;
+ if (size > max_size)
+ break;
+ buf = para_realloc(buf, size);
}
}
- if (ret < 0) {
- ret = -E_READ;
- goto out;
- }
- ret = p - *buf;
-out:
- if (ret < 0 && *buf)
- free(*buf);
+ obj->data = buf;
+ obj->size = received;
+ if (ret < 0)
+ free(buf);
return ret;
}
* \return Negative on errors, the return value of the underlying call to
* send_callback_request() otherwise.
*/
-int stdin_command(struct osl_object *arg_obj, callback_function *f,
+int stdin_command(int fd, struct osl_object *arg_obj, callback_function *f,
unsigned max_len, struct osl_object *result)
{
- char *stdin_buf;
- size_t stdin_len;
- struct osl_object query;
- int ret = fd2buf(STDIN_FILENO, &stdin_buf, max_len);
+ struct osl_object query, stdin_obj;
+ int ret;
+ ret = send_buffer(fd, AWAITING_DATA_MSG);
+ if (ret < 0)
+ return ret;
+ ret = fd2buf(fd, max_len, &stdin_obj);
if (ret < 0)
return ret;
- stdin_len = ret;
- query.size = arg_obj->size + stdin_len;
+ query.size = arg_obj->size + stdin_obj.size;
query.data = para_malloc(query.size);
memcpy(query.data, arg_obj->data, arg_obj->size);
- memcpy((char *)query.data + arg_obj->size, stdin_buf, stdin_len);
- free(stdin_buf);
+ memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
+ free(stdin_obj.data);
ret = send_callback_request(f, &query, result);
free(query.data);
return ret;
memcpy(output->data, obj.data, obj.size);
return osl_close_disk_object(&obj);
}
+
static int com_catblob(callback_function *f, int fd, int argc,
char * const * const argv)
{
if (!*argv[1]) /* empty name is reserved of the dummy row */
return -E_BLOB_SYNTAX;
ret = send_standard_callback_request(1, argv + 1, f, &cat_output);
- if (ret >= 0 && cat_output.data)
- ret = send_buffer(fd, (char *)cat_output.data);
+ if (ret > 0)
+ ret = send_bin_buffer(fd, (char *)cat_output.data, cat_output.size);
free(cat_output.data);
return ret;
return osl_add_row(table, objs);
}
-static int com_addblob(callback_function *f, __a_unused int fd, int argc,
+static int com_addblob(callback_function *f, int fd, int argc,
char * const * const argv)
{
struct osl_object arg_obj;
PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
arg_obj.size = strlen(argv[1]) + 1;
arg_obj.data = (char *)argv[1];
- return stdin_command(&arg_obj, f, 10 * 1024 * 1024, NULL);
+ return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
}
static int com_rmblob_callback(struct osl_table *table,