void db_save(const char* const name, uint8_t* data, uint32_t data_len) { if(false == db_init_done) return; if(0 != db_find(name)) db_delete(name); uint32_t i, j; uint8_t name_len = strlen(name); i = db_index; for(j = 0; j < name_len; j++) { db_buf[i + j] = name[j]; } db_buf[i + j] = '='; i = i + j + 1; for(j = 0; j < DATA_MSG_LEN; j++) { db_buf[i + j] = (uint8_t)(data_len >> (j << 3)); } i = i + j; for(j = 0; j < data_len; j++) { db_buf[i + j] = data[j]; } db_buf[i + j] = '\n'; db_index = i + j + 1; return; }
void db_read(const char* const name, uint8_t* data) { if(false == db_init_done) return; uint32_t i, j, data_len; i = db_find(name); if(i == 0) { data[0] = 0; return; } data_len = get_len_by_pos(i); for(j = 0; j < data_len; j++) { data[j] = db_buf[i + j]; } data[j] = 0; }
void db_delete(const char* const name) { uint32_t i, pos, start, end, len; uint32_t data_len = 0; pos = db_find(name); if(0 == pos) return; data_len = get_len_by_pos(pos); for(start = pos; '\n' != db_buf[start]; start--); end = pos + data_len; len = end - start; for(i = end; i < db_index; i++) { db_buf[i - len] = db_buf[i]; } for(i = i - len; i < db_index; i++) { db_buf[i] = 0xff; } db_index -= len; }
int db_delete(db_handle_t *db, uint64_t cardid) { int returnVal = 0; if ( NULL == db) { // This should not happen. So warn and return error DBG_PRINT(DBG_WARN, ("Db handle is NULL")); returnVal = HANDLE_NULL_ERROR; goto DEL_ERROR; } // if ( NULL == db ) #ifdef SQL_LITE char sql[BUF_SIZE]; char *errMsg; int counter = 0; memset(sql, 0, BUF_SIZE); #ifdef ALLOW_DUPLICATES sprintf(sql, "SELECT * FROM %s WHERE id = %llu LIMIT 1", TABLE_NAME, cardid); #else sprintf(sql, "SELECT * FROM %s WHERE id = %llu", TABLE_NAME, cardid); #endif returnVal = sqlite3_exec(db->handle, sql, callback, (void *)&counter, &errMsg); if ( returnVal ) { DBG_PRINT(DBG_WARN, ("Error While Searching id = %lld. %s", cardid, errMsg)); sqlite3_free(errMsg); returnVal = FIND_ERROR; goto DEL_ERROR; } // if ( returnVal ) if ( counter == 0 ) { DBG_PRINT(DBG_WARN, ("Card id = %llu Not Found in Our Database", cardid)); returnVal = ELE_NOT_FOUND_ERROR; goto DEL_ERROR; } else { memset(sql, 0, BUF_SIZE); sprintf(sql, "DELETE FROM %s WHERE id = %llu LIMIT 1", TABLE_NAME, cardid); returnVal = sqlite3_exec(db->handle, sql, NULL, 0, &errMsg); if ( returnVal ) { DBG_PRINT(DBG_WARN, ("Error While Deleting id = %llu. %s", cardid, errMsg)); sqlite3_free(errMsg); returnVal = DELETE_ERROR; goto DEL_ERROR; } // if ( returnVal ) } // if ( counter == 0) #else returnVal = db_find(db, cardid); if ( returnVal != 0 ) { DBG_PRINT(DBG_WARN, ("Card id %llu Do Not Exists in Our Database")); return ELE_NOT_FOUND_ERROR; } uint64_t id = 0ULL; uint64_t prevId = 0ULL; long left = -1L; long right = -1L; long curPos = -1L; int totlen = sizeof(id) + sizeof(left) + sizeof(right); fseek(db->handle, (long)-totlen, SEEK_CUR); // Move to the begining of record. returnVal = fread(&id, sizeof(id), 1, db->handle); returnVal = fread(&left, sizeof(left), 1, db->handle); returnVal = fread(&right, sizeof(right), 1, db->handle); if ( -1L == left && -1L == right) { // Node is leaf. curPos = ftell(db->handle) - ((long) (sizeof(uint64_t) + 2 * sizeof(long))); if ( 0L == curPos ) { // only one node in the tree. Truncate the file. fclose(db->handle); db->handle = fopen(db->fileName, "wb"); // reopen in write mode. It will truncate it. returnVal = 0; goto DEL_ERROR; } // Delete the link fseek(db->handle, prevPos, SEEK_SET); fread(&prevId, sizeof(prevId), 1, db->handle); if ( prevId < id ) { fseek(db->handle, prevPos + sizeof(left) + sizeof(prevId), SEEK_SET); returnVal = fwrite(&right, sizeof(right), 1, db->handle); } else { returnVal = fwrite(&left, sizeof(left), 1, db->handle); } if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); perror(NULL); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); // Delete the node. fseek(db->handle, curPos, SEEK_SET); id = -1ULL; returnVal = fwrite(&id, sizeof(id), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); return 0; } if ( -1L == left || -1L == right ) { //Node with one child if ( -1L == left ) { curPos = ftell(db->handle) - ((long) (sizeof(uint64_t) + 2 * sizeof(long))); fseek(db->handle, right, SEEK_SET); fread(&id, sizeof(id), 1, db->handle); fread(&left, sizeof(left), 1, db->handle); fread(&right, sizeof(right), 1, db->handle); fseek(db->handle, curPos, SEEK_SET); returnVal = fwrite(&id, sizeof(id), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&left, sizeof(left), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&right, sizeof(right), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); return 0; } // if ( -1L == left ) if ( -1L == right ) { curPos = ftell(db->handle) - ((long) (sizeof(uint64_t) + 2 * sizeof(long))); fseek(db->handle, left, SEEK_SET); fread(&id, sizeof(id), 1, db->handle); fread(&left, sizeof(left), 1, db->handle); fread(&right, sizeof(right), 1, db->handle); fseek(db->handle, curPos, SEEK_SET); returnVal = fwrite(&id, sizeof(id), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&left, sizeof(left), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&right, sizeof(right), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); return 0; } // if ( -1L == left ) } // if ( -1L == left || -1L == right ) // go to the left child and keep on going right untill you see an // node with no right child. curPos = ftell(db->handle) - ((long) (sizeof(uint64_t) + 2 * sizeof(long))); long parentPos = curPos; // go to left. fseek(db->handle, left, SEEK_SET); long temp = -1L; while ( 1 ) { // Keep on going right. fread(&id, sizeof(id), 1, db->handle); fread(&left, sizeof(left), 1, db->handle); fread(&right, sizeof(right), 1, db->handle); temp = ftell(db->handle) - ((long) sizeof(uint64_t) + 2 * sizeof(long)); if ( -1L == right ) break; parentPos = temp; fseek(db->handle, right, SEEK_SET); } long deletedPos = ftell(db->handle) - ((long) sizeof(uint64_t) + 2 * sizeof(long)); uint64_t replacementValue = id; // delete the node. if ( left == -1L && right == -1L ) { fseek(db->handle, deletedPos, SEEK_SET); fread(&id, sizeof(id), 1, db->handle); fseek(db->handle, parentPos, SEEK_SET); uint64_t parentId = -1ULL; fread(&parentId, sizeof(parentId), 1, db->handle); if ( parentId > id ) { fwrite(&left, sizeof(left), 1, db->handle); } else { fseek(db->handle, sizeof(left), SEEK_CUR); fwrite(&right, sizeof(right), 1, db->handle); } fseek(db->handle, deletedPos, SEEK_SET); id = -1ULL; returnVal = fwrite( &id, sizeof(id), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); fseek(db->handle, (long)(temp + sizeof(id) + sizeof(left)), SEEK_SET); right = -1L; returnVal = fwrite(&right, sizeof(right), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); // Replace the node. fseek(db->handle, curPos, SEEK_SET); returnVal = fwrite(&replacementValue, sizeof(replacementValue), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); return 0; } else { fseek(db->handle, curPos, SEEK_SET); returnVal = fwrite(&replacementValue, sizeof(replacementValue), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); fseek(db->handle, left, SEEK_SET); fread(&id, sizeof(id), 1, db->handle); fread(&left, sizeof(left), 1, db->handle); fread(&right, sizeof(right), 1, db->handle); fseek(db->handle, deletedPos, SEEK_SET); returnVal = fwrite(&id, sizeof(id), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&left, sizeof(left), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } returnVal = fwrite(&right, sizeof(right), 1, db->handle); if ( 1 != returnVal ) { DBG_PRINT_ERR(("Something bad Happened\n Database Corrupted. Sorry !!")); returnVal = DB_CORRUPT_ERROR; goto DEL_ERROR; } fflush(db->handle); return 0; } #endif // SQL_LITE DEL_ERROR: return returnVal; }