// createDirWithOverlayOpaque creates a directory with trusted.overlay.opaque xattr,
// without calling setxattr, so as to allow creating opaque dir in userns on Ubuntu.
774287
func createDirWithOverlayOpaque(tmp string) (string, error) {
	lower := filepath.Join(tmp, "l")
	upper := filepath.Join(tmp, "u")
	work := filepath.Join(tmp, "w")
	merged := filepath.Join(tmp, "m")
	for _, s := range []string{lower, upper, work, merged} {
		if err := os.MkdirAll(s, 0700); err != nil {
			return "", errors.Wrapf(err, "failed to mkdir %s", s)
		}
	}
	dummyBase := "d"
	lowerDummy := filepath.Join(lower, dummyBase)
	if err := os.MkdirAll(lowerDummy, 0700); err != nil {
		return "", errors.Wrapf(err, "failed to create a dummy lower directory %s", lowerDummy)
	}
	mOpts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lower, upper, work)
	// docker/pkg/mount.Mount() requires procfs to be mounted. So we use syscall.Mount() directly instead.
	if err := syscall.Mount("overlay", merged, "overlay", uintptr(0), mOpts); err != nil {
		return "", errors.Wrapf(err, "failed to mount overlay (%s) on %s", mOpts, merged)
	}
	mergedDummy := filepath.Join(merged, dummyBase)
	if err := os.Remove(mergedDummy); err != nil {
		syscall.Unmount(merged, 0)
		return "", errors.Wrapf(err, "failed to rmdir %s", mergedDummy)
	}
	// upperDummy becomes a 0,0-char device file here
	if err := os.Mkdir(mergedDummy, 0700); err != nil {
		syscall.Unmount(merged, 0)
		return "", errors.Wrapf(err, "failed to mkdir %s", mergedDummy)
	}
	// upperDummy becomes a directory with trusted.overlay.opaque xattr
	// (but can't be verified in userns)
	if err := syscall.Unmount(merged, 0); err != nil {
		return "", errors.Wrapf(err, "failed to unmount %s", merged)
	}
	upperDummy := filepath.Join(upper, dummyBase)
	return upperDummy, nil
}
909735
func (c *Client) CreateVolumeDir(
	ctx context.Context,
	volumeName, dirPath string,
	fileMode os.FileMode,
	overwrite, recursive bool) error {

	return apiv2.ContainerCreateDir(
		ctx, c.API, volumeName, dirPath,
		apiv2.FileMode(fileMode), overwrite, recursive)
}
402888
func (o *Me) CreateDomainFIPAclTemplate(child *DomainFIPAclTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}
158750
func maskResourceWithPrefix(resource corev1.ResourceName, prefix string) corev1.ResourceName {
	return corev1.ResourceName(fmt.Sprintf("%s%s", prefix, string(resource)))
}
482526
func (o *Me) CreateUnderlay(child *Underlay) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}
153547
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) (retErr error) {

	if opts != nil && len(opts.StorageOpt) != 0 {
		return fmt.Errorf("--storage-opt is not supported for overlay")
	}

	dir := d.dir(id)

	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
	if err != nil {
		return err
	}
	root := idtools.Identity{UID: rootUID, GID: rootGID}

	if err := idtools.MkdirAllAndChown(path.Dir(dir), 0700, root); err != nil {
		return err
	}
	if err := idtools.MkdirAndChown(dir, 0700, root); err != nil {
		return err
	}

	defer func() {
		// Clean up on failure
		if retErr != nil {
			os.RemoveAll(dir)
		}
	}()

	// Toplevel images are just a "root" dir
	if parent == "" {
		return idtools.MkdirAndChown(path.Join(dir, "root"), 0755, root)
	}

	parentDir := d.dir(parent)

	// Ensure parent exists
	if _, err := os.Lstat(parentDir); err != nil {
		return err
	}

	// If parent has a root, just do an overlay to it
	parentRoot := path.Join(parentDir, "root")

	if s, err := os.Lstat(parentRoot); err == nil {
		if err := idtools.MkdirAndChown(path.Join(dir, "upper"), s.Mode(), root); err != nil {
			return err
		}
		if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
			return err
		}
		return ioutil.WriteFile(path.Join(dir, "lower-id"), []byte(parent), 0666)
	}

	// Otherwise, copy the upper and the lower-id from the parent

	lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id"))
	if err != nil {
		return err
	}

	if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0666); err != nil {
		return err
	}

	parentUpperDir := path.Join(parentDir, "upper")
	s, err := os.Lstat(parentUpperDir)
	if err != nil {
		return err
	}

	upperDir := path.Join(dir, "upper")
	if err := idtools.MkdirAndChown(upperDir, s.Mode(), root); err != nil {
		return err
	}
	if err := idtools.MkdirAndChown(path.Join(dir, "work"), 0700, root); err != nil {
		return err
	}

	return copy.DirCopy(parentUpperDir, upperDir, copy.Content, true)
}
295221
func (c *Client) CreateServicePlanVisibilityByUniqueId(uniqueId string, organizationGuid string) (ServicePlanVisibility, error) {
	q := url.Values{}
	q.Set("q", fmt.Sprintf("unique_id:%s", uniqueId))
	plans, err := c.ListServicePlansByQuery(q)
	if err != nil {
		return ServicePlanVisibility{}, errors.Wrap(err, fmt.Sprintf("Couldn't find a service plan with unique_id: %s", uniqueId))
	}
	return c.CreateServicePlanVisibility(plans[0].Guid, organizationGuid)
}
916972
func (s *RouterService) NewCreateVirtualRouterElementParams(nspid string) *CreateVirtualRouterElementParams {
	p := &CreateVirtualRouterElementParams{}
	p.p = make(map[string]interface{})
	p.p["nspid"] = nspid
	return p
}
595155
func CreateDirRecursively(dirToCreate string, permissions os.FileMode,
	owner, group int, backupOriginal bool) error {
	info, err := os.Stat(dirToCreate)
	if err == nil {
		if !info.IsDir() {
			return errors.New(dirToCreate + " exsists but it's not directory")
		}
		if backupOriginal {
			if err := os.Rename(dirToCreate, dirToCreate+".bkp"); err != nil {
				return err
			}
		}
	} else {
		if err := os.MkdirAll(dirToCreate, permissions); err != nil {
			return err
		}
	}
	if err = os.Chown(dirToCreate, owner, group); err != nil {
		return err
	}
	return nil
}
922702
func CreateLayer(path, parent string) (err error) {
	title := "hcsshim::CreateLayer"
	fields := logrus.Fields{
		"parent": parent,
		"path":   path,
	}
	logrus.WithFields(fields).Debug(title)
	defer func() {
		if err != nil {
			fields[logrus.ErrorKey] = err
			logrus.WithFields(fields).Error(err)
		} else {
			logrus.WithFields(fields).Debug(title + " - succeeded")
		}
	}()

	err = createLayer(&stdDriverInfo, path, parent)
	if err != nil {
		return hcserror.New(err, title+" - failed", "")
	}
	return nil
}
378577
func (c Config) CreateParentDir(fileName string) error {
	return os.MkdirAll(filepath.Dir(filepath.Join(c.Path, fileName)), 0755)
}
812348
func (c *cachedFS) Create(ctx context.Context, path string) (io.WriteCloser, error) {
	return c.src.Create(ctx, path)
}
840310
func (o *Me) CreateInfrastructureVscProfile(child *InfrastructureVscProfile) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}
479889
func (c *Container) bitmapXorRange(i, j uint64) {
	x := i >> 6
	y := (j - 1) >> 6
	var X uint64 = maxBitmap << (i % 64)
	var Y uint64 = maxBitmap >> (63 - ((j - 1) % 64))
	bitmap := c.bitmap()
	if x == y {
		cnt := popcount(bitmap[x])
		bitmap[x] ^= (X & Y) //// flip
		c.n += int32(popcount(bitmap[x]) - cnt)
	} else {
		cnt := popcount(bitmap[x])
		bitmap[x] ^= X
		c.n += int32(popcount(bitmap[x]) - cnt)
		for i := x + 1; i < y; i++ {
			cnt = popcount(bitmap[i])
			bitmap[i] ^= maxBitmap
			c.n += int32(popcount(bitmap[i]) - cnt)
		}
		cnt = popcount(bitmap[y])
		bitmap[y] ^= Y
		c.n += int32(popcount(bitmap[y]) - cnt)
	}
}
784437
func (o *L2Domain) CreateOverlayMirrorDestination(child *OverlayMirrorDestination) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}
572216
func prepareAppImage(cfg PrepareConfig, appName types.ACName, img types.Hash, cdir string, useOverlay bool) (string, error) {
	debug("Loading image %s", img.String())

	am, err := cfg.Store.GetImageManifest(img.String())
	if err != nil {
		return "", errwrap.Wrap(errors.New("error getting the manifest"), err)
	}

	if _, hasOS := am.Labels.Get("os"); !hasOS {
		return "", fmt.Errorf("missing os label in the image manifest")
	}

	if _, hasArch := am.Labels.Get("arch"); !hasArch {
		return "", fmt.Errorf("missing arch label in the image manifest")
	}

	if err := types.IsValidOSArch(am.Labels.ToMap(), ValidOSArch); err != nil {
		return "", err
	}

	appInfoDir := common.AppInfoPath(cdir, appName)
	if err := os.MkdirAll(appInfoDir, common.DefaultRegularDirPerm); err != nil {
		return "", errwrap.Wrap(errors.New("error creating apps info directory"), err)
	}

	var treeStoreID string
	if useOverlay {
		if cfg.PrivateUsers.Shift > 0 {
			return "", fmt.Errorf("cannot use both overlay and user namespace: not implemented yet. (Try --no-overlay)")
		}

		treeStoreID, _, err = cfg.TreeStore.Render(img.String(), false)
		if err != nil {
			return "", errwrap.Wrap(errors.New("error rendering tree image"), err)
		}

		if err := ioutil.WriteFile(common.AppTreeStoreIDPath(cdir, appName), []byte(treeStoreID), common.DefaultRegularFilePerm); err != nil {
			return "", errwrap.Wrap(errors.New("error writing app treeStoreID"), err)
		}
	} else {
		ad := common.AppPath(cdir, appName)

		err := os.MkdirAll(ad, common.DefaultRegularDirPerm)
		if err != nil {
			return "", errwrap.Wrap(errors.New("error creating image directory"), err)
		}

		shiftedUid, shiftedGid, err := cfg.PrivateUsers.ShiftRange(uint32(os.Getuid()), uint32(os.Getgid()))
		if err != nil {
			return "", errwrap.Wrap(errors.New("error getting uid, gid"), err)
		}

		if err := os.Chown(ad, int(shiftedUid), int(shiftedGid)); err != nil {
			return "", errwrap.Wrap(fmt.Errorf("error shifting app %q's stage2 dir", appName), err)
		}

		if err := aci.RenderACIWithImageID(img, ad, cfg.Store, cfg.PrivateUsers); err != nil {
			return "", errwrap.Wrap(errors.New("error rendering ACI"), err)
		}
	}

	if err := writeManifest(*cfg.CommonConfig, img, appInfoDir); err != nil {
		return "", errwrap.Wrap(errors.New("error writing manifest"), err)
	}

	return treeStoreID, nil
}
1246492
func (o *L2DomainTemplate) CreateOverlayMirrorDestinationTemplate(child *OverlayMirrorDestinationTemplate) *bambou.Error {

	return bambou.CurrentSession().CreateChild(o, child)
}