8 "git.curoverse.com/arvados.git/sdk/go/keepclient"
9 "git.curoverse.com/arvados.git/sdk/go/manifest"
23 type ManifestStreamWriter struct {
25 *manifest.ManifestStream
32 type IKeepClient interface {
33 PutHB(hash string, buf []byte) (string, int, error)
36 func (m *ManifestStreamWriter) Write(p []byte) (int, error) {
37 n, err := m.ReadFrom(bytes.NewReader(p))
41 func (m *ManifestStreamWriter) ReadFrom(r io.Reader) (n int64, err error) {
47 m.Block = &Block{make([]byte, keepclient.BLOCKSIZE), 0}
49 count, err = r.Read(m.Block.data[m.Block.offset:])
51 m.Block.offset += int64(count)
52 if m.Block.offset == keepclient.BLOCKSIZE {
66 func (m *ManifestStreamWriter) goUpload() {
68 uploader := m.uploader
70 for block := range uploader {
71 hash := fmt.Sprintf("%x", md5.Sum(block.data[0:block.offset]))
72 signedHash, _, err := m.ManifestWriter.IKeepClient.PutHB(hash, block.data[0:block.offset])
74 errors = append(errors, err)
76 m.ManifestStream.Blocks = append(m.ManifestStream.Blocks, signedHash)
82 type ManifestWriter struct {
85 Streams map[string]*ManifestStreamWriter
88 func (m *ManifestWriter) WalkFunc(path string, info os.FileInfo, err error) error {
94 if len(path) > (len(m.stripPrefix) + len(info.Name()) + 1) {
95 dir = path[len(m.stripPrefix)+1 : (len(path) - len(info.Name()) - 1)]
101 fn := path[(len(path) - len(info.Name())):]
103 if m.Streams[dir] == nil {
104 m.Streams[dir] = &ManifestStreamWriter{
106 &manifest.ManifestStream{StreamName: dir},
111 go m.Streams[dir].goUpload()
114 stream := m.Streams[dir]
116 fileStart := stream.offset
118 file, err := os.Open(path)
123 log.Printf("Uploading %v/%v (%v bytes)", dir, fn, info.Size())
126 count, err = io.Copy(stream, file)
131 stream.offset += count
133 stream.ManifestStream.FileStreamSegments = append(stream.ManifestStream.FileStreamSegments,
134 manifest.FileStreamSegment{uint64(fileStart), uint64(count), fn})
139 func (m *ManifestWriter) Finish() error {
141 for _, stream := range m.Streams {
142 if stream.uploader == nil {
145 if stream.Block != nil {
146 stream.uploader <- stream.Block
148 close(stream.uploader)
149 stream.uploader = nil
151 errors := <-stream.finish
155 for _, r := range errors {
156 errstring = fmt.Sprintf("%v%v\n", errstring, r.Error())
160 return errors.New(errstring)
166 func (m *ManifestWriter) ManifestText() string {
170 dirs := make([]string, len(m.Streams))
172 for k := range m.Streams {
178 for _, k := range dirs {
184 k = strings.Replace(k, " ", "\\040", -1)
185 k = strings.Replace(k, "\n", "", -1)
186 buf.WriteString("./" + k)
188 for _, b := range v.Blocks {
192 for _, f := range v.FileStreamSegments {
194 name := strings.Replace(f.Name, " ", "\\040", -1)
195 name = strings.Replace(name, "\n", "", -1)
196 buf.WriteString(fmt.Sprintf("%d:%d:%s", f.SegPos, f.SegLen, name))
198 buf.WriteString("\n")
203 func WriteTree(kc IKeepClient, root string) (manifest string, err error) {
204 mw := ManifestWriter{kc, root, map[string]*ManifestStreamWriter{}}
205 err = filepath.Walk(root, mw.WalkFunc)
216 return mw.ManifestText(), nil