Beispiel #1
0
int diskManager_readContents(u32int inodeNumber, char* contents, u32int length, u32int offset) {
    iNodeDisk inode;
    _getiNode(inodeNumber, &inode);
    if (inode.data.magic != MAGIC_NUMBER) {
        log(L_ERROR, "Trying to read a corrupted page at [%d, %d]", inode.data.nextSector, inode.data.nextOffset);
        errno = E_CORRUPTED_FILE;
        return -1;
    }
    // log(L_TRACE, "READING %d bytes from inode %d with %d bytes / %d", length, inodeNumber, inode.data.totalLength, inode.data.usedBytes);
    return _readBlock(&inode.data, contents, length, offset);
}
int iFuseBufferedFsReadBlock(iFuseFd_t *iFuseFd, char *buf, unsigned int blockID) {
    int status = 0;
    
    assert(iFuseFd != NULL);
    assert(buf != NULL);
    
    iFuseRodsClientLog(LOG_DEBUG, "iFuseBufferedFsReadBlock: %s, blockID: %u", iFuseFd->iRodsPath, blockID);
    
    status = _readBlock(iFuseFd, buf, blockID);
    if(status < 0) {
        iFuseRodsClientLogError(LOG_ERROR, status, "iFuseBufferedFsReadBlock: _readBlock of %s error, status = %d",
            iFuseFd->iRodsPath, status);
        return status;
    }
        
    return status;
}
/*
 * Read buffer cache data
 */
int iFuseBufferedFsRead(iFuseFd_t *iFuseFd, char *buf, off_t off, size_t size) {
    int status = 0;
    size_t readSize = 0;
    size_t remain = 0;
    off_t curOffset = 0;
    char *blockBuffer = (char*)calloc(1, IFUSE_BUFFER_CACHE_BLOCK_SIZE);
    if(blockBuffer == NULL) {
        return SYS_MALLOC_ERR;
    }

    assert(iFuseFd != NULL);
    assert(buf != NULL);

    iFuseRodsClientLog(LOG_DEBUG, "iFuseBufferedFsRead: %s, offset: %lld, size: %lld", iFuseFd->iRodsPath, (long long)off, (long long)size);
    
    // read in block level
    remain = size;
    curOffset = off;
    while(remain > 0) {
        off_t inBlockOffset = getInBlockOffset(curOffset);
        size_t inBlockAvail = IFUSE_BUFFER_CACHE_BLOCK_SIZE - inBlockOffset;
        size_t curSize = inBlockAvail > remain ? remain : inBlockAvail;
        size_t blockSize = 0;
        
        status = _readBlock(iFuseFd, blockBuffer, getBlockID(curOffset));
        if(status < 0) {
            iFuseRodsClientLogError(LOG_ERROR, status, "iFuseBufferedFsRead: _readBlock of %s error, status = %d",
                iFuseFd->iRodsPath, status);
            return status;
        } else if(status == 0) {
            // eof
            break;
        }

        blockSize = (size_t)status;

        iFuseRodsClientLog(LOG_DEBUG, "iFuseBufferedFsRead: _readBlock of %s, offset: %lld, in-block offset: %lld, curSize: %lld, size: %lld", iFuseFd->iRodsPath, (long long)curOffset, (long long)inBlockOffset, (long long)curSize, (long long)blockSize);

        if(inBlockOffset + curSize > blockSize) {
            curSize = blockSize - inBlockOffset;
        }

        if (curSize == 0) {
            // eof
            break;
        }

        iFuseRodsClientLog(LOG_DEBUG, "iFuseBufferedFsRead: block read of %s - offset %lld, size %lld", iFuseFd->iRodsPath, (long long)curOffset, (long long)curSize);

        // copy
        memcpy(buf + readSize, blockBuffer + inBlockOffset, curSize);

        readSize += curSize;
        remain -= curSize;
        curOffset += curSize;

        if(blockSize < IFUSE_BUFFER_CACHE_BLOCK_SIZE) {
            // eof
            break;
        }
    }

    free(blockBuffer);
    
    return readSize;
}