Files
yatm/internal/executor/tape.go
T
Samuel CuiandClaude Opus 5.5 47b525b747 Write, read and publish encrypted tar Media
Compose tar FORMAT/APPEND, Restore, Scan and Verify through complete Media sessions and the configured encrypt adapter. Publish only complete members at normal or capacity/EW completion, maintain position maps, apply Logical or Auto read order, record passive read learning observations in each Job and refit learned read parameters after drive release. LTFS keeps its adapters and gains the shared read order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 00:01:44 +08:00

55 lines
1.6 KiB
Go

package executor
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"github.com/samuelncui/yatm/internal/tools"
"github.com/sirupsen/logrus"
)
// ReadTapeBarcode returns an empty value when the probe explicitly reports no barcode.
// FORMAT writes the requested barcode; APPEND and Restore require an existing identity.
func (e *Executor) ReadTapeBarcode(ctx context.Context, device string) (string, error) {
// Run the configured device probe and decode its stable JSON boundary.
cmd := exec.CommandContext(ctx, e.scripts.ReadInfo)
cmd.Env = append(cmd.Env, fmt.Sprintf("DEVICE=%s", device))
data, err := tools.RunCmdWithReturn(logrus.StandardLogger(), cmd, e.paths.Work)
if err != nil {
return "", fmt.Errorf("read tape info failed, %w", err)
}
var info struct {
Barcode *string `json:"barcode"`
}
if err := json.Unmarshal(data, &info); err != nil {
return "", fmt.Errorf("decode tape info failed, %w", err)
}
if info.Barcode == nil {
return "", fmt.Errorf("read tape info failed: missing barcode field")
}
if strings.TrimSpace(*info.Barcode) == "" {
return "", nil
}
// Keep reported cartridge labels separate from strict operator-supplied targets.
return normalizeReportedTapeBarcode(*info.Barcode)
}
func normalizeReportedTapeBarcode(value string) (string, error) {
// Historical probes and MAM can include the media suffix after the six-character identity.
value = strings.TrimSpace(value)
if value == "" {
return "", nil
}
if len(value) > 6 {
value = value[:6]
}
barcode, err := NormalizeTapeBarcode(value)
if err != nil {
return "", fmt.Errorf("invalid tape barcode, %w", err)
}
return barcode, nil
}