Skip to content

Commit bf0831e

Browse files
author
Edward Thomson
authored
Merge pull request #19 from jacquesg/mysql-rot
Repair MySQL backend bitrot
2 parents 45dd8f9 + adde07d commit bf0831e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

mysql/mysql.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <assert.h>
2727
#include <string.h>
2828
#include <git2.h>
29-
#include <git2/odb_backend.h>
29+
#include <git2/sys/odb_backend.h>
3030

3131
/* MySQL C Api docs:
3232
* http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html
@@ -82,13 +82,13 @@ int mysql_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend
8282
if (mysql_stmt_num_rows(backend->st_read_header) == 1) {
8383
result_buffers[0].buffer_type = MYSQL_TYPE_TINY;
8484
result_buffers[0].buffer = type_p;
85-
result_buffers[0].buffer_length = sizeof(type_p);
86-
memset(type_p, 0, sizeof(type_p));
85+
result_buffers[0].buffer_length = sizeof(*type_p);
86+
memset(type_p, 0, sizeof(*type_p));
8787

8888
result_buffers[1].buffer_type = MYSQL_TYPE_LONGLONG;
8989
result_buffers[1].buffer = len_p;
90-
result_buffers[1].buffer_length = sizeof(len_p);
91-
memset(len_p, 0, sizeof(len_p));
90+
result_buffers[1].buffer_length = sizeof(*len_p);
91+
memset(len_p, 0, sizeof(*len_p));
9292

9393
if(mysql_stmt_bind_result(backend->st_read_header, result_buffers) != 0)
9494
return GIT_ERROR;
@@ -97,7 +97,7 @@ int mysql_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend
9797
if(mysql_stmt_fetch(backend->st_read_header) != 0)
9898
return GIT_ERROR;
9999

100-
error = GIT_SUCCESS;
100+
error = GIT_OK;
101101
} else {
102102
error = GIT_ENOTFOUND;
103103
}
@@ -145,13 +145,13 @@ int mysql_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb
145145
if (mysql_stmt_num_rows(backend->st_read) == 1) {
146146
result_buffers[0].buffer_type = MYSQL_TYPE_TINY;
147147
result_buffers[0].buffer = type_p;
148-
result_buffers[0].buffer_length = sizeof(type_p);
149-
memset(type_p, 0, sizeof(type_p));
148+
result_buffers[0].buffer_length = sizeof(*type_p);
149+
memset(type_p, 0, sizeof(*type_p));
150150

151151
result_buffers[1].buffer_type = MYSQL_TYPE_LONGLONG;
152152
result_buffers[1].buffer = len_p;
153-
result_buffers[1].buffer_length = sizeof(len_p);
154-
memset(len_p, 0, sizeof(len_p));
153+
result_buffers[1].buffer_length = sizeof(*len_p);
154+
memset(len_p, 0, sizeof(*len_p));
155155

156156
// by setting buffer and buffer_length to 0, this tells libmysql
157157
// we want it to set data_len to the *actual* length of that field
@@ -181,7 +181,7 @@ int mysql_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb
181181
return GIT_ERROR;
182182
}
183183

184-
error = GIT_SUCCESS;
184+
error = GIT_OK;
185185
} else {
186186
error = GIT_ENOTFOUND;
187187
}
@@ -235,7 +235,7 @@ int mysql_backend__exists(git_odb_backend *_backend, const git_oid *oid)
235235
return found;
236236
}
237237

238-
int mysql_backend__write(git_oid *oid, git_odb_backend *_backend, const void *data, size_t len, git_otype type)
238+
int mysql_backend__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_otype type)
239239
{
240240
int error;
241241
mysql_backend *backend;
@@ -246,9 +246,6 @@ int mysql_backend__write(git_oid *oid, git_odb_backend *_backend, const void *da
246246

247247
backend = (mysql_backend *)_backend;
248248

249-
if ((error = git_odb_hash(oid, data, len, type)) < 0)
250-
return error;
251-
252249
memset(bind_buffers, 0, sizeof(bind_buffers));
253250

254251
// bind the oid
@@ -292,7 +289,7 @@ int mysql_backend__write(git_oid *oid, git_odb_backend *_backend, const void *da
292289
if (mysql_stmt_reset(backend->st_read_header) != 0)
293290
return GIT_ERROR;
294291

295-
return GIT_SUCCESS;
292+
return GIT_OK;
296293
}
297294

298295
void mysql_backend__free(git_odb_backend *_backend)
@@ -329,7 +326,7 @@ static int create_table(MYSQL *db)
329326
if (mysql_real_query(db, sql_create, strlen(sql_create)) != 0)
330327
return GIT_ERROR;
331328

332-
return GIT_SUCCESS;
329+
return GIT_OK;
333330
}
334331

335332
static int init_db(MYSQL *db)
@@ -354,7 +351,7 @@ static int init_db(MYSQL *db)
354351
error = create_table(db);
355352
} else if (num_rows > 0) {
356353
/* the table was found */
357-
error = GIT_SUCCESS;
354+
error = GIT_OK;
358355
} else {
359356
error = GIT_ERROR;
360357
}
@@ -410,7 +407,7 @@ static int init_statements(mysql_backend *backend)
410407
return GIT_ERROR;
411408

412409

413-
return GIT_SUCCESS;
410+
return GIT_OK;
414411
}
415412

416413
int git_odb_backend_mysql(git_odb_backend **backend_out, const char *mysql_host,
@@ -422,8 +419,10 @@ int git_odb_backend_mysql(git_odb_backend **backend_out, const char *mysql_host,
422419
my_bool reconnect;
423420

424421
backend = calloc(1, sizeof(mysql_backend));
425-
if (backend == NULL)
426-
return GIT_ENOMEM;
422+
if (backend == NULL) {
423+
giterr_set_oom();
424+
return GIT_ERROR;
425+
}
427426

428427
backend->db = mysql_init(backend->db);
429428

@@ -445,14 +444,15 @@ int git_odb_backend_mysql(git_odb_backend **backend_out, const char *mysql_host,
445444
if (error < 0)
446445
goto cleanup;
447446

447+
backend->parent.version = GIT_ODB_BACKEND_VERSION;
448448
backend->parent.read = &mysql_backend__read;
449449
backend->parent.read_header = &mysql_backend__read_header;
450450
backend->parent.write = &mysql_backend__write;
451451
backend->parent.exists = &mysql_backend__exists;
452452
backend->parent.free = &mysql_backend__free;
453453

454454
*backend_out = (git_odb_backend *)backend;
455-
return GIT_SUCCESS;
455+
return GIT_OK;
456456

457457
cleanup:
458458
mysql_backend__free((git_odb_backend *)backend);

0 commit comments

Comments
 (0)