]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/cats/sql_create.c
Implement restore files
[bacula/bacula] / bacula / src / cats / sql_create.c
index 55b7eb13a571c06e6fbc568758a5607b84b1282d..1406ca4dfbae2874222fafa0ca7dfa836d3804df 100644 (file)
@@ -114,8 +114,10 @@ int
 db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
 {
    int stat;
+   int count;
 
    db_lock(mdb);
+#ifdef not_used_in_new_code
    Mmsg(&mdb->cmd, "SELECT JobId, MediaId FROM JobMedia WHERE \
 JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
 
@@ -131,14 +133,23 @@ JobId=%d AND MediaId=%d", jm->JobId, jm->MediaId);
       }
       sql_free_result(mdb);
    }
+#endif
+
+   /* Now get count for VolIndex */
+   Mmsg(&mdb->cmd, "SELECT count(*) from JobMedia");
+   count = get_sql_record_max(jcr, mdb);
+   if (count < 0) {
+      count = 0;
+   }
+   count++;
 
    /* Must create it */
    Mmsg(&mdb->cmd, 
 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,\
-StartFile,EndFile,StartBlock,EndBlock) \
-VALUES (%u,%u,%u,%u,%u,%u,%u,%u)", 
+StartFile,EndFile,StartBlock,EndBlock,VolIndex) \
+VALUES (%u,%u,%u,%u,%u,%u,%u,%u,%u)", 
        jm->JobId, jm->MediaId, jm->FirstIndex, jm->LastIndex,
-       jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock);
+       jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count);
 
    Dmsg0(30, mdb->cmd);
    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
@@ -350,6 +361,44 @@ FileRetention, JobRetention) VALUES \
 }
 
 
+/* 
+ * Create a Unique record for the counter -- no duplicates 
+ * Returns: 0 on failure
+ *         1 on success with counter filled in
+ */
+int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
+{
+   COUNTER_DBR mcr;
+   int stat;
+
+   db_lock(mdb);
+   memset(&mcr, 0, sizeof(mcr));
+   bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
+   if (db_get_counter_record(jcr, mdb, &mcr)) {
+      memcpy(cr, &mcr, sizeof(COUNTER_DBR));
+      db_unlock(mdb);
+      return 1;
+   }
+
+   /* Must create it */
+   Mmsg(&mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
+      "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
+      cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
+      cr->WrapCounter);
+
+   if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
+      Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
+           mdb->cmd, sql_strerror(mdb));
+      Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
+      stat = 0;
+   } else {
+      stat = 1;
+   }
+   db_unlock(mdb);
+   return stat;
+}
+
+
 /* 
  * Create a FileSet record. This record is unique in the
  *  name and the MD5 signature of the include/exclude sets.
@@ -360,10 +409,12 @@ int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
 {
    SQL_ROW row;
    int stat;
+   struct tm tm;
 
    db_lock(mdb);
-   Mmsg(&mdb->cmd, "SELECT FileSetId FROM FileSet WHERE \
-FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
+   fsr->created = false;
+   Mmsg(&mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
+"FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
 
    fsr->FileSetId = 0;
    if (QUERY_DB(jcr, mdb, mdb->cmd)) {
@@ -383,6 +434,11 @@ FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
            return 0;
         }
         fsr->FileSetId = atoi(row[0]);
+        if (row[1] == NULL) {
+           fsr->cCreateTime[0] = 0;
+        } else {
+           bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
+        }
         sql_free_result(mdb);
         db_unlock(mdb);
         return 1;
@@ -390,9 +446,15 @@ FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
       sql_free_result(mdb);
    }
 
+   if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
+      fsr->CreateTime = time(NULL);
+   }
+   localtime_r(&fsr->CreateTime, &tm);
+   strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
+
    /* Must create it */
-   Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet, MD5) VALUES \
-('%s', '%s')", fsr->FileSet, fsr->MD5);
+      Mmsg(&mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
+"VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
 
    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
       Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
@@ -402,6 +464,7 @@ FileSet='%s' and MD5='%s'", fsr->FileSet, fsr->MD5);
       stat = 0;
    } else {
       fsr->FileSetId = sql_insert_id(mdb);
+      fsr->created = true;
       stat = 1;
    }
 
@@ -448,7 +511,8 @@ int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
    /*
     * Make sure we have an acceptable attributes record.
     */
-   if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || ar->Stream == STREAM_WIN32_ATTRIBUTES)) {
+   if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES || 
+        ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
       Mmsg0(&mdb->errmsg, _("Attempt to put non-attributes into catalog\n"));
       Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
       return 0;
@@ -497,8 +561,8 @@ static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
 
    /* Must create it */
    Mmsg(&mdb->cmd,
-"INSERT INTO File (FileIndex, JobId, PathId, FilenameId, \
-LStat, MD5) VALUES (%u, %u, %u, %u, '%s', '0')", 
+"INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
+"LStat,MD5) VALUES (%u,%u,%u,%u,'%s','0')", 
       ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId, 
       ar->attr);
 
@@ -565,7 +629,7 @@ static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
       sql_free_result(mdb);
    }
 
-   Mmsg(&mdb->cmd, "INSERT INTO Path (Path)  VALUES ('%s')", mdb->esc_name);
+   Mmsg(&mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
 
    if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
       Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),