Exemple #1
0
int chksumLocFile(
    char*       _file_name,
    char*       _checksum,
    const char* _hash_scheme ) {
    if ( !_file_name ||
            !_checksum  ||
            !_hash_scheme ) {
        rodsLog(
            LOG_ERROR,
            "chksumLocFile :: null input param - %p %p %p",
            _file_name,
            _checksum,
            _hash_scheme );
        return SYS_INVALID_INPUT_PARAM;
    }

    // =-=-=-=-=-=-=-
    // capture client side configuration
    rodsEnv env;
    int status = getRodsEnv( &env );
    if ( status < 0 ) {
        return status;
    }

    // =-=-=-=-=-=-=-
    // capture the configured scheme if it is valid
    std::string env_scheme( irods::MD5_NAME );
    if ( strlen( env.rodsDefaultHashScheme ) > 0 ) {
        env_scheme = env.rodsDefaultHashScheme;

    }

    // =-=-=-=-=-=-=-
    // capture the configured hash match policy if it is valid
    std::string env_policy;
    if ( strlen( env.rodsMatchHashPolicy ) > 0 ) {
        env_policy = env.rodsMatchHashPolicy;
        // =-=-=-=-=-=-=-
        // hash scheme keywords are all lowercase
        std::transform(
            env_scheme.begin(),
            env_scheme.end(),
            env_scheme.begin(),
            ::tolower );
    }

    // =-=-=-=-=-=-=-
    // capture the incoming scheme if it is valid
    std::string hash_scheme;
    if ( _hash_scheme &&
            strlen( _hash_scheme ) > 0 &&
            strlen( _hash_scheme ) < NAME_LEN ) {
        hash_scheme = _hash_scheme;
        // =-=-=-=-=-=-=-
        // hash scheme keywords are all lowercase
        std::transform(
            hash_scheme.begin(),
            hash_scheme.end(),
            hash_scheme.begin(),
            ::tolower );
    }
    else {
        hash_scheme = env_scheme;

    }

    // =-=-=-=-=-=-=-
    // hash scheme keywords are all lowercase
    std::transform(
        hash_scheme.begin(),
        hash_scheme.end(),
        hash_scheme.begin(),
        ::tolower );
    std::transform(
        env_scheme.begin(),
        env_scheme.end(),
        env_scheme.begin(),
        ::tolower );


    // =-=-=-=-=-=-=-
    // verify checksum scheme against configuration
    // if requested
    std::string final_scheme( env_scheme );
    if ( !hash_scheme.empty() ) {
        if ( !env_policy.empty() ) {
            if ( irods::STRICT_HASH_POLICY == env_policy ) {
                if ( env_scheme != hash_scheme ) {
                    return USER_HASH_TYPE_MISMATCH;
                }
            }
        }

        final_scheme = hash_scheme;
    }

    // =-=-=-=-=-=-=-
    // init the hasher object
    irods::Hasher hasher;
    irods::error ret = irods::getHasher(
                           final_scheme,
                           hasher );
    if ( !ret.ok() ) {
        irods::log( PASS( ret ) );
        return ret.code();

    }

    // =-=-=-=-=-=-=-
    // open the local file
    std::ifstream in_file(
        _file_name,
        std::ios::in | std::ios::binary );
    if ( !in_file.is_open() ) {
        status = UNIX_FILE_OPEN_ERR - errno;
        rodsLogError(
            LOG_NOTICE,
            status,
            "chksumLocFile - fopen failed for %s, status = %d",
            _file_name,
            status );
        return status;
    }

    char buffer_read[ MD5_BUF_SZ ];
    std::streamsize bytes_read = in_file.readsome(
                                     buffer_read,
                                     MD5_BUF_SZ );
    while ( bytes_read > 0 ) {
        hasher.update(
            std::string(
                buffer_read,
                bytes_read ) );
        bytes_read = in_file.readsome(
                         buffer_read,
                         MD5_BUF_SZ );
    }

    // =-=-=-=-=-=-=-
    // capture the digest
    std::string digest;
    hasher.digest( digest );
    strncpy(
        _checksum,
        digest.c_str(),
        digest.size() + 1 );

    return 0;

} // chksumLocFile
Exemple #2
0
int
chksumLocFile( char *fileName, char *chksumStr, const char* scheme ) {
    FILE *file = 0;
    int len = 0;
    char buffer[MD5_BUF_SZ];
    // =-=-=-=-=-=-=-
    // capture client side configuration
    rodsEnv env;
    int status = getRodsEnv( &env );
    if ( status < 0 ) {
        return status;
    }

    // =-=-=-=-=-=-=-
    // capture the configured scheme if it is valid
    std::string env_scheme( irods::MD5_NAME );
    if ( strlen( env.rodsDefaultHashScheme ) > 0 ) {
        env_scheme = env.rodsDefaultHashScheme;
    }

    // =-=-=-=-=-=-=-
    // capture the configured hash match policy if it is valid
    std::string env_policy;
    if ( strlen( env.rodsMatchHashPolicy ) > 0 ) {
        env_policy = env.rodsMatchHashPolicy;
    }

    // =-=-=-=-=-=-=-
    // capture the incoming scheme if it is valid
    std::string hash_scheme;
    if ( scheme &&
            strlen( scheme ) > 0 &&
            strlen( scheme ) < NAME_LEN ) {
        hash_scheme = scheme;
    }

    // =-=-=-=-=-=-=-
    // verify checksum scheme against configuration
    // if requested
    std::string final_scheme( env_scheme );
    if ( !hash_scheme.empty() ) {
        if ( !env_policy.empty() ) {
            if ( irods::STRICT_HASH_POLICY == env_policy ) {
                if ( env_scheme != hash_scheme ) {
                    return USER_HASH_TYPE_MISMATCH;
                }
            }
        }

        final_scheme = hash_scheme;
    }

    // =-=-=-=-=-=-=-
    // open the local file
    if ( ( file = fopen( fileName, "rb" ) ) == NULL ) {
        status = UNIX_FILE_OPEN_ERR - errno;
        rodsLogError( LOG_NOTICE, status,
                      "chksumFile; fopen failed for %s. status = %d", fileName, status );
        return status;
    }

    // =-=-=-=-=-=-=-
    // init the hasher object
    irods::Hasher hasher;
    irods::error ret = irods::getHasher( final_scheme, hasher );

    while ( ( len = fread( buffer, 1, MD5_BUF_SZ, file ) ) > 0 ) {
        hasher.update( std::string( buffer, len ) );
    }
    fclose( file );

    // =-=-=-=-=-=-=-
    // capture the digest
    std::string digest;
    hasher.digest( digest );
    strncpy( chksumStr, digest.c_str(), digest.size() + 1 );
    return 0;
}