BurstFS_Client/src/cruise-sysio.c
int cruise_fd_read(int fd, off_t pos, void* buf, size_t count, size_t* retcount)
{
    /* get the file id for this file descriptor */
    int fid = cruise_get_fid_from_fd(fd);
    if (fid < 0) {
        return CRUISE_ERR_BADF;
    }
       
    /* it's an error to read from a directory */
    if(cruise_fid_is_dir(fid)){
       /* TODO: note that read/pread can return this, but not fread */
       return CRUISE_ERR_ISDIR;
    }

    /* check that file descriptor is open for read */
    cruise_fd_t* filedesc = cruise_get_filedesc_from_fd(fd);
    if (! filedesc->read) {
        return CRUISE_ERR_BADF;
    }

    /* TODO: is it safe to assume that off_t is bigger than size_t? */
    /* check that we don't overflow the file length */
    if (cruise_would_overflow_offt(pos, (off_t) count)) {
        return CRUISE_ERR_OVERFLOW;
    }

    /* TODO: check that file is open for reading */

    /* check that we don't try to read past the end of the file */
    off_t lastread = pos + (off_t) count;
    off_t filesize = cruise_fid_size(fid);
    if (filesize < lastread) {
        /* adjust count so we don't read past end of file */
        if (filesize > pos) {
            /* read all bytes until end of file */
            count = (size_t) (filesize - pos);
        } else {
            /* pos is already at or past the end of the file */
            count = 0;
        }
    }

    /* record number of bytes that we'll actually read */
    *retcount = count;

    /* if we don't read any bytes, return success */
    if (count == 0){
        return CRUISE_SUCCESS;
    }

    /* read data from file */
    int read_rc = cruise_fid_read(fid, pos, buf, count);
    return read_rc;
}
/* read count bytes info buf from file starting at offset pos,
 * returns number of bytes actually read in retcount,
 * retcount will be less than count only if an error occurs
 * or end of file is reached */
read count bytes info buf from file starting at offset pos,
returns number of bytes actually read in retcount,
retcount will be less than count only if an error occurs
or end of file is reached
read count bytes info buf from file starting at offset pos returns number of bytes actually read in retcount retcount will be less than count only if an error occurs or end of file is reached
int cruise_fd_read(int fd, off_t pos, void* buf, size_t count, size_t* retcount)
{
    int fid = cruise_get_fid_from_fd(fd);
    if (fid < 0) {
        return CRUISE_ERR_BADF;
    }
    if(cruise_fid_is_dir(fid)){
       return CRUISE_ERR_ISDIR;
    }
    cruise_fd_t* filedesc = cruise_get_filedesc_from_fd(fd);
    if (! filedesc->read) {
        return CRUISE_ERR_BADF;
    }
    if (cruise_would_overflow_offt(pos, (off_t) count)) {
        return CRUISE_ERR_OVERFLOW;
    }
    off_t lastread = pos + (off_t) count;
    off_t filesize = cruise_fid_size(fid);
    if (filesize < lastread) {
        if (filesize > pos) {
            count = (size_t) (filesize - pos);
        } else {
            count = 0;
        }
    }
    *retcount = count;
    if (count == 0){
        return CRUISE_SUCCESS;
    }
    int read_rc = cruise_fid_read(fid, pos, buf, count);
    return read_rc;
}
int cruise_fd_read ( int fd , off_t pos , void * buf , size_t count , size_t * retcount ) { int fid = cruise_get_fid_from_fd ( fd ) ; if ( fid < 0 ) { return CRUISE_ERR_BADF ; } if ( cruise_fid_is_dir ( fid ) ) { return CRUISE_ERR_ISDIR ; } cruise_fd_t * filedesc = cruise_get_filedesc_from_fd ( fd ) ; if ( ! filedesc -> read ) { return CRUISE_ERR_BADF ; } if ( cruise_would_overflow_offt ( pos , ( off_t ) count ) ) { return CRUISE_ERR_OVERFLOW ; } off_t lastread = pos + ( off_t ) count ; off_t filesize = cruise_fid_size ( fid ) ; if ( filesize < lastread ) { if ( filesize > pos ) { count = ( size_t ) ( filesize - pos ) ; } else { count = 0 ; } } * retcount = count ; if ( count == 0 ) { return CRUISE_SUCCESS ; } int read_rc = cruise_fid_read ( fid , pos , buf , count ) ; return read_rc ; }
read count bytes info buf from file starting at offset pos,
 returns number of bytes actually read in retcount,
 retcount will be less than count only if an error occurs
 or end of file is reached
read count bytes info buf from file starting at offset pos returns number of bytes actually read in retcount retcount will be less than count only if an error occurs or end of file is reached
/* get the file id for this file descriptor */ /* it's an error to read from a directory */ /* TODO: note that read/pread can return this, but not fread */ /* check that file descriptor is open for read */ /* TODO: is it safe to assume that off_t is bigger than size_t? */ /* check that we don't overflow the file length */ /* TODO: check that file is open for reading */ /* check that we don't try to read past the end of the file */ /* adjust count so we don't read past end of file */ /* read all bytes until end of file */ /* pos is already at or past the end of the file */ /* record number of bytes that we'll actually read */ /* if we don't read any bytes, return success */ /* read data from file */