Changeset 1451 for trunk/core/ptp.c


Ignore:
Timestamp:
12/04/11 01:40:36 (18 months ago)
Author:
reyalp
Message:

re-write ptp file upload code to workaround vxworks corruption issue, see http://chdk.setepontos.com/index.php?topic=6730.msg76760#msg76760

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/ptp.c

    r1450 r1451  
    3838} 
    3939 
     40/* 
     41WARNING: it appears that on some vxworks cameras, 
     42if a call to recv_ptp_data doesn't end on a word (4 byte) boundery, 
     43subsequent calls will return corrupt data 
     44the final call not ending on a word boundery is OK. 
     45see http://chdk.setepontos.com/index.php?topic=6730.msg76760#msg76760 
     46*/ 
    4047static int recv_ptp_data(ptp_data *data, char *buf, int size) 
    4148  // repeated calls per transaction are ok 
     
    381388    case PTP_CHDK_UploadFile: 
    382389      { 
    383         FILE *f; 
    384         int s,r,fn_len; 
    385         char *buf, *fn; 
    386  
    387         s = data->get_data_size(data->handle); 
    388  
    389         recv_ptp_data(data,(char *) &fn_len,4); 
    390         s -= 4; 
    391  
    392         fn = (char *) malloc(fn_len+1); 
    393         if ( fn == NULL ) 
    394         { 
    395           ptp.code = PTP_RC_GeneralError; 
    396           break; 
    397         } 
    398         fn[fn_len] = '\0'; 
    399  
    400         recv_ptp_data(data,fn,fn_len); 
    401         s -= fn_len; 
    402  
    403         f = fopen(fn,"wb"); 
    404         if ( f == NULL ) 
    405         { 
    406           ptp.code = PTP_RC_GeneralError; 
    407           free(fn); 
    408           break; 
    409         } 
    410         free(fn); 
    411  
    412         buf = (char *) malloc(buf_size); 
    413         if ( buf == NULL ) 
    414         { 
    415           ptp.code = PTP_RC_GeneralError; 
    416           break; 
    417         } 
    418         while ( s > 0 ) 
    419         { 
    420           if ( s >= buf_size ) 
    421           { 
    422             recv_ptp_data(data,buf,buf_size); 
    423             fwrite(buf,1,buf_size,f); 
    424             s -= buf_size; 
    425           } else { 
    426             recv_ptp_data(data,buf,s); 
    427             fwrite(buf,1,s,f); 
    428             s = 0; 
    429           } 
    430         } 
    431  
    432         fclose(f); 
    433  
     390        FILE *f=NULL; 
     391        char *buf=NULL, *fn=NULL; 
     392        unsigned data_size,fn_len,chunk_size; 
     393        data_size = data->get_data_size(data->handle); 
     394        while ( data_size > 0 ) { 
     395            chunk_size = (data_size > buf_size) ? buf_size:data_size; 
     396            // first time through 
     397            // allocate buffer, parse out the file name and open file 
     398            if(!buf) { 
     399                buf=malloc(chunk_size); 
     400                if(!buf) { 
     401                    ptp.code = PTP_RC_GeneralError; 
     402                    break; 
     403                } 
     404                recv_ptp_data(data,buf,chunk_size); 
     405                fn_len = *(unsigned *)buf; 
     406                fn = malloc(fn_len); 
     407                if(!fn) { 
     408                    ptp.code = PTP_RC_GeneralError; 
     409                    break; 
     410                } 
     411                memcpy(fn,buf+4,fn_len); 
     412                fn[fn_len] = 0; 
     413                f = fopen(fn,"wb"); 
     414                free(fn); 
     415                if(!f) { 
     416                    ptp.code = PTP_RC_GeneralError; 
     417                    break; 
     418                } 
     419                fwrite(buf+4+fn_len,1,chunk_size - 4 - fn_len,f); 
     420            } else { 
     421                recv_ptp_data(data,buf,chunk_size); 
     422                fwrite(buf,1,chunk_size,f); 
     423            } 
     424            data_size -= chunk_size; 
     425        } 
     426        if(f) { 
     427            fclose(f); 
     428        } 
    434429        free(buf); 
    435430        break; 
Note: See TracChangeset for help on using the changeset viewer.