Changeset 39
- Timestamp:
- 11/09/07 20:06:06 (1 year ago)
- Files:
-
- vowfsc/db.c (modified) (26 diffs)
- vowfsc/db.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
vowfsc/db.c
r38 r39 130 130 131 131 // Make the request 132 char *pq_request[BUFFER_SIZE];132 char pq_request[BUFFER_SIZE]; 133 133 sprintf(pq_request,"SELECT file_id" 134 134 "FROM file WHERE file_name = \'%s\' " … … 142 142 int file_id=*(int*)PQgetvalue(res,0,0); 143 143 144 // Free the result145 144 PQfreemem(res); 146 145 … … 152 151 file_t stat; 153 152 154 char *pq_request[BUFFER_SIZE];153 char pq_request[BUFFER_SIZE]; 155 154 sprintf(pq_request,"SELECT * " 156 155 "FROM file " … … 182 181 #include "file.db" 183 182 184 // Free the result185 183 PQfreemem(res); 186 184 … … 192 190 file_t stat; 193 191 194 char *pq_request[BUFFER_SIZE];192 char pq_request[BUFFER_SIZE]; 195 193 sprintf(pq_request,"SELECT * " 196 194 "FROM file " … … 223 221 #include "file.db" 224 222 225 // Free the result226 223 PQfreemem(res); 227 224 … … 229 226 } 230 227 231 int db_get_ file_max_revision( int file_id ){228 int db_get_max_file_revision( int file_id ){ 232 229 // Make the request 233 char *pq_request[BUFFER_SIZE];230 char pq_request[BUFFER_SIZE]; 234 231 sprintf(pq_request,"SELECT MAX(revision_id) " 235 232 "FROM file " … … 243 240 int revision=*(int*)PQgetvalue(res,0,0); 244 241 245 // Free the result246 242 PQfreemem(res); 247 243 … … 251 247 252 248 char *db_get_file_data( int file_id ){ 253 char *pq_request[BUFFER_SIZE];249 char pq_request[BUFFER_SIZE]; 254 250 sprintf(pq_request,"SELECT diff" 255 251 "FROM file" … … 265 261 memcpy(data,PQgetvalue(res,0,0),data_len); 266 262 267 // Free the result268 263 PQfreemem(res); 269 264 … … 273 268 274 269 char *get_file_data( int file_id, int revision ){ 275 int i = get_ file_max_revision(file_id);270 int i = get_max_file_revision(file_id); 276 271 char *data, *diff, *temp; 277 272 278 char *pq_request[BUFFER_SIZE];273 char pq_request[BUFFER_SIZE]; 279 274 sprintf(pq_request,"SELECT diff " 280 275 "FROM file " … … 286 281 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 287 282 288 int data_len=PQgetlength(res,0,0) ;289 char *data=malloc(data_len) ;283 int data_len=PQgetlength(res,0,0), diff_len, temp_len; 284 char *data=malloc(data_len), *temp, *diff; 290 285 memcpy(data,PQgetvalue(res,0,0),data_len); 291 286 292 // Free the result293 287 PQfreemem(res); 294 288 295 289 while( --i >= revision ){ 296 char *pq_request[BUFFER_SIZE];290 char pq_request[BUFFER_SIZE]; 297 291 sprintf(pq_request,"SELECT diff" 298 292 "FROM file" … … 304 298 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 305 299 306 int data_len=PQgetlength(res,0,0); 307 char *diff=malloc(data_len); 308 memcpy(data,PQgetvalue(res,0,0),data_len); 309 310 temp = xdelta_patch( data, patch ); 300 diff_len=PQgetlength(res,0,0); 301 diff=malloc(data_len); 302 303 temp=malloc(XD3_ALLOCSIZE); 304 305 xd3_decode_memory(diff, diff_len, 306 data, data_len, 307 temp, &temp_len, XD3_ALLOCSIZE, 308 0); 309 310 free(diff); 311 311 free(data); 312 312 data = temp; 313 data_len = temp_len; 313 314 } 314 315 … … 321 322 322 323 // Make the request 323 char *pq_request[BUFFER_SIZE];324 char pq_request[BUFFER_SIZE]; 324 325 sprintf(pq_request,"SELECT directory_id " 325 326 "FROM directory " … … 334 335 int dir_id=*(int*)PQgetvalue(res,0,0); 335 336 336 // Free the result337 337 PQfreemem(res); 338 338 … … 344 344 dir_t stat; 345 345 346 char *pq_request[BUFFER_SIZE];346 char pq_request[BUFFER_SIZE]; 347 347 sprintf(pq_request,"SELECT * " 348 348 "FROM directory " … … 374 374 #include "dir.db" 375 375 376 // Free the result377 376 PQfreemem(res); 378 377 … … 384 383 dir_t stat; 385 384 386 char *pq_request[BUFFER_SIZE];385 char pq_request[BUFFER_SIZE]; 387 386 sprintf(pq_request,"SELECT * " 388 387 "FROM directory " … … 415 414 #include "dir.db" 416 415 417 // Free the result418 416 PQfreemem(res); 419 417 … … 423 421 int db_get_dir_max_revision( int dir_id ){ 424 422 // Make the request 425 char *pq_request[BUFFER_SIZE];423 char pq_request[BUFFER_SIZE]; 426 424 sprintf(pq_request,"SELECT MAX(revision_id) " 427 425 "FROM directory " … … 435 433 int revision=*(int*)PQgetvalue(res,0,0); 436 434 437 // Free the result438 435 PQfreemem(res); 439 436 … … 443 440 444 441 int db_init(char *host, int port, char *dbname, char *username, char *password){ 445 char *pq_request[BUFFER_SIZE];442 char pq_request[BUFFER_SIZE]; 446 443 sprintf(pq_request,"host=%s port=%i dbname=%s user=%s password=%s", host, port, dbname, username, password); 447 444 pq_conn=PQconnectdb(pq_request); … … 462 459 stream_t stat; 463 460 464 char *pq_request[BUFFER_SIZE];461 char pq_request[BUFFER_SIZE]; 465 462 sprintf(pq_request,"SELECT * " 466 "FROM stream ectory"467 "WHERE stream ectory_id = \'%i\'",463 "FROM stream " 464 "WHERE stream_id = \'%i\'", 468 465 stream_id); 469 466 PGresult *res=PQexec(pq_conn,pq_request); … … 488 485 489 486 // Fill stat 490 #define db(type, name) stat.name = *(type*)PQgetvalue(pq_data, /*row*/, stream_format.name );491 #define db_string(name) stat.name = strdup(PQgetvalue(pq_data, /*row*/, stream_format.name ));487 #define db(type, name) stat.name = *(type*)PQgetvalue(pq_data, 0, stream_format.name ); 488 #define db_string(name) stat.name = strdup(PQgetvalue(pq_data, 0, stream_format.name )); 492 489 #include "stream.db" 493 490 494 // Free the result495 491 PQfreemem(res); 496 492 497 493 return stat; 498 494 } 499 500 495 501 496 stream_t db_get_stream( int stream_id, int revision ){ 502 497 stream_t stat; 503 498 504 char *pq_request[BUFFER_SIZE];499 char pq_request[BUFFER_SIZE]; 505 500 sprintf(pq_request,"SELECT * " 506 "FROM stream ectory"507 "WHERE stream ectory_id = \'%i\' "501 "FROM stream " 502 "WHERE stream_id = \'%i\' " 508 503 "AND revision_id = \'%i\'", 509 504 stream_id, revision); … … 533 528 #include "stream.db" 534 529 535 // Free the result536 530 PQfreemem(res); 537 531 … … 539 533 } 540 534 541 535 int db_get_max_stream_id(){ 536 PGresult *res=PQexec(pq_conn,"SELECT MAX(stream_id) FROM stream"); 537 538 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 539 540 int stream_id=*(int*)PQgetvalue(res,0,0); 541 542 PQfreemem(res); 543 544 return stream_id; 545 } 546 547 548 int db_create_stream( char *stream_name, char *stream ){ 549 int stream_id; 550 551 char pq_request[BUFFER_SIZE]; 552 sprintf(pq_request, "SELECT stream_id " 553 "FROM stream " 554 "WHERE stream_name = \'%s\' " 555 "AND stream = \'%s\'", 556 stream_name, stream); 557 PGresult *res = PQexec(pq_conn,pq_request); 558 559 if( PQresultStatus == PGRES_TUPLES_OK ){ 560 stream_id = *(int*)PQgetvalue(res,0,0); 561 PQfreemem(res); 562 563 sprintf(pq_request, "UPDATE stream " 564 "SET use_count = use_count + 1 " 565 "WHERE stream_id = \'%i\'", 566 stream_id); 567 PQfreemem(PQexec(pq_conn,pq_request)); 568 }else{ 569 PQfreemem(res); 570 stream_id = db_get_max_stream_id() + 1; 571 572 sprintf(pq_request, "INSERT stream " 573 "(stream_id, use_count, stream_name, stream) " 574 "VALUES" 575 "%i, 0, \'%s\', \'%s\'", 576 stream_id, stream_name, stream); 577 PQfreemem(PQexec(pq_conn,pq_request)); 578 } 579 return stream_id; 580 } 581 582 583 int db_get_max_file_id(){ 584 PGresult *res=PQexec(pq_conn,"SELECT MAX(file_id) FROM file"); 585 586 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 587 588 int file_id=*(int*)PQgetvalue(res,0,0); 589 590 PQfreemem(res); 591 592 return file_id; 593 } 594 595 int db_get_max_file_uid(){ 596 PGresult *res=PQexec(pq_conn,"SELECT MAX(file_uid) FROM file"); 597 598 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 599 600 int file_uid=*(int*)PQgetvalue(res,0,0); 601 602 PQfreemem(res); 603 604 return file_uid; 605 } 606 607 int db_create_file(char *file_name, char *file_path, int stream_id ){ 608 int file_uid = db_get_max_file_uid() + 1; 609 int file_id = db_get_max_file_id() + 1; 610 611 char *dir_name = normalize_dir_name( file_path ); 612 613 char pq_request[BUFFER_SIZE]; 614 sprintf(pq_request, "INSERT file " 615 "(file_uid, file_id, file_path, file_name, stream_id) " 616 "VALUES " 617 "%i, %i, \'%s\', \'%s\', %i", 618 file_uid, file_id, dir_name, file_name, stream_id); 619 PQexec(pq_conn, pq_request); 620 621 return file_id; 622 } 623 624 625 vowfsc/db.h
r38 r39 77 77 * a new stream and returns the new stream's id. 78 78 */ 79 int db_create_stream( char *stream_name, char *stream ); // TODO79 int db_create_stream( char *stream_name, char *stream ); 80 80 81 /* Sets the delete flag in the file 81 /* Copies the file to a new reversion, and sets the delete flag 82 * in the new file. It then returns the revision number of the 83 * new file. 82 84 */ 83 85 int db_remove_file(int file_id); // TODO … … 88 90 * revisions it. 89 91 */ 90 int db_create_file(char *file_name, char *file_path, stream_t stream); // TODO92 int db_create_file(char *file_name, char *file_path, int stream_id ); // TODO 91 93 92 94
