From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 872C1742C8
 for <pbs-devel@lists.proxmox.com>; Mon, 21 Jun 2021 10:18:24 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 75D3E1979D
 for <pbs-devel@lists.proxmox.com>; Mon, 21 Jun 2021 10:18:24 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id B8D321978B
 for <pbs-devel@lists.proxmox.com>; Mon, 21 Jun 2021 10:18:23 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 8B6FB40812
 for <pbs-devel@lists.proxmox.com>; Mon, 21 Jun 2021 10:18:23 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon, 21 Jun 2021 10:18:22 +0200
Message-Id: <20210621081822.9628-2-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210621081822.9628-1-d.csapak@proxmox.com>
References: <20210621081822.9628-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.862 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH proxmox-backup v2 2/2] api2/tape/restore: use
 file offset to compensate for some tape drives
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 21 Jun 2021 08:18:24 -0000

by calculating the offset after moving to a file and add the offset
to the drive

assuming the offset is static (e.g. always 1), we only have to do
that for the first file, all others should be correct then

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* rename to locate_offset

 src/api2/tape/restore.rs | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/src/api2/tape/restore.rs b/src/api2/tape/restore.rs
index 14e20ee4..fcaa4428 100644
--- a/src/api2/tape/restore.rs
+++ b/src/api2/tape/restore.rs
@@ -685,6 +685,32 @@ fn get_media_set_catalog(
     Ok(catalog)
 }
 
+fn compensate_locate_offset(
+    worker: &WorkerTask,
+    target_file: u64,
+    real_file: u64,
+    drive: &mut Box<dyn TapeDriver>,
+) -> Result<(), Error> {
+    task_log!(worker, "landed on wrong file, adding offset and try again");
+    let offset: i64 =
+        i64::try_from((target_file as i128) - (real_file as i128)).map_err(|err| {
+            format_err!(
+                "offset between {} and {} invalid: {}",
+                target_file,
+                real_file,
+                err
+            )
+        })?;
+    drive.set_locate_offset(offset);
+    drive.move_to_file(target_file)?;
+    let current_file = drive.current_file_number()?;
+    if current_file != target_file {
+        bail!("Compensating locate_file with offset did not work, aborting...");
+    }
+
+    Ok(())
+}
+
 fn restore_snapshots_to_tmpdir(
     worker: Arc<WorkerTask>,
     path: &PathBuf,
@@ -727,6 +753,10 @@ fn restore_snapshots_to_tmpdir(
             drive.move_to_file(*file_num)?;
             let current_file_number = drive.current_file_number()?;
             task_log!(worker, "now at file {}", current_file_number);
+            if current_file_number != *file_num {
+                compensate_locate_offset(&worker, *file_num, current_file_number, &mut drive)?;
+                task_log!(worker, "now at file {}", current_file_number);
+            }
         }
         let mut reader = drive.read_next_file()?;
 
@@ -806,6 +836,10 @@ fn restore_file_chunk_map(
             drive.move_to_file(*nr)?;
             let current_file_number = drive.current_file_number()?;
             task_log!(worker, "now at file {}", current_file_number);
+            if current_file_number != *nr {
+                compensate_locate_offset(&worker, *nr, current_file_number, drive)?;
+                task_log!(worker, "now at file {}", current_file_number);
+            }
         }
         let mut reader = drive.read_next_file()?;
         let header: MediaContentHeader = unsafe { reader.read_le_value()? };
-- 
2.20.1