/*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    int32_t n = 0;
    BSOCK *fd = jcr->file_bsock;
    bool fd_ok = false;
-   uint32_t JobFiles, Errors;
+   uint32_t JobFiles, JobErrors;
    uint64_t ReadBytes = 0;
    uint64_t JobBytes = 0;
    int VSS = 0;
       while ((n = bget_dirmsg(fd)) >= 0) {
          if (!fd_ok && 
              (sscanf(fd->msg, EndJob, &jcr->FDJobStatus, &JobFiles,
-                     &ReadBytes, &JobBytes, &Errors, &VSS, &Encrypt) == 7 ||
+                     &ReadBytes, &JobBytes, &JobErrors, &VSS, &Encrypt) == 7 ||
               sscanf(fd->msg, OldEndJob, &jcr->FDJobStatus, &JobFiles,
-                     &ReadBytes, &JobBytes, &Errors) == 5)) {
+                     &ReadBytes, &JobBytes, &JobErrors) == 5)) {
             fd_ok = true;
             set_jcr_job_status(jcr, jcr->FDJobStatus);
             Dmsg1(100, "FDStatus=%c\n", (char)jcr->JobStatus);
       cancel_storage_daemon_job(jcr);
    }
 
-   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */
+   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/JobErrors */
    wait_for_storage_daemon_termination(jcr);
 
    /* Return values from FD */
    if (fd_ok) {
       jcr->JobFiles = JobFiles;
-      jcr->Errors = Errors;
+      jcr->JobErrors += JobErrors;       /* Keep total errors */
       jcr->ReadBytes = ReadBytes;
       jcr->JobBytes = JobBytes;
       jcr->VSS = VSS;
 
    switch (jcr->JobStatus) {
       case JS_Terminated:
-         if (jcr->Errors || jcr->SDErrors) {
+         if (jcr->JobErrors || jcr->SDErrors) {
             term_msg = _("Backup OK -- with warnings");
          } else {
             term_msg = _("Backup OK");
         jcr->VolSessionTime,
         edit_uint64_with_commas(mr.VolBytes, ec7),
         edit_uint64_with_suffix(mr.VolBytes, ec8),
-        jcr->Errors,
+        jcr->JobErrors,
         jcr->SDErrors,
         fd_term_msg,
         sd_term_msg,
 
    jcr->jr.ReadBytes = jcr->ReadBytes;
    jcr->jr.VolSessionId = jcr->VolSessionId;
    jcr->jr.VolSessionTime = jcr->VolSessionTime;
-   jcr->jr.JobErrors = jcr->Errors;
+   jcr->jr.JobErrors = jcr->JobErrors;
    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
          db_strerror(jcr->db));
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2004-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    set_jcr_job_status(mig_jcr, JS_Running);
 
    /* Pickup Job termination data */
-   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */
+   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/JobErrors */
    wait_for_storage_daemon_termination(jcr);
    set_jcr_job_status(jcr, jcr->SDJobStatus);
    db_write_batch_file_records(jcr);    /* used by bulk batch file insert */
 
       switch (jcr->JobStatus) {
       case JS_Terminated:
-         if (jcr->Errors || jcr->SDErrors) {
+         if (jcr->JobErrors || jcr->SDErrors) {
             term_msg = _("%s OK -- with warnings");
          } else {
             term_msg = _("%s OK");
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
         edit_uint64_with_commas((uint64_t)jcr->jr.JobFiles, ec2),
         edit_uint64_with_commas(jcr->jr.JobBytes, ec3),
         (float)kbps,
-        jcr->Errors,
+        jcr->JobErrors,
         fd_term_msg,
         sd_term_msg,
         term_msg);
 
    UAContext *ua = (UAContext *)ctx;
    POOL_MEM rows(PM_MESSAGE);
 
+   /* Check for nonsense */
+   if (num_field == 0 || row == NULL || row[0] == NULL) {
+      return 0;                       /* nothing returned */
+   }
    for (int i=0; num_field--; i++) {
       if (i == 0) {
          pm_strcpy(rows, NPRT(row[0]));
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2008-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2008-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    set_jcr_job_status(jcr, JS_Running);
 
    /* Pickup Job termination data */
-   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */
+   /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/JobErrors */
    wait_for_storage_daemon_termination(jcr);
    set_jcr_job_status(jcr, jcr->SDJobStatus);
    db_write_batch_file_records(jcr);    /* used by bulk batch file insert */
 
    switch (jcr->JobStatus) {
       case JS_Terminated:
-         if (jcr->Errors || jcr->SDErrors) {
+         if (jcr->JobErrors || jcr->SDErrors) {
             term_msg = _("Backup OK -- with warnings");
          } else {
             term_msg = _("Backup OK");
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
            edt,
            edit_uint64_with_commas(jcr->ExpectedFiles, ec1),
            edit_uint64_with_commas(jcr->JobFiles, ec2),
-           jcr->Errors,
+           jcr->JobErrors,
            fd_term_msg,
            sd_term_msg,
            term_msg);
            sdt,
            edt,
            edit_uint64_with_commas(jcr->JobFiles, ec1),
-           jcr->Errors,
+           jcr->JobErrors,
            fd_term_msg,
            term_msg);
    }
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
       berrno be;
       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not access \"%s\": ERR=%s\n"), ff_pkt->fname,
          be.bstrerror(ff_pkt->ff_errno));
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_NOFOLLOW: {
       berrno be;
       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not follow link \"%s\": ERR=%s\n"), 
            ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_NOSTAT: {
       berrno be;
       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not stat \"%s\": ERR=%s\n"), ff_pkt->fname,
          be.bstrerror(ff_pkt->ff_errno));
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_DIRNOCHG:
       berrno be;
       Jmsg(jcr, M_NOTSAVED, 0, _("     Could not open directory \"%s\": ERR=%s\n"), 
            ff_pkt->fname, be.bstrerror(ff_pkt->ff_errno));
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    default:
       Jmsg(jcr, M_NOTSAVED, 0,  _("     Unknown file type %d; not saved: %s\n"), 
            ff_pkt->type, ff_pkt->fname);
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
 
          if (signing_digest == NULL) {
             Jmsg(jcr, M_NOTSAVED, 0, _("%s signature digest initialization failed\n"),
                stream_to_ascii(signing_algorithm));
-            jcr->Errors++;
+            jcr->JobErrors++;
             goto good_rtn;
          }
       }
          berrno be;
          Jmsg(jcr, M_NOTSAVED, 0, _("     Cannot open \"%s\": ERR=%s.\n"), ff_pkt->fname,
               be.bstrerror());
-         jcr->Errors++;
+         jcr->JobErrors++;
          if (tid) {
             stop_thread_timer(tid);
             tid = NULL;
             berrno be;
             Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open resource fork for \"%s\": ERR=%s.\n"), 
                  ff_pkt->fname, be.bstrerror());
-            jcr->Errors++;
+            jcr->JobErrors++;
             if (is_bopen(&ff_pkt->bfd)) {
                bclose(&ff_pkt->bfd);
             }
       berrno be;
       Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
          ff_pkt->fname, be.bstrerror(ff_pkt->bfd.berrno));
-      if (jcr->Errors++ > 1000) {       /* insanity check */
+      if (jcr->JobErrors++ > 1000) {       /* insanity check */
          Jmsg(jcr, M_FATAL, 0, _("Too many errors.\n"));
       }
    } else if (ff_pkt->flags & FO_ENCRYPT) {
 
       /* Send termination status back to Dir */
       dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
                  edit_uint64(jcr->ReadBytes, ed1),
-                 edit_uint64(jcr->JobBytes, ed2), jcr->Errors, jcr->VSS,
+                 edit_uint64(jcr->JobBytes, ed2), jcr->JobErrors, jcr->VSS,
                  jcr->crypto.pki_encrypt);
       Dmsg1(110, "End FD msg: %s\n", dir->msg);
    }
             Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
             if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {               
                Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
-               jcr->Errors++;
+               jcr->JobErrors++;
             } else {
                /* tell user if snapshot creation of a specific drive failed */
                int i;
                for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
                   if (islower(szWinDriveLetters[i])) {
                      Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
-                     jcr->Errors++;
+                     jcr->JobErrors++;
                   }
                }
                /* inform user about writer states */
                for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)                
                   if (g_pVSSClient->GetWriterState(i) < 1) {
                      Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));                    
