r16230: Fix Klocwork #861 and others. localtime and asctime
[kai/samba.git] / source3 / client / clitar.c
index 9ddb92d3df0813067b4368da2762332b391f3d76..14c28acfc5a712302c8fe8f26c940bee9c9bd958 100644 (file)
@@ -523,6 +523,8 @@ static BOOL ensurepath(char *fname)
 
        if ((partpath == NULL) || (ffname == NULL)){
                DEBUG(0, ("Out of memory in ensurepath: %s\n", fname));
+               SAFE_FREE(partpath);
+               SAFE_FREE(ffname);
                return(False);
        }
 
@@ -559,15 +561,15 @@ static BOOL ensurepath(char *fname)
        return True;
 }
 
-static int padit(char *buf, int bufsize, int padsize)
+static int padit(char *buf, SMB_BIG_UINT bufsize, SMB_BIG_UINT padsize)
 {
        int berr= 0;
        int bytestowrite;
   
-       DEBUG(5, ("Padding with %d zeros\n", padsize));
-       memset(buf, 0, bufsize);
+       DEBUG(5, ("Padding with %0.f zeros\n", (double)padsize));
+       memset(buf, 0, (size_t)bufsize);
        while( !berr && padsize > 0 ) {
-               bytestowrite= MIN(bufsize, padsize);
+               bytestowrite= (int)MIN(bufsize, padsize);
                berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
                padsize -= bytestowrite;
        }
@@ -680,12 +682,11 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
                DEBUG(4, ("skipping %s - hidden bit is set\n", finfo.name));
                shallitime=0;
        } else {
+               BOOL wrote_tar_header = False;
+
                DEBUG(3,("getting file %s of size %.0f bytes as a tar file %s",
                        finfo.name, (double)finfo.size, lname));
       
-               /* write a tar header, don't bother with mode - just set to 100644 */
-               writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0", ftype);
-
                while (nread < finfo.size && !close_done) {
              
                        DEBUG(3,("nread=%.0f\n",(double)nread));
@@ -699,6 +700,13 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
              
                        nread += datalen;
 
+                       /* Only if the first read succeeds, write out the tar header. */
+                       if (!wrote_tar_header) {
+                               /* write a tar header, don't bother with mode - just set to 100644 */
+                               writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0", ftype);
+                               wrote_tar_header = True;
+                       }
+
                        /* if file size has increased since we made file size query, truncate
                                read so tar header for this file will be correct.
                        */
@@ -725,20 +733,25 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
                        datalen=0;
                }
 
-               /* pad tar file with zero's if we couldn't get entire file */
-               if (nread < finfo.size) {
-                       DEBUG(0, ("Didn't get entire file. size=%.0f, nread=%d\n",
-                                               (double)finfo.size, (int)nread));
-                       if (padit(data, sizeof(data), finfo.size - nread))
-                               DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
-               }
+               if (wrote_tar_header) {
+                       /* pad tar file with zero's if we couldn't get entire file */
+                       if (nread < finfo.size) {
+                               DEBUG(0, ("Didn't get entire file. size=%.0f, nread=%d\n",
+                                                       (double)finfo.size, (int)nread));
+                               if (padit(data, (SMB_BIG_UINT)sizeof(data), finfo.size - nread))
+                                       DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
+                       }
 
-               /* round tar file to nearest block */
-               if (finfo.size % TBLOCK)
-                       dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
+                       /* round tar file to nearest block */
+                       if (finfo.size % TBLOCK)
+                               dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
       
-               ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
-               ntarf++;
+                       ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
+                       ntarf++;
+               } else {
+                       DEBUG(4, ("skipping %s - initial read failed (file was locked ?)\n", finfo.name));
+                       shallitime=0;
+               }
        }
   
        cli_close(cli, fnum);
@@ -1110,6 +1123,7 @@ static void do_tarput(void)
                /* Get us to the next block, or the first block first time around */
                if (next_block(tarbuf, &buffer_p, tbufsiz) <= 0) {
                        DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
+                       SAFE_FREE(longfilename);
                        return;
                }
 
@@ -1181,6 +1195,7 @@ static void do_tarput(void)
                                }
                                break;
                        case 'L':
+                               SAFE_FREE(longfilename);
                                longfilename = get_longfilename(finfo);
                                if (!longfilename) {
                                        DEBUG(0, ("abandoning restore\n"));
@@ -1626,7 +1641,7 @@ int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
                                        if (sys_stat(argv[Optind], &stbuf) == 0) {
                                                newer_than = stbuf.st_mtime;
                                                DEBUG(1,("Getting files newer than %s",
-                                                       asctime(localtime(&newer_than))));
+                                                       time_to_asc(&newer_than)));
                                                newOptind++;
                                                Optind++;
                                        } else {
@@ -1720,6 +1735,7 @@ int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
 
                        if ((tmpstr = (char *)SMB_MALLOC(strlen(cliplist[clipcount])+1)) == NULL) {
                                DEBUG(0, ("Could not allocate space for a cliplist item, # %i\n", clipcount));
+                               SAFE_FREE(tmplist);
                                return 0;
                        }