//<"LSCH"><CeHeader><CacheKey><ResponseHeader><ResponseBody>
int DirHashCacheEntry::loadCeHeader()
{
    int fd = getFdStore();
    if ( fd == -1 )
    {
        errno = EBADF;
        return -1;
    }
    if ( nio_lseek( fd, getStartOffset(), SEEK_SET ) == -1 )
        return -1;
    char achBuf[4 + sizeof( CeHeader ) ];
    if ( (size_t)nio_read( fd, achBuf, 4 + sizeof( CeHeader ) ) 
                < 4 + sizeof( CeHeader ) )
        return -1;
//  if ( *( uint32_t *)achBuf != CE_ID )
//     return -1;
    if (memcmp(achBuf, CE_ID, 4) != 0)
        return -1;
    
    memmove( &getHeader(), &achBuf[4], sizeof( CeHeader ) );
    int len = getHeader().m_keyLen;
    if ( len > 0 )
    {
        char * p = getKey().prealloc( len+1 );
        if ( !p )   
            return -1;
        if ( nio_read( fd, p, len ) < len )
            return -1;
        *(p+len) = 0;
    }
    
    char tmpBUf[4096];
#ifdef CACHE_RESP_HEADER
    if (getHeader().m_valPart1Len < 4096 ) //< 4K
    {
        if ( nio_read( fd, tmpBUf, getHeader().m_valPart1Len ) < getHeader().m_valPart1Len )
            return -1;
        
        m_sRespHeader.append(tmpBUf, getHeader().m_valPart1Len);
    }
#endif

    //load part3 to buffer
    int part3offset = getHeaderSize() + getContentTotalLen();
    if ( nio_lseek( fd, part3offset, SEEK_SET ) != -1 )
    {
        while((len = nio_read( fd, tmpBUf, 4096 )) > 0)
            m_sPart3Buf.append(tmpBUf, len);
    }
    
    return 0;

}
int LshttpdMain::processAdminSockConn( int fd )
{
    char achBuf[4096];
    char * pEnd = &achBuf[4096];
    char * p = achBuf;
    int len;
    while( (len = nio_read( fd, p, pEnd - p )) > 0 )
    {
        p += len;
    }
    
    if (( len == -1 )||( pEnd == p ))
    {
        LOG_ERR(( "[ADMIN] failed to read command, command buf len=%d",
                    (int)(p - achBuf) ));
        return -1;
    }
    char * pEndAuth = strchr( achBuf, '\n' );
    if ( !pEndAuth )
        return -1;
    if ( m_pServer->authAdminReq( achBuf ) != 0 )
        return -1;
    ++pEndAuth;
    return processAdminBuffer( pEndAuth, p );
    
}
const char * FileCacheDataEx::getCacheData(
    off_t offset, off_t& wanted, char *pBuf, long len )
{
    if ( isCached() )
    {
        if ( offset > m_lSize )
        {
            wanted = 0;
            return pBuf;
        }
        if ( wanted > m_lSize - offset )
            wanted = m_lSize - offset;
        return m_pCache + offset;
    }
    else
    {
        assert( m_fd != -1 );
        off_t off = nio_lseek( m_fd, offset, SEEK_SET );
/*        if ( D_ENABLED( DL_MORE ))
            LOG_D(( "lseek() return %d", (int)off ));*/
        if ( off == offset )
        {
            wanted = nio_read( m_fd, pBuf, len );
        }
        else
        {
            wanted = -1;
        }
        return pBuf;
    }

}
int FileCacheDataEx::readyData(const char * pPath)
{
    int fd;
    int ret = openFile( pPath, fd );
    if ( ret )
        return ret;
    if ( (size_t)m_lSize < s_iMaxInMemCacheSize )
    {
        ret = allocateCache( m_lSize );
        if ( ret == 0 )
        {
            ret = nio_read( fd, m_pCache, m_lSize );
            if ( ret == m_lSize )
            {
                close( fd );
                return 0;
            }
            else
            {
                release();
            }
        }
    }
    else if (( (size_t)m_lSize < s_iMaxMMapCacheSize )
            &&((size_t)m_lSize + s_iCurTotalMMAPCache < s_iMaxTotalMMAPCache ))
    {
        m_pCache = (char *)mmap( 0, m_lSize, PROT_READ,
                MAP_PRIVATE, fd, 0 );
        s_iCurTotalMMAPCache += m_lSize;
        if ( D_ENABLED( DL_MORE ))
            LOG_D(( "[MMAP] Map %p to file:%s", m_pCache, pPath ));
        if ( m_pCache == MAP_FAILED )
        {
            m_pCache = 0;
        }
        else
        {
            setStatus( MMAPED );
            close( fd );
            return 0;
        }
    }
    setfd( fd );
    fcntl( fd, F_SETFD, FD_CLOEXEC );
    return 0;
}
Beispiel #5
0
int GzipBuf::processFile( int type, const char * pFileName,
                    const char * pCompressFileName )
{
    int fd;
    int ret = 0;
    fd = open( pFileName, O_RDONLY );
    if ( fd == -1 )
        return -1;
    VMemBuf gzFile;
    ret = gzFile.set( pCompressFileName, -1 );
    if ( !ret )
    {
        setCompressCache( &gzFile );
        if ( ((ret = init( type, 6 )) == 0 )&&((ret = beginStream()) == 0 ))
        {
            int len;
            char achBuf[16384];
            while( true )
            {
                len = nio_read( fd, achBuf, sizeof( achBuf ) );
                if ( len <= 0 )
                    break;
                if ( this->write( achBuf, len ) )
                {
                    ret = -1;
                    break;
                }
            }
            if ( !ret )
            {
                ret = endStream();
                long size;
                if ( !ret )
                    ret = gzFile.exactSize( &size );
            }
            gzFile.close();
        }
    }
    ::close( fd );
    return ret;
}