- Document need to add "-u root" to most of MySQL script calls
   (./create_mys... ./make_my...).
 - Document c:/working directory better than /working directory.
+- Document all the status codes JobLevel, JobType, JobStatus.
 
           
 Testing to do: (painful)
 - Figure out how to use ssh or stunnel to protect Bacula communications.
 
 For 1.31 release:
+- Non-fatal errors are not correct counting attribs.c:277
+- In Win portable restore the directory is not create    
+   27-Jun-2003 16:52 tibs-fd: kernsrestore.2003-06-27_16.52.20 Error:
+   create_file.c:175 Could not create
+   /tmp/bacula-restores/cygwin/home/kern/bacula/k/src/dird/dird_conf.o: 0
+   ERR=The system cannot find the path specified.
 - Finish Windows implementation (add setting of correct type on restore,
   add Portable Data Format flag).
 - Remove multiple simultaneous devices code in SD.
 
 #define STREAM_SHA1_SIGNATURE    10    /* SHA1 signature for the file */
 #define STREAM_WIN32_DATA        11    /* Win32 BackupRead data */
 #define STREAM_WIN32_GZIP_DATA   12    /* Gzipped Win32 BackupRead data */
-#define STREAM_WIN32_ATTRIBUTES  13    /* Unix attribs, but WIN32_DATA follows */
 
 /* 
  *  File type (Bacula defined).           
 
 
    binit(&ff_pkt->bfd);
    if (ff_pkt->flags & FO_PORTABLE) {
-      set_win32_backup(&ff_pkt->bfd, 0); /* disable Win32 BackupRead() */
+      set_portable_backup(&ff_pkt->bfd); /* disable Win32 BackupRead() */
    }
 
    /* 
    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) && 
         ff_pkt->statp.st_size > 0) || 
         ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO ||
-        (is_win32_backup() && ff_pkt->type == FT_DIR)) {
+        (!is_portable_backup(&ff_pkt->bfd) && ff_pkt->type == FT_DIR)) {
       btimer_id tid;   
       if (ff_pkt->type == FT_FIFO) {
         tid = start_thread_timer(pthread_self(), 60);
 
         }  
         break;
 
-      /* Windows Backup data stream */
-      case STREAM_WIN32_DATA:  
-        if (!is_win32_backup()) {
-           if (!non_support_data) {
-               Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
-                 stream_to_ascii(stream));
-           }
-           extract = FALSE;
-           non_support_data++;
-           continue;
-        }
-        goto extract_data;
-
       /* Data stream */
       case STREAM_FILE_DATA:
       case STREAM_SPARSE_DATA: 
+      case STREAM_WIN32_DATA:  
 
-extract_data:
         if (extract) {
            if (stream == STREAM_SPARSE_DATA) {
               ser_declare;
         }
         break;
 
-      /* Windows Backup GZIP data stream */
-      case STREAM_WIN32_GZIP_DATA:  
-        if (!is_win32_backup()) {
-           if (!non_support_attr) {
-               Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
-                 stream_to_ascii(stream));
-           }
-           extract = FALSE;
-           non_support_attr++;
-           continue;
-        }
-        /* Fall through desired */
-
       /* GZIP data stream */
       case STREAM_GZIP_DATA:
       case STREAM_SPARSE_GZIP_DATA:  
+      case STREAM_WIN32_GZIP_DATA:  
 #ifdef HAVE_LIBZ
         if (extract) {
            ser_declare;
 
 #ifdef HAVE_CYGWIN
 
 /* Forward referenced subroutines */
-static
-int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd);
+static int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd);
 void unix_name_to_win32(POOLMEM **win32_name, char *name);
 void win_error(JCR *jcr, char *prefix, POOLMEM *ofile);
 HANDLE bget_handle(BFILE *bfd);
    int stream;
 
    /* Note, no sparse option for win32_data */
