github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/snapstate/backend/copydata.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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  	"os"
    24  
    25  	"github.com/snapcore/snapd/logger"
    26  	"github.com/snapcore/snapd/progress"
    27  	"github.com/snapcore/snapd/snap"
    28  )
    29  
    30  // CopySnapData makes a copy of oldSnap data for newSnap in its data directories.
    31  func (b Backend) CopySnapData(newSnap, oldSnap *snap.Info, meter progress.Meter) error {
    32  	// deal with the old data or
    33  	// otherwise just create a empty data dir
    34  
    35  	// Make sure the base data directory exists for instance snaps
    36  	if newSnap.InstanceKey != "" {
    37  		err := os.MkdirAll(snap.BaseDataDir(newSnap.SnapName()), 0755)
    38  		if err != nil && !os.IsExist(err) {
    39  			return err
    40  		}
    41  	}
    42  
    43  	// Make sure the common data directory exists, even if this isn't a new
    44  	// install.
    45  	if err := os.MkdirAll(newSnap.CommonDataDir(), 0755); err != nil {
    46  		return err
    47  	}
    48  
    49  	if oldSnap == nil {
    50  		return os.MkdirAll(newSnap.DataDir(), 0755)
    51  	} else if oldSnap.Revision == newSnap.Revision {
    52  		// nothing to do
    53  		return nil
    54  	}
    55  
    56  	return copySnapData(oldSnap, newSnap)
    57  }
    58  
    59  // UndoCopySnapData removes the copy that may have been done for newInfo snap of oldInfo snap data and also the data directories that may have been created for newInfo snap.
    60  func (b Backend) UndoCopySnapData(newInfo *snap.Info, oldInfo *snap.Info, meter progress.Meter) error {
    61  	if oldInfo != nil && oldInfo.Revision == newInfo.Revision {
    62  		// nothing to do
    63  		return nil
    64  	}
    65  	err1 := b.RemoveSnapData(newInfo)
    66  	if err1 != nil {
    67  		logger.Noticef("Cannot remove data directories for %q: %v", newInfo.InstanceName(), err1)
    68  	}
    69  
    70  	var err2 error
    71  	if oldInfo == nil {
    72  		// first install, remove created common data dir
    73  		err2 = b.RemoveSnapCommonData(newInfo)
    74  		if err2 != nil {
    75  			logger.Noticef("Cannot remove common data directories for %q: %v", newInfo.InstanceName(), err2)
    76  		}
    77  	} else {
    78  		err2 = b.untrashData(newInfo)
    79  		if err2 != nil {
    80  			logger.Noticef("Cannot restore original data for %q while undoing: %v", newInfo.InstanceName(), err2)
    81  		}
    82  	}
    83  
    84  	return firstErr(err1, err2)
    85  }
    86  
    87  // ClearTrashedData removes the trash. It returns no errors on the assumption that it is called very late in the game.
    88  func (b Backend) ClearTrashedData(oldSnap *snap.Info) {
    89  	dirs, err := snapDataDirs(oldSnap)
    90  	if err != nil {
    91  		logger.Noticef("Cannot remove previous data for %q: %v", oldSnap.InstanceName(), err)
    92  		return
    93  	}
    94  
    95  	for _, d := range dirs {
    96  		if err := clearTrash(d); err != nil {
    97  			logger.Noticef("Cannot remove %s: %v", d, err)
    98  		}
    99  	}
   100  }