1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Changeset 2330

Show
Ignore:
Timestamp:
12/14/11 01:38:28 (18 months ago)
Author:
jkoshy
Message:

Code cleanups in ar: use the elftc_copyfile() helper function
to copy file contents.

Reviewed by: kaiwang27
Ticket(s): #284, #367

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/ar/acpyacc.y

    r2230 r2330  
    3232#include <sys/queue.h> 
    3333#include <sys/stat.h> 
     34 
    3435#include <archive.h> 
    3536#include <archive_entry.h> 
     
    4243#include <unistd.h> 
    4344 
     45#include "libelftc.h" 
     46 
    4447#include "ar.h" 
    4548 
     
    6265static void     arscp_addmod(struct list *list); 
    6366static void     arscp_clear(void); 
    64 static int      arscp_copy(int ifd, int ofd); 
    6567static void     arscp_create(char *in, char *out); 
    6668static void     arscp_delete(struct list *list); 
     
    296298                 * input archive. 
    297299                 */ 
    298                 if ((ifd = open(in, O_RDONLY)) < 0) { 
    299                         bsdar_warnc(bsdar, errno, "open failed"); 
     300                if ((ifd = open(in, O_RDONLY)) < 0 || 
     301                    elftc_copyfile(ifd, ofd) < 0) { 
     302                        bsdar_warnc(bsdar, errno, "'OPEN' failed"); 
     303                        (void) close(ofd); 
     304                        if (ifd != -1) 
     305                                (void) close(ifd); 
    300306                        return; 
    301307                } 
    302                 if (arscp_copy(ifd, ofd)) { 
    303                         bsdar_warnc(bsdar, 0, "arscp_copy failed"); 
    304                         return; 
    305                 } 
    306                 close(ifd); 
    307                 close(ofd); 
     308                (void) close(ifd); 
     309                (void) close(ofd); 
    308310        } else { 
    309311                /* 
     
    325327        target = out; 
    326328        bsdar->filename = tmpac; 
    327 } 
    328  
    329 /* 
    330  * A file copying implementation using mmap(). 
    331  */ 
    332 static int 
    333 arscp_copy(int ifd, int ofd) 
    334 { 
    335         struct stat              sb; 
    336         char                    *buf, *p; 
    337         ssize_t                  w; 
    338         size_t                   bytes; 
    339  
    340         if (fstat(ifd, &sb) < 0) { 
    341                 bsdar_warnc(bsdar, errno, "fstate failed"); 
    342                 return (1); 
    343         } 
    344         if ((p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, 
    345             (off_t)0)) == MAP_FAILED) { 
    346                 bsdar_warnc(bsdar, errno, "mmap failed"); 
    347                 return (1); 
    348         } 
    349         for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) { 
    350                 w = write(ofd, buf, bytes); 
    351                 if (w <= 0) { 
    352                         bsdar_warnc(bsdar, errno, "write failed"); 
    353                         break; 
    354                 } 
    355         } 
    356         if (munmap(p, sb.st_size) < 0) 
    357                 bsdar_errc(bsdar, errno, "munmap failed"); 
    358         if (bytes > 0) 
    359                 return (1); 
    360  
    361         return (0); 
    362329} 
    363330