Skip to content

Commit

Permalink
Add BulkDelete support for swifttest
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored and ncw committed Nov 12, 2019
1 parent 1bde4b7 commit 27a552e
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions swifttest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,70 @@ func (r rootResource) post(a *action) interface{} {
return nil
}

func (rootResource) delete(a *action) interface{} {
func (r rootResource) delete(a *action) interface{} {
if a.req.URL.Query().Get("bulk-delete") == "1" {
fatalf(403, "Operation forbidden", "Bulk delete is not supported")
data, err := ioutil.ReadAll(a.req.Body)
if err != nil {
fatalf(400, "Bad Request", "read error")
}
var nb, notFound int
for _, obj := range strings.Fields(string(data)) {
parts := strings.SplitN(obj, "/", 3)
if len(parts) < 3 {
fatalf(403, "Operation forbidden", "Bulk delete is not supported for containers")
}
b := containerResource{
name: parts[1],
container: a.user.Containers[parts[1]],
}
if b.container == nil {
notFound++
continue
}

objr := objectResource{
name: parts[2],
container: b.container,
}
objr.container.RLock()
if obj := objr.container.objects[objr.name]; obj != nil {
objr.object = obj
}
objr.container.RUnlock()
if objr.object == nil {
notFound++
continue
}

objr.container.Lock()
objr.object.Lock()
objr.container.bytes -= int64(len(objr.object.data))
delete(objr.container.objects, objr.name)
objr.object.Unlock()
objr.container.Unlock()

atomic.AddInt64(&a.user.BytesUsed, -int64(len(objr.object.data)))
atomic.AddInt64(&a.user.Objects, -1)
nb++
}

accept := a.req.Header.Get("Accept")
if strings.HasPrefix(accept, "application/json") {
a.w.Header().Set("Content-Type", "application/json")
resp := map[string]interface{}{
"Number Deleted": nb,
"Number Not Found": notFound,
"Errors": []string{},
"Response Status": "200 OK",
"Response Body": "",
}
jsonMarshal(a.w, resp)
return nil
}

resp := fmt.Sprintf("Number Deleted: %d\nNumber Not Found: %d\nErrors: \nResponse Status: 200 OK\n", nb, notFound)
a.w.Write([]byte(resp))
return nil
}

return notAllowed()
Expand Down

0 comments on commit 27a552e

Please sign in to comment.