Skip to content

Commit

Permalink
pkg/gluster: don't use tmp file for PUT (#3869)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD authored Jul 3, 2023
1 parent 187dcd0 commit 52f3428
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ juicefs.fdb: Makefile cmd/*.go pkg/*/*.go
juicefs.gluster: Makefile cmd/*.go pkg/*/*.go
go build -tags gluster -ldflags="$(LDFLAGS)" -o juicefs.gluster .

juicefs.all: Makefile cmd/*.go pkg/*/*.go
go build -tags ceph,fdb,gluster -ldflags="$(LDFLAGS)" -o juicefs.all .

# This is the script for compiling the Linux version on the MacOS platform.
# Please execute the `brew install FiloSottile/musl-cross/musl-cross` command before using it.
juicefs.linux:
Expand Down
17 changes: 5 additions & 12 deletions pkg/object/gluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ import (
"fmt"
"io"
"io/fs"
"math/rand"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -105,21 +103,19 @@ func (c *gluster) Put(key string, in io.Reader) error {
if strings.HasSuffix(key, dirSuffix) {
return c.vol.MkdirAll(key, os.FileMode(0777))
}
p := key
tmp := filepath.Join(filepath.Dir(p), "."+filepath.Base(p)+".tmp"+strconv.Itoa(rand.Int()))
f, err := c.vol.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
f, err := c.vol.OpenFile(key, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil && os.IsNotExist(err) {
if err := c.vol.MkdirAll(filepath.Dir(p), os.FileMode(0777)); err != nil {
if err := c.vol.MkdirAll(filepath.Dir(key), os.FileMode(0777)); err != nil {
return err
}
f, err = c.vol.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
f, err = c.vol.OpenFile(key, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
}
if err != nil {
return err
}
defer func() {
if err != nil {
_ = c.vol.Unlink(tmp)
_ = c.vol.Unlink(key)
}
}()

Expand All @@ -134,10 +130,7 @@ func (c *gluster) Put(key string, in io.Reader) error {
_ = f.Close()
return err
}
if err = f.Close(); err != nil {
return err
}
err = c.vol.Rename(tmp, p)
err = f.Close()
return err
}

Expand Down

0 comments on commit 52f3428

Please sign in to comment.