Changeset 54
- Timestamp:
- 11/12/07 18:42:45 (10 months ago)
- Files:
-
- vowfsc/Makefile (modified) (1 diff)
- vowfsc/db-util.h (modified) (1 diff)
- vowfsc/db.c (modified) (36 diffs)
- vowfsc/db.h (modified) (9 diffs)
- vowfsc/test.c (modified) (1 diff)
- vowfsc/xdelta.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
vowfsc/Makefile
r53 r54 3 3 4 4 db_test: test.o db.o xdelta3.o 5 gcc test.o xdelta3.o db.o -lpq -o test5 gcc test.o xdelta3.o db.o -lpq -o db_test 6 6 7 7 xdelta_test: xdelta.o xdelta3.o 8 gcc xdelta.o xdelta3.o -o xdelta_test 8 9 9 10 xdelta.o: xdelta.c vowfsc/db-util.h
r52 r54 2 2 3 3 #ifdef DEBUG 4 #define db_debug_rq( function ) printf(" %s: %s\n", #function, pq_request)4 #define db_debug_rq( function ) printf("\n%s: %s\n", #function, pq_request) 5 5 #else 6 6 #define db_debug_rq( function ) vowfsc/db.c
r52 r54 199 199 int revision_id=atoi(PQgetvalue(res,0,0)); 200 200 201 PQ freemem(res);201 PQclear(res); 202 202 203 203 return revision_id; … … 229 229 free(dir_name); 230 230 free(file_name); 231 PQ freemem(res);231 PQclear(res); 232 232 233 233 return file_id; … … 255 255 db_parse_file_res(res, stat); 256 256 257 PQ freemem(res);257 PQclear(res); 258 258 259 259 return stat; … … 280 280 db_parse_file_res(res, stat); 281 281 282 PQ freemem(res);282 PQclear(res); 283 283 284 284 return stat; … … 287 287 char *db_get_file_data( int file_id ){ 288 288 char pq_request[BUFFER_SIZE]; 289 sprintf(pq_request,"SELECT d iff"289 sprintf(pq_request,"SELECT data " 290 290 "FROM file " 291 291 "WHERE file_id = \'%i\' " 292 292 "AND revision_id = (SELECT MAX(revision_id) FROM file WHERE file_id = \'%i\')", 293 293 file_id, file_id); 294 db_debug_rq(db_get_file_data); 294 295 PGresult *res=PQexec(pq_conn,pq_request); 295 296 … … 299 300 char *data=malloc(data_len); 300 301 memcpy(data,PQgetvalue(res,0,0),data_len); 301 302 PQfreemem(res); 303 304 return data; 302 int udata_len; 303 char *udata=PQunescapeBytea(data,&udata_len); 304 305 free(data); 306 PQclear(res); 307 308 return udata; 305 309 } 306 310 … … 309 313 int i = db_get_file_max_revision(file_id); 310 314 char *data, *diff, *temp; 311 312 char pq_request[BUFFER_SIZE]; 313 sprintf(pq_request,"SELECT d iff"315 316 char pq_request[BUFFER_SIZE]; 317 sprintf(pq_request,"SELECT data " 314 318 "FROM file " 315 319 "WHERE file_id = \'%i\' " … … 323 327 data=malloc(data_len); 324 328 memcpy(data,PQgetvalue(res,0,0),data_len); 325 326 PQfreemem(res); 329 int udata_len; 330 char *udata=PQunescapeBytea(data,&udata_len); 331 free(data); 332 333 PQclear(res); 327 334 328 335 while( --i >= revision_id ){ 329 336 char pq_request[BUFFER_SIZE]; 330 sprintf(pq_request,"SELECT d iff"337 sprintf(pq_request,"SELECT data " 331 338 "FROM file " 332 339 "WHERE file_id = \'%i\' " 333 340 "AND revision_id = \'%i\'", 334 341 file_id,i); 342 db_debug_rq(db_get_file_rev_udata); 335 343 PGresult *res=PQexec(pq_conn,pq_request); 336 344 … … 338 346 339 347 diff_len=PQgetlength(res,0,0); 340 diff=malloc(data_len); 341 348 diff=malloc(diff_len); 349 memcpy(diff,PQgetvalue(res,0,0),diff_len); 350 351 int udiff_len; 352 char *udiff = PQunescapeBytea(diff,&udiff_len); 353 342 354 temp=malloc(XD3_ALLOCSIZE); 343 355 344 xd3_decode_memory( diff,diff_len,345 data,data_len,356 xd3_decode_memory(udiff, udiff_len, 357 udata, udata_len, 346 358 temp, &temp_len, XD3_ALLOCSIZE, 347 359 0); 348 360 349 361 free(diff); 350 free(data); 351 data = temp; 352 data_len = temp_len; 362 PQfreemem(udata); 363 PQfreemem(udiff); 364 PQclear(res); 365 udata = temp; 366 udata_len = temp_len; 353 367 } 354 368 355 return data;369 return udata; 356 370 } 357 371 … … 378 392 // Clean up and return 379 393 free(dir_name); 380 PQ freemem(res);394 PQclear(res); 381 395 382 396 return dir_id; … … 397 411 db_parse_dir_res(res,stat); 398 412 399 PQ freemem(res);413 PQclear(res); 400 414 401 415 return stat; … … 416 430 db_parse_dir_res( res, stat ); 417 431 418 PQ freemem(res);432 PQclear(res); 419 433 420 434 return stat; … … 435 449 int revision_id=atoi(PQgetvalue(res,0,0)); 436 450 437 PQ freemem(res);451 PQclear(res); 438 452 439 453 return revision_id; … … 498 512 #include "stream.db" 499 513 500 PQ freemem(res);514 PQclear(res); 501 515 502 516 return stat; … … 510 524 int stream_id=atoi(PQgetvalue(res,0,0)); 511 525 512 PQ freemem(res);526 PQclear(res); 513 527 514 528 return stream_id; … … 530 544 stream_id = atoi(PQgetvalue(res,0,0)); 531 545 }else{ 532 PQ freemem(res);546 PQclear(res); 533 547 stream_id = db_get_stream_max_id() + 1; 534 548 535 sprintf(pq_request, "INSERT stream "549 sprintf(pq_request, "INSERT INTO stream " 536 550 "(stream_id, stream_name, stream) " 537 551 "VALUES" 538 552 "\'%i\', \'%s\', \'%s\'", 539 553 stream_id, stream_name, stream); 540 PQ freemem(PQexec(pq_conn,pq_request));554 PQclear(PQexec(pq_conn,pq_request)); 541 555 } 542 556 return stream_id; … … 551 565 int file_id=atoi(PQgetvalue(res,0,0)); 552 566 553 PQ freemem(res);567 PQclear(res); 554 568 555 569 return file_id; … … 563 577 int file_uid=atoi(PQgetvalue(res,0,0)); 564 578 565 PQ freemem(res);579 PQclear(res); 566 580 567 581 return file_uid; … … 585 599 db_split_file_name(file_path, &dir_name, &file_name); 586 600 601 int now = time(NULL); 602 587 603 // Make the PGSQL request 588 604 char pq_request[BUFFER_SIZE]; 589 605 sprintf(pq_request, "INSERT INTO file " 590 "(file_uid, file_id, revision_id, directory_path, file_name, stream_id ) "606 "(file_uid, file_id, revision_id, directory_path, file_name, stream_id, end_time) " 591 607 "VALUES " 592 "(\'%i\', \'%i\', \'%i\', \'%s\', \'%s\', \'%i\' )",593 file_uid, file_id, revision_id, dir_name, file_name, stream_id );608 "(\'%i\', \'%i\', \'%i\', \'%s\', \'%s\', \'%i\', \'%i\')", 609 file_uid, file_id, revision_id, dir_name, file_name, stream_id, now); 594 610 db_debug_rq(db_create_file); 595 611 PGresult *res = PQexec(pq_conn, pq_request); … … 601 617 free(dir_name); 602 618 free(file_name); 603 PQ freemem(res);619 PQclear(res); 604 620 605 621 return file_id; … … 624 640 db_parse_dir_res(res, &listing[i]); 625 641 626 PQ freemem(res);642 PQclear(res); 627 643 628 644 return listing; … … 647 663 db_parse_file_res(res, &listing[i]); 648 664 649 PQ freemem(res);665 PQclear(res); 650 666 651 667 return listing; … … 655 671 char pq_request[BUFFER_SIZE]; 656 672 sprintf(pq_request, "SELECT file_uid " 673 "FROM file " 657 674 "WHERE file_id = \'%i\' AND revision_id = \'%i\'", 658 675 file_id, revision_id); 676 db_debug_rq(db_get_file_id); 659 677 PGresult *res = PQexec(pq_conn, pq_request); 660 678 … … 662 680 663 681 int file_uid = atoi(PQgetvalue(res, 0,0)); 664 665 PQfreemem(res); 666 667 return file_id; 682 PQclear(res); 683 684 return file_uid; 668 685 } 669 686 … … 682 699 char *diff=malloc(XD3_ALLOCSIZE); 683 700 int diff_len; 684 701 printf("DATA!: %s(%i)(%i)\n",data,data_len,strlen(data)); 685 702 xd3_encode_memory(data, data_len, 686 703 data, data_len, 687 704 diff, &diff_len, XD3_ALLOCSIZE, 688 705 0); 689 690 char pq_request[BUFFER_SIZE]; 691 sprintf(pq_request, "INSERT file " 706 707 int now = time(NULL); 708 709 int udata_len; 710 char *udata = PQescapeByteaConn(pq_conn,data,data_len,&udata_len); 711 712 char pq_request[BUFFER_SIZE]; 713 sprintf(pq_request, "INSERT INTO file " 692 714 "(file_uid, file_id, revision_id, file_length, " 693 715 "parent_file_id, stream_id, end_time, deleted_flag, " 694 716 "file_name, directory_path, directory_id, data) " 695 717 "VALUES " 696 " \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'NOW\', "697 "\' TRUE\', \'%s\', \'%s\', \'%s\'",718 "(\'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', " 719 "\'t\', \'%s\', \'%s\', \'%i\', \'%s\')", 698 720 file_uid, file_id, file.revision_id, file.file_length, 699 parent_file_id, file.stream_id, file.file_name, 700 file.directory_path, file.directory_id, data); 701 PQfreemem(PQexec(pq_conn, pq_request)); 702 721 parent_file_id, file.stream_id, now, file.file_name, 722 file.directory_path, file.directory_id, udata); 723 db_debug_rq(db_remove_file); 724 PGresult *res = PQexec(pq_conn, pq_request); 725 PQclear(res); 726 727 PQfreemem(udata); 728 udata = PQescapeByteaConn(pq_conn,data,data_len,&udata_len); 729 703 730 // Insert the diff into the old file 704 731 sprintf(pq_request, "UPDATE file " 705 732 "SET data = \'%s\' " 706 733 "WHERE file_uid = \'%i\'", 707 diff, parent_file_id); 708 PQfreemem(PQexec(pq_conn, pq_request)); 734 udata, parent_file_id); 735 db_debug_rq(db_remove_file); 736 res = PQexec(pq_conn, pq_request); 737 PQclear(res); 738 PQfreemem(udata); 709 739 710 740 return file.revision_id; … … 737 767 // Insert the new file 738 768 char pq_request[BUFFER_SIZE]; 739 sprintf(pq_request, "INSERT file "769 sprintf(pq_request, "INSERT INTO file " 740 770 "(file_uid, file_id, revision_id, file_length, " 741 771 "parent_file_id, stream_id, end_time, deleted_flag, " … … 747 777 file.stream_id, new_file_name, new_dir_name, 748 778 new_dir_id, data); 749 PQ freemem(PQexec(pq_conn, pq_request));779 PQclear(PQexec(pq_conn, pq_request)); 750 780 751 781 return file.revision_id; … … 782 812 // Insert the new file 783 813 char pq_request[BUFFER_SIZE]; 784 sprintf(pq_request, "INSERT file "814 sprintf(pq_request, "INSERT INTO file " 785 815 "(file_uid, file_id, revision_id, file_length, " 786 816 "parent_file_id, stream_id, end_time, deleted_flag, " … … 792 822 parent_file_id, new_stream_id, file.file_name, 793 823 file.directory_path, file.directory_id, data); 794 PQ freemem(PQexec(pq_conn, pq_request));824 PQclear(PQexec(pq_conn, pq_request)); 795 825 796 826 // Insert the diff into the old file … … 799 829 "WHERE file_uid = \'%i\'", 800 830 diff, parent_file_id); 801 PQ freemem(PQexec(pq_conn, pq_request));831 PQclear(PQexec(pq_conn, pq_request)); 802 832 803 833 return file.revision_id; … … 808 838 file_t file = *db_get_file( file_id ); 809 839 810 int file_uid = db_get_file_max_uid() + 1;840 int file_uid = db_get_file_max_uid() + 1; 811 841 812 842 int parent_file_id = db_get_file_uid( file_id, file.revision_id ); 813 843 file.revision_id++; 814 844 815 char old_data=db_get_file_data( file_id ); 845 int now = time(NULL); 846 847 int escaped_len; 848 char *escaped=PQescapeByteaConn(pq_conn,data,data_len,&escaped_len); 849 816 850 int old_data_len = file.file_length; 851 char *old_data=db_get_file_data( file_id ); 852 853 // Insert the new file 854 char pq_request[BUFFER_SIZE]; 855 sprintf(pq_request, "INSERT INTO file " 856 "(file_uid, file_id, revision_id, file_length, " 857 "parent_file_id, stream_id, end_time, deleted_flag, " 858 "file_name, directory_path, directory_id, data) " 859 "VALUES " 860 "(\'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', " 861 "\'0\', \'%s\', \'%s\', \'%i\', \'%s\')", 862 file_uid, file_id, file.revision_id, data_len, 863 parent_file_id, file.stream_id, now, file.file_name, 864 file.directory_path, file.directory_id, escaped); 865 db_debug_rq(db_modify_file_data); 866 PGresult *res = PQexec(pq_conn, pq_request); 867 868 if( PQresultStatus(res) != PGRES_COMMAND_OK ) return NULL; 869 870 PQclear(res); 871 817 872 818 873 // Diff the files 874 819 875 char *diff=malloc(XD3_ALLOCSIZE); 820 876 int diff_len; … … 825 881 0); 826 882 827 char pq_request[BUFFER_SIZE]; 828 829 // Insert the new file 830 sprintf(pq_request, "INSERT file " 831 "(file_uid, file_id, revision_id, file_length, " 832 "parent_file_id, stream_id, end_time, deleted_flag, " 833 "file_name, directory_path, directory_id, data) " 834 "VALUES " 835 "\'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'%i\', \'NOW\', " 836 "\'TRUE\', \'%s\', \'%s\', \'%s\'", 837 file_uid, file_id, file.revision_id, data_len, 838 parent_file_id, file.stream_id, file.file_name, 839 file.directory_path, file.directory_id, data); 840 PQfreemem(PQexec(pq_conn, pq_request)); 841 883 escaped=PQescapeBytea(diff,diff_len,&escaped_len); 884 885 842 886 // Insert the diff into the old file 843 887 sprintf(pq_request, "UPDATE file " 844 888 "SET data = \'%s\' " 845 889 "WHERE file_uid = \'%i\'", 846 diff, parent_file_id); 847 PQfreemem(PQexec(pq_conn, pq_request)); 890 escaped, parent_file_id); 891 db_debug_rq(db_modify_file_data); 892 res = PQexec(pq_conn, pq_request); 893 894 if( PQresultStatus(res) != PGRES_COMMAND_OK ) return NULL; 895 896 PQclear(res); 848 897 849 898 return file.revision_id; … … 866 915 int stream_id = PQgetvalue(res, 0,0); 867 916 868 PQ freemem(res);917 PQclear(res); 869 918 870 919 sprintf(pq_request,"SELECT * " … … 899 948 #include "stream.db" 900 949 901 PQ freemem(res);950 PQclear(res); 902 951 903 952 return stat; … … 919 968 int stream_id = atoi(PQgetvalue(res, 0,0)); 920 969 921 PQ freemem(res);970 PQclear(res); 922 971 923 972 sprintf(pq_request,"SELECT * " … … 952 1001 #include "stream.db" 953 1002 954 PQ freemem(res);1003 PQclear(res); 955 1004 956 1005 return stat; vowfsc/db.h
r51 r54 41 41 * file_id as an integer. 42 42 */ 43 int db_get_file_id( char *file_path ); 43 int db_get_file_id( char *file_path ); // TESTED 44 44 45 45 … … 48 48 * file. 49 49 */ 50 int db_get_file_max_revision( int file_id ); 50 int db_get_file_max_revision( int file_id ); // TESTED 51 51 52 52 … … 55 55 * look at the file.db file. 56 56 */ 57 file_t *db_get_file( int file_id ); 57 file_t *db_get_file( int file_id ); // TESTED 58 58 file_t *db_get_file_rev( int file_id, int revision_id ); 59 59 … … 62 62 * returns the data from the most current file. 63 63 */ 64 char *db_get_file_data( int file_id ); 65 char *db_get_file_rev_data( int file_id, int revision_id ); 64 char *db_get_file_data( int file_id ); // TESTED 65 char *db_get_file_rev_data( int file_id, int revision_id ); // TESTED 66 66 67 67 … … 83 83 * new file. 84 84 */ 85 int db_remove_file(int file_id); 85 int db_remove_file(int file_id); // TESTED 86 #define db_delete_file(file_id) db_remove_file(file_id) 86 87 87 88 … … 90 91 * revisions it. 91 92 */ 92 int db_create_file(char *file_path, int stream_id ); 93 int db_create_file(char *file_path, int stream_id ); // TESTED 93 94 94 95 … … 109 110 * file_id 110 111 */ 111 int db_modify_file_data(int file_id, char *data, int data_len ); 112 int db_modify_file_data(int file_id, char *data, int data_len ); // TESTED 112 113 113 114 … … 149 150 file_t *db_get_dir_files( int dir_id, int *length ); 150 151 152 /* Does what it says, creates a new directory. 153 * Returns the dir_id of the new directory or null in 154 * the case of a failture. 155 */ 156 int db_create_dir( char *dir_path ); // TODO 157 151 158 /* db_init and db_destoy create and destroy connections 152 159 * to the database respectivly. They are wrappers around … … 154 161 * non-zero on failture. 155 162 */ 156 int db_init(char *host, int port, char *dbname, char *username, char *password); 157 int db_destroy(); 163 int db_init(char *host, int port, char *dbname, char *username, char *password); // TESTED 164 int db_destroy(); // TESTED 158 165 166 /* Creates a new blank file system 167 */ 168 int db_format_fs(); // TODO vowfsc/test.c
r52 r54 29 29 printf("The revision is: %i\n", file->revision_id ); 30 30 31 char *string1=strdup("Most folks are about as happy as they make up their minds to be."); 32 char *string2=strdup("The secret of happiness is to make others believe they are the cause of it."); 33 34 int string1_len=strlen(string1)+1; //include '\0' 35 int string2_len=strlen(string2)+1; 36 37 db_modify_file_data(file_id, string1, string1_len); 38 db_modify_file_data(file_id, string2, string2_len); 39 40 41 char *data=db_get_file_data(file_id); 42 printf("data: %s\n",data); 43 44 char *old_data=db_get_file_rev_data(file_id, file->revision_id +1); 45 printf("old_data: %s\n",old_data); 46 47 db_remove_file(file_id); 48 data=db_get_file_data(file_id); 49 printf("data: %s\n",data); 50 51 52 free(string1); 53 free(data); 54 free(old_data); 55 free(string2); 31 56 free(file); 32 57 free(dir); vowfsc/xdelta.c
r41 r54 17 17 int diff_len; 18 18 19 xd3_encode_memory(string 1, string1_len,19 xd3_encode_memory(string2, string2_len, 20 20 string1, string1_len, 21 21 diff, &diff_len, XD3_ALLOCSIZE, … … 25 25 int output_len; 26 26 27 printf("Diff( String 1-> String1 ): %s(%i)\n",diff,diff_len);27 printf("Diff( String2 -> String1 ): %s(%i)\n",diff,diff_len); 28 28 29 29 xd3_decode_memory(diff, diff_len, … … 32 32 0); 33 33 34 printf("Output( Diff -> String2): %s(%i)\n",output,output_len); 34 printf("Output( Diff -> String2 ): %s(%i)\n",output,output_len); 35 36 xd3_decode_memory(diff, diff_len, 37 string1, string1_len, 38 output, &output_len, XD3_ALLOCSIZE, 39 0); 40 41 printf("Output( Diff -> String1 ): %s(%i)\n",output,output_len); 42 35 43 36 44
