gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/host/contractmanager/storagefolderremove.go (about) 1 package contractmanager 2 3 import ( 4 "path/filepath" 5 ) 6 7 type ( 8 // storageFolderRemoval indicates a storage folder that has been removed 9 // from the WAL. 10 storageFolderRemoval struct { 11 Index uint16 12 Path string 13 } 14 ) 15 16 // commitStorageFolderRemoval will finalize a storage folder removal from the 17 // contract manager. 18 func (wal *writeAheadLog) commitStorageFolderRemoval(sfr storageFolderRemoval) { 19 // Close any open file handles. 20 sf, exists := wal.cm.storageFolders[sfr.Index] 21 if exists { 22 delete(wal.cm.storageFolders, sfr.Index) 23 } 24 if exists && sf.metadataFile != nil { 25 err := sf.metadataFile.Close() 26 if err != nil { 27 wal.cm.log.Printf("Error: unable to close metadata file as storage folder %v is removed\n", sf.path) 28 } 29 } 30 if exists && sf.sectorFile != nil { 31 err := sf.sectorFile.Close() 32 if err != nil { 33 wal.cm.log.Printf("Error: unable to close sector file as storage folder %v is removed\n", sf.path) 34 } 35 } 36 37 // Delete the files. 38 err := wal.cm.dependencies.RemoveFile(filepath.Join(sfr.Path, metadataFile)) 39 if err != nil { 40 wal.cm.log.Printf("Error: unable to remove metadata file as storage folder %v is removed\n", sfr.Path) 41 } 42 err = wal.cm.dependencies.RemoveFile(filepath.Join(sfr.Path, sectorFile)) 43 if err != nil { 44 wal.cm.log.Printf("Error: unable to reomve sector file as storage folder %v is removed\n", sfr.Path) 45 } 46 } 47 48 // RemoveStorageFolder will delete a storage folder from the contract manager, 49 // moving all of the sectors in the storage folder to new storage folders. 50 func (cm *ContractManager) RemoveStorageFolder(index uint16, force bool) error { 51 cm.tg.Add() 52 defer cm.tg.Done() 53 54 // Retrieve the specified storage folder. 55 cm.wal.mu.Lock() 56 sf, exists := cm.storageFolders[index] 57 if !exists { 58 cm.wal.mu.Unlock() 59 return errStorageFolderNotFound 60 } 61 cm.wal.mu.Unlock() 62 63 // Lock the storage folder for the duration of the operation. 64 sf.mu.Lock() 65 defer sf.mu.Unlock() 66 67 // Clear out the sectors in the storage folder. 68 _, err := cm.wal.managedEmptyStorageFolder(index, 0) 69 if err != nil && !force { 70 return err 71 } 72 73 // Wait for a synchronize to confirm that all of the moves have succeeded 74 // in full. 75 cm.wal.mu.Lock() 76 syncChan := cm.wal.syncChan 77 cm.wal.mu.Unlock() 78 <-syncChan 79 80 // Submit a storage folder removal to the WAL and wait until the update is 81 // synced. 82 cm.wal.mu.Lock() 83 cm.wal.appendChange(stateChange{ 84 StorageFolderRemovals: []storageFolderRemoval{{ 85 Index: index, 86 Path: sf.path, 87 }}, 88 }) 89 90 // Wait until the removal action has been synchronized. 91 syncChan = cm.wal.syncChan 92 cm.wal.mu.Unlock() 93 <-syncChan 94 return nil 95 }