Changeset 55
- Timestamp:
- 04/02/08 18:26:34 (5 months ago)
- Files:
-
- vowfsc/Makefile (modified) (1 diff)
- vowfsc/db.c (modified) (8 diffs)
- vowfsc/db.h (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
vowfsc/Makefile
r54 r55 21 21 22 22 clean: 23 rm -f *.o 24 rm -f test 23 rm -f *.o db_test xdelta_test vowfsc/db.c
r54 r55 153 153 int db_parse_dir_res( PGresult *res, dir_t *dir ){ 154 154 // Initialize format 155 if( !NUM_ FILE_FIELDS ){156 NUM_ FILE_FIELDS = sizeof(dir_format)/4+1;157 158 if( PQnfields(res) != NUM_ FILE_FIELDS ){159 NUM_ FILE_FIELDS = 0;155 if( !NUM_DIR_FIELDS ){ 156 NUM_DIR_FIELDS = sizeof(dir_format)/4; 157 158 if( PQnfields(res) != NUM_DIR_FIELDS ){ 159 NUM_DIR_FIELDS = 0; 160 160 return NULL; 161 161 } … … 170 170 if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 171 171 172 if( PQnfields(res) != NUM_ FILE_FIELDS ) return NULL;172 if( PQnfields(res) != NUM_DIR_FIELDS ) return NULL; 173 173 174 174 // Fill stat … … 583 583 584 584 int db_create_file(char *file_path, int stream_id ){ 585 int revision_id, file_id, file_uid; 585 int revision_id, file_id, file_uid, parent_file_id; 586 char pq_request[BUFFER_SIZE]; 586 587 587 588 file_uid = db_get_file_max_uid() + 1; … … 592 593 file_id = db_get_file_max_id() + 1; 593 594 revision_id = 0; 595 parent_file_id = 0; 594 596 }else{ 595 revision_id = db_get_file_max_revision( file_id ) + 1; 597 file_t *file=db_get_file(file_id); 598 revision_id = file->revision_id + 1; 599 600 parent_file_id = db_get_file_uid(file_id,file->revision_id); 601 602 int diff_len; 603 char *data = db_get_file(file_id); 604 char *diff = malloc(XD3_ALLOCSIZE); 605 606 xd3_encode_memory(data,file->file_length, 607 NULL,0, 608 diff,&diff_len,XD3_ALLOCSIZE, 609 0); 610 611 char *udiff = PQescapeByteaConn(pq_conn,diff,diff_len,&diff_len); 612 613 // Insert the diff into the old file 614 sprintf(pq_request, "UPDATE file " 615 "SET data = \'%s\' " 616 "WHERE file_uid = \'%i\'", 617 udiff, parent_file_id); 618 db_debug_rq(db_remove_file); 619 PQclear(PQexec(pq_conn, pq_request)); 620 621 PQfreemem(udiff); 622 free(data); 623 free(diff); 624 free(file); 596 625 } 597 626 … … 602 631 603 632 // Make the PGSQL request 604 char pq_request[BUFFER_SIZE];605 633 sprintf(pq_request, "INSERT INTO file " 606 "(file_uid, file_id, revision_id, directory_path, file_name, stream_id, end_time ) "634 "(file_uid, file_id, revision_id, directory_path, file_name, stream_id, end_time, parent_file_id) " 607 635 "VALUES " 608 "(\'%i\', \'%i\', \'%i\', \'%s\', \'%s\', \'%i\', \'%i\' )",609 file_uid, file_id, revision_id, dir_name, file_name, stream_id, now );636 "(\'%i\', \'%i\', \'%i\', \'%s\', \'%s\', \'%i\', \'%i\', \'%i\')", 637 file_uid, file_id, revision_id, dir_name, file_name, stream_id, now, parent_file_id); 610 638 db_debug_rq(db_create_file); 611 639 PGresult *res = PQexec(pq_conn, pq_request); … … 726 754 727 755 PQfreemem(udata); 728 udata = PQescapeByteaConn(pq_conn,d ata,data_len,&udata_len);756 udata = PQescapeByteaConn(pq_conn,diff,diff_len,&udata_len); 729 757 730 758 // Insert the diff into the old file … … 907 935 "FROM file " 908 936 "WHERE file_id = \'%i\' " 909 "AND revision_id = MAX( SELECT revision_idFROM file WHERE file_id = \'%i\')",937 "AND revision_id = ( SELECT MAX(revision_id) FROM file WHERE file_id = \'%i\')", 910 938 file_id, file_id); 911 939 PGresult *res=PQexec(pq_conn,pq_request); … … 1006 1034 } 1007 1035 1036 // TODO: Get this working 1037 int db_create_dir(char *dir_path, int stream_id ){ 1038 int revision_id, dir_id, dir_uid, parent_dir_id; 1039 char pq_request[BUFFER_SIZE]; 1040 1041 dir_uid = db_get_dir_max_uid() + 1; 1042 1043 // Check to see that the dir does not exist already 1044 dir_id = db_get_dir_id( dir_path ); 1045 if( !dir_id ){ 1046 dir_id = db_get_dir_max_id() + 1; 1047 revision_id = 0; 1048 parent_dir_id = 0; 1049 }else{ 1050 dir_t *dir=db_get_dir(dir_id); 1051 revision_id = dir->revision_id + 1; 1052 1053 parent_dir_id = db_get_dir_uid(dir_id,dir->revision_id); 1054 free(dir); 1055 } 1056 1057 char *dir_name, *dir_name; 1058 db_split_dir_name(dir_path, &dir_name, &dir_name); 1059 1060 int now = time(NULL); 1061 1062 // Make the PGSQL request 1063 sprintf(pq_request, "INSERT INTO dir " 1064 "(dir_uid, dir_id, revision_id, directory_path, dir_name, stream_id, end_time, parent_dir_id) " 1065 "VALUES " 1066 "(\'%i\', \'%i\', \'%i\', \'%s\', \'%s\', \'%i\', \'%i\', \'%i\')", 1067 dir_uid, dir_id, revision_id, dir_name, dir_name, stream_id, now, parent_dir_id); 1068 db_debug_rq(db_create_dir); 1069 PGresult *res = PQexec(pq_conn, pq_request); 1070 1071 // Check that the command went through 1072 if( PQresultStatus(res) != PGRES_COMMAND_OK ) return NULL; 1073 1074 // Clean up and return 1075 free(dir_name); 1076 free(dir_name); 1077 PQclear(res); 1078 1079 return dir_id; 1080 } 1081 1082 vowfsc/db.h
r54 r55 56 56 */ 57 57 file_t *db_get_file( int file_id ); // TESTED 58 file_t *db_get_file_rev( int file_id, int revision_id ); 58 file_t *db_get_file_rev( int file_id, int revision_id ); // TESTED 59 59 60 60 61 /* db_get_file_data returns the undiffed data from … … 73 74 stream_t *db_get_file_rev_stream( int file_id, int revision_id ); 74 75 76 75 77 /* Looks first to see if the given stream exists, and 76 78 * returns it's stream id if it does. If not, it creates … … 78 80 */ 79 81 int db_create_stream( char *stream_name, char *stream ); 82 80 83 81 84 /* Copies the file to a new reversion, and sets the delete flag … … 96 99 /* Branches a file by creating a new file based on a 97 100 * given revision and a file_id. This is used for 98 * moving files to new directories, etc. If either 99 * new_file_name OR new_file_path is NULL, 100 * db_branch_file uses the name OR path of the old 101 * file. YOU CANNOT PASS NULL FOR BOTH. 101 * moving files to new directories, etc. 102 102 * 103 103 * db_branch_file returns the file_id of the new 104 104 * file or NULL in the case of a failture. 105 105 */ 106 int db_branch_file(int file_id, int revision_id, char *new_file_name, char *new_file_path ); 106 int db_branch_file(int file_id, int revision_id, char *new_file_path ); 107 107 108 108 109 /* For revisioning an existing file, this function … … 125 126 * and then querying the database. 126 127 */ 127 int db_get_dir_id( char *dir_name ); 128 int db_get_dir_id( char *dir_name ); // TESTED 128 129 129 130 … … 139 140 * struct, look at the dir.db file. 140 141 */ 141 dir_t *db_get_dir( int dir_id ); 142 dir_t *db_get_dir( int dir_id ); // TESTED 142 143 dir_t *db_get_dir_rev( int dir_id, int revision_id ); 143 144 … … 150 151 file_t *db_get_dir_files( int dir_id, int *length ); 151 152 153 152 154 /* Does what it says, creates a new directory. 153 155 * Returns the dir_id of the new directory or null in … … 155 157 */ 156 158 int db_create_dir( char *dir_path ); // TODO 159 160 161 /* Copies the directory entry and sets the delete flag 162 */ 163 int db_remove_dif( int dir_id ); // TODO 164 157 165 158 166 /* db_init and db_destoy create and destroy connections … … 164 172 int db_destroy(); // TESTED 165 173 174 166 175 /* Creates a new blank file system 167 176 */
