Skip to content

Commit

Permalink
Added a tar target for streaming out files in bulk.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Oct 2, 2012
1 parent edb410f commit afaf944
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions http.go
Expand Up @@ -32,6 +32,7 @@ const (
listPrefix = "/.cbfs/list/"
configPrefix = "/.cbfs/config/"
zipPrefix = "/.cbfs/zip/"
tarPrefix = "/.cbfs/tar/"
fsckPrefix = "/.cbfs/fsck/"
)

Expand Down Expand Up @@ -856,6 +857,8 @@ func doGet(w http.ResponseWriter, req *http.Request) {
doListDocs(w, req, minusPrefix(req.URL.Path, listPrefix))
case strings.HasPrefix(req.URL.Path, zipPrefix):
doZipDocs(w, req, minusPrefix(req.URL.Path, zipPrefix))
case strings.HasPrefix(req.URL.Path, tarPrefix):
doTarDocs(w, req, minusPrefix(req.URL.Path, tarPrefix))
case strings.HasPrefix(req.URL.Path, fsckPrefix):
dofsck(w, req, minusPrefix(req.URL.Path, fsckPrefix))
case strings.HasPrefix(req.URL.Path, "/.cbfs/"):
Expand Down
52 changes: 52 additions & 0 deletions tar.go
@@ -0,0 +1,52 @@
package main

import (
"archive/tar"
"log"
"net/http"
)

func doTarDocs(w http.ResponseWriter, req *http.Request,
path string) {

quit := make(chan bool)
defer close(quit)
ch := make(chan *namedFile)
cherr := make(chan error)

go pathGenerator(path, ch, cherr, quit)
go logErrors("tar", cherr)

w.Header().Set("Content-Type", "application/x-tar")
w.WriteHeader(200)

tw := tar.NewWriter(w)
for nf := range ch {
if nf.err != nil {
log.Printf("Error on %v: %v", nf.name, nf.err)
continue
}

fh := tar.Header{
Name: nf.name,
Mode: 0644,
Size: nf.meta.Length,
ModTime: nf.meta.Modified,
}

err := tw.WriteHeader(&fh)
if err != nil {
log.Printf("Error writing header %#v", fh)
continue
}

err = copyBlob(tw, nf.meta.OID)
if err != nil {
log.Printf("Error copying blob for %v: %v",
nf.name, err)
// Client will get a broken tar file
return
}
}
tw.Close()
}

0 comments on commit afaf944

Please sign in to comment.