BurstFS_Client/src/cruise-sysio.c
int cruise_fd_write(int fd, off_t pos, const void* buf, size_t count)
{

    /* 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 write to a directory */
    if(cruise_fid_is_dir(fid)){
        return CRUISE_ERR_INVAL;
    }

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

    /* TODO: is it safe to assume that off_t is bigger than size_t? */
    /* check that our write won't overflow the length */
    if (cruise_would_overflow_offt(pos, (off_t) count)) {
        /* TODO: want to return EFBIG here for streams */
        return CRUISE_ERR_OVERFLOW;
    }

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

    /* get current file size before extending the file */
    off_t filesize = cruise_fid_size(fid);
    cruise_filemeta_t* meta = cruise_get_meta_from_fid(fid);
    off_t newpos;

    if (meta->storage == FILE_STORAGE_FIXED_CHUNK) {
        /* extend file size and allocate chunks if needed */
	    	newpos = pos + (off_t) count;
        int extend_rc = cruise_fid_extend(fid, newpos);
        if (extend_rc != CRUISE_SUCCESS) {
            return extend_rc;
        }

        /* fill any new bytes between old size and pos with zero values */
        if (filesize < pos) {
            off_t gap_size = pos - filesize;
            int zero_rc = cruise_fid_write_zero(fid, filesize, gap_size);
            if (zero_rc != CRUISE_SUCCESS) {
                return zero_rc;
            }
        }
    }

    else if (meta->storage == FILE_STORAGE_LOGIO) {
   		 	newpos = filesize + (off_t)count;

        int extend_rc = cruise_fid_extend(fid, newpos);
        if (extend_rc != CRUISE_SUCCESS) {

            return extend_rc;
        }
    }
    else {
			return CRUISE_ERR_IO;
		}
    /* finally write specified data to file */
    int write_rc = cruise_fid_write(fid, pos, buf, count);
   
	if (meta->storage == FILE_STORAGE_LOGIO) {
			cruise_filemeta_t *meta = cruise_get_meta_from_fid(fid);
			if (write_rc == 0)
					meta->size = newpos;
					meta->real_size = pos+count;
	}
    return write_rc;
}
/* write count bytes from buf into file starting at offset pos,
 * allocates new bytes and updates file size as necessary,
 * fills any gaps with zeros */
write count bytes from buf into file starting at offset pos,
allocates new bytes and updates file size as necessary,
fills any gaps with zeros
write count bytes from buf into file starting at offset pos allocates new bytes and updates file size as necessary fills any gaps with zeros
int cruise_fd_write(int fd, off_t pos, const void* buf, size_t count)
{
    int fid = cruise_get_fid_from_fd(fd);
    if (fid < 0) {
        return CRUISE_ERR_BADF;
    }
    if(cruise_fid_is_dir(fid)){
        return CRUISE_ERR_INVAL;
    }
    cruise_fd_t* filedesc = cruise_get_filedesc_from_fd(fd);
    if (! filedesc->write) {
        return CRUISE_ERR_BADF;
    }
    if (cruise_would_overflow_offt(pos, (off_t) count)) {
        return CRUISE_ERR_OVERFLOW;
    }
    off_t filesize = cruise_fid_size(fid);
    cruise_filemeta_t* meta = cruise_get_meta_from_fid(fid);
    off_t newpos;
    if (meta->storage == FILE_STORAGE_FIXED_CHUNK) {
	    	newpos = pos + (off_t) count;
        int extend_rc = cruise_fid_extend(fid, newpos);
        if (extend_rc != CRUISE_SUCCESS) {
            return extend_rc;
        }
        if (filesize < pos) {
            off_t gap_size = pos - filesize;
            int zero_rc = cruise_fid_write_zero(fid, filesize, gap_size);
            if (zero_rc != CRUISE_SUCCESS) {
                return zero_rc;
            }
        }
    }
    else if (meta->storage == FILE_STORAGE_LOGIO) {
   		 	newpos = filesize + (off_t)count;
        int extend_rc = cruise_fid_extend(fid, newpos);
        if (extend_rc != CRUISE_SUCCESS) {
            return extend_rc;
        }
    }
    else {
			return CRUISE_ERR_IO;
		}
    int write_rc = cruise_fid_write(fid, pos, buf, count);
	if (meta->storage == FILE_STORAGE_LOGIO) {
			cruise_filemeta_t *meta = cruise_get_meta_from_fid(fid);
			if (write_rc == 0)
					meta->size = newpos;
					meta->real_size = pos+count;
	}
    return write_rc;
}
int cruise_fd_write ( int fd , off_t pos , const void * buf , size_t count ) { int fid = cruise_get_fid_from_fd ( fd ) ; if ( fid < 0 ) { return CRUISE_ERR_BADF ; } if ( cruise_fid_is_dir ( fid ) ) { return CRUISE_ERR_INVAL ; } cruise_fd_t * filedesc = cruise_get_filedesc_from_fd ( fd ) ; if ( ! filedesc -> write ) { return CRUISE_ERR_BADF ; } if ( cruise_would_overflow_offt ( pos , ( off_t ) count ) ) { return CRUISE_ERR_OVERFLOW ; } off_t filesize = cruise_fid_size ( fid ) ; cruise_filemeta_t * meta = cruise_get_meta_from_fid ( fid ) ; off_t newpos ; if ( meta -> storage == FILE_STORAGE_FIXED_CHUNK ) { newpos = pos + ( off_t ) count ; int extend_rc = cruise_fid_extend ( fid , newpos ) ; if ( extend_rc != CRUISE_SUCCESS ) { return extend_rc ; } if ( filesize < pos ) { off_t gap_size = pos - filesize ; int zero_rc = cruise_fid_write_zero ( fid , filesize , gap_size ) ; if ( zero_rc != CRUISE_SUCCESS ) { return zero_rc ; } } } else if ( meta -> storage == FILE_STORAGE_LOGIO ) { newpos = filesize + ( off_t ) count ; int extend_rc = cruise_fid_extend ( fid , newpos ) ; if ( extend_rc != CRUISE_SUCCESS ) { return extend_rc ; } } else { return CRUISE_ERR_IO ; } int write_rc = cruise_fid_write ( fid , pos , buf , count ) ; if ( meta -> storage == FILE_STORAGE_LOGIO ) { cruise_filemeta_t * meta = cruise_get_meta_from_fid ( fid ) ; if ( write_rc == 0 ) meta -> size = newpos ; meta -> real_size = pos + count ; } return write_rc ; }
write count bytes from buf into file starting at offset pos,
 allocates new bytes and updates file size as necessary,
 fills any gaps with zeros
write count bytes from buf into file starting at offset pos allocates new bytes and updates file size as necessary fills any gaps with zeros
/* get the file id for this file descriptor */ /* it's an error to write to a directory */ /* check that file descriptor is open for write */ /* TODO: is it safe to assume that off_t is bigger than size_t? */ /* check that our write won't overflow the length */ /* TODO: want to return EFBIG here for streams */ /* TODO: check that file is open for writing */ /* get current file size before extending the file */ /* extend file size and allocate chunks if needed */ /* fill any new bytes between old size and pos with zero values */ /* finally write specified data to file */