2011-03-05 19:34:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2011-03-05 20:46:28 +00:00
|
|
|
"bytes"
|
2011-03-05 19:34:12 +00:00
|
|
|
"camli/blobref"
|
|
|
|
"fmt"
|
2011-03-05 20:46:28 +00:00
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"json"
|
2011-03-05 19:34:12 +00:00
|
|
|
"os"
|
2011-03-05 20:46:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2011-03-05 19:34:12 +00:00
|
|
|
)
|
|
|
|
|
2011-03-05 20:46:28 +00:00
|
|
|
type removeResponse struct {
|
2011-03-05 21:43:24 +00:00
|
|
|
Removed []string "removed"
|
2011-03-05 20:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the list of blobs. An error is returned if the server failed to
|
|
|
|
// remove a blob. Removing a non-existent blob isn't an error.
|
|
|
|
func (c *Client) RemoveBlobs(blobs []*blobref.BlobRef) os.Error {
|
|
|
|
url := fmt.Sprintf("%s/camli/remove", c.server)
|
|
|
|
params := make(map[string][]string) // "blobN" -> BlobRefStr
|
|
|
|
needsDelete := make(map[string]bool) // BlobRefStr -> true
|
|
|
|
for n, b := range blobs {
|
2011-03-05 21:43:24 +00:00
|
|
|
if b == nil {
|
|
|
|
return os.NewError("Cannot delete nil blobref")
|
|
|
|
}
|
|
|
|
key := fmt.Sprintf("blob%v", n+1)
|
2011-03-05 20:46:28 +00:00
|
|
|
params[key] = []string{b.String()}
|
|
|
|
needsDelete[b.String()] = true
|
|
|
|
}
|
|
|
|
body := http.EncodeQuery(params)
|
|
|
|
|
2011-05-10 12:59:36 +00:00
|
|
|
req, err := http.NewRequest("POST", url, strings.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating RemoveBlobs POST request: %v", err)
|
|
|
|
}
|
2011-03-05 20:46:28 +00:00
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2011-05-10 12:59:36 +00:00
|
|
|
// TODO-GO: teach http.NewRequest how to find a
|
|
|
|
// strings.NewReader's Content-Length.
|
|
|
|
req.ContentLength = int64(len(body))
|
2011-03-05 20:46:28 +00:00
|
|
|
req.Header.Add("Content-Length", strconv.Itoa(len(body)))
|
|
|
|
if c.HasAuthCredentials() {
|
|
|
|
req.Header.Add("Authorization", c.authHeader())
|
|
|
|
}
|
2011-05-10 13:25:18 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2011-03-05 20:46:28 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return os.NewError(fmt.Sprintf("Got status code %d from blobserver for remove %s", resp.StatusCode, body))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only valid HTTP responses are 200.
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return os.NewError(fmt.Sprintf("Invalid http response %d in remove response", resp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: LimitReader here for paranoia
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
var remResp removeResponse
|
|
|
|
if jerr := json.Unmarshal(buf.Bytes(), &remResp); jerr != nil {
|
2011-03-05 21:43:24 +00:00
|
|
|
return os.NewError(fmt.Sprintf("Failed to parse remove response %q: %s", buf.String(), jerr))
|
2011-03-05 20:46:28 +00:00
|
|
|
}
|
2011-03-05 21:43:24 +00:00
|
|
|
for _, value := range remResp.Removed {
|
2011-03-05 20:46:28 +00:00
|
|
|
needsDelete[value] = false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(needsDelete) > 0 {
|
|
|
|
return os.NewError(fmt.Sprintf("Failed to remove blobs %s", strings.Join(stringKeys(needsDelete), ", ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the single blob. An error is returned if the server failed to remove
|
|
|
|
// the blob. Removing a non-existent blob isn't an error.
|
|
|
|
func (c *Client) RemoveBlob(b *blobref.BlobRef) os.Error {
|
|
|
|
return c.RemoveBlobs([]*blobref.BlobRef{b})
|
|
|
|
}
|
|
|
|
|
2011-03-05 21:43:24 +00:00
|
|
|
func stringKeys(m map[string]bool) (s []string) {
|
2011-03-05 20:46:28 +00:00
|
|
|
s = make([]string, 0, len(m))
|
|
|
|
for key, _ := range m {
|
|
|
|
s = append(s, key)
|
|
|
|
}
|
|
|
|
return
|
2011-03-05 19:34:12 +00:00
|
|
|
}
|