Ejemplo n.º 1
0
/* Reads the file from the current position to the end into a string. */
static MVMString * slurp(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    uv_fs_t req;
    ensure_decode_stream(tc, data);

    /* Typically we're slurping an entire file, so just request the bytes
     * until the end; repeat to ensure we get 'em all. */
    if (uv_fs_fstat(tc->loop, &req, data->fd, NULL) < 0) {
        MVM_exception_throw_adhoc(tc, "slurp from filehandle failed: %s", uv_strerror(req.result));
    }
    /* Sometimes - usually for special files like those in /proc - the file
     * size comes up 0, even though the S_ISREG test succeeds. So in that case
     * we try a small read and switch to a "read chunks until EOF" impl.
     * Otherwise we just read the exact size of the file. */
    if (req.statbuf.st_size == 0) {
        if (read_to_buffer(tc, data, 32) > 0) {
            while (read_to_buffer(tc, data, 4096) > 0)
                ;
        }
    } else {
        while (read_to_buffer(tc, data, req.statbuf.st_size) > 0)
            ;
    }
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 2
0
 UniversalContainer uc_from_json_file(const char* fname)
 {
   Buffer* buf = read_to_buffer(fname);
   UniversalContainer uc = uc_decode_json(buf);
   delete buf;
   return uc;
 }
Ejemplo n.º 3
0
static void do_test_libeprom24x(void)
{  
  int value;

  do {
    print_menu();
    
    printf("Enter choice : ");
    scanf("%d",&value);
    
    switch(value) {
    case 1:
      get_prod_info();
      break;
    case 2:
      get_last_error();
      break;
    case 3:
      initialize();
      break;
    case 4:
      finalize();
      break;
    case 5:
      read_u8();
      break;
    case 6:
      read_u16();
      break;
    case 7:
      read_u32();
      break;
    case 8:
      read_to_buffer();
      break;
    case 9:
      write_u8();
      break;
    case 10:
      write_u16();
      break;
    case 11:
      write_u32();
      break;
    case 12:
      write_from_buffer();
      break;
    case 13:
      erase_chip();
      break;
    case 100: /* Exit */
      break;
    default:
      printf("Illegal choice!\n");
    }
  } while (value != 100);

  return;
}
Ejemplo n.º 4
0
int read_file(ino iNode, char *buffer, int offset, int numbytes) {
    char iNodeDataBlock[BLOCK_SIZE];
    int linesRead = -2;

    if (iNode < 16) {
        ReadBlock(4, iNodeDataBlock);
        iNodeEntry *iNodes = (iNodeEntry *)iNodeDataBlock;
        linesRead = read_to_buffer(iNodes[iNode], buffer, offset, numbytes);
    } else {
        ReadBlock(5, iNodeDataBlock);
        iNodeEntry *iNodes = (iNodeEntry *)iNodeDataBlock;
        linesRead =
            read_to_buffer(iNodes[iNode - 16], buffer, offset, numbytes);
    }

    return linesRead;
}
Ejemplo n.º 5
0
 UniversalContainer load_ini_file(const char* filename)
 {
   UniversalContainer uc;
   Buffer* buf = read_to_buffer(filename);
   if (!buf) return uc;
   uc = uc_decode_ini(buf);
   delete buf;
   return uc;
 }
Ejemplo n.º 6
0
/* Reads the stream from the current position to the end into a string,
 * fetching as much data is available. */
MVMString * MVM_io_syncstream_slurp(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* Fetch as much data as we can (XXX this can be more efficient, by
     * passing on down that we want to get many buffers from libuv). */
    while (read_to_buffer(tc, data, CHUNK_SIZE))
        ;
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 7
0
/* Reads the specified number of bytes into a the supplied buffer, returing
 * the number actually read. */
MVMint64 MVM_io_syncstream_read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf, MVMint64 bytes) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* See if we've already enough; if not, try and grab more. */
    if (!MVM_string_decodestream_have_bytes(tc, data->ds, bytes))
        read_to_buffer(tc, data, bytes > CHUNK_SIZE ? bytes : CHUNK_SIZE);

    /* Read as many as we can, up to the limit. */
    return MVM_string_decodestream_bytes_to_buf(tc, data->ds, buf, bytes);
}
Ejemplo n.º 8
0
/* Reads the specified number of bytes into a the supplied buffer, returing
 * the number actually read. */
static MVMint64 read_bytes(MVMThreadContext *tc, MVMOSHandle *h, char **buf, MVMint64 bytes) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* Keep requesting bytes until we have enough in the buffer or we hit
     * end of file. */
    while (!MVM_string_decodestream_have_bytes(tc, data->ds, bytes)) {
        if (read_to_buffer(tc, data, bytes) <= 0)
            break;
    }

    /* Read as many as we can, up to the limit. */
    return MVM_string_decodestream_bytes_to_buf(tc, data->ds, buf, bytes);
}
Ejemplo n.º 9
0
/* Gets the specified number of characters from the file. */
static MVMString * read_chars(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 chars) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* Pull data until we can read the chars we want. */
    do {
        MVMString *result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
        if (result != NULL)
            return result;
    } while (read_to_buffer(tc, data, CHUNK_SIZE) > 0);

    /* Reached end of file, so just take what we have. */
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 10
0
/* Reads the file from the current position to the end into a string. */
static MVMString * slurp(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    uv_fs_t req;
    ensure_decode_stream(tc, data);

    /* Typically we're slurping an entire file, so just request the bytes
     * until the end; repeat to ensure we get 'em all. */
    if (uv_fs_fstat(tc->loop, &req, data->fd, NULL) < 0) {
        MVM_exception_throw_adhoc(tc, "slurp from filehandle failed: %s", uv_strerror(req.result));
    }
    while (read_to_buffer(tc, data, req.statbuf.st_size) > 0)
        ;
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 11
0
/* Reads a single line from the file handle. May serve it from a buffer, if we
 * already read enough data. */
static MVMString * read_line(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    ensure_decode_stream(tc, data);

    /* Pull data until we can read a line. */
    do {
        MVMString *line = MVM_string_decodestream_get_until_sep(tc, data->ds, data->sep);
        if (line != NULL)
            return line;
    } while (read_to_buffer(tc, data, CHUNK_SIZE) > 0);

    /* Reached end of file, or last (non-termianted) line. */
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 12
0
/* Gets the specified number of characters from the stream. */
MVMString * MVM_io_syncstream_read_chars(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 chars) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)h->body.data;
    MVMString *result;
    ensure_decode_stream(tc, data);

    /* Do we already have the chars available? */
    result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
    if (result) {
        return result;
    }
    else {
        /* No; read and try again. */
        read_to_buffer(tc, data, CHUNK_SIZE);
        result = MVM_string_decodestream_get_chars(tc, data->ds, chars);
        if (result != NULL)
            return result;
    }

    /* Fetched all we immediately can, so just take what we have. */
    return MVM_string_decodestream_get_all(tc, data->ds);
}
Ejemplo n.º 13
0
void assert_last_line (HANDLE      h_stream,
                       const char *starts_with) {
	char  buffer[4096];
	char *c = buffer,
	     *last_line_position = c;

	read_to_buffer (h_stream, (char **)&buffer, 4096);

	while (1) {
		for (; *c != '\r' && *c != '\n' && *c != 0; c++);

		if (*c == 0 || *(c+1) == 0 || *(c+2) == 0)
			break;

		c ++;
		last_line_position = c;
	};

	if (strncmp (last_line_position, starts_with, strlen (starts_with)))
		rpc_log_error ("tester: assert_last_line: '%s'", buffer);
}
Ejemplo n.º 14
0
void dump (HANDLE h_stream) {
	char buffer[4096];

	read_to_buffer (h_stream, (char **)&buffer, 4095);
	printf ("%s", buffer);
}
Ejemplo n.º 15
0
void run_log_forwarder(struct fd_pair *internal, struct fd_pair *input, struct fd_pair *output,
                       struct fd_pair *error, int interactive) {

    int i;

    fd_set read_set;
    fd_set write_set;
    struct timeval timeout;

    int stdin_open    = 1;
    int stdout_open   = 1;
    int stderr_open   = 1;
    int internal_open = 1;
    int input_open    = 1;
    int output_open   = 1;
    int error_open    = error ? 1 : 0;

    int max_fd = internal->write_side;
    if (input->write_side > max_fd) max_fd = input->write_side;
    if (output->read_side > max_fd) max_fd = output->read_side;
    if (error_open && error->read_side > max_fd) max_fd = error->read_side;
    max_fd++;

    reserve = NULL;
    struct list internal_buffer = {NULL, NULL};
    struct list input_buffer = {NULL, NULL};
    struct list stdout_buffer = {NULL, NULL};
    struct list stderr_buffer = {NULL, NULL};

    /* non-blocking read/write */
    int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
    fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(STDERR_FILENO, F_GETFL, 0);
    fcntl(STDERR_FILENO, F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(internal->write_side, F_GETFL, 0);
    fcntl(internal->write_side, F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(input->write_side, F_GETFL, 0);
    fcntl(input->write_side, F_SETFL, flags | O_NONBLOCK);
    flags = fcntl(output->read_side, F_GETFL, 0);
    fcntl(output->read_side, F_SETFL, flags | O_NONBLOCK);
    if (error_open) {
        flags = fcntl(error->read_side, F_GETFL, 0);
        fcntl(error->read_side, F_SETFL, flags | O_NONBLOCK);
    }

    /* as long as there is a parent process and something to read */
    while (getppid() > 1 && (output_open || error_open))  {

        /* prepare read set */
        FD_ZERO(&read_set);
        if (stdin_open)  FD_SET(STDIN_FILENO,      &read_set);
        if (output_open) FD_SET(output->read_side, &read_set);
        if (error_open)  FD_SET(error->read_side,  &read_set);

        /* prepare write set */
        FD_ZERO(&write_set);
        if (internal_buffer.head && internal_open) FD_SET(internal->write_side, &write_set);
        if (input_buffer.head && input_open)       FD_SET(input->write_side,    &write_set);
        if (stdout_buffer.head && stdout_open)     FD_SET(STDOUT_FILENO,        &write_set);
        if (stderr_buffer.head && stderr_open)     FD_SET(STDERR_FILENO,        &write_set);

        /* timeout to avoid hanging processes if PPID changes to 1 between getppid and select */
        timeout.tv_sec = 300;
        timeout.tv_usec = 0;

        /* what to do */
        int result = select(max_fd, &read_set, &write_set, NULL, &timeout);

        /* error handling */
        if (result < 0) {
            if (EINTR == errno) continue;
            perror("select");
            break;
        }
        /* timeout */
        else if (0 == result) {
            continue;
        }

        /* non-blocking write */
        if (FD_ISSET(input->write_side, &write_set)) {
            input_open = write_from_buffer(input->write_side, &input_buffer, 0);
        }
        if (FD_ISSET(STDERR_FILENO, &write_set)) {
            stderr_open = write_from_buffer(STDERR_FILENO, &stderr_buffer, 0);
        }
        if (FD_ISSET(STDOUT_FILENO, &write_set)) {
            stdout_open = write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0);
        }
        if (FD_ISSET(internal->write_side, &write_set)) {
            internal_open = write_from_buffer(internal->write_side, &internal_buffer, 1);
        }

        /* non-blocking read */
        if (FD_ISSET(STDIN_FILENO, &read_set)) {
            stdin_open = read_to_buffer(STDIN_FILENO, &input_buffer,
                                        (internal_open && !interactive) ? &internal_buffer : NULL);
        }
        if (error_open && FD_ISSET(error->read_side, &read_set)) {
            error_open = read_to_buffer(error->read_side, &stderr_buffer,
                                        internal_open ? &internal_buffer : NULL);
        }
        if (FD_ISSET(output->read_side, &read_set)) {
            output_open = read_to_buffer(output->read_side, &stdout_buffer,
                                         internal_open ? &internal_buffer : NULL);
        }

        /* propagate closed input */
        if (!stdin_open && !input_buffer.head && input_open) {
            close(input->write_side);
            input_open = 0;
        }

    }

    /* do we have something left out for writing? (reset to blocking mode before writing) */
    flags = fcntl(STDERR_FILENO, F_GETFL, 0);
    fcntl(STDERR_FILENO, F_SETFL, flags & (~O_NONBLOCK));
    flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
    fcntl(STDOUT_FILENO, F_SETFL, flags & (~O_NONBLOCK));
    flags = fcntl(internal->write_side, F_GETFL, 0);
    fcntl(internal->write_side, F_SETFL, flags & (~O_NONBLOCK));

    while (stderr_buffer.head) {
        write_from_buffer(STDERR_FILENO, &stderr_buffer, 0);
    }
    while (stdout_buffer.head) {
        write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0);
    }
    while (internal_open && internal_buffer.head) {
        internal_open = write_from_buffer(internal->write_side, &internal_buffer, 1);
    }

    /* still some input? Write it out immediately 1:1*/
    if (internal_open) {
        while (error_open && read_to_buffer(error->read_side, &stderr_buffer, &internal_buffer)) {
            write_from_buffer(STDERR_FILENO, &stderr_buffer, 0);
            write_from_buffer(internal->write_side, &internal_buffer, 1);
        }
        while (read_to_buffer(output->read_side, &stdout_buffer, &internal_buffer)) {
            write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0);
            write_from_buffer(internal->write_side, &internal_buffer, 1);
        }
    }
    else {
        while (error_open && read_to_buffer(error->read_side, &stderr_buffer, NULL)) {
            write_from_buffer(STDERR_FILENO, &stderr_buffer, 0);
        }
        while (read_to_buffer(output->read_side, &stdout_buffer, NULL)) {
            write_from_buffer(STDOUT_FILENO, &stdout_buffer, 0);
        }
    }

    /* final cleanup */
    if (NULL != reserve) {
        free(reserve);
    }
}