Avoid format truncation warnings
[openafs.git] / src / volser / restorevol.c
index 1da076f..184951c 100644 (file)
@@ -77,8 +77,8 @@ readvalue(int size)
     ptr = (char *)&value;
 
     s = sizeof(value) - size;
-    if (size < 0) {
-       fprintf(stderr, "Too much data in afs_int32\n");
+    if (s < 0) {
+       fprintf(stderr, "Too much data for afs_int32\n");
        return 0;
     }
 
@@ -162,6 +162,11 @@ ReadDumpHeader(struct DumpHeader *dh)
 
        case 't':
            dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
+           if (dh->nDumpTimes > MAXDUMPTIMES) {
+               fprintf(stderr, "Too many dump times in header (%d > %d)\n",
+                       dh->nDumpTimes, MAXDUMPTIMES);
+               dh->nDumpTimes = MAXDUMPTIMES;
+           }
            for (i = 0; i < dh->nDumpTimes; i++) {
                dh->dumpTimes[i].from = ntohl(readvalue(4));
                dh->dumpTimes[i].to = ntohl(readvalue(4));
@@ -648,6 +653,7 @@ ReadVNode(afs_int32 count)
                int fid;
                int lfile;
                afs_sfsize_t size, s;
+               ssize_t count;
 
                /* Check if its vnode-file-link exists. If not,
                 * then the file will be an orphaned file.
@@ -671,14 +677,14 @@ ReadVNode(afs_int32 count)
 
                /* Write the file out */
                fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
+               if (fid < 0) {
+                   fprintf(stderr, "Open %s: Errno = %d\n", filename, errno);
+                   goto open_fail;
+               }
                size = vn.dataSize;
                while (size > 0) {
                    s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
                    code = fread(buf, 1, s, dumpfile);
-                   if (code > 0) {
-                       (void)write(fid, buf, code);
-                       size -= code;
-                   }
                    if (code != s) {
                        if (code < 0)
                            fprintf(stderr, "Code = %d; Errno = %d\n", code,
@@ -693,6 +699,20 @@ ReadVNode(afs_int32 count)
                        }
                        break;
                    }
+                   if (code > 0) {
+                       count = write(fid, buf, code);
+                       if (count < 0) {
+                           fprintf(stderr, "Count = %ld, Errno = %d\n",
+                                   (long)count, errno);
+                           break;
+                       } else if (count != code) {
+                           fprintf(stderr, "Wrote %llu bytes out of %llu\n",
+                                   (afs_uintmax_t) count,
+                                   (afs_uintmax_t) code);
+                           break;
+                       }
+                       size -= code;
+                   }
                }
                close(fid);
                if (size != 0) {
@@ -700,6 +720,7 @@ ReadVNode(afs_int32 count)
                            filename, fname);
                }
 
+open_fail:
                /* Remove the link to the file */
                if (lfile) {
                    unlink(filename);
@@ -720,7 +741,7 @@ ReadVNode(afs_int32 count)
                 */
                snprintf(linkname, sizeof linkname, "%s" OS_DIRSEP "%s%d",
                         parentdir, AFILE, vn.vnode);
-               len = readlink(linkname, fname, MAXNAMELEN);
+               len = readlink(linkname, fname, MAXNAMELEN - 1);
                if (len < 0) {
                    snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s%d",
                             rootdir, OFILE, vn.vnode);
@@ -779,8 +800,8 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
     afs_int32 type, count, vcount;
     DIR *dirP, *dirQ;
     struct dirent *dirE, *dirF;
-    char name[MAXNAMELEN];
-    char thisdir[MAXPATHLEN], *t;
+    char name[2*MAXNAMELEN+1];
+    char thisdir[MAXPATHLEN+4], *t;
     struct DumpHeader dh;      /* Defined in dump.h */
 #if 0/*ndef HAVE_GETCWD*/      /* XXX enable when autoconf happens */
     extern char *getwd();
@@ -955,7 +976,7 @@ main(int argc, char **argv)
 
     setlinebuf(stdout);
 
-    ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
+    ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, 0, "vldb check");
     cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
     cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
     cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");