github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/fs.go (about)

     1  // Package cos provides common low-level types and utilities for all aistore projects
     2  /*
     3   * Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package cos
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  
    12  	jsoniter "github.com/json-iterator/go"
    13  )
    14  
    15  // FsID is unified and cross-platform syscall.Fsid type which implements JSON marshaling.
    16  type FsID [2]int32
    17  
    18  func (d FsID) MarshalJSON() ([]byte, error) { return jsoniter.Marshal(d.String()) }
    19  func (d FsID) String() string               { return fmt.Sprintf("%d,%d", d[0], d[1]) }
    20  func (d *FsID) UnmarshalJSON(b []byte) error {
    21  	v := bytes.Split(bytes.Trim(b, `"`), []byte{','})
    22  	if len(v) != 2 {
    23  		return errors.New("invalid fsid, expected 2 numbers separated by comma")
    24  	}
    25  	for i := range 2 {
    26  		if err := jsoniter.Unmarshal(v[i], &d[i]); err != nil {
    27  			return err
    28  		}
    29  	}
    30  	return nil
    31  }
    32  
    33  func AddUniqueFsID(ids []FsID, nid FsID) ([]FsID, bool /*added*/) {
    34  	for _, id := range ids {
    35  		if nid == id {
    36  			return ids, false
    37  		}
    38  	}
    39  	return append(ids, nid), true
    40  }