// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
// The mounts are valid during the call to the f.
// Finally we will unmount and remove the temp dir regardless of the result of f.
349527
func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
	root, uerr := ioutil.TempDir(tempMountLocation, "containerd-mount")
	if uerr != nil {
		return errors.Wrapf(uerr, "failed to create temp dir")
	}
	// We use Remove here instead of RemoveAll.
	// The RemoveAll will delete the temp dir and all children it contains.
	// When the Unmount fails, RemoveAll will incorrectly delete data from
	// the mounted dir. However, if we use Remove, even though we won't
	// successfully delete the temp dir and it may leak, we won't loss data
	// from the mounted dir.
	// For details, please refer to #1868 #1785.
	defer func() {
		if uerr = os.Remove(root); uerr != nil {
			log.G(ctx).WithError(uerr).WithField("dir", root).Errorf("failed to remove mount temp dir")
		}
	}()

	// We should do defer first, if not we will not do Unmount when only a part of Mounts are failed.
	defer func() {
		if uerr = UnmountAll(root, 0); uerr != nil {
			uerr = errors.Wrapf(uerr, "failed to unmount %s", root)
			if err == nil {
				err = uerr
			} else {
				err = errors.Wrap(err, uerr.Error())
			}
		}
	}()
	if uerr = All(mounts, root); uerr != nil {
		return errors.Wrapf(uerr, "failed to mount %s", root)
	}
	return errors.Wrapf(f(root), "mount callback failed on %s", root)
}
1196147
func (spec TempDirectorySpec) Close() error {
	if err := spec.CleanUp(); err != nil {
		return errors.Annotate(err, "could not clean up temp dir")
	}
	return nil
}
394407
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
	file, err := ioutil.TempFile(dir, prefix)
	if err != nil {
		return nil, err
	}
	return &defaultFile{file}, nil
}
893309
func (mount *MountInfo) IsMounted() bool {
	ret := C.ceph_is_mounted(mount.mount)
	return ret == 1
}
345721
func (fs *FS) TempFile(dir, prefix string) (billy.File, error) {
	if err := fs.requireNonEmpty(); err != nil {
		return nil, err
	}

	// We'd have to turn off journaling to support TempFile perfectly,
	// but the given uniq ID and a random number should be good
	// enough.  Especially since most users will end up renaming the
	// temp file before journal flushing even happens.
	b := make([]byte, 8)
	_, err := rand.Read(b)
	if err != nil {
		return nil, err
	}
	suffix := fs.uniqID + "-" + base64.URLEncoding.EncodeToString(b)
	return fs.OpenFile(path.Join(dir, prefix+suffix),
		os.O_CREATE|os.O_EXCL, 0600)
}
902061
func TmpDir(t TestReporter, dir string) string {
	name, err := afero.TempDir(appFs, dir, "dir")
	if err != nil {
		t.Error("Failed to create the tmpDir: "+name, err)
	}

	Files = append(Files, name)
	return name
}
805041
func bindMountImp(src, dst string) error {
	glog.Infof("bindMount %s at %s", src, dst)
	if mounted, err := mp.IsMounted(dst); err != nil {
		return err
	} else if mounted {
		return nil
	}
	if err := os.MkdirAll(dst, 0775); err != nil {
		return err
	}
	runMountCommand := func(options ...string) ([]byte, error) {
		cmd, args := mntArgs(src, dst, "", options...)
		mount := exec.Command(cmd, args...)
		glog.Infof("running mount: %s %s", cmd, strings.Join(args, " "))
		return mount.CombinedOutput()
	}
	out, returnErr := runMountCommand("bind")
	if returnErr != nil {
		// If the mount fails, it could be due to a stale NFS handle, signalled
		// by a return code of 32. Stale handle can occur if e.g., the source
		// directory has been deleted and restored (a common occurrence in the
		// dev workflow) Try again, with remount option.
		if exitcode, ok := utils.GetExitStatus(returnErr); ok && (exitcode&32) != 0 {
			out, returnErr = runMountCommand("bind", "remount")
		}
	}
	if returnErr != nil {
		return fmt.Errorf("%s: %s", out, returnErr)
	}
	return nil
}
1078127
func (mount *MountInfo) Mount() error {
	ret := C.ceph_mount(mount.mount, nil)
	if ret != 0 {
		log.Errorf("Mount: Failed to mount")
		return cephError(ret)
	}
	return nil
}
87998
func (td *TempDir) Restore() {
	err := os.RemoveAll(td.dir)
	if err != nil {
		msg := fmt.Sprintf("cannot remove temporary directory %q: %v", td.dir, err)
		td.assert.Fail(msg)
	}
}
758852
func NewTempFile(filename string) (*TempFile, error) {
	installationTmpFile, err := ioutil.TempFile("", filename)
	return &TempFile{
		FileRef: installationTmpFile,
	}, err
}
296580
func deferDeleteTempFile(path string) {
	tempFilesMu.Lock()
	tempFiles = append(tempFiles, path)
	tempFilesMu.Unlock()
}
1277050
func (u *Updater) Cleanup(tmpDir string) {
	if tmpDir != "" {
		u.log.Debugf("Remove temporary directory: %q", tmpDir)
		if err := os.RemoveAll(tmpDir); err != nil {
			u.log.Warningf("Error removing temporary directory %q: %s", tmpDir, err)
		}
	}
}
1238075
func (DefaultFs) TempDir(dir, prefix string) (string, error) {
	return ioutil.TempDir(dir, prefix)
}
145488
func mount(pendingMounts ...Mounter) {
	for _, mounter := range pendingMounts {
		if mounter == nil {
			continue
		}
		if c, ok := mounter.(Component); ok {
			if c.Context().mounted {
				continue
			}
			c.Context().mounted = true
			c.Context().unmounted = false
		}
		mounter.Mount()
	}
}
273634
func prepareTmp(topTmpDir string) (string, error) {
	tmpdir, err := ioutil.TempDir(topTmpDir, "runctop")
	if err != nil {
		return "", err
	}
	if err := unix.Mount(tmpdir, tmpdir, "bind", unix.MS_BIND, ""); err != nil {
		return "", err
	}
	if err := unix.Mount("", tmpdir, "", uintptr(unix.MS_PRIVATE), ""); err != nil {
		return "", err
	}
	return tmpdir, nil
}
63903
func TempDir(prefix string) (path string, err error) {
	// Create temp file
	var f *os.File
	if f, err = ioutil.TempFile(os.TempDir(), prefix); err != nil {
		err = errors.Wrap(err, "creating temporary file failed")
		return
	}
	path = f.Name()

	// Close temp file
	if err = f.Close(); err != nil {
		err = errors.Wrapf(err, "closing file %s failed", path)
		return
	}

	// Delete temp file
	if err = os.Remove(path); err != nil {
		err = errors.Wrapf(err, "removing %s failed", path)
		return
	}

	// Create temp dir
	if err = os.MkdirAll(path, 0755); err != nil {
		err = errors.Wrapf(err, "mkdirall of %s failed", path)
		return
	}
	return
}
317865
func Remove(tempdir string) error {
	defer func() {
		var k string
		runningMutex.Lock()
		for key, value := range running {
			if value == tempdir {
				k = key
			}
		}
		delete(running, k)
		runningMutex.Unlock()
	}()
	if !strings.HasPrefix(tempdir, os.TempDir()) {
		return errors.New("Remove can only remove temporary directories, tryied to remove " + tempdir)
	}
	path := os.Getenv("PATH")
	index := strings.Index(path, tempdir)
	if index < 0 {
		return fmt.Errorf("%q is not in $PATH", tempdir)
	}
	path = path[:index] + path[index+len(tempdir)+1:]
	err := os.Setenv("PATH", path)
	if err != nil {
		return err
	}
	return os.RemoveAll(tempdir)
}