예제 #1
0
파일: archive.go 프로젝트: maruel/dumbcas
// Archives the items.
func (s *stats) archiveInputs(a DumbcasApplication, cas dumbcaslib.CasTable, items <-chan itemToArchive) <-chan string {
	c := make(chan string)
	go func() {
		defer func() {
			close(c)
			s.done <- true
		}()
		entryRoot := &dumbcaslib.Entry{}
		cont := true
		for cont {
			select {
			case <-interrupt.Channel:
				// Early exit.
				s.interrupted.Add(1)
				return
			case item, ok := <-items:
				if !ok {
					cont = false
					continue
				}
				//s.out <- fmt.Sprintf("Archiving: %s", item.relPath)
				makeEntry(entryRoot, item)
				s.archiveItem(item, cas)
			}
		}
		// Serializes the entry file to archive it too.
		data, err := json.Marshal(entryRoot)
		if err != nil {
			s.errors.Add(1)
			s.out <- fmt.Sprintf("Failed to marshal entry file: %s", err)
		} else {
			entrySha1, err := dumbcaslib.AddBytes(cas, data)
			if os.IsExist(err) {
				s.nbNotArchived.Add(1)
				s.bytesNotArchived.Add(int64(len(data)))
				c <- entrySha1
			} else if err == nil {
				s.nbArchived.Add(1)
				s.bytesArchived.Add(int64(len(data)))
				c <- entrySha1
			} else {
				s.errors.Add(1)
				s.out <- fmt.Sprintf("Failed to archive entry file: %s", err)
			}
		}
	}()
	return c
}
예제 #2
0
// 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
}