github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapshotstate/backend/restorestate.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package backend 21 22 import ( 23 "fmt" 24 "os" 25 "regexp" 26 27 "github.com/snapcore/snapd/logger" 28 "github.com/snapcore/snapd/randutil" 29 ) 30 31 // RestoreState stores information that can be used to cleanly revert (or finish 32 // cleaning up) a snapshot Restore. 33 // 34 // This is useful when a Restore is part of a chain of operations, and a later 35 // one failing necessitates undoing the Restore. 36 type RestoreState struct { 37 Done bool `json:"done,omitempty"` 38 Created []string `json:"created,omitempty"` 39 Moved []string `json:"moved,omitempty"` 40 // Config is here for convenience; this package doesn't touch it 41 Config map[string]interface{} `json:"config,omitempty"` 42 } 43 44 // Cleanup the backed up data from disk. 45 func (rs *RestoreState) Cleanup() { 46 if rs.Done { 47 logger.Noticef("Internal error: attempting to clean up a snapshot.RestoreState twice.") 48 return 49 } 50 rs.Done = true 51 for _, dir := range rs.Moved { 52 if err := os.RemoveAll(dir); err != nil { 53 logger.Noticef("Cannot remove directory tree rooted at %q: %v.", dir, err) 54 } 55 } 56 } 57 58 func restoreStateFilename(fn string) string { 59 return fmt.Sprintf("%s.~%s~", fn, randutil.RandomString(9)) 60 } 61 62 var restoreStateRx = regexp.MustCompile(`\.~[a-zA-Z0-9]{9}~$`) 63 64 func restoreState2orig(fn string) string { 65 if idx := restoreStateRx.FindStringIndex(fn); len(idx) > 0 { 66 return fn[:idx[0]] 67 } 68 return "" 69 } 70 71 // Revert the backed up data: remove what was added, move back what was moved aside. 72 func (rs *RestoreState) Revert() { 73 if rs.Done { 74 logger.Noticef("Internal error: attempting to revert a snapshot.RestoreState twice.") 75 return 76 } 77 rs.Done = true 78 for _, dir := range rs.Created { 79 logger.Debugf("Removing %q.", dir) 80 if err := os.RemoveAll(dir); err != nil { 81 logger.Noticef("While undoing changes because of a previous error: cannot remove %q: %v.", dir, err) 82 } 83 } 84 for _, dir := range rs.Moved { 85 orig := restoreState2orig(dir) 86 if orig == "" { 87 // dir is not restore state?!? 88 logger.Debugf("Skipping restore of %q: unrecognised filename.", dir) 89 continue 90 } 91 logger.Debugf("Restoring %q to %q.", dir, orig) 92 if err := os.Rename(dir, orig); err != nil { 93 logger.Noticef("While undoing changes because of a previous error: cannot restore %q to %q: %v.", dir, orig, err) 94 } 95 } 96 }