int main(int argc, char *argv[]) { int const num_legalchars = strlen(legalchars); FILE *randomf = fopen("/dev/urandom", "r"); char identifier[256]; int remaining = 0; DIE_UNLESS(argc == 2); remaining = atoi(argv[1]); DIE_UNLESS(remaining > 0); DIE_UNLESS(randomf != NULL); for (; remaining > 0; remaining--) { uint8_t identifier_length = 0; while (identifier_length == 0) { DIE_UNLESS(fread(&identifier_length, 1, 1, randomf) == 1); } for (int pos = 0; pos < identifier_length; pos++) { uint8_t alea = num_legalchars; while (alea >= num_legalchars) { DIE_UNLESS(fread(&alea, 1, 1, randomf) == 1); } identifier[pos] = legalchars[alea]; } identifier[identifier_length] = '\0'; printf("(#\"%s\" %lu)\n", identifier, elf_hash(identifier)); } fclose(randomf); return 0; }
int seg_readbuf( btFileSet *fs, int piece, int start, char *buf, int len) { btFile *f; _int64 addr = ((_int64)piece * fs->blocksize)+start; int ifile; if (!bs_isSet(&fs->completed, piece)) { printf("Attempted to read uncompleted block %d from disk.\n", piece); return -1; } for (ifile=0; ifile < fs->nfiles; ifile++) { f=fs->file[ifile]; if ( f->start+f->len >= addr && f->start < addr + len) { /* this file (partly) includes this piece */ int fd = cacheopen( f->path, O_RDONLY, 0); off_t fpos = addr - f->start; _int64 rlen; _int64 beg = 0; ssize_t res; #if 0 fprintf( stderr, "cacheopen( %s, O_RDONLY)\n", f->path); #endif if (fd == -1) { #if 0 bts_perror(errno, "readbuf.cacheopen"); #endif return -1; } if (fpos < 0) { beg = -fpos; fpos = 0; } rlen = min(f->len - fpos, len - beg); if (lseek( fd, fpos, SEEK_SET) != fpos) { SYSDIE("lseek failed"); return -1; } DIE_UNLESS( rlen <= fs->blocksize); res = read(fd, buf+beg, rlen); if (res != rlen) { if (res == 0) { /* EOF */ return -1; } fprintf(stderr, "On file '%s'\n", f->path); fprintf(stderr, "read( ..., buf[%lld], %lld) = %d\n", beg, rlen, res); bts_perror(errno, "readbuf.read"); return -1; } } } return 0; }
static void verify_col_data(const char *table, const char *col, const char *exp_data) { static char query[MAX_TEST_QUERY_LENGTH]; MYSQL_RES *result; MYSQL_ROW row; int rc, field= 1; if (table && col) { strxmov(query, "SELECT ", col, " FROM ", table, " LIMIT 1", NullS); if (!opt_silent) fprintf(stdout, "\n %s", query); rc= mysql_query(mysql, query); myquery(rc); field= 0; } result= mysql_use_result(mysql); mytest(result); if (!(row= mysql_fetch_row(result)) || !row[field]) { fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***"); exit(1); } if (strcmp(row[field], exp_data)) { fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", row[field], exp_data); DIE_UNLESS(FALSE); } mysql_free_result(result); }
static void verify_param_count(MYSQL_STMT *stmt, long exp_count) { long param_count= mysql_stmt_param_count(stmt); if (!opt_silent) fprintf(stdout, "\n total parameters in stmt: `%ld` (expected: `%ld`)", param_count, exp_count); DIE_UNLESS(param_count == exp_count); }
static void verify_affected_rows(ulonglong exp_count) { ulonglong affected_rows= mysql_affected_rows(mysql); if (!opt_silent) fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)", (long) affected_rows, (long) exp_count); DIE_UNLESS(affected_rows == exp_count); }
static void verify_field_count(MYSQL_RES *result, uint exp_count) { uint field_count= mysql_num_fields(result); if (!opt_silent) fprintf(stdout, "\n total fields in the result set: `%d` (expected: `%d`)", field_count, exp_count); DIE_UNLESS(field_count == exp_count); }
/* * Return 0 if continue downloading * Return 1 if block complete */ int seg_writebuf( btFileSet *fs, int piece, int offset, char *buf, int len) { int res=0; int blocksize; btPartialPiece *p=fs->partial; while(p && p->piecenumber!=piece) { p=p->next; } if (!p) { printf("got extra packet %d, offset %d\n", piece, offset); return 0; } blocksize = seg_piecelen( fs, piece); DIE_UNLESS(offset>=0 && offset<blocksize && offset+len<=blocksize && len>0); memcpy(p->buffer + offset, buf, len); bs_setRange( &p->filled, offset, offset+len); #if 0 printf("packet %d of %d, offset %d, blocksize %d\n", piece, fs->npieces, offset, blocksize); #endif if ( p->isdone || bs_isFull( &p->filled)) { /* last data for this piece */ p->isdone=1; if (_checkhash( fs, piece, p->buffer)) { /* Remove from partial list */ btPartialPiece *pp=fs->partial; if(pp==p) { /* First in list */ fs->partial=p->next; } else { while(pp->next != p) pp=pp->next; pp->next=p->next; } fs->dl += blocksize; fs->left -= blocksize; #if 1 printf("hash ok for block %d\n", piece); #endif seg_write( fs, p); kBitSet_finit( &p->filled); btfree(p); res = 1; } else { printf("hash bad for block %d\n", piece); } } return res; }
static my_bool check_have_innodb(MYSQL *conn) { MYSQL_RES *res; MYSQL_ROW row; int rc; my_bool result; rc= mysql_query(conn, "show variables like 'have_innodb'"); myquery(rc); res= mysql_use_result(conn); DIE_UNLESS(res); row= mysql_fetch_row(res); DIE_UNLESS(row); result= strcmp(row[1], "YES") == 0; mysql_free_result(res); return result; }
void my_process_warnings(MYSQL *conn, unsigned expected_warning_count) { MYSQL_RES *result; int rc; if (!opt_silent) fprintf(stdout, "\n total warnings: %u (expected: %u)\n", mysql_warning_count(conn), expected_warning_count); DIE_UNLESS(mysql_warning_count(mysql) == expected_warning_count); rc= mysql_query(conn, "SHOW WARNINGS"); DIE_UNLESS(rc == 0); result= mysql_store_result(conn); mytest(result); rc= my_process_result_set(result); mysql_free_result(result); }
static my_bool check_have_innodb(MYSQL *conn) { MYSQL_RES *res; MYSQL_ROW row; int rc; my_bool result; rc= mysql_query(conn, "SELECT (support = 'YES' or support = 'DEFAULT' or support = 'ENABLED') " "AS `TRUE` FROM information_schema.engines WHERE engine = 'innodb'"); myquery(rc); res= mysql_use_result(conn); DIE_UNLESS(res); row= mysql_fetch_row(res); DIE_UNLESS(row); result= strcmp(row[1], "1") == 0; mysql_free_result(res); return result; }
int btFileSet_addfile( btFileSet *fs, const char *path, _int64 len) { int ifile=fs->nfiles++; btFile *f; #if 0 printf("addfile( ..., %s, %d)\n", path, len); #endif fs->file = btrealloc(fs->file, sizeof(btFile *)*fs->nfiles); DIE_UNLESS(fs->file); f=btcalloc(1,sizeof(btFile)); DIE_UNLESS(f); f->path = strdup(path); f->len = len; if (ifile > 0) { btFile *lf = fs->file[ifile-1]; f->start = lf->start + lf->len; } fs->file[ifile]=f; return 0; }
int bts_printf( btStream* bts, char *fmt, ...) { static char buf[8192]; int res; int osize; va_list va; va_start(va, fmt); osize=vsnprintf(buf, sizeof(buf), fmt, va); DIE_UNLESS(osize < sizeof(buf)); res = bts_write( bts, buf, osize); va_end(va); return res; }
int seg_piecelen( btFileSet *fs, int piece) { int blocklen; blocklen = fs->blocksize; if (piece == fs->npieces-1) { /* last block may be short */ _int64 modulus = fs->tsize % (_int64)fs->blocksize; if (modulus) blocklen = (int)modulus; } DIE_UNLESS(piece>=0 && piece<fs->npieces); return blocklen; }
btFileSet* btFileSet_create( btFileSet *fs, int npieces, int blocksize, const char *hashbuf) { if (!fs) { fs = btcalloc( 1, sizeof(btFileSet)); DIE_UNLESS(fs); } fs->npieces = npieces; fs->blocksize = blocksize; fs->dl = 0; fs->ul = 0; fs->hashes = btmalloc( npieces * SHA_DIGEST_LENGTH); memcpy(fs->hashes, hashbuf, npieces * SHA_DIGEST_LENGTH); kBitSet_create(&fs->completed, npieces); return fs; }
static void execute_prepare_query(const char *query, ulonglong exp_count) { MYSQL_STMT *stmt; ulonglong affected_rows; int rc; stmt= mysql_simple_prepare(mysql, query); check_stmt(stmt); rc= mysql_stmt_execute(stmt); myquery(rc); affected_rows= mysql_stmt_affected_rows(stmt); if (!opt_silent) fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)", (long) affected_rows, (long) exp_count); DIE_UNLESS(affected_rows == exp_count); mysql_stmt_close(stmt); }
btPartialPiece * seg_getPiece( btFileSet *fs, int piece) { btPartialPiece *p; DIE_UNLESS(piece>=0 && piece<fs->npieces); p=fs->partial; while(p && p->piecenumber!=piece) { p=p->next; } if(!p) { int blocksize=seg_piecelen(fs, piece); /* Allocation trick: the memory after the structure * is the piece contents */ p=btcalloc(1, sizeof(btPartialPiece)+blocksize); p->piecenumber=piece; kBitSet_create( &p->filled, blocksize); pp_addtail( fs, p); } return p; }
static int seg_write( btFileSet *fs, btPartialPiece *piece) { btFile *f; _int64 addr = (_int64)piece->piecenumber * fs->blocksize; int ifile; for (ifile=0; ifile < fs->nfiles; ifile++) { int blocklen = seg_piecelen( fs, piece->piecenumber); f=fs->file[ifile]; if ( f->start+f->len >= addr && f->start < addr + blocklen) { /* this file (partly) includes this piece */ _int64 beg = f->start - addr; off_t fpos=0; _int64 len; int fd = openPath( f->path, O_CREAT | O_WRONLY); if (fd < 0) { SYSDIE("open failed"); } if (beg<0) { fpos=-beg; beg=0; } len = min(f->len - fpos, (_int64)blocklen - beg); if (lseek( fd, fpos, SEEK_SET) != fpos) { SYSDIE("lseek failed"); } DIE_UNLESS( len <= fs->blocksize); if (write( fd, piece->buffer+beg, len) != len) { SYSDIE("write failed"); } } } bs_set( &fs->completed, piece->piecenumber); return 0; }
static int my_process_stmt_result(MYSQL_STMT *stmt) { int field_count; int row_count= 0; MYSQL_BIND buffer[MAX_RES_FIELDS]; MYSQL_FIELD *field; MYSQL_RES *result; char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE]; ulong length[MAX_RES_FIELDS]; my_bool is_null[MAX_RES_FIELDS]; int rc, i; if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */ { while (!mysql_stmt_fetch(stmt)) row_count++; return row_count; } field_count= MY_MIN(mysql_num_fields(result), MAX_RES_FIELDS); memset(buffer, 0, sizeof(buffer)); memset(length, 0, sizeof(length)); memset(is_null, 0, sizeof(is_null)); for(i= 0; i < field_count; i++) { buffer[i].buffer_type= MYSQL_TYPE_STRING; buffer[i].buffer_length= MAX_FIELD_DATA_SIZE; buffer[i].length= &length[i]; buffer[i].buffer= (void *) data[i]; buffer[i].is_null= &is_null[i]; } rc= mysql_stmt_bind_result(stmt, buffer); check_execute(stmt, rc); rc= 1; mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc); rc= mysql_stmt_store_result(stmt); check_execute(stmt, rc); my_print_result_metadata(result); mysql_field_seek(result, 0); while ((rc= mysql_stmt_fetch(stmt)) == 0) { if (!opt_silent) { fputc('\t', stdout); fputc('|', stdout); } mysql_field_seek(result, 0); for (i= 0; i < field_count; i++) { field= mysql_fetch_field(result); if (!opt_silent) { if (is_null[i]) fprintf(stdout, " %-*s |", (int) field->max_length, "NULL"); else if (length[i] == 0) { data[i][0]= '\0'; /* unmodified buffer */ fprintf(stdout, " %*s |", (int) field->max_length, data[i]); } else if (IS_NUM(field->type)) fprintf(stdout, " %*s |", (int) field->max_length, data[i]); else fprintf(stdout, " %-*s |", (int) field->max_length, data[i]); } } if (!opt_silent) { fputc('\t', stdout); fputc('\n', stdout); } row_count++; } DIE_UNLESS(rc == MYSQL_NO_DATA); if (!opt_silent) { if (row_count) my_print_dashes(result); fprintf(stdout, "\n\t%d %s returned\n", row_count, row_count == 1 ? "row" : "rows"); } mysql_free_result(result); return row_count; }
static void do_verify_prepare_field(MYSQL_RES *result, unsigned int no, const char *name, const char *org_name, enum enum_field_types type, const char *table, const char *org_table, const char *db, unsigned long length, const char *def, const char *file, int line) { MYSQL_FIELD *field; CHARSET_INFO *cs; ulonglong expected_field_length; if (!(field= mysql_fetch_field_direct(result, no))) { fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***"); exit(1); } cs= get_charset(field->charsetnr, 0); DIE_UNLESS(cs); if ((expected_field_length= length * cs->mbmaxlen) > UINT_MAX32) expected_field_length= UINT_MAX32; if (!opt_silent) { fprintf(stdout, "\n field[%d]:", no); fprintf(stdout, "\n name :`%s`\t(expected: `%s`)", field->name, name); fprintf(stdout, "\n org_name :`%s`\t(expected: `%s`)", field->org_name, org_name); fprintf(stdout, "\n type :`%d`\t(expected: `%d`)", field->type, type); if (table) fprintf(stdout, "\n table :`%s`\t(expected: `%s`)", field->table, table); if (org_table) fprintf(stdout, "\n org_table:`%s`\t(expected: `%s`)", field->org_table, org_table); fprintf(stdout, "\n database :`%s`\t(expected: `%s`)", field->db, db); fprintf(stdout, "\n length :`%lu`\t(expected: `%llu`)", field->length, expected_field_length); fprintf(stdout, "\n maxlength:`%ld`", field->max_length); fprintf(stdout, "\n charsetnr:`%d`", field->charsetnr); fprintf(stdout, "\n default :`%s`\t(expected: `%s`)", field->def ? field->def : "(null)", def ? def: "(null)"); fprintf(stdout, "\n"); } DIE_UNLESS(strcmp(field->name, name) == 0); DIE_UNLESS(strcmp(field->org_name, org_name) == 0); /* XXX: silent column specification change works based on number of bytes a column occupies. So CHAR -> VARCHAR upgrade is possible even for CHAR(2) column if its character set is multibyte. VARCHAR -> CHAR downgrade won't work for VARCHAR(3) as one would expect. */ if (cs->mbmaxlen == 1) { if (field->type != type) { fprintf(stderr, "Expected field type: %d, got type: %d in file %s, line %d\n", (int) type, (int) field->type, file, line); DIE_UNLESS(field->type == type); } } if (table) DIE_UNLESS(strcmp(field->table, table) == 0); if (org_table) DIE_UNLESS(strcmp(field->org_table, org_table) == 0); DIE_UNLESS(strcmp(field->db, db) == 0); /* Character set should be taken into account for multibyte encodings, such as utf8. Field length is calculated as number of characters * maximum number of bytes a character can occupy. */ if (length && (field->length != expected_field_length)) { fprintf(stderr, "Expected field length: %llu, got length: %lu\n", expected_field_length, field->length); DIE_UNLESS(field->length == expected_field_length); } if (def) DIE_UNLESS(strcmp(field->def, def) == 0); }