gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/files.go (about)

     1  package renter
     2  
     3  import (
     4  	"gitlab.com/SkynetLabs/skyd/skymodules"
     5  
     6  	"gitlab.com/NebulousLabs/errors"
     7  )
     8  
     9  // DeleteFile removes a file entry from the renter and deletes its data from
    10  // the hosts it is stored on.
    11  func (r *Renter) DeleteFile(siaPath skymodules.SiaPath) error {
    12  	err := r.tg.Add()
    13  	if err != nil {
    14  		return err
    15  	}
    16  	defer r.tg.Done()
    17  
    18  	// Perform the delete operation.
    19  	err = r.staticFileSystem.DeleteFile(siaPath)
    20  	if err != nil {
    21  		return errors.AddContext(err, "unable to delete siafile from filesystem")
    22  	}
    23  
    24  	// Update the filesystem metadata.
    25  	//
    26  	// TODO: This is incorrect, should be running the metadata update call on a
    27  	// node, not on a siaPath. The node should be returned by the delete call.
    28  	// Need a metadata update func that operates on a node to do that.
    29  	dirSiaPath, err := siaPath.Dir()
    30  	if err != nil {
    31  		r.staticLog.Printf("Unable to fetch the directory from a siaPath %v for deleted siafile: %v", siaPath, err)
    32  		// Return 'nil' because the delete operation succeeded, it was only the
    33  		// metadata update operation that failed.
    34  		return nil
    35  	}
    36  
    37  	// Queue an update to the directory.
    38  	r.staticDirUpdateBatcher.callQueueDirUpdate(dirSiaPath)
    39  	return nil
    40  }
    41  
    42  // FileList loops over all the files within the directory specified by siaPath
    43  // and will then call the provided listing function on the file.
    44  func (r *Renter) FileList(siaPath skymodules.SiaPath, recursive, cached bool, flf skymodules.FileListFunc) error {
    45  	if err := r.tg.Add(); err != nil {
    46  		return err
    47  	}
    48  	defer r.tg.Done()
    49  	var err error
    50  	if cached {
    51  		err = r.staticFileSystem.CachedList(siaPath, recursive, flf, func(skymodules.DirectoryInfo) {})
    52  	} else {
    53  		offlineMap, goodForRenewMap, contractsMap, _ := r.callRenterContractsAndUtilities()
    54  		err = r.staticFileSystem.List(siaPath, recursive, offlineMap, goodForRenewMap, contractsMap, flf, func(skymodules.DirectoryInfo) {})
    55  	}
    56  	if err != nil {
    57  		return err
    58  	}
    59  	return err
    60  }
    61  
    62  // File returns file from siaPath queried by user.
    63  // Update based on FileList
    64  func (r *Renter) File(siaPath skymodules.SiaPath) (skymodules.FileInfo, error) {
    65  	if err := r.tg.Add(); err != nil {
    66  		return skymodules.FileInfo{}, err
    67  	}
    68  	defer r.tg.Done()
    69  	offline, goodForRenew, contracts, _ := r.callRenterContractsAndUtilities()
    70  	fi, err := r.staticFileSystem.FileInfo(siaPath, offline, goodForRenew, contracts)
    71  	if err != nil {
    72  		return skymodules.FileInfo{}, errors.AddContext(err, "unable to get the fileinfo from the filesystem")
    73  	}
    74  	return fi, nil
    75  }
    76  
    77  // FileCached returns file from siaPath queried by user, using cached values for
    78  // health and redundancy.
    79  func (r *Renter) FileCached(siaPath skymodules.SiaPath) (skymodules.FileInfo, error) {
    80  	if err := r.tg.Add(); err != nil {
    81  		return skymodules.FileInfo{}, err
    82  	}
    83  	defer r.tg.Done()
    84  	return r.staticFileSystem.CachedFileInfo(siaPath)
    85  }
    86  
    87  // RenameFile takes an existing file and changes the nickname. The original
    88  // file must exist, and there must not be any file that already has the
    89  // replacement nickname.
    90  func (r *Renter) RenameFile(currentName, newName skymodules.SiaPath) error {
    91  	if err := r.tg.Add(); err != nil {
    92  		return err
    93  	}
    94  	defer r.tg.Done()
    95  
    96  	// Rename file.
    97  	err := r.staticFileSystem.RenameFile(currentName, newName)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	// Queue an update on each parent dir of the filenames to ensure that the
   103  	// aggregate metadata is correctly updated.
   104  	oldDirSiaPath, err := currentName.Dir()
   105  	if err != nil {
   106  		return err
   107  	}
   108  	newDirSiaPath, err := newName.Dir()
   109  	if err != nil {
   110  		return err
   111  	}
   112  	r.staticDirUpdateBatcher.callQueueDirUpdate(oldDirSiaPath)
   113  	r.staticDirUpdateBatcher.callQueueDirUpdate(newDirSiaPath)
   114  	return nil
   115  }
   116  
   117  // SetFileStuck sets the Stuck field of the whole siafile to stuck.
   118  func (r *Renter) SetFileStuck(siaPath skymodules.SiaPath, stuck bool) (err error) {
   119  	if err := r.tg.Add(); err != nil {
   120  		return err
   121  	}
   122  	defer r.tg.Done()
   123  	// Open the file.
   124  	entry, err := r.staticFileSystem.OpenSiaFile(siaPath)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	defer func() {
   129  		err = errors.Compose(err, entry.Close())
   130  	}()
   131  	// Update the file.
   132  	return entry.SetAllStuck(stuck)
   133  }