-                     jcr->Errors++;
+                     jcr->JobErrors++;
                   }                            
             }
         } else {
             int msg_type = M_INFO;
             if (g_pVSSClient->GetWriterState(i) < 1) {
                msg_type = M_WARNING;
-               jcr->Errors++;
+               jcr->JobErrors++;
             }
             Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
          }
 
 bail_out:
 
-   if (jcr->Errors) {
+   if (jcr->JobErrors) {
       set_jcr_job_status(jcr, JS_ErrorTerminated);
    }
 
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2001-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2001-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
       berrno be;
       be.set_errno(ff_pkt->ff_errno);
       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, be.bstrerror());
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_NOFOLLOW: {
       berrno be;
       be.set_errno(ff_pkt->ff_errno);
       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, be.bstrerror());
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_NOSTAT: {
       berrno be;
       be.set_errno(ff_pkt->ff_errno);
       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, be.bstrerror());
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    case FT_DIRNOCHG:
       berrno be;
       be.set_errno(ff_pkt->ff_errno);
       Jmsg(jcr, M_NOTSAVED, 1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, be.bstrerror());
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
    default:
       Jmsg(jcr, M_NOTSAVED, 0, _("     Unknown file type %d: %s\n"), ff_pkt->type, ff_pkt->fname);
-      jcr->Errors++;
+      jcr->JobErrors++;
       return 1;
    }
 
          size = sizeof(md);
          
          if (digest_file(jcr, ff_pkt, digest) != 0) {
-            jcr->Errors++;
+            jcr->JobErrors++;
             goto good_rtn;
          }
 
       Dmsg2(100, "Error reading file %s: ERR=%s\n", jcr->last_fname, be.bstrerror());
       Jmsg(jcr, M_ERROR, 1, _("Error reading file %s: ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
-      jcr->Errors++;
+      jcr->JobErrors++;
       return -1;
    }
    return 0;
 
 {
    int count = 0;
    int32_t xattr_list_len,
-           xattr_value_len,
-           expected_serialize_len = 0;
+           xattr_value_len;
+   uint32_t expected_serialize_len = 0;
    char *xattr_list, *bp;
    xattr_t *xattr_value_list, *current_xattr;
 
 
    uint32_t VolSessionId;
    uint32_t VolSessionTime;
    uint32_t JobFiles;                 /* Number of files written, this job */
-   uint32_t JobErrors;                /* */
+   uint32_t JobErrors;                /* Number of non-fatal errors this job */
    uint64_t JobBytes;                 /* Number of bytes processed this job */
    uint64_t ReadBytes;                /* Bytes read -- before compression */
    FileId_t FileId;                   /* Last FileId used */
-   uint32_t Errors;                   /* Number of non-fatal errors */
    volatile int32_t JobStatus;        /* ready, running, blocked, terminated */
    int32_t JobPriority;               /* Job priority */
    time_t sched_time;                 /* job schedule time, i.e. when it should start */
 
          num_jobs_run++;
          je = (struct s_last_job *)malloc(sizeof(struct s_last_job));
          memset(je, 0, sizeof(struct s_last_job));  /* zero in case unset fields */
-         je->Errors = jcr->Errors;
+         je->Errors = jcr->JobErrors;
          je->JobType = jcr->get_JobType();
          je->JobId = jcr->JobId;
          je->VolSessionId = jcr->VolSessionId;
 
     case M_ERROR:
        len = bsnprintf(rbuf, sizeof(rbuf), _("%s JobId %u: Error: "), my_name, JobId);
        if (jcr) {
-          jcr->Errors++;
+          jcr->JobErrors++;
        }
        break;
     case M_WARNING:
 
 ECHO_CMD=@
 
-DIRS=  dll \
+DIRS=  lib \
        cats \
        filed \
        filed/plugins \
 
 a function name has been added, changed, or an argument modified,    
 simply do the following:
 
-   cd .../bacula/src/win32/dll 
+   cd .../bacula/src/win32/lib
    make                  (to build the .o files, note the link will fail)
    ./make_def >bacula.def
 
 Exporting a function requires a bit more work.  You must determine the
 C++ mangled name of the new function.
 
-   strings .../bacula/src/win32/dll/<file>.o | grep <symbol>
+   strings .../bacula/src/win32/lib/<file>.o | grep <symbol>
 
 Note, strings often will not show the desired symbol. In that case,
 use:
    
-   nm .../bacula/src/win32/dll/<file>.o
+   nm .../bacula/src/win32/lib/<file>.o
 
 Replace <file> with the base part of the name of the source code file
 which contains the new function.  Replace <symbol> with the name of
 the new function.  Remove the leading underscore and place the result
 in the file
 
-   .../bacula/src/win32/dll/bacula.def
+   .../bacula/src/win32/lib/bacula.def
 === end manual changing of bacula.def ==========
 
 If you add a new file, you will need to specify its name in
 
-   .../bacula/src/win32/dll/Makefile
+   .../bacula/src/win32/lib/Makefile
 and
    .../bacula/src/win32/libbac/Makefile
 
 Note, strings often will not show the desired symbol. In that case,
 use:
 
-   nm .../bacula/src/win32/dll/<file>.o
+   nm .../bacula/src/win32/lib/<file>.o
 
 Replace <file> with the base part of the name of the source code file
 which contains the new function.  Replace <symbol> with the name of
 
  \r
 ; sql_update.o\r
 _Z15db_update_statsP3JCRP4B_DBx\r
-_Z19db_mark_file_recordP3JCRP4B_DBjj\r
+_Z19db_mark_file_recordP3JCRP4B_DByj\r
 _Z21db_update_pool_recordP3JCRP4B_DBP8POOL_DBR\r
 _Z22db_update_media_recordP3JCRP4B_DBP9MEDIA_DBR\r
 _Z23db_update_client_recordP3JCRP4B_DBP10CLIENT_DBR\r
 _Z24db_update_media_defaultsP3JCRP4B_DBP9MEDIA_DBR\r
 _Z24db_update_storage_recordP3JCRP4B_DBP11STORAGE_DBR\r
 _Z26db_update_job_start_recordP3JCRP4B_DBP7JOB_DBR\r
-_Z28db_add_digest_to_file_recordP3JCRP4B_DBjPci\r
+_Z28db_add_digest_to_file_recordP3JCRP4B_DByPci\r
  \r
 _Z11db_get_typev\r
 _Z12db_sql_queryP4B_DBPKcPFiPviPPcES3_\r
 
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2004-2008 Free Software Foundation Europe e.V.
+   Copyright (C) 2004-2009 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
 
 #include <malloc.h>
 
+#ifndef INT64
+#define INT64 long long int
+#endif
+
 typedef UINT64 u_int64_t;
 typedef UINT64 uint64_t;
 typedef INT64 int64_t;
 typedef signed short int16_t;
 typedef signed char int8_t;
 typedef int __daddr_t;
+
 #if !defined(HAVE_MINGW)
 typedef long int32_t;
 typedef float float32_t;
 
                 break;
             case 's':
                 strvalue = va_arg(args, char *);
-                if (max < 0) 
+                if (max < 0) {
                     max = maxlen; /* ie, no max */
+                }
                 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max, outch);
                 break;
             case 'p':
-                strvalue = (char *) va_arg(args, void *);
                 flags |= DP_F_UNSIGNED;
-                fmtint(buffer, &currlen, maxlen, (INT64) strvalue, 16, min, max,
+                if (sizeof(char *) == 4) {
+                   value = va_arg(args, uint32_t);
+                } else if (sizeof(char *) == 8) {
+                   value = va_arg(args, uint64_t);
+                } else {
+                   value = 0;             /* we have a problem */
+                }
+                fmtint(buffer, &currlen, maxlen, value, 16, min, max,
                        flags, outch);
                 break;
             case 'n':
 
  
 ; attribs.o
 _Z11decode_statPcP4statPi
-_Z11encode_statPcP6FF_PKTi
+_Z11encode_statPcP4statii
 _Z13decode_LinkFIPcP4stat
 _Z14set_attributesP3JCRP4ATTRP5BFILE
 _Z16encode_attribsExP3JCRPcP6FF_PKT
 
 mixed priorities
 
 General:
+06Mar09
+kes  Move src/win32/dll to src/win32/lib, which is much more logical.
+kes  Fix the Win32 build.
+kes  Fix broken casting in src/compat/print.cpp.
+kes  Eliminate jcr Errors and always use jcr JobErrors.  This should
+     ensure that SD and FD errors are correctly reported.
 28Feb09
 mvw  Implemented xattr support for Solaris 9 and above and extensible
      attributes for OpenSolaris.