Imported Upstream version 3.3.2
[debian/amanda] / device-src / tape-device.c
index c6b88ced0a30549b4e10b4d042492975eb5d85d9..0c7fe243b34536c8852bb8f586c72f7b3ab040f2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 as published
@@ -578,6 +578,9 @@ tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
        /* looks good .. let's start the device over, though */
        device_clear_volume_details(p_self);
     } else {
+       device_set_error(p_self,
+           g_strdup("Error setting COMPRESION property"),
+           DEVICE_STATUS_DEVICE_ERROR);
        return FALSE;
     }
 
@@ -603,6 +606,9 @@ tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GN
     if (read_block_size != 0 &&
            ((gsize)read_block_size < p_self->block_size ||
             (gsize)read_block_size > p_self->max_block_size))
+       device_set_error(p_self,
+           g_strdup_printf("Error setting READ-BLOCk-SIZE property to '%u', it must be between %zu and %zu", read_block_size, p_self->block_size, p_self->max_block_size),
+           DEVICE_STATUS_DEVICE_ERROR);
        return FALSE;
 
     self->private->read_block_size = read_block_size;
@@ -919,6 +925,7 @@ static DeviceStatusFlags tape_device_read_label(Device * dself) {
        return dself->status;
     }
 
+    dself->header_block_size = buffer_len;
     header = dself->volume_header = g_new(dumpfile_t, 1);
     fh_init(header);
 
@@ -988,6 +995,9 @@ tape_device_write_block(Device * pself, guint size, gpointer data) {
     }
 
     pself->block++;
+    g_mutex_lock(pself->device_mutex);
+    pself->bytes_written += size;
+    g_mutex_unlock(pself->device_mutex);
 
     return TRUE;
 }
@@ -1018,6 +1028,9 @@ static int tape_device_read_block (Device * pself, gpointer buf,
     case RESULT_SUCCESS:
         *size_req = size;
         pself->block++;
+       g_mutex_lock(pself->device_mutex);
+       pself->bytes_read += size;
+       g_mutex_unlock(pself->device_mutex);
         return size;
     case RESULT_SMALL_BUFFER: {
         gsize new_size;
@@ -1052,7 +1065,9 @@ static int tape_device_read_block (Device * pself, gpointer buf,
     }
     case RESULT_NO_DATA:
         pself->is_eof = TRUE;
+       g_mutex_lock(pself->device_mutex);
        pself->in_file = FALSE;
+       g_mutex_unlock(pself->device_mutex);
        device_set_error(pself,
            stralloc(_("EOF")),
            DEVICE_STATUS_SUCCESS);
@@ -1111,6 +1126,7 @@ static gboolean write_tapestart_header(TapeDevice * self, char * label,
        return FALSE;
      }
 
+     d_self->header_block_size = d_self->block_size;
      amfree(header_buf);
 
      if (!tape_weof(self->fd, 1)) {
@@ -1152,7 +1168,9 @@ tape_device_start (Device * d_self, DeviceAccessMode mode, char * label,
     }
 
     d_self->access_mode = mode;
+    g_mutex_lock(d_self->device_mutex);
     d_self->in_file = FALSE;
+    g_mutex_unlock(d_self->device_mutex);
 
     if (IS_WRITABLE_ACCESS_MODE(mode)) {
         if (self->write_open_errno != 0) {
@@ -1264,10 +1282,13 @@ static gboolean tape_device_start_file(Device * d_self,
     amfree(amanda_header);
 
     /* arrange the file numbers correctly */
-    d_self->in_file = TRUE;
     d_self->block = 0;
     if (d_self->file >= 0)
         d_self->file ++;
+    g_mutex_lock(d_self->device_mutex);
+    d_self->in_file = TRUE;
+    d_self->bytes_written = 0;
+    g_mutex_unlock(d_self->device_mutex);
     return TRUE;
 }
 
@@ -1287,7 +1308,9 @@ tape_device_finish_file (Device * d_self) {
         return FALSE;
     }
 
+    g_mutex_lock(d_self->device_mutex);
     d_self->in_file = FALSE;
+    g_mutex_unlock(d_self->device_mutex);
     return TRUE;
 }
 
@@ -1315,9 +1338,12 @@ tape_device_seek_file (Device * d_self, guint file) {
         difference --;
     }
 
-    d_self->in_file = FALSE;
     d_self->is_eof = FALSE;
     d_self->block = 0;
+    g_mutex_lock(d_self->device_mutex);
+    d_self->in_file = FALSE;
+    d_self->bytes_read = 0;
+    g_mutex_unlock(d_self->device_mutex);
 
 reseek:
     if (difference > 0) {
@@ -1444,7 +1470,9 @@ reseek:
         return NULL;
     }
 
+    g_mutex_lock(d_self->device_mutex);
     d_self->in_file = TRUE;
+    g_mutex_unlock(d_self->device_mutex);
     d_self->file = file;
 
     return rval;
@@ -1538,9 +1566,14 @@ tape_device_finish (Device * d_self) {
     }
 
     /* Polish off this file, if relevant. */
+    g_mutex_lock(d_self->device_mutex);
     if (d_self->in_file && IS_WRITABLE_ACCESS_MODE(d_self->access_mode)) {
-        if (!device_finish_file(d_self))
+       g_mutex_unlock(d_self->device_mutex);
+        if (!device_finish_file(d_self)) {
            goto finish_error;
+       }
+    } else {
+       g_mutex_unlock(d_self->device_mutex);
     }
 
     /* Straighten out the filemarks.  We already wrote one in finish_file, and