style: wsl

Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
Matej Vasek
2020-11-04 22:24:04 +01:00
parent 430729a391
commit 7da4083549

View File

@ -49,6 +49,7 @@ func handleHeadOrGet(w http.ResponseWriter, r *http.Request, decoder *schema.Dec
utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.Wrap(err, "couldn't decode the query")) utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.Wrap(err, "couldn't decode the query"))
return return
} }
if query.Path == "" { if query.Path == "" {
utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.New("missing `path` parameter")) utils.Error(w, "Bad Request.", http.StatusBadRequest, errors.New("missing `path` parameter"))
return return
@ -64,11 +65,13 @@ func handleHeadOrGet(w http.ResponseWriter, r *http.Request, decoder *schema.Dec
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err)
return return
} }
mountPoint, err := ctr.Mount() mountPoint, err := ctr.Mount()
if err != nil { if err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "failed to mount the container")) utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "failed to mount the container"))
return return
} }
defer func() { defer func() {
if err := ctr.Unmount(true); err != nil { if err := ctr.Unmount(true); err != nil {
logrus.Warnf("failed to unmount container %s: %q", containerName, err) logrus.Warnf("failed to unmount container %s: %q", containerName, err)
@ -88,14 +91,18 @@ func handleHeadOrGet(w http.ResponseWriter, r *http.Request, decoder *schema.Dec
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "failed to get stats about file")) utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "failed to get stats about file"))
return return
} }
if len(stats) <= 0 || len(stats[0].Globbed) <= 0 { if len(stats) <= 0 || len(stats[0].Globbed) <= 0 {
errs := make([]string, 0, len(stats)) errs := make([]string, 0, len(stats))
for _, stat := range stats { for _, stat := range stats {
if stat.Error != "" { if stat.Error != "" {
errs = append(errs, stat.Error) errs = append(errs, stat.Error)
} }
} }
utils.Error(w, "Not found.", http.StatusNotFound, fmt.Errorf("file doesn't exist (errs: %q)", strings.Join(errs, ";"))) utils.Error(w, "Not found.", http.StatusNotFound, fmt.Errorf("file doesn't exist (errs: %q)", strings.Join(errs, ";")))
return return
} }
@ -114,6 +121,7 @@ func handleHeadOrGet(w http.ResponseWriter, r *http.Request, decoder *schema.Dec
errors.Wrapf(err, "error getting IDMappingOptions")) errors.Wrapf(err, "error getting IDMappingOptions"))
return return
} }
destOwner := idtools.IDPair{UID: os.Getuid(), GID: os.Getgid()} destOwner := idtools.IDPair{UID: os.Getuid(), GID: os.Getgid()}
opts := copier.GetOptions{ opts := copier.GetOptions{
@ -125,6 +133,7 @@ func handleHeadOrGet(w http.ResponseWriter, r *http.Request, decoder *schema.Dec
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
err = copier.Get(mountPoint, "", opts, []string{filepath.Join(mountPoint, path)}, w) err = copier.Get(mountPoint, "", opts, []string{filepath.Join(mountPoint, path)}, w)
if err != nil { if err != nil {
logrus.Error(errors.Wrapf(err, "failed to copy from the %s container path %s", containerName, query.Path)) logrus.Error(errors.Wrapf(err, "failed to copy from the %s container path %s", containerName, query.Path))
@ -156,11 +165,13 @@ func handlePut(w http.ResponseWriter, r *http.Request, decoder *schema.Decoder,
utils.Error(w, "Not found", http.StatusNotFound, errors.Wrapf(err, "the %s container doesn't exists", ctrName)) utils.Error(w, "Not found", http.StatusNotFound, errors.Wrapf(err, "the %s container doesn't exists", ctrName))
return return
} }
mountPoint, err := ctr.Mount() mountPoint, err := ctr.Mount()
if err != nil { if err != nil {
utils.Error(w, "Something went wrong", http.StatusInternalServerError, errors.Wrapf(err, "failed to mount the %s container", ctrName)) utils.Error(w, "Something went wrong", http.StatusInternalServerError, errors.Wrapf(err, "failed to mount the %s container", ctrName))
return return
} }
defer func() { defer func() {
if err := ctr.Unmount(true); err != nil { if err := ctr.Unmount(true); err != nil {
logrus.Warnf("failed to unmount container %s", ctrName) logrus.Warnf("failed to unmount container %s", ctrName)
@ -172,11 +183,13 @@ func handlePut(w http.ResponseWriter, r *http.Request, decoder *schema.Decoder,
utils.Error(w, "Something went wrong", http.StatusInternalServerError, err) utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
return return
} }
idMappingOpts, err := ctr.IDMappings() idMappingOpts, err := ctr.IDMappings()
if err != nil { if err != nil {
utils.Error(w, "Something went wrong", http.StatusInternalServerError, errors.Wrapf(err, "error getting IDMappingOptions")) utils.Error(w, "Something went wrong", http.StatusInternalServerError, errors.Wrapf(err, "error getting IDMappingOptions"))
return return
} }
destOwner := idtools.IDPair{UID: int(user.UID), GID: int(user.GID)} destOwner := idtools.IDPair{UID: int(user.UID), GID: int(user.GID)}
opts := copier.PutOptions{ opts := copier.PutOptions{
@ -193,6 +206,7 @@ func handlePut(w http.ResponseWriter, r *http.Request, decoder *schema.Decoder,
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
err = copier.Put(mountPoint, filepath.Join(mountPoint, path), opts, r.Body) err = copier.Put(mountPoint, filepath.Join(mountPoint, path), opts, r.Body)
if err != nil { if err != nil {
logrus.Error(errors.Wrapf(err, "failed to copy to the %s container path %s", ctrName, query.Path)) logrus.Error(errors.Wrapf(err, "failed to copy to the %s container path %s", ctrName, query.Path))
@ -222,10 +236,12 @@ func statsToHeader(stats *copier.StatForItem) (string, error) {
buff := bytes.NewBuffer(make([]byte, 0, 128)) buff := bytes.NewBuffer(make([]byte, 0, 128))
base64encoder := base64.NewEncoder(base64.StdEncoding, buff) base64encoder := base64.NewEncoder(base64.StdEncoding, buff)
_, err = base64encoder.Write(jsonBytes) _, err = base64encoder.Write(jsonBytes)
if err != nil { if err != nil {
return "", err return "", err
} }
err = base64encoder.Close() err = base64encoder.Close()
if err != nil { if err != nil {
return "", err return "", err
@ -241,6 +257,7 @@ func getUser(mountPoint string, userspec string) (specs.User, error) {
GID: gid, GID: gid,
Username: userspec, Username: userspec,
} }
if !strings.Contains(userspec, ":") { if !strings.Contains(userspec, ":") {
groups, err2 := chrootuser.GetAdditionalGroupsForUser(mountPoint, uint64(u.UID)) groups, err2 := chrootuser.GetAdditionalGroupsForUser(mountPoint, uint64(u.UID))
if err2 != nil { if err2 != nil {
@ -251,6 +268,7 @@ func getUser(mountPoint string, userspec string) (specs.User, error) {
u.AdditionalGids = groups u.AdditionalGids = groups
} }
} }
return u, err return u, err
} }
@ -258,6 +276,7 @@ func fixUpMountPointAndPath(runtime *libpod.Runtime, ctr *libpod.Container, moun
if !filepath.IsAbs(ctrPath) { if !filepath.IsAbs(ctrPath) {
endsWithSep := strings.HasSuffix(ctrPath, string(filepath.Separator)) endsWithSep := strings.HasSuffix(ctrPath, string(filepath.Separator))
ctrPath = filepath.Join(ctr.WorkingDir(), ctrPath) ctrPath = filepath.Join(ctr.WorkingDir(), ctrPath)
if endsWithSep { if endsWithSep {
ctrPath = ctrPath + string(filepath.Separator) ctrPath = ctrPath + string(filepath.Separator)
} }
@ -267,6 +286,7 @@ func fixUpMountPointAndPath(runtime *libpod.Runtime, ctr *libpod.Container, moun
if err != nil { if err != nil {
return "", "", errors.Wrapf(err, "error getting source path from volume %s", volDestName) return "", "", errors.Wrapf(err, "error getting source path from volume %s", volDestName)
} }
mountPoint = newMountPoint mountPoint = newMountPoint
ctrPath = path ctrPath = path
} else if isBindMount, mount := isBindMountDestName(ctrPath, ctr); isBindMount { //nolint(gocritic) } else if isBindMount, mount := isBindMountDestName(ctrPath, ctr); isBindMount { //nolint(gocritic)
@ -274,23 +294,28 @@ func fixUpMountPointAndPath(runtime *libpod.Runtime, ctr *libpod.Container, moun
mountPoint = newMountPoint mountPoint = newMountPoint
ctrPath = path ctrPath = path
} }
return mountPoint, ctrPath, nil return mountPoint, ctrPath, nil
} }
func isVolumeDestName(path string, ctr *libpod.Container) (bool, string, string) { func isVolumeDestName(path string, ctr *libpod.Container) (bool, string, string) {
separator := string(os.PathSeparator) separator := string(os.PathSeparator)
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
path = strings.TrimPrefix(path, separator) path = strings.TrimPrefix(path, separator)
} }
if path == "" { if path == "" {
return false, "", "" return false, "", ""
} }
for _, vol := range ctr.Config().NamedVolumes { for _, vol := range ctr.Config().NamedVolumes {
volNamePath := strings.TrimPrefix(vol.Dest, separator) volNamePath := strings.TrimPrefix(vol.Dest, separator)
if matchVolumePath(path, volNamePath) { if matchVolumePath(path, volNamePath) {
return true, vol.Dest, vol.Name return true, vol.Dest, vol.Name
} }
} }
return false, "", "" return false, "", ""
} }
@ -299,38 +324,47 @@ func pathWithVolumeMount(runtime *libpod.Runtime, volDestName, volName, path str
if err != nil { if err != nil {
return "", "", errors.Wrapf(err, "error getting volume destination %s", volName) return "", "", errors.Wrapf(err, "error getting volume destination %s", volName)
} }
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = filepath.Join(string(os.PathSeparator), path) path = filepath.Join(string(os.PathSeparator), path)
} }
return destVolume.MountPoint(), strings.TrimPrefix(path, volDestName), err return destVolume.MountPoint(), strings.TrimPrefix(path, volDestName), err
} }
func isBindMountDestName(path string, ctr *libpod.Container) (bool, specs.Mount) { func isBindMountDestName(path string, ctr *libpod.Container) (bool, specs.Mount) {
separator := string(os.PathSeparator) separator := string(os.PathSeparator)
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
path = strings.TrimPrefix(path, string(os.PathSeparator)) path = strings.TrimPrefix(path, string(os.PathSeparator))
} }
if path == "" { if path == "" {
return false, specs.Mount{} return false, specs.Mount{}
} }
for _, m := range ctr.Config().Spec.Mounts { for _, m := range ctr.Config().Spec.Mounts {
if m.Type != "bind" { if m.Type != "bind" {
continue continue
} }
mDest := strings.TrimPrefix(m.Destination, separator) mDest := strings.TrimPrefix(m.Destination, separator)
if matchVolumePath(path, mDest) { if matchVolumePath(path, mDest) {
return true, m return true, m
} }
} }
return false, specs.Mount{} return false, specs.Mount{}
} }
func matchVolumePath(path, target string) bool { func matchVolumePath(path, target string) bool {
pathStr := filepath.Clean(path) pathStr := filepath.Clean(path)
target = filepath.Clean(target) target = filepath.Clean(target)
for len(pathStr) > len(target) && strings.Contains(pathStr, string(os.PathSeparator)) { for len(pathStr) > len(target) && strings.Contains(pathStr, string(os.PathSeparator)) {
pathStr = pathStr[:strings.LastIndex(pathStr, string(os.PathSeparator))] pathStr = pathStr[:strings.LastIndex(pathStr, string(os.PathSeparator))]
} }
return pathStr == target return pathStr == target
} }
@ -338,5 +372,6 @@ func pathWithBindMountSource(m specs.Mount, path string) (string, string) {
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = filepath.Join(string(os.PathSeparator), path) path = filepath.Join(string(os.PathSeparator), path)
} }
return m.Source, strings.TrimPrefix(path, m.Destination) return m.Source, strings.TrimPrefix(path, m.Destination)
} }