Skip to content

Commit 68f2601

Browse files
committed
fix is directory empty check
1 parent c031b18 commit 68f2601

File tree

8 files changed

+11
-11
lines changed

8 files changed

+11
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (rf *CopyFileChange) ApplyChangeV2(ctx context.Context, allocationRoot, cli
136136

137137
rf.Type = srcRef.Type
138138
if srcRef.Type == reference.DIRECTORY {
139-
isEmpty, err := reference.IsDirectoryEmpty(ctx, srcRef.Path)
139+
isEmpty, err := reference.IsDirectoryEmpty(ctx, rf.AllocationID, srcRef.Path)
140140
if err != nil {
141141
return err
142142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (nf *DeleteFileChange) ApplyChangeV2(_ context.Context, _, _ string, numFil
5454
return err
5555
}
5656
if ref.Type == reference.DIRECTORY {
57-
isEmpty, err := reference.IsDirectoryEmpty(ctx, nf.Path)
57+
isEmpty, err := reference.IsDirectoryEmpty(ctx, nf.AllocationID, nf.Path)
5858
if err != nil {
5959
logging.Logger.Error("deleted_object_error", zap.Error(err))
6060
return err

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (rf *MoveFileChange) ApplyChangeV2(ctx context.Context, allocationRoot, cli
155155
return common.NewError("invalid_reference_path", "file already exists")
156156
}
157157
if srcRef.Type == reference.DIRECTORY {
158-
isEmpty, err := reference.IsDirectoryEmpty(ctx, srcRef.Path)
158+
isEmpty, err := reference.IsDirectoryEmpty(ctx, rf.AllocationID, srcRef.Path)
159159
if err != nil {
160160
return err
161161
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (rf *RenameFileChange) ApplyChangeV2(ctx context.Context, allocationRoot, c
134134
return 0, common.NewError("invalid_reference_path", err.Error())
135135
}
136136
if ref.Type == reference.DIRECTORY {
137-
isEmpty, err := reference.IsDirectoryEmpty(ctx, ref.Path)
137+
isEmpty, err := reference.IsDirectoryEmpty(ctx, rf.AllocationID, ref.Path)
138138
if err != nil {
139139
return 0, common.NewError("invalid_reference_path", err.Error())
140140
}

code/go/0chain.net/blobbercore/handler/file_command_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (cmd *DeleteFileCommand) IsValidated(ctx context.Context, req *http.Request
6060
return common.NewError("bad_db_operation", err.Error())
6161
}
6262
if allocationObj.StorageVersion == 1 && cmd.existingFileRef.Type == reference.DIRECTORY {
63-
isEmpty, err := reference.IsDirectoryEmpty(ctx, path)
63+
isEmpty, err := reference.IsDirectoryEmpty(ctx, allocationObj.ID, path)
6464
if err != nil {
6565
return err
6666
}

code/go/0chain.net/blobbercore/handler/object_operation_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ func (fsh *StorageHandler) RenameObject(ctx context.Context, r *http.Request) (i
12811281
}
12821282

12831283
if objectRef.Type != reference.FILE {
1284-
isEmpty, err := reference.IsDirectoryEmpty(ctx, objectRef.Path)
1284+
isEmpty, err := reference.IsDirectoryEmpty(ctx, allocationID, objectRef.Path)
12851285
if err != nil {
12861286
return nil, common.NewError("invalid_operation", "Error checking if directory is empty "+err.Error())
12871287
}
@@ -1374,7 +1374,7 @@ func (fsh *StorageHandler) CopyObject(ctx context.Context, r *http.Request) (int
13741374
return nil, common.NewError("invalid_parameters", "Invalid destination path. Cannot copy to the same parent directory.")
13751375
}
13761376
if allocationObj.StorageVersion == 1 && objectRef.Type == reference.DIRECTORY {
1377-
isEmpty, err := reference.IsDirectoryEmpty(ctx, objectRef.Path)
1377+
isEmpty, err := reference.IsDirectoryEmpty(ctx, allocationID, objectRef.Path)
13781378
if err != nil {
13791379
return nil, err
13801380
}
@@ -1497,7 +1497,7 @@ func (fsh *StorageHandler) MoveObject(ctx context.Context, r *http.Request) (any
14971497
return nil, common.NewError("invalid_parameters", "Invalid destination path. Cannot move to the same parent directory.")
14981498
}
14991499
if allocationObj.StorageVersion == 1 && objectRef.Type == reference.DIRECTORY {
1500-
isEmpty, err := reference.IsDirectoryEmpty(ctx, objectRef.Path)
1500+
isEmpty, err := reference.IsDirectoryEmpty(ctx, allocationID, objectRef.Path)
15011501
if err != nil {
15021502
return nil, err
15031503
}

code/go/0chain.net/blobbercore/handler/storage_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (fsh *StorageHandler) GetFileMeta(ctx context.Context, r *http.Request) (in
136136
}
137137
fileref.AllocationRoot = alloc.AllocationRoot
138138
if fileref.Type == reference.DIRECTORY {
139-
fileref.IsEmpty, err = reference.IsDirectoryEmpty(ctx, fileref.Path)
139+
fileref.IsEmpty, err = reference.IsDirectoryEmpty(ctx, allocationID, fileref.Path)
140140
if err != nil {
141141
return nil, common.NewError("invalid_parameters", "Invalid path. "+err.Error())
142142
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,10 @@ func UpdateCustomMeta(ctx context.Context, ref *Ref, customMeta string) error {
763763
return db.Exec("UPDATE reference_objects SET custom_meta = ? WHERE id = ?", customMeta, ref.ID).Error
764764
}
765765

766-
func IsDirectoryEmpty(ctx context.Context, path string) (bool, error) {
766+
func IsDirectoryEmpty(ctx context.Context, allocationID, path string) (bool, error) {
767767
db := datastore.GetStore().GetTransaction(ctx)
768768
var ref Ref
769-
err := db.Model(&Ref{}).Select("id").Where("parent_path = ?", path).Take(&ref).Error
769+
err := db.Model(&Ref{}).Select("id").Where("allocation_id=? AND parent_path = ?", allocationID, path).Take(&ref).Error
770770
if err != nil {
771771
if err == gorm.ErrRecordNotFound {
772772
return true, nil

0 commit comments

Comments
 (0)