Skip to content

Commit

Permalink
cmd/gateway: create dst dir path when copy-object (#4058)
Browse files Browse the repository at this point in the history
Create the parent directory of the target on demand when need
Co-authored-by: Davies Liu <[email protected]>
  • Loading branch information
zhijian-pro authored and SandyXSD committed Nov 24, 2023
1 parent e91dd5b commit aa007d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
13 changes: 13 additions & 0 deletions integration/s3gateway_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,19 @@ function test_copy_object() {
fi
fi

if [ $rv -eq 0 ]; then
function="${AWS} s3api copy-object --bucket ${bucket_name} --key /not-exist-dir/datafile-1-kB-copy --copy-source ${bucket_name}/datafile-1-kB"
test_function=${function}
out=$($function)
rv=$?
hash2=$(echo "$out" | jq -r .CopyObjectResult.ETag | sed -e 's/^"//' -e 's/"$//')
if [ $rv -eq 0 ] && [ "$HASH_1_KB" != "$hash2" ]; then
# Verification failed
rv=1
out="Hash mismatch expected $HASH_1_KB, got $hash2"
fi
fi

${AWS} s3 rb s3://"${bucket_name}" --force > /dev/null 2>&1
if [ $rv -eq 0 ]; then
log_success "$(get_duration "$start_time")" "${test_function}"
Expand Down
42 changes: 30 additions & 12 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,11 @@ func (n *jfsObjects) CopyObject(ctx context.Context, srcBucket, srcObject, dstBu
return n.GetObjectInfo(ctx, srcBucket, srcObject, minio.ObjectOptions{})
}
tmp := n.tpath(dstBucket, "tmp", minio.MustGetUUID())
_ = n.mkdirAll(ctx, path.Dir(tmp))
f, eno := n.fs.Create(mctx, tmp, 0666, n.gConf.Umask)
if eno == syscall.ENOENT {
_ = n.mkdirAll(ctx, path.Dir(tmp))
f, eno = n.fs.Create(mctx, tmp, 0666, n.gConf.Umask)
}
if eno != 0 {
logger.Errorf("create %s: %s", tmp, eno)
return
Expand All @@ -509,6 +512,14 @@ func (n *jfsObjects) CopyObject(ctx context.Context, srcBucket, srcObject, dstBu
return
}
eno = n.fs.Rename(mctx, tmp, dst, 0)
if eno == syscall.ENOENT {
if err = n.mkdirAll(ctx, path.Dir(dst)); err != nil {
logger.Errorf("mkdirAll %s: %s", path.Dir(dst), err)
err = jfsToObjectErr(ctx, err, dstBucket, dstObject)
return
}
eno = n.fs.Rename(mctx, tmp, dst, 0)
}
if eno != 0 {
err = jfsToObjectErr(ctx, eno, srcBucket, srcObject)
logger.Errorf("rename %s to %s: %s", tmp, dst, err)
Expand Down Expand Up @@ -642,8 +653,11 @@ func (n *jfsObjects) mkdirAll(ctx context.Context, p string) error {

func (n *jfsObjects) putObject(ctx context.Context, bucket, object string, r *minio.PutObjReader, opts minio.ObjectOptions) (err error) {
tmpname := n.tpath(bucket, "tmp", minio.MustGetUUID())
_ = n.mkdirAll(ctx, path.Dir(tmpname))
f, eno := n.fs.Create(mctx, tmpname, 0666, n.gConf.Umask)
if eno == syscall.ENOENT {
_ = n.mkdirAll(ctx, path.Dir(tmpname))
f, eno = n.fs.Create(mctx, tmpname, 0666, n.gConf.Umask)
}
if eno != 0 {
logger.Errorf("create %s: %s", tmpname, eno)
err = eno
Expand Down Expand Up @@ -678,13 +692,17 @@ func (n *jfsObjects) putObject(ctx context.Context, bucket, object string, r *mi
if err != nil {
return
}
dir := path.Dir(object)
if dir != "" {
_ = n.mkdirAll(ctx, dir)
eno = n.fs.Rename(mctx, tmpname, object, 0)
if eno == syscall.ENOENT {
if err = n.mkdirAll(ctx, path.Dir(object)); err != nil {
logger.Errorf("mkdirAll %s: %s", path.Dir(object), err)
err = jfsToObjectErr(ctx, err, bucket, object)
return
}
eno = n.fs.Rename(mctx, tmpname, object, 0)
}
if eno := n.fs.Rename(mctx, tmpname, object, 0); eno != 0 {
if eno != 0 {
err = jfsToObjectErr(ctx, eno, bucket, object)
return
}
return
}
Expand Down Expand Up @@ -917,16 +935,16 @@ func (n *jfsObjects) CompleteMultipartUpload(ctx context.Context, bucket, object
}

name := n.path(bucket, object)
dir := path.Dir(name)
if dir != "" {
if err = n.mkdirAll(ctx, dir); err != nil {
eno = n.fs.Rename(mctx, tmp, name, 0)
if eno == syscall.ENOENT {
if err = n.mkdirAll(ctx, path.Dir(name)); err != nil {
logger.Errorf("mkdirAll %s: %s", path.Dir(name), err)
_ = n.fs.Delete(mctx, tmp)
err = jfsToObjectErr(ctx, err, bucket, object, uploadID)
return
}
eno = n.fs.Rename(mctx, tmp, name, 0)
}

eno = n.fs.Rename(mctx, tmp, name, 0)
if eno != 0 {
_ = n.fs.Delete(mctx, tmp)
err = jfsToObjectErr(ctx, eno, bucket, object, uploadID)
Expand Down

0 comments on commit aa007d7

Please sign in to comment.