Skip to content

Commit 66ca94e

Browse files
authored
fix duplicate dir (#1504)
* fix duplicate dir * fix duplicate check
1 parent 421e1a4 commit 66ca94e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

code/go/0chain.net/blobbercore/allocation/newdirchange.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/util"
1212
"github.com/0chain/blobber/code/go/0chain.net/core/common"
1313
"github.com/0chain/blobber/code/go/0chain.net/core/encryption"
14+
"github.com/0chain/blobber/code/go/0chain.net/core/logging"
15+
"go.uber.org/zap"
1416
"gorm.io/gorm"
1517
)
1618

@@ -33,6 +35,19 @@ func (nf *NewDir) ApplyChange(ctx context.Context,
3335
if parentRef == nil || parentRef.ID == 0 {
3436
_, err = reference.Mkdir(ctx, nf.AllocationID, nf.Path, allocationVersion, ts, collector)
3537
} else {
38+
collector.LockTransaction()
39+
defer collector.UnlockTransaction()
40+
dirLookupHash := reference.GetReferenceLookup(nf.AllocationID, nf.Path)
41+
dRef, err := reference.GetLimitedRefFieldsByLookupHash(ctx, nf.AllocationID, dirLookupHash, []string{"id"})
42+
if err != nil && err != gorm.ErrRecordNotFound {
43+
logging.Logger.Error("ApplyChange:Newdir", zap.Error(err))
44+
return err
45+
}
46+
err = nil
47+
// already exists
48+
if dRef != nil && dRef.ID != 0 {
49+
return nil
50+
}
3651
parentIDRef := &parentRef.ID
3752
newRef := reference.NewDirectoryRef()
3853
newRef.AllocationID = nf.AllocationID
@@ -43,7 +58,7 @@ func (nf *NewDir) ApplyChange(ctx context.Context,
4358
newRef.Name = filepath.Base(nf.Path)
4459
newRef.PathLevel = len(strings.Split(strings.TrimRight(nf.Path, "/"), "/"))
4560
newRef.ParentID = parentIDRef
46-
newRef.LookupHash = reference.GetReferenceLookup(nf.AllocationID, nf.Path)
61+
newRef.LookupHash = dirLookupHash
4762
newRef.CreatedAt = ts
4863
newRef.UpdatedAt = ts
4964
newRef.FileMetaHash = encryption.FastHash(newRef.GetFileMetaHashData())

code/go/0chain.net/blobbercore/reference/ref.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func Mkdir(ctx context.Context, allocationID, destpath string, allocationVersion
256256
newRef.AllocationVersion = allocationVersion
257257
err = db.Create(newRef).Error
258258
if err != nil {
259+
logging.Logger.Error("mkdir: failed to create directory", zap.Error(err), zap.String("path", fields[i]))
259260
return nil, err
260261
}
261262
collector.AddToCache(newRef)

0 commit comments

Comments
 (0)