BackupPC-XS

 view release on metacpan or  search on metacpan

bpc_lib.c  view on Meta::CPAN

         * up to 1MB.
         */
        int seekPosn = (bufferLen > 1048576 ? 1048576 : bufferLen) - 131072;
        md5_update(&md5, buffer, 131072);
        md5_update(&md5, buffer + seekPosn, 131072);
    } else {
        /*
         * add the whole buffer
         */
        md5_update(&md5, buffer, bufferLen);
    }
    md5_result(&md5, digest->digest);
    digest->len = MD5_DIGEST_LEN;
}

static void bpc_fileNameEltMangle2(char *path, int pathSize, char *pathUM, int stopAtSlash)
{
    if ( !*pathUM || (stopAtSlash && *pathUM == '/') ) {
        *path = '\0';
        return;
    }
    *path++ = 'f'; pathSize--;
    for ( ; *pathUM && pathSize > 4 ; ) {
        if ( stopAtSlash && *pathUM == '/' ) break;
        if ( *pathUM != '%' && *pathUM != '/' && *pathUM != '\n' && *pathUM != '\r' ) {
            *path++ = *pathUM++; pathSize--;
        } else {
            *path++ = '%'; pathSize--;
            bpc_byte2hex(path, *pathUM++);
            path += 2; pathSize -= 2;
        }
    }
    *path = '\0';
}

void bpc_fileNameEltMangle(char *path, int pathSize, char *pathUM)
{
    bpc_fileNameEltMangle2(path, pathSize, pathUM, 0);
}

void bpc_fileNameMangle(char *path, int pathSize, char *pathUM)
{
    char *p;

    for ( ; *pathUM && pathSize > 4 ; ) {
        int len;

        bpc_fileNameEltMangle2(path, pathSize, pathUM, 1);
        len = strlen(path);
        path += len; pathSize -= len;
        if ( !(p = strchr(pathUM, '/')) ) break;
        for ( pathUM = p + 1 ; *pathUM == '/' ; pathUM++ ) { }
        if ( *pathUM ) {
            *path++ = '/'; pathSize--;
        }
    }
    *path = '\0';
}

/* 
 * Simple logging functions.  If you register callbacks, they will be called.
 * Otherwise, messages are accumulated, and a callback allows the
 * log strings to be fetched.
 *
 * We store the data in a single buffer of '\0'-terminated strings.
 */
typedef struct {
    char *mesg;
    size_t mesgSize;
    size_t mesgLen;
    unsigned long errorCnt;
} LogMsgData;

static LogMsgData LogData;
static void (*LogMsgCB)(int, char *mesg, size_t mesgLen);

void bpc_logMsgf(char *fmt, ...)
{
    int strLen, pad = 0;
    va_list args;
    va_start(args, fmt); 

    if ( !LogData.mesg ) {
        LogData.mesgSize = 8192;
        LogData.mesgLen  = 0;
        if ( !(LogData.mesg = malloc(LogData.mesgSize)) ) {
            printf("bpc_logMessagef: panic: can't allocate %lu bytes\n", (unsigned long)LogData.mesgSize);
            LogData.errorCnt++;
            return;
        }
    }
    if ( BPC_TmpFileUnique >= 0 ) pad = 2;
    strLen = vsnprintf(LogData.mesg + LogData.mesgLen + pad, LogData.mesgSize - LogData.mesgLen - pad, fmt, args);
    if ( strLen + 2 + pad + LogData.mesgLen > LogData.mesgSize ) {
        LogData.mesgSize += LogData.mesgSize + strLen + 2 + pad;
        if ( !(LogData.mesg = realloc(LogData.mesg, LogData.mesgSize)) ) {
            printf("bpc_logMessagef: panic: can't realloc %lu bytes\n", (unsigned long)LogData.mesgSize);
            LogData.errorCnt++;
            return;
        }
        va_start(args, fmt); 
        strLen = vsnprintf(LogData.mesg + LogData.mesgLen + pad, LogData.mesgSize - LogData.mesgLen - pad, fmt, args);
        va_end(args);
    }
    if ( strLen > 0 ) {
        if ( pad ) {
            LogData.mesg[LogData.mesgLen++] = BPC_TmpFileUnique ? 'G' : 'R';
            LogData.mesg[LogData.mesgLen++] = ' ';
        }
        LogData.mesgLen += strLen + 1;
    }
    if ( LogMsgCB ) {
        (*LogMsgCB)(0, LogData.mesg, LogData.mesgLen - 1);
        LogData.mesgLen = 0;
    }
    va_end(args);
}

/*
 * For now, this is just the same as bpc_logMsgf().  We can't call a common routine that
 * does the work, since args might need to be parsed twice when the buffer is grown.



( run in 2.637 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )