github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/mount/temp_unix.go (about)

     1  // +build !windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package mount
    20  
    21  import (
    22  	"os"
    23  	"path/filepath"
    24  	"sort"
    25  	"strings"
    26  )
    27  
    28  // SetTempMountLocation sets the temporary mount location
    29  func SetTempMountLocation(root string) error {
    30  	root, err := filepath.Abs(root)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	if err := os.MkdirAll(root, 0700); err != nil {
    35  		return err
    36  	}
    37  	tempMountLocation = root
    38  	return nil
    39  }
    40  
    41  // CleanupTempMounts all temp mounts and remove the directories
    42  func CleanupTempMounts(flags int) (warnings []error, err error) {
    43  	mounts, err := Self()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	var toUnmount []string
    48  	for _, m := range mounts {
    49  		if strings.HasPrefix(m.Mountpoint, tempMountLocation) {
    50  			toUnmount = append(toUnmount, m.Mountpoint)
    51  		}
    52  	}
    53  	sort.Sort(sort.Reverse(sort.StringSlice(toUnmount)))
    54  	for _, path := range toUnmount {
    55  		if err := UnmountAll(path, flags); err != nil {
    56  			warnings = append(warnings, err)
    57  			continue
    58  		}
    59  		if err := os.Remove(path); err != nil {
    60  			warnings = append(warnings, err)
    61  		}
    62  	}
    63  	return warnings, nil
    64  }