github.com/orangebees/go-oneutils@v0.0.10/GlobalStore/FileInfoMap.go (about)

     1  package GlobalStore
     2  
     3  import (
     4  	"github.com/orangebees/go-oneutils/Convert"
     5  	"sort"
     6  )
     7  
     8  // FileInfoMap 文件相对路径 文件信息
     9  type FileInfoMap map[string]*FileInfo
    10  
    11  // Equal 检测两个FileInfoMap是否相等
    12  func (m FileInfoMap) Equal(fim FileInfoMap) bool {
    13  	if len(m) != len(fim) {
    14  		return false
    15  	}
    16  	for k, info := range m {
    17  		fileInfo, ok := fim[k]
    18  		if !ok {
    19  			return false
    20  		}
    21  		if info.Integrity != fileInfo.Integrity {
    22  			return false
    23  		}
    24  		if info.Size != fileInfo.Size {
    25  			return false
    26  		}
    27  		if info.Mode != fileInfo.Mode {
    28  			return false
    29  		}
    30  		if info.CheckedAt != fileInfo.CheckedAt {
    31  			return false
    32  		}
    33  	}
    34  	return true
    35  }
    36  
    37  // FileEqual 仅检测两个FileInfoMap的文件是否相等
    38  func (m FileInfoMap) FileEqual(fim FileInfoMap) bool {
    39  	if len(m) != len(fim) {
    40  		return false
    41  	}
    42  	for k, info := range m {
    43  		fileInfo, ok := fim[k]
    44  		if !ok {
    45  			return false
    46  		}
    47  		if info.Integrity != fileInfo.Integrity {
    48  			return false
    49  		}
    50  		if info.Size != fileInfo.Size {
    51  			return false
    52  		}
    53  
    54  	}
    55  	return true
    56  }
    57  
    58  // GetIntegrity 生成这个FileInfoMap唯一的Integrity
    59  func (m FileInfoMap) GetIntegrity() Integrity {
    60  	t := "sha512"
    61  	var sumlists = make([]string, len(m))
    62  	for s, info := range m {
    63  		value, err := info.Integrity.GetRawHashValue()
    64  		if err != nil {
    65  			return ""
    66  		}
    67  		t = info.Integrity.GetType()
    68  		nameit := NewIntegrity(t, []byte(s))
    69  		hashValue, err := nameit.GetRawHashValue()
    70  		if err != nil {
    71  			return ""
    72  		}
    73  		hashValue = append(hashValue, value...)
    74  		sumlists = append(sumlists, Convert.B2S(hashValue))
    75  	}
    76  	sort.Strings(sumlists)
    77  	var sumstr string
    78  	for i := 0; i < len(sumlists); i++ {
    79  		sumstr += sumlists[i]
    80  	}
    81  	return NewIntegrity(t, Convert.S2B(sumstr))
    82  }