Changeset 55 for vowfsc

Show
Ignore:
Timestamp:
04/02/08 18:26:34 (6 months ago)
Author:
nlawren2
Message:

Committed, and dead

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • vowfsc/Makefile

    r54 r55  
    2121 
    2222clean: 
    23         rm -f *.o 
    24         rm -f test 
     23        rm -f *.o db_test xdelta_test 
  • vowfsc/db.c

    r54 r55  
    153153int db_parse_dir_res( PGresult *res, dir_t *dir ){ 
    154154        // 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; 
    160160                        return NULL; 
    161161                } 
     
    170170        if( PQresultStatus(res) != PGRES_TUPLES_OK ) return NULL; 
    171171         
    172         if( PQnfields(res) != NUM_FILE_FIELDS ) return NULL; 
     172        if( PQnfields(res) != NUM_DIR_FIELDS ) return NULL; 
    173173 
    174174        // Fill stat 
     
    583583 
    584584int 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]; 
    586587         
    587588        file_uid = db_get_file_max_uid() + 1; 
     
    592593                file_id = db_get_file_max_id() + 1; 
    593594                revision_id = 0; 
     595                parent_file_id = 0; 
    594596        }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); 
    596625        } 
    597626         
     
    602631 
    603632        // Make the PGSQL request 
    604         char pq_request[BUFFER_SIZE]; 
    605633        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) " 
    607635                            "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); 
    610638        db_debug_rq(db_create_file); 
    611639        PGresult *res = PQexec(pq_conn, pq_request); 
     
    726754         
    727755        PQfreemem(udata); 
    728         udata = PQescapeByteaConn(pq_conn,data,data_len,&udata_len); 
     756        udata = PQescapeByteaConn(pq_conn,diff,diff_len,&udata_len); 
    729757         
    730758        // Insert the diff into the old file 
     
    907935                           "FROM file " 
    908936                           "WHERE file_id = \'%i\' " 
    909                            "AND revision_id = MAX( SELECT revision_id FROM file WHERE file_id = \'%i\')", 
     937                           "AND revision_id = ( SELECT MAX(revision_id) FROM file WHERE file_id = \'%i\')", 
    910938                           file_id, file_id); 
    911939        PGresult *res=PQexec(pq_conn,pq_request); 
     
    10061034} 
    10071035 
     1036// TODO: Get this working 
     1037int 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  
    5656 */ 
    5757file_t *db_get_file( int file_id ); // TESTED 
    58 file_t *db_get_file_rev( int file_id, int revision_id ); 
     58file_t *db_get_file_rev( int file_id, int revision_id ); // TESTED 
     59 
    5960 
    6061/* db_get_file_data returns the undiffed data from  
     
    7374stream_t *db_get_file_rev_stream( int file_id, int revision_id ); 
    7475 
     76 
    7577/* Looks first to see if the given stream exists, and 
    7678 * returns it's stream id if it does.  If not, it creates 
     
    7880 */ 
    7981int db_create_stream( char *stream_name, char *stream ); 
     82 
    8083 
    8184/* Copies the file to a new reversion, and sets the delete flag  
     
    9699/* Branches a file by creating a new file based on a 
    97100 * 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.   
    102102 * 
    103103 * db_branch_file returns the file_id of the new 
    104104 * file or NULL in the case of a failture. 
    105105 */ 
    106 int db_branch_file(int file_id, int revision_id, char *new_file_name, char *new_file_path ); 
     106int db_branch_file(int file_id, int revision_id, char *new_file_path ); 
     107 
    107108 
    108109/* For revisioning an existing file, this function 
     
    125126 * and then querying the database. 
    126127 */ 
    127 int db_get_dir_id( char *dir_name ); 
     128int db_get_dir_id( char *dir_name ); // TESTED 
    128129 
    129130 
     
    139140 * struct, look at the dir.db file. 
    140141 */ 
    141 dir_t *db_get_dir( int dir_id ); 
     142dir_t *db_get_dir( int dir_id ); // TESTED 
    142143dir_t *db_get_dir_rev( int dir_id, int revision_id ); 
    143144 
     
    150151file_t *db_get_dir_files( int dir_id, int *length ); 
    151152 
     153 
    152154/* Does what it says, creates a new directory. 
    153155 * Returns the dir_id of the new directory or null in 
     
    155157 */ 
    156158int db_create_dir( char *dir_path ); // TODO 
     159 
     160 
     161/* Copies the directory entry and sets the delete flag 
     162 */ 
     163int db_remove_dif( int dir_id ); // TODO 
     164 
    157165 
    158166/* db_init and db_destoy create and destroy connections 
     
    164172int db_destroy(); // TESTED 
    165173 
     174 
    166175/* Creates a new blank file system 
    167176 */