// archiveData archives a tree fictious data. // Returns (tree of sha1s, name of the node, sha1 of the node entry). // Accept the paths as posix. func archiveData(t testing.TB, cas dumbcaslib.CasTable, nodes dumbcaslib.NodesTable, tree map[string]string) (map[string]string, string, string) { sha1tree, entries := marshalData(t, tree) for k, v := range tree { err := cas.AddEntry(bytes.NewBuffer([]byte(v)), sha1tree[k]) ut.AssertEqualf(t, true, err == nil || err == os.ErrExist, "Unexpected error: %s", err) } entrySha1, err := dumbcaslib.AddBytes(cas, entries) ut.AssertEqual(t, nil, err) // And finally add the node. now := time.Now().UTC() nodeName, err := nodes.AddEntry(&dumbcaslib.Node{entrySha1, "useful comment"}, "fictious") ut.AssertEqual(t, nil, err) ut.AssertEqualf(t, true, strings.HasPrefix(nodeName, now.Format("2006-01")+string(filepath.Separator)), "Invalid node name %s", nodeName) return sha1tree, nodeName, entrySha1 }
// Archives one item in the CAS table. func (s *stats) archiveItem(item itemToArchive, cas dumbcaslib.CasTable) { f, err := os.Open(item.fullPath) if err != nil { s.errors.Add(1) s.out <- fmt.Sprintf("Failed to archive %s: %s", item.fullPath, err) return } defer func() { _ = f.Close() }() err = cas.AddEntry(f, item.sha1) if os.IsExist(err) { s.nbNotArchived.Add(1) s.bytesNotArchived.Add(item.size) } else if err == nil { s.nbArchived.Add(1) s.bytesArchived.Add(item.size) } else { s.errors.Add(1) s.out <- fmt.Sprintf("Failed to archive %s: %s", item.fullPath, err) } }