github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/fs/restore.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fs
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/sync"
    19  )
    20  
    21  // RestoreEnvironment is the restore environment for file systems. It consists
    22  // of things that change across save and restore and therefore cannot be saved
    23  // in the object graph.
    24  type RestoreEnvironment struct {
    25  	// MountSources maps Filesystem.Name() to mount arguments.
    26  	MountSources map[string][]MountArgs
    27  
    28  	// ValidateFileSize indicates file size should not change across S/R.
    29  	ValidateFileSize bool
    30  
    31  	// ValidateFileTimestamp indicates file modification timestamp should
    32  	// not change across S/R.
    33  	ValidateFileTimestamp bool
    34  }
    35  
    36  // MountArgs holds arguments to Mount.
    37  type MountArgs struct {
    38  	// Dev corresponds to the devname argumnent of Mount.
    39  	Dev string
    40  
    41  	// Flags corresponds to the flags argument of Mount.
    42  	Flags MountSourceFlags
    43  
    44  	// DataString corresponds to the data argument of Mount.
    45  	DataString string
    46  
    47  	// DataObj corresponds to the data interface argument of Mount.
    48  	DataObj interface{}
    49  }
    50  
    51  // restoreEnv holds the fs package global RestoreEnvironment.
    52  var restoreEnv = struct {
    53  	mu  sync.Mutex
    54  	env RestoreEnvironment
    55  	set bool
    56  }{}
    57  
    58  // SetRestoreEnvironment sets the RestoreEnvironment. Must be called before
    59  // state.Load and only once.
    60  func SetRestoreEnvironment(r RestoreEnvironment) {
    61  	restoreEnv.mu.Lock()
    62  	defer restoreEnv.mu.Unlock()
    63  	if restoreEnv.set {
    64  		panic("RestoreEnvironment may only be set once")
    65  	}
    66  	restoreEnv.env = r
    67  	restoreEnv.set = true
    68  }
    69  
    70  // CurrentRestoreEnvironment returns the current, read-only RestoreEnvironment.
    71  // If no RestoreEnvironment was ever set, returns (_, false).
    72  func CurrentRestoreEnvironment() (RestoreEnvironment, bool) {
    73  	restoreEnv.mu.Lock()
    74  	defer restoreEnv.mu.Unlock()
    75  	e := restoreEnv.env
    76  	set := restoreEnv.set
    77  	return e, set
    78  }