-   if (is_win32_backup()) {
+   if (!is_portable_backup(&ff_pkt->bfd)) {
       stream = STREAM_WIN32_DATA;
       ff_pkt->flags &= ~FO_SPARSE;
    } else if (ff_pkt->flags & FO_SPARSE) {
    int stat = 1;
 
 #ifdef HAVE_CYGWIN
+   if (attr->data_stream == STREAM_WIN32_DATA ||
+       attr->data_stream == STREAM_WIN32_GZIP_DATA) {
+      if (is_bopen(ofd)) {
+        bclose(ofd); 
+      }
+      return 1;
+   }
+
    if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
        set_win32_attributes(jcr, attr, ofd)) {
+      if (is_bopen(ofd)) {
+        bclose(ofd); 
+      }
       return 1;
    }
    /*
    if (attr->type == FT_LNK) {
       /* Change owner of link, not of real file */
       if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
-         Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
+         Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
            attr->ofname, strerror(errno));
         stat = 0;
       }
    } else {
       if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
-         Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
+         Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
            attr->ofname, strerror(errno));
         stat = 0;
       }
       if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
-         Jmsg2(jcr, M_WARNING, 0, _("Unable to set file modes %s: ERR=%s\n"),
+         Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
            attr->ofname, strerror(errno));
         stat = 0;
       }
       /* FreeBSD user flags */
 #ifdef HAVE_CHFLAGS
       if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
-         Jmsg2(jcr, M_WARNING, 0, _("Unable to set file flags %s: ERR=%s\n"),
+         Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
            attr->ofname, strerror(errno));
         stat = 0;
       }
 
    attribsEx[0] = 0;                 /* no extended attributes */
 
-   if (!p_GetFileAttributesEx) {
+   if (!p_GetFileAttributesEx) {                                
       return STREAM_UNIX_ATTRIBUTES;
    }
 
  * Returns:  1 on success
  *          0 on failure
  */
-static
-int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
+static int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
 {
    char *p = attr->attrEx;
    int64_t val;
 
    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
 
-
    if (!is_bopen(ofd)) {
       Dmsg1(100, "File not open: %s\n", attr->ofname);
       bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0);  /* attempt to open the file */
 
  *
  * ===============================================================
  */
+
+int is_win32_stream(int stream)
+{
+   switch (stream) {
+   case STREAM_WIN32_DATA:
+   case STREAM_WIN32_GZIP_DATA:
+      return 1;
+   }
+   return 0;
+}
+
 char *stream_to_ascii(int stream)
 {
    static char buf[20];
       return "GZIP data";
    case STREAM_SPARSE_GZIP_DATA:
       return "GZIP sparse data";
-   case STREAM_WIN32_ATTRIBUTES:
       return "Win32 attributes";
    case STREAM_WIN32_DATA:
       return "Win32 data";
 extern "C" HANDLE get_osfhandle(int fd);
 
 
+
 void binit(BFILE *bfd)
 {
    bfd->fid = -1;
    bfd->mode = BF_CLOSED;
-   bfd->use_backup_api = p_BackupRead && p_BackupWrite;
+   bfd->use_backup_api = have_win32_api();
    bfd->errmsg = NULL;
    bfd->lpContext = NULL;
    bfd->lerror = 0;
 }
 
 /*
- * Enables/disables using the Backup API (win32_data).
+ * Enables using the Backup API (win32_data).
  *   Returns 1 if function worked
  *   Returns 0 if failed (i.e. do not have Backup API on this machine)
  */
-int set_win32_backup(BFILE *bfd, int enable)
+int set_win32_backup(BFILE *bfd) 
 {
-   if (!enable) {
-      bfd->use_backup_api = 0;
-      return 1;
-   }
    /* We enable if possible here */
-   bfd->use_backup_api = p_BackupRead && p_BackupWrite;
+   bfd->use_backup_api = have_win32_api();
    return bfd->use_backup_api;
 }
 
+
+int set_portable_backup(BFILE *bfd)
+{
+   bfd->use_backup_api = 0;
+   return 1;
+}
+
 /*
- * Return 1 if we can do Win32 backup
- * return 0 if not
+ * Return 1 if we are NOT using Win32 BackupWrite() 
+ * return 0 if are
  */
-int is_win32_backup(void)
+int is_portable_backup(BFILE *bfd) 
+{
+   return !bfd->use_backup_api;
+}
+
+int have_win32_api()
 {
    return p_BackupRead && p_BackupWrite;
 }
 
+
+
 /*
  * Return 1 if we support the stream
  *       0 if we do not support the stream
    case STREAM_SPARSE_GZIP_DATA:
       return 0;
 #endif
-   case STREAM_WIN32_ATTRIBUTES:
    case STREAM_WIN32_DATA:
    case STREAM_WIN32_GZIP_DATA:
-      return is_win32_backup();   /* check if we support BackupRead/Write data */
+      return have_win32_api();
 
    /* Known streams */
 #ifdef HAVE_LIBZ
 
    } else if (flags & O_WRONLY) {     /* Open existing for write */
       if (bfd->use_backup_api) {
-        dwaccess = FILE_ALL_ACCESS|WRITE_OWNER|WRITE_DAC|ACCESS_SYSTEM_SECURITY;
+        dwaccess = GENERIC_WRITE|FILE_ALL_ACCESS|WRITE_OWNER|WRITE_DAC|ACCESS_SYSTEM_SECURITY;
         dwflags = FILE_FLAG_BACKUP_SEMANTICS;
       } else {
         dwaccess = GENERIC_WRITE;
    bfd->fid = -1;
 }
 
-int set_win32_backup(BFILE *bfd, int enable)
+int have_win32_api()
+{ 
+   return 0;                         /* no can do */
+} 
+
+/*
+ * Enables using the Backup API (win32_data).
+ *   Returns 1 if function worked
+ *   Returns 0 if failed (i.e. do not have Backup API on this machine)
+ */
+int set_win32_backup(BFILE *bfd) 
+{
+   return 0;                         /* no can do */
+}
+
+
+int set_portable_backup(BFILE *bfd)
 {
-   return !enable;
+   return 1;                         /* no problem */
 }
 
-int is_win32_backup(void)
+/*
+ * Return 1 if we are writing in portable format
+ * return 0 if not
+ */
+int is_portable_backup(BFILE *bfd) 
 {
-   return 0;
+   return 1;                         /* portable by definition */
 }
 
 int is_stream_supported(int stream)
    case STREAM_GZIP_DATA:
    case STREAM_SPARSE_GZIP_DATA:
 #endif
-   case STREAM_WIN32_ATTRIBUTES:
    case STREAM_WIN32_DATA:
    case STREAM_WIN32_GZIP_DATA:
       return 0;
 
 
 void   binit(BFILE *bfd);
 int    is_bopen(BFILE *bfd);
-int    set_win32_backup(BFILE *bfd, int enable);
-int    is_win32_backup();
+int    set_win32_backup(BFILE *bfd);
+int    set_portable_backup(BFILE *bfd);
+int    have_win32_api();
+int    is_portable_backup(BFILE *bfd);
 int    is_stream_supported(int stream);
+int    is_win32_stream(int stream);
 char   *berror(BFILE *bfd);
 int    bopen(BFILE *bfd, const char *fname, int flags, mode_t mode);
 int    bclose(BFILE *bfd);
 
  *     files.
  *
  */
-int create_file(JCR *jcr, ATTR *attr, BFILE *ofd, int replace)
+int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
 {
    int new_mode, parent_mode, mode;
    uid_t uid;
    bool exists = false;
    struct stat mstatp;
 
-   binit(ofd);
-   /* Set desired writing mode (BackupWrite() or write()) */
-   switch (attr->data_stream) {
-   case 0:
-      break;                         /* use default defined by system */
-
-   /* These streams require using BackupWrite() */
-   case STREAM_WIN32_ATTRIBUTES:
-   case STREAM_WIN32_DATA:
-   case STREAM_WIN32_GZIP_DATA:
-      if (!set_win32_backup(ofd, 1)) { /* use BackupWrite() */
-         Jmsg(jcr, M_ERROR, 0, _("Could not set Win32 output format.\n"));
-        return CF_ERROR;
-      }
-
-   /* All other stream use standard system I/O (portable) */
-   default:
-      set_win32_backup(ofd, 0);       /* Disable using BackupWrite() */
-      break;
+   if (is_win32_stream(attr->data_stream)) { 
+      set_win32_backup(bfd);
+   } else {
+      set_portable_backup(bfd);
    }
 
    new_mode = attr->statp.st_mode;
             * exists. Normally, this should do nothing.
             */
            if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
-               Dmsg1(0, "Could not make path. %s\n", attr->ofname);
+               Dmsg1(10, "Could not make path. %s\n", attr->ofname);
+              attr->ofname[pnl] = savechr;     /* restore full name */
               return CF_ERROR;
            }
         }
            mode |= O_CTG;               /* set contiguous bit if needed */
         }
          Dmsg1(50, "Create file: %s\n", attr->ofname);
-        if ((bopen(ofd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
-            Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), 
-                 attr->ofname, berror(ofd));
+        if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
+            Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: %d ERR=%s\n"), 
+                 attr->ofname, berror(bfd));
+
            return CF_ERROR;
         }
         return CF_EXTRACT;
            } else {
               tid = NULL;
            }
-           if ((bopen(ofd, attr->ofname, mode, 0)) < 0) {
+           if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
                Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), 
-                    attr->ofname, berror(ofd));
+                    attr->ofname, berror(bfd));
               stop_thread_timer(tid);
               return CF_ERROR;
            }
        *   directory so that the security info will be read
        *   and saved.
        */
-      if (is_win32_backup()) {
-        if ((bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
+      if (!is_portable_backup(bfd)) {
+        if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
             Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), 
-                 attr->ofname, berror(ofd));
+                 attr->ofname, berror(bfd));
            return CF_ERROR;
         }
         return CF_EXTRACT;
 
       dev_t our_device = ff_pkt->statp.st_dev;
 
       /*  
-       * If we are using Win32 backup API, don't check
+       * If we are using Win32 (non-portable) backup API, don't check
        *  access as everything is more complicated, and
        *  in principle, we should be able to access everything.
        */
-      if (!is_win32_backup()) {
+      if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
         if (access(fname, R_OK) == -1 && geteuid() != 0) {
            /* Could not access() directory */
            ff_pkt->type = FT_NOACCESS;
 
         }  
       }
 
-   /* Windows Backup data stream */
-   case STREAM_WIN32_DATA:  
-      if (!is_win32_backup()) {
-        if (!non_support_data) {
-            Jmsg(jcr, M_ERROR, 0, _("Win32 backup data not supported on this Client.\n"));
-        }
-        extract = FALSE;
-        non_support_data++;
-        return;
-      }
-      goto extract_data;
-   
-
    /* Data stream and extracting */
    case STREAM_FILE_DATA:
    case STREAM_SPARSE_DATA:
+   case STREAM_WIN32_DATA:  
 
-extract_data:
       if (extract) {
         if (rec->Stream == STREAM_SPARSE_DATA) {
            ser_declare;
         fileAddr += wsize;
       }
 
-   /* Windows Backup GZIP data stream */
-   case STREAM_WIN32_GZIP_DATA:  
-      if (!is_win32_backup()) {
-        if (!non_support_attr) {
-            Jmsg(jcr, M_ERROR, 0, _("Win32 GZIP backup data not supported on this Client.\n"));
-        }
-        extract = FALSE;
-        non_support_attr++;
-        return;
-      }
-      /* Fall through desired */
 
    /* GZIP data stream */
    case STREAM_GZIP_DATA:
    case STREAM_SPARSE_GZIP_DATA: 
+   case STREAM_WIN32_GZIP_DATA:  
 #ifdef HAVE_LIBZ
       if (extract) {
         uLongf compress_len;