xl: Avoid removing a directory if it is not an object dir (#6395)

DeleteObject should not remove any directory unless the latter
is an empty directory.

Fixes #6394
master
Anis Elleuch 6 years ago committed by kannappanr
parent 4487f70f08
commit 85117d554f
  1. 27
      cmd/xl-v1-object.go
  2. 14
      cmd/xl-v1-object_test.go

@ -843,24 +843,27 @@ func (xl xlObjects) DeleteObject(ctx context.Context, bucket, object string) (er
return err return err
} }
if hasSuffix(object, slashSeparator) { if !xl.isObject(bucket, object) {
// Delete the object on all disks. return ObjectNotFound{bucket, object}
if err = xl.deleteObject(ctx, bucket, object, len(xl.getDisks())/2+1, true); err != nil {
return toObjectErr(err, bucket, object)
}
} }
// Read metadata associated with the object from all disks. var writeQuorum int
partsMetadata, errs := readAllXLMetadata(ctx, xl.getDisks(), bucket, object) var isObjectDir = hasSuffix(object, slashSeparator)
// get Quorum for this object if isObjectDir {
_, writeQuorum, err := objectQuorumFromMeta(ctx, xl, partsMetadata, errs) writeQuorum = len(xl.getDisks())/2 + 1
if err != nil { } else {
return toObjectErr(err, bucket, object) // Read metadata associated with the object from all disks.
partsMetadata, errs := readAllXLMetadata(ctx, xl.getDisks(), bucket, object)
// get Quorum for this object
_, writeQuorum, err = objectQuorumFromMeta(ctx, xl, partsMetadata, errs)
if err != nil {
return toObjectErr(err, bucket, object)
}
} }
// Delete the object on all disks. // Delete the object on all disks.
if err = xl.deleteObject(ctx, bucket, object, writeQuorum, false); err != nil { if err = xl.deleteObject(ctx, bucket, object, writeQuorum, isObjectDir); err != nil {
return toObjectErr(err, bucket, object) return toObjectErr(err, bucket, object)
} }

@ -71,11 +71,14 @@ func TestXLDeleteObjectBasic(t *testing.T) {
object string object string
expectedErr error expectedErr error
}{ }{
{".test", "obj", BucketNameInvalid{Bucket: ".test"}}, {".test", "dir/obj", BucketNameInvalid{Bucket: ".test"}},
{"----", "obj", BucketNameInvalid{Bucket: "----"}}, {"----", "dir/obj", BucketNameInvalid{Bucket: "----"}},
{"bucket", "", ObjectNameInvalid{Bucket: "bucket", Object: ""}}, {"bucket", "", ObjectNameInvalid{Bucket: "bucket", Object: ""}},
{"bucket", "doesnotexist", ObjectNotFound{Bucket: "bucket", Object: "doesnotexist"}}, {"bucket", "doesnotexist", ObjectNotFound{Bucket: "bucket", Object: "doesnotexist"}},
{"bucket", "obj", nil}, {"bucket", "dir/doesnotexist", ObjectNotFound{Bucket: "bucket", Object: "dir/doesnotexist"}},
{"bucket", "dir", ObjectNotFound{Bucket: "bucket", Object: "dir"}},
{"bucket", "dir/", ObjectNotFound{Bucket: "bucket", Object: "dir/"}},
{"bucket", "dir/obj", nil},
} }
// Create an instance of xl backend // Create an instance of xl backend
@ -84,14 +87,13 @@ func TestXLDeleteObjectBasic(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// Make bucket for Test 7 to pass
err = xl.MakeBucketWithLocation(context.Background(), "bucket", "") err = xl.MakeBucketWithLocation(context.Background(), "bucket", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Create object "obj" under bucket "bucket" for Test 7 to pass // Create object "dir/obj" under bucket "bucket" for Test 7 to pass
_, err = xl.PutObject(context.Background(), "bucket", "obj", mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil) _, err = xl.PutObject(context.Background(), "bucket", "dir/obj", mustGetHashReader(t, bytes.NewReader([]byte("abcd")), int64(len("abcd")), "", ""), nil)
if err != nil { if err != nil {
t.Fatalf("XL Object upload failed: <ERROR> %s", err) t.Fatalf("XL Object upload failed: <ERROR> %s", err)
} }

Loading…
Cancel
Save