Example #1
0
/*---------------------------------------------------------------------------*/
int
cfs_read(int fd, void *buf, unsigned size)
{
  struct file_desc *fdp;
  struct file *file;
#if COFFEE_MICRO_LOGS
  struct file_header hdr;
  struct log_param lp;
  unsigned bytes_left;
  int r;
#endif

  if(!(FD_VALID(fd) && FD_READABLE(fd))) {
    return -1;
  }

  fdp = &coffee_fd_set[fd];
  file = fdp->file;
  if(fdp->offset + size > file->end) {
    size = file->end - fdp->offset;
  }

  /* If the file is allocated, read directly in the file. */
  if(!FILE_MODIFIED(file)) {
    COFFEE_READ(buf, size, absolute_offset(file->page, fdp->offset));
    fdp->offset += size;
    return size;
  }

#if COFFEE_MICRO_LOGS
  read_header(&hdr, file->page);

  /*
   * Fill the buffer by copying from the log in first hand, or the
   * ordinary file if the page has no log record.
   */
  for(bytes_left = size; bytes_left > 0; bytes_left -= r) {
    r = -1;

    lp.offset = fdp->offset;
    lp.buf = buf;
    lp.size = bytes_left;
    r = read_log_page(&hdr, file->record_count, &lp);

    /* Read from the original file if we cannot find the data in the log. */
    if(r < 0) {
      COFFEE_READ(buf, lp.size, absolute_offset(file->page, fdp->offset));
      r = lp.size;
    }
    fdp->offset += r;
    buf = (char *)buf + r;
  }
#endif /* COFFEE_MICRO_LOGS */

  return size;
}
Example #2
0
/*---------------------------------------------------------------------------*/
int
cfs_read(int fd, void *buf, unsigned size)
{
  struct file_desc *fdp;
  struct file *file;
#if COFFEE_MICRO_LOGS
  struct file_header hdr;
  struct log_param lp;
  unsigned bytes_left;
  int r;
#endif

  if(!(FD_VALID(fd) && FD_READABLE(fd))) {
    return -1;
  }

  fdp = &coffee_fd_set[fd];
  file = fdp->file;
  if(fdp->offset + size > file->end) {
    size = file->end - fdp->offset;
  }

  /* If the file is not modified, read directly from the file extent. */
  if(!FILE_MODIFIED(file)) {
    COFFEE_READ(buf, size, absolute_offset(file->page, fdp->offset));
    fdp->offset += size;
    return size;
  }

#if COFFEE_MICRO_LOGS
  read_header(&hdr, file->page);

  /*
   * Copy the contents of the most recent log record. If there is
   * no log record for the file area to read from, we simply read
   * from the original file extent.
   */
  for(bytes_left = size; bytes_left > 0; bytes_left -= r) {
    lp.offset = fdp->offset;
    lp.buf = buf;
    lp.size = bytes_left;
    r = read_log_page(&hdr, file->record_count, &lp);

    /* Read from the original file if we cannot find the data in the log. */
    if(r < 0) {
      COFFEE_READ(buf, lp.size, absolute_offset(file->page, fdp->offset));
      r = lp.size;
    }
    fdp->offset += r;
    buf = (char *)buf + r;
  }
#endif /* COFFEE_MICRO_LOGS */

  return size;
}
/*---------------------------------------------------------------------------*/
int
cfs_read(int fd, void *buf, unsigned size)
{
    struct file_header hdr;
    struct file_desc *fdp;
    struct file *file;
    unsigned bytes_left;
    int r;
#if COFFEE_MICRO_LOGS
    struct log_param lp;
#endif

    if(!(FD_VALID(fd) && FD_READABLE(fd))) {
        return -1;
    }

    fdp = &coffee_fd_set[fd];
    file = fdp->file;
    if(fdp->offset + size > file->end) {
        size = file->end - fdp->offset;
    }

    bytes_left = size;
    if(FILE_MODIFIED(file)) {
        read_header(&hdr, file->page);
    }

    /*
     * Fill the buffer by copying from the log in first hand, or the
     * ordinary file if the page has no log record.
     */
    while(bytes_left) {
        watchdog_periodic();
        r = -1;
#if COFFEE_MICRO_LOGS
        if(FILE_MODIFIED(file)) {
            lp.offset = fdp->offset;
            lp.buf = buf;
            lp.size = bytes_left;
            r = read_log_page(&hdr, file->record_count, &lp);
        }
#endif /* COFFEE_MICRO_LOGS */
        /* Read from the original file if we cannot find the data in the log. */
        if(r < 0) {
            r = bytes_left;
            COFFEE_READ(buf, r, absolute_offset(file->page, fdp->offset));
        }
        bytes_left -= r;
        fdp->offset += r;
        buf += r;
    }
    return size;
}