From a36ff707f35997aa062dfd8225f90fe5e98807d5 Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Mon, 6 Nov 2023 14:13:48 +0530 Subject: [PATCH] cephfs: replaced string comparison with errno Signed-off-by: Riya Singhal --- internal/cephfs/core/snapshot_metadata.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/cephfs/core/snapshot_metadata.go b/internal/cephfs/core/snapshot_metadata.go index f168fbf8cd41..273e212dae92 100644 --- a/internal/cephfs/core/snapshot_metadata.go +++ b/internal/cephfs/core/snapshot_metadata.go @@ -19,7 +19,7 @@ package core import ( "errors" "fmt" - "strings" + "syscall" fsAdmin "github.com/ceph/go-ceph/cephfs/admin" ) @@ -121,18 +121,22 @@ func (s *snapshotClient) UnsetAllSnapshotMetadata(keys []string) error { for _, key := range keys { err := s.removeSnapshotMetadata(key) - // TODO: replace string comparison with errno. - if err != nil && !strings.Contains(err.Error(), "No such file or directory") { - return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w", - key, s.SnapshotID, s.VolID, s.FsName, err) + if err != nil { + if errno, ok := err.(syscall.Errno); ok && errno == syscall.ENOENT { + // Handle "No such file or directory" error + return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w", + key, s.SnapshotID, s.VolID, s.FsName, err) + } } } err := s.removeSnapshotMetadata(clusterNameKey) - // TODO: replace string comparison with errno. - if err != nil && !strings.Contains(err.Error(), "No such file or directory") { - return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w", - clusterNameKey, s.SnapshotID, s.VolID, s.FsName, err) + if err != nil { + if errno, ok := err.(syscall.Errno); ok && errno == syscall.ENOENT { + // Handle "No such file or directory" error + return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w", + clusterNameKey, s.SnapshotID, s.VolID, s.FsName, err) + } } return nil