Changeset 40
- Timestamp:
- 11/10/07 13:28:27 (1 year ago)
- Files:
-
- vowfs (deleted)
- vowfsc/db.c (modified) (22 diffs)
- vowfsc/db.h (modified) (6 diffs)
- vowfsc/file.db (modified) (1 diff)
- vowfsc/todo (added)
- vowfsc/xdelta.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
vowfsc/db.c
r39 r40 161 161 NUM_FILE_FIELDS = sizeof(file_format)/4; 162 162 163 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){163 if( PQnfields(res) != NUM_FILE_FIELDS ){ 164 164 NUM_FILE_FIELDS = 0; 165 165 return NULL; … … 167 167 168 168 // Fill file_format with column numbers 169 #define db(type, name) file_format.name = PQfnumber( pq_data, #name );170 #define db_string(name) file_format.name = PQfnumber( pq_data, #name );169 #define db(type, name) file_format.name = PQfnumber(res, #name ); 170 #define db_string(name) file_format.name = PQfnumber(res, #name ); 171 171 #include "file.db" 172 172 } … … 174 174 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 175 175 176 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;176 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 177 177 178 178 // Fill stat 179 #define db(type, name) stat.name = *(type*)PQgetvalue( pq_data, 0, file_format.name );180 #define db_string(name) stat.name = strdup(PQgetvalue( pq_data, 0, file_format.name ));179 #define db(type, name) stat.name = *(type*)PQgetvalue(res, 0, file_format.name ); 180 #define db_string(name) stat.name = strdup(PQgetvalue(res, 0, file_format.name )); 181 181 #include "file.db" 182 182 … … 201 201 NUM_FILE_FIELDS = sizeof(file_format)/4; 202 202 203 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){203 if( PQnfields(res) != NUM_FILE_FIELDS ){ 204 204 NUM_FILE_FIELDS = 0; 205 205 return NULL; … … 207 207 208 208 // Fill file_format with column numbers 209 #define db(type, name) file_format.name = PQfnumber( pq_data, #name );210 #define db_string(name) file_format.name = PQfnumber( pq_data, #name );209 #define db(type, name) file_format.name = PQfnumber(res, #name ); 210 #define db_string(name) file_format.name = PQfnumber(res, #name ); 211 211 #include "file.db" 212 212 } … … 214 214 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 215 215 216 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;216 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 217 217 218 218 // Fill stat 219 #define db(type, name) stat.name = *(type*)PQgetvalue( pq_data, /*row*/, file_format.name );220 #define db_string(name) stat.name = strdup(PQgetvalue( pq_data, /*row*/, file_format.name ));219 #define db(type, name) stat.name = *(type*)PQgetvalue(res, /*row*/, file_format.name ); 220 #define db_string(name) stat.name = strdup(PQgetvalue(res, /*row*/, file_format.name )); 221 221 #include "file.db" 222 222 … … 354 354 NUM_FILE_FIELDS = sizeof(dir_format)/4; 355 355 356 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){356 if( PQnfields(res) != NUM_FILE_FIELDS ){ 357 357 NUM_FILE_FIELDS = 0; 358 358 return NULL; … … 360 360 361 361 // Fill dir_format with column numbers 362 #define db(type, name) dir_format.name = PQfnumber( pq_data, #name );363 #define db_string(name) dir_format.name = PQfnumber( pq_data, #name );362 #define db(type, name) dir_format.name = PQfnumber(res, #name ); 363 #define db_string(name) dir_format.name = PQfnumber(res, #name ); 364 364 #include "dir.db" 365 365 } … … 367 367 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 368 368 369 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;369 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 370 370 371 371 // Fill stat 372 #define db(type, name) stat.name = *(type*)PQgetvalue( pq_data, /*row*/, dir_format.name );373 #define db_string(name) stat.name = strdup(PQgetvalue( pq_data, /*row*/, dir_format.name ));372 #define db(type, name) stat.name = *(type*)PQgetvalue(res, /*row*/, dir_format.name ); 373 #define db_string(name) stat.name = strdup(PQgetvalue(res, /*row*/, dir_format.name )); 374 374 #include "dir.db" 375 375 … … 394 394 NUM_FILE_FIELDS = sizeof(dir_format)/4; 395 395 396 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){396 if( PQnfields(res) != NUM_FILE_FIELDS ){ 397 397 NUM_FILE_FIELDS = 0; 398 398 return NULL; … … 400 400 401 401 // Fill dir_format with column numbers 402 #define db(type, name) dir_format.name = PQfnumber( pq_data, #name );403 #define db_string(name) dir_format.name = PQfnumber( pq_data, #name );402 #define db(type, name) dir_format.name = PQfnumber(res, #name ); 403 #define db_string(name) dir_format.name = PQfnumber(res, #name ); 404 404 #include "dir.db" 405 405 } … … 407 407 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 408 408 409 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;409 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 410 410 411 411 // Fill stat 412 #define db(type, name) stat.name = *(type*)PQgetvalue( pq_data, 0, dir_format.name );413 #define db_string(name) stat.name = strdup(PQgetvalue( pq_data, 0, dir_format.name ));412 #define db(type, name) stat.name = *(type*)PQgetvalue(res, 0, dir_format.name ); 413 #define db_string(name) stat.name = strdup(PQgetvalue(res, 0, dir_format.name )); 414 414 #include "dir.db" 415 415 … … 443 443 sprintf(pq_request,"host=%s port=%i dbname=%s user=%s password=%s", host, port, dbname, username, password); 444 444 pq_conn=PQconnectdb(pq_request); 445 446 NUM_FILE_FIELDS = 0; 447 NUM_DIR_FIELDS = 0; 448 NUM_STREAM_FIELDS = 0; 449 445 450 if( PQstatus( pq_conn ) != CONNECTION_OK ) 446 451 return -1; … … 469 474 NUM_FILE_FIELDS = sizeof(stream_format)/4; 470 475 471 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){476 if( PQnfields(res) != NUM_FILE_FIELDS ){ 472 477 NUM_FILE_FIELDS = 0; 473 478 return NULL; … … 475 480 476 481 // Fill stream_format with column numbers 477 #define db(type, name) stream_format.name = PQfnumber( pq_data, #name );478 #define db_string(name) stream_format.name = PQfnumber( pq_data, #name );482 #define db(type, name) stream_format.name = PQfnumber(res, #name ); 483 #define db_string(name) stream_format.name = PQfnumber(res, #name ); 479 484 #include "stream.db" 480 485 } … … 482 487 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 483 488 484 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;489 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 485 490 486 491 // Fill stat 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 #define db(type, name) stat.name = *(type*)PQgetvalue(res, 0, stream_format.name ); 493 #define db_string(name) stat.name = strdup(PQgetvalue(res, 0, stream_format.name )); 489 494 #include "stream.db" 490 495 … … 508 513 NUM_FILE_FIELDS = sizeof(stream_format)/4; 509 514 510 if( PQnfields( pq_data) != NUM_FILE_FIELDS ){515 if( PQnfields(res) != NUM_FILE_FIELDS ){ 511 516 NUM_FILE_FIELDS = 0; 512 517 return NULL; … … 514 519 515 520 // Fill stream_format with column numbers 516 #define db(type, name) stream_format.name = PQfnumber( pq_data, #name );517 #define db_string(name) stream_format.name = PQfnumber( pq_data, #name );521 #define db(type, name) stream_format.name = PQfnumber(res, #name ); 522 #define db_string(name) stream_format.name = PQfnumber(res, #name ); 518 523 #include "stream.db" 519 524 } … … 521 526 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 522 527 523 if( PQnfields( pq_data) != NUM_FILE_FIELDS ) return NULL;528 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 524 529 525 530 // Fill stat 526 #define db(type, name) stat.name = *(type*)PQgetvalue( pq_data, 0, stream_format.name );527 #define db_string(name) stat.name = strdup(PQgetvalue( pq_data, 0, stream_format.name ));531 #define db(type, name) stat.name = *(type*)PQgetvalue(res, 0, stream_format.name ); 532 #define db_string(name) stat.name = strdup(PQgetvalue(res, 0, stream_format.name )); 528 533 #include "stream.db" 529 534 … … 584 589 PGresult *res=PQexec(pq_conn,"SELECT MAX(file_id) FROM file"); 585 590 586 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL;591 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return 0; 587 592 588 593 int file_id=*(int*)PQgetvalue(res,0,0); … … 596 601 PGresult *res=PQexec(pq_conn,"SELECT MAX(file_uid) FROM file"); 597 602 598 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL;603 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return 0; 599 604 600 605 int file_uid=*(int*)PQgetvalue(res,0,0); … … 623 628 624 629 625 630 dir_t *db_get_dir_subdirs( int dir_id, int *length ){ 631 char pq_request[BUFFER_SIZE]; 632 sprintf(pq_request, "SELECT * " 633 "WHERE directory_id = %i", 634 dir_id); 635 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 636 637 *length = PQntuples(res); 638 file_t *listing=malloc(*length * sizeof(dir_t)*); 639 640 641 if( NUM_DIR_FIELDS ){ 642 NUM_DIR_FIELDS = sizeof(dir_format)/4; 643 644 if( PQnfields(res) != NUM_DIR_FIELDS ){ 645 NUM_DIR_FIELDS = 0; 646 return NULL; 647 } 648 649 // Fill dir_format with column numbers 650 #define db(type, name) dir_format.name = PQfnumber(res, #name ); 651 #define db_string(name) dir_format.name = PQfnumber(res, #name ); 652 #include "dir.db" 653 } 654 655 if( PQnfields(res) != NUM_DIR_FIELDS ) return NULL; 656 657 int i; 658 for( i=0; i< *length; ++i ){ 659 #define db(type, name) listing[i].name = *(type*)PQgetvalue(res, i, dir_format.name ); 660 #define db_string(name) listing[i].name = strdup(PQgetvalue(res, i, dir_format.name )); 661 #include "dir.db" 662 } 663 PQfreemem(res); 664 665 return listing; 666 } 667 668 669 file_t *db_get_file_subfiles( int file_id, int *length ){ 670 char pq_request[BUFFER_SIZE]; 671 sprintf(pq_request, "SELECT * " 672 "WHERE fileectory_id = %i", 673 file_id); 674 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 675 676 *length = PQntuples(res); 677 file_t *listing=malloc(*length * sizeof(file_t)*); 678 679 680 if( NUM_FILE_FIELDS ){ 681 NUM_FILE_FIELDS = sizeof(file_format)/4; 682 683 if( PQnfields(res) != NUM_FILE_FIELDS ){ 684 NUM_FILE_FIELDS = 0; 685 return NULL; 686 } 687 688 // Fill file_format with column numbers 689 #define db(type, name) file_format.name = PQfnumber(res, #name ); 690 #define db_string(name) file_format.name = PQfnumber(res, #name ); 691 #include "file.db" 692 } 693 694 if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 695 696 int i; 697 for( i=0; i< *length; ++i ){ 698 #define db(type, name) listing[i].name = *(type*)PQgetvalue(res, i, file_format.name ); 699 #define db_string(name) listing[i].name = strdup(PQgetvalue(res, i, file_format.name )); 700 #include "file.db" 701 } 702 PQfreemem(res); 703 704 return listing; 705 } 706 707 int db_get_file_uid( int file_id, int revision_id ){ 708 char pq_request[BUFFER_SIZE]; 709 sprintf(pq_request, "SELECT file_uid " 710 "WHERE file_id = %i AND revision_id = %i", 711 file_id, revision_id); 712 PQexec(pq_request); 713 714 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 715 716 int file_uid = PQgetvalue(res, 0,0); 717 718 PQfreemem(res); 719 720 return file_id; 721 } 722 723 int db_remove_file( int file_id ){ 724 file_t file = db_get_file( file_id ); 725 726 int file_uid = db_get_max_file_uid() + 1; 727 int parent_file_id = db_get_file_uid( file_id, file.revision_id ); 728 729 file.revision_id++; 730 731 char *data=db_get_file_data( file_id ); 732 int data_len = file.file_length; 733 734 // Diff the files 735 char *diff=malloc(XD3_ALLOCSIZE); 736 int diff_len; 737 738 xd3_encode_memory(data, data_len, 739 data, data_len, 740 diff, &diff_len, XD3_ALLOCSIZE, 741 0); 742 743 char pq_request[BUFFER_SIZE]; 744 sprintf(pq_request, "INSERT file " 745 "(file_uid, file_id, revision_id, file_length, " 746 "parent_file_id, stream_id, end_time, deleted_flag, " 747 "file_name, directory_path, directory_id, data) " 748 "VALUES " 749 "%i, %i, %i, %i, %i, %i, NOW, TRUE, %s, %s, %s", 750 file_uid, file_id, file.revision_id, file.file_length, 751 parent_file_id, file.stream_id, file.file_name, 752 file.directory_path, file.directory_id, data); 753 PQfreemem(PQexec(pq_conn, pq_request)); 754 755 // Insert the diff into the old file 756 sprintf(pq_request, "UPDATE file " 757 "SET data = \'%s\' " 758 "WHERE file_uid = %i", 759 diff, parent_file_id); 760 PQfreemem(PQexec(pq_conn, pq_request)); 761 762 return revision; 763 } 764 765 766 int db_branch_file( int file_id, int revision_id, char *new_file_name, char *new_file_path ){ 767 if( !new_file_path && !new_file_name ) return NULL; 768 769 file_t file = db_get_file( file_id ); 770 771 int file_id = db_get_max_file_id() +1; 772 int file_uid = db_get_max_file_uid() +1; 773 774 int parent_file_id = db_get_file_uid( file_id, revision_id ); 775 776 char *new_dir_name 777 if( new_file_path ) 778 new_dir_name = normalize_dir_name(new_file_path); 779 else 780 new_dir_name = file.directory_name; 781 int new_dir_id = db_get_dir_id( new_dir_name ); 782 783 if( !new_file_name ) 784 new_file_name = file.file_name; 785 786 787 char *data=db_get_file_data( file_id ); 788 789 // Insert the new file 790 char pq_request[BUFFER_SIZE]; 791 sprintf(pq_request, "INSERT file " 792 "(file_uid, file_id, revision_id, file_length, " 793 "parent_file_id, stream_id, end_time, deleted_flag, " 794 "file_name, directory_path, directory_id, data) " 795 "VALUES " 796 "%i, %i, 0, %i, %i, %i, NOW, FALSE, %s, %s, %s", 797 file_uid, file_id, file.file_length, parent_file_id, 798 file.stream_id, new_file_name, new_dir_name, 799 new_dir_id, data); 800 PQfreemem(PQexec(pq_conn, pq_request)); 801 802 return revision; 803 } 804 805 806 int db_modify_file_stream( int file_id, char *new_stream_name, char *new_stream ){ 807 stream_t stream = db_get_file_stream( file_id ); 808 file_t file = db_get_file( file_id ); 809 810 if( !new_stream_name ) new_stream_name = stream.stream_name; 811 if( !new_stream ) new_stream = stream.stream; 812 813 int new_stream_id = db_create_stream(new_stream_name, new_stream); 814 815 int file_uid = db_get_max_file_uid() +1; 816 817 int parent_file_id = db_get_file_uid( file_id, file.revision_id ); 818 file.revision_id++; 819 820 char *data=db_get_file_data( file_id ); 821 int data_len=file.file_length; 822 823 // Diff the files 824 char *diff=malloc(XD3_ALLOCSIZE); 825 int diff_len; 826 827 xd3_encode_memory(data, data_len, 828 data, data_len, 829 diff, &diff_len, XD3_ALLOCSIZE, 830 0); 831 832 833 // Insert the new file 834 char pq_request[BUFFER_SIZE]; 835 sprintf(pq_request, "INSERT file " 836 "(file_uid, file_id, revision_id, file_length, " 837 "parent_file_id, stream_id, end_time, deleted_flag, " 838 "file_name, directory_path, directory_id, data) " 839 "VALUES " 840 "%i, %i, %i, %i, %i, %i, NOW, TRUE, %s, %s, %s", 841 file_uid, file_id, file.revision_id, file.file_length, 842 parent_file_id, new_stream_id, file.file_name, 843 file.directory_path, file.directory_id, data); 844 PQfreemem(PQexec(pq_conn, pq_request)); 845 846 // Insert the diff into the old file 847 sprintf(pq_request, "UPDATE file " 848 "SET data = \'%s\' " 849 "WHERE file_uid = %i", 850 diff, parent_file_id); 851 PQfreemem(PQexec(pq_conn, pq_request)); 852 853 return revision; 854 } 855 856 int db_modify_file_data( int file_id, char *data, int data_len ){ 857 if( !data ) return NULL; 858 859 int file_uid = db_get_max_file_uid() +1; 860 861 int parent_file_id = db_get_file_uid( file_id, file.revision_id ); 862 file.revision_id++; 863 864 char old_data=db_get_file_data( file_id ); 865 int old_data_len = file.file_length; 866 867 // Diff the files 868 char *diff=malloc(XD3_ALLOCSIZE); 869 int diff_len; 870 871 xd3_encode_memory(old_data, old_data_len, 872 data, data_len, 873 diff, &diff_len, XD3_ALLOCSIZE, 874 0); 875 876 char pq_request[BUFFER_SIZE]; 877 878 // Insert the new file 879 sprintf(pq_request, "INSERT file " 880 "(file_uid, file_id, revision_id, file_length, " 881 "parent_file_id, stream_id, end_time, deleted_flag, " 882 "file_name, directory_path, directory_id, data) " 883 "VALUES " 884 "%i, %i, %i, %i, %i, %i, NOW, TRUE, %s, %s, %s", 885 file_uid, file_id, file.revision_id, data_len, 886 parent_file_id, file.stream_id, file.file_name, 887 file.directory_path, file.directory_id, data); 888 PQfreemem(PQexec(pq_conn, pq_request)); 889 890 // Insert the diff into the old file 891 sprintf(pq_request, "UPDATE file " 892 "SET data = \'%s\' " 893 "WHERE file_uid = %i", 894 diff, parent_file_id); 895 PQfreemem(PQexec(pq_conn, pq_request)); 896 897 return revision; 898 899 } 900 vowfsc/db.h
r39 r40 6 6 #include <stdlib.h> 7 7 #include <string.h> 8 9 #include "xdelta3.h" 8 10 9 11 #define BUFFER_SIZE 1024 … … 83 85 * new file. 84 86 */ 85 int db_remove_file(int file_id); // TODO87 int db_remove_file(int file_id); 86 88 87 89 … … 90 92 * revisions it. 91 93 */ 92 int db_create_file(char *file_name, char *file_path, int stream_id ); // TODO94 int db_create_file(char *file_name, char *file_path, int stream_id ); 93 95 94 96 … … 103 105 * file or NULL in the case of a failture. 104 106 */ 105 int db_branch_file(int file_id, int revision, char *new_file_name, char *new_file_path ); // TODO107 int db_branch_file(int file_id, int revision, char *new_file_name, char *new_file_path ); 106 108 107 109 /* For revisioning an existing file, this function … … 109 111 * file_id 110 112 */ 111 int db_modify_file_data(int file_id, char *data ); // TODO113 int db_modify_file_data(int file_id, char *data, int data_len ); 112 114 113 115 114 116 /* For changing the permissions on an existing file 115 117 * modifies the stream in an existing file given by 116 * file_id. 118 * file_id. If either stream_name or stream are NULL, 119 * they will be replaced with the current values. 117 120 */ 118 int db_modify_file_stream(int file_id, stream_t stream); // TODO121 int db_modify_file_stream(int file_id, char *new_stream_name, char *new_stream ); 119 122 120 123 … … 141 144 142 145 146 /* Get subdirs and get files both return an array of 147 * either directories or files. The length pointer 148 * returns the number of elements in the array. 149 */ 150 dir_t *db_get_dir_subdirs( int dir_id, int *length ); 151 file_t *db_get_dir_files( int dir_id, int *length ); 152 143 153 /* db_init and db_destoy create and destroy connections 144 154 * to the database respectivly. They are wrappers around vowfsc/file.db
r34 r40 3 3 db(int,file_length) 4 4 db(int,parent_file_id) 5 db(int,directory_id) 5 6 db(int,stream_id) 6 7 vowfsc/xdelta.c
r37 r40 1 1 #include "xdelta3.h" 2 #include "xdelta3-extra.h" 2 3 #include <stdio.h> 3 4 #include <stdlib.h> … … 18 19 19 20 xd3_encode_memory(string1, string1_len, 20 string 2, string2_len,21 string1, string1_len, 21 22 diff, &diff_len, XD3_ALLOCSIZE, 22 23 0); … … 25 26 int output_len; 26 27 27 printf("Diff( String1 -> String 2): %s(%i)\n",diff,diff_len);28 printf("Diff( String1 -> String1 ): %s(%i)\n",diff,diff_len); 28 29 29 30 xd3_decode_memory(diff, diff_len,
