Changeset 2469

Show
Ignore:
Timestamp:
08/19/07 03:03:04 (1 year ago)
Author:
paule
Message:

Improve error handling - stop using perror and properly error out if creating the backup dir fails

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/opie-sync/src/opie_comms.c

    r2467 r2469  
    116116 * create a backup file from a string 
    117117 */ 
    118 int backup_file(const char *backupfile, const char *str, int len) { 
     118gboolean backup_file(const char *backupfile, const char *str, int len) { 
    119119        osync_trace(TRACE_ENTRY, "%s(%s, %p, %i)", __func__, backupfile, str, len); 
    120120         
    121121        int destfd = 0; 
    122         int rc = FALSE; 
    123122        int bufsize = 1024; 
    124123        int wbytes; 
    125124        int pos = 0; 
     125        char *errmsg = NULL; 
    126126         
    127127        destfd = open(backupfile, O_CREAT | O_WRONLY | O_EXCL, 0600); 
    128128        if(destfd == -1) { 
    129                 osync_trace( TRACE_INTERNAL, "error creating backup file" ); 
    130                 perror("error creating backup file"); 
     129                errmsg = g_strdup_printf("error creating backup file: %s", strerror(errno)); 
    131130                goto error; 
    132131        } 
     
    138137                wbytes = write(destfd, str + pos, bufsize); 
    139138                if(wbytes == -1) { 
    140                         osync_trace( TRACE_INTERNAL, "error writing to backup file" ); 
    141                         perror("error writing to backup file"); 
    142                         close(destfd); 
     139                        errmsg = g_strdup_printf("error writing to backup file: %s", strerror(errno)); 
    143140                        goto error; 
    144141                } 
     
    152149        } 
    153150         
    154         rc = TRUE; 
    155  
     151        osync_trace(TRACE_EXIT, "%s(%i)", __func__, TRUE); 
     152        return TRUE; 
     153         
    156154error: 
    157          
    158         osync_trace(TRACE_EXIT, "%s(%i)", __func__, rc); 
    159         return rc
     155        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, errmsg); 
     156        g_free(errmsg); 
     157        return FALSE
    160158} 
    161159 
     
    179177        backuppath = g_build_filename(backupdir, datestamp, NULL); 
    180178        if(g_mkdir_with_parents(backuppath, 0700)) { 
    181                 perror("error creating backup directory"); 
    182                 osync_trace(TRACE_EXIT_ERROR, "error creating backup directory"); 
     179                osync_trace(TRACE_EXIT_ERROR, "error creating backup directory: %s", strerror(errno)); 
    183180                goto error; 
    184181        } 
     
    279276                                g_free(basename); 
    280277                        } 
     278                        else 
     279                                rc = FALSE; 
    281280                } 
    282281                 
     
    941940        osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, env, remotefile, data); 
    942941         
    943         gboolean rc = TRUE; 
    944942        char* scpcommand = NULL; 
    945943        int scpretval = 0; 
    946944        int scpexitstatus = 0; 
     945        char *errmsg = NULL; 
    947946 
    948947        TempFile *tmpfile = create_temp_file(); 
    949948        if(!tmpfile) { 
    950                 /* could not create temp file - error message has already been sent to trace */ 
    951                 rc = FALSE; 
     949                /* could not create temp file */ 
     950                errmsg = g_strdup("failed to create temp file");  
     951                goto error; 
    952952        } 
    953953        else { 
     
    963963                        wbytes = write(tmpfile->fd, data + pos, bufsize); 
    964964                        if(wbytes == -1) { 
    965                                 osync_trace( TRACE_INTERNAL, "error writing to backup file" ); 
    966                                 perror("error writing to backup file"); 
    967                                 rc = FALSE; 
     965                                errmsg = g_strdup_printf("error writing to temp file: %s", strerror(errno)); 
    968966                                goto error; 
    969967                        } 
     
    992990                 
    993991                if((scpretval == -1) || (scpexitstatus != 0)) { 
    994                         rc = FALSE
    995                         osync_trace( TRACE_INTERNAL, "ssh create path failed" )
     992                        errmsg = g_strdup("ssh create path failed")
     993                        goto error
    996994                } 
    997995                g_free(scpcommand); 
     
    10081006 
    10091007                if((scpretval == -1) || (scpexitstatus != 0)) { 
    1010                         rc = FALSE
    1011                         osync_trace( TRACE_INTERNAL, "scp upload failed" )
     1008                        errmsg = g_strdup("scp upload failed")
     1009                        goto error
    10121010                } 
    10131011                else { 
    1014                         rc = TRUE; 
    10151012                        osync_trace( TRACE_INTERNAL, "scp upload ok" ); 
    10161013                } 
     
    10191016        } 
    10201017         
    1021 error: 
    1022  
    10231018        if(tmpfile) 
    10241019                cleanup_temp_file(tmpfile); 
    10251020         
    1026         osync_trace(TRACE_EXIT, "%s(%d)", __func__, rc ); 
    1027         return rc; 
     1021        osync_trace(TRACE_EXIT, "%s(%d)", __func__, TRUE ); 
     1022        return TRUE; 
     1023         
     1024error: 
     1025 
     1026        if(tmpfile) 
     1027                cleanup_temp_file(tmpfile); 
     1028        if(scpcommand) 
     1029                g_free(scpcommand); 
     1030         
     1031        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, errmsg ); 
     1032        return FALSE; 
    10281033} 
    10291034