github.com/0chain/gosdk@v1.17.11/zboxcore/allocationchange/deletefile.go (about)

     1  package allocationchange
     2  
     3  import (
     4  	"path"
     5  
     6  	"github.com/0chain/errors"
     7  	"github.com/0chain/gosdk/core/common"
     8  	"github.com/0chain/gosdk/zboxcore/fileref"
     9  )
    10  
    11  type DeleteFileChange struct {
    12  	change
    13  	FileMetaRef fileref.RefEntity
    14  }
    15  
    16  var ErrRefNotFound = errors.New("ref_not_found", "Ref not found")
    17  
    18  func (ch *DeleteFileChange) ProcessChange(rootRef *fileref.Ref, _ map[string]string) (err error) {
    19  
    20  	if ch.FileMetaRef.GetPath() == "/" {
    21  		rootRef.Children = nil
    22  		return
    23  	}
    24  
    25  	parentPath := path.Dir(ch.FileMetaRef.GetPath())
    26  
    27  	fields, err := common.GetPathFields(parentPath)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	dirRef := rootRef
    33  	for _, name := range fields {
    34  		found := false
    35  		for _, child := range dirRef.Children {
    36  			if child.GetName() == name {
    37  				dirRef = child.(*fileref.Ref)
    38  				found = true
    39  				break
    40  			}
    41  		}
    42  
    43  		if !found {
    44  			err = ErrRefNotFound
    45  			return
    46  		}
    47  	}
    48  
    49  	for i, child := range dirRef.Children {
    50  		if child.GetName() == ch.FileMetaRef.GetName() {
    51  			dirRef.RemoveChild(i)
    52  			return
    53  		}
    54  	}
    55  
    56  	err = errors.New("file_not_found", "File to delete not found in blobber")
    57  	return
    58  }
    59  
    60  func (n *DeleteFileChange) GetAffectedPath() []string {
    61  	if n.FileMetaRef != nil {
    62  		return []string{n.FileMetaRef.GetPath()}
    63  	}
    64  	return nil
    65  }
    66  
    67  func (n *DeleteFileChange) GetSize() int64 {
    68  	if n.FileMetaRef != nil {
    69  		return -n.FileMetaRef.GetSize()
    70  	}
    71  	return int64(0)
    72  }