Skip to content

Commit

Permalink
merge branch 'pr-357'
Browse files Browse the repository at this point in the history
Mateusz Kwiatkowski (3):
  Provide system dependend wrapper for unix.Mknod
  Make Makefile more portable
  Add vim swap file to .gitignore

LGTMs: @cyphar @tyhc0
Closes #357
cyphar committed Mar 11, 2021
2 parents 130e11a + ea32e1d commit 5412ded
Showing 11 changed files with 101 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@
/umoci.cov*
/.cache
/release
*.swp
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@ RUN zypper -n in \
python-xattr \
runc \
skopeo \
tar
tar \
which
RUN useradd -u 1000 -m -d /home/rootless -s /bin/bash rootless

ENV GOPATH=/go PATH=/go/bin:$PATH
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -14,11 +14,13 @@
# limitations under the License.

# Use bash, so that we can do process substitution.
SHELL = /bin/bash
SHELL := $(shell which bash)

# Go tools.
GO ?= go
GO_MD2MAN ?= go-md2man
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
export GO111MODULE=on

# Set up the ... lovely ... GOPATH hacks.
@@ -68,13 +70,20 @@ BASE_FLAGS := ${BUILD_FLAGS} -tags "${BUILDTAGS}"
BASE_LDFLAGS := -s -w -X ${PROJECT}.gitCommit=${COMMIT} -X ${PROJECT}.version=${VERSION}

# Specific build flags for build type.
DYN_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS}"
TEST_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test"
ifeq ($(GOOS), linux)
TEST_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test" DYN_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS}"
TEST_BUILD_FLAGS := ${BASE_FLAGS} -buildmode=pie -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test"
else
DYN_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS}"
TEST_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS} -X ${PROJECT}/pkg/testutils.binaryType=test"
endif


STATIC_BUILD_FLAGS := ${BASE_FLAGS} -ldflags "${BASE_LDFLAGS} -extldflags '-static'"

# Installation directories.
DESTDIR ?=
PREFIX ?=/usr
PREFIX ?=/usr/local
BINDIR ?=$(PREFIX)/bin
MANDIR ?=$(PREFIX)/share/man

@@ -85,7 +94,7 @@ GO_SRC = $(shell find . -type f -name '*.go')
# NOTE: If you change these make sure you also update local-validate-build.

umoci: $(GO_SRC)
$(GO) build ${DYN_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD}
GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build ${DYN_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD}

umoci.static: $(GO_SRC)
env CGO_ENABLED=0 $(GO) build ${STATIC_BUILD_FLAGS} -o $(BUILD_DIR)/$@ ${CMD}
5 changes: 3 additions & 2 deletions oci/layer/generate_linux_test.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
"testing"

"github.com/opencontainers/umoci/pkg/fseval"
"github.com/opencontainers/umoci/pkg/system"
"github.com/stretchr/testify/assert"
"github.com/vbatts/go-mtree"
"golang.org/x/sys/unix"
@@ -32,7 +33,7 @@ func TestInsertLayerTranslateOverlayWhiteouts(t *testing.T) {
}

testNode := path.Join(dir, "test")
err = unix.Mknod(testNode, unix.S_IFCHR|0666, int(unix.Mkdev(0, 0)))
err = system.Mknod(testNode, unix.S_IFCHR|0666, unix.Mkdev(0, 0))
assert.NoError(err)

packOptions := RepackOptions{TranslateOverlayWhiteouts: true}
@@ -69,7 +70,7 @@ func TestGenerateLayerTranslateOverlayWhiteouts(t *testing.T) {
}

testNode := path.Join(dir, "test")
err = unix.Mknod(testNode, unix.S_IFCHR|0666, int(unix.Mkdev(0, 0)))
err = system.Mknod(testNode, unix.S_IFCHR|0666, unix.Mkdev(0, 0))
assert.NoError(err)

packOptions := RepackOptions{TranslateOverlayWhiteouts: true}
3 changes: 2 additions & 1 deletion oci/layer/tar_extract_linux_test.go
Original file line number Diff line number Diff line change
@@ -26,12 +26,13 @@ import (
"path/filepath"
"testing"

"github.com/opencontainers/umoci/pkg/system"
"golang.org/x/sys/unix"
)

func canMknod(dir string) (bool, error) {
testNode := filepath.Join(dir, "test")
err := unix.Mknod(testNode, unix.S_IFCHR|0666, int(unix.Mkdev(0, 0)))
err := system.Mknod(testNode, unix.S_IFCHR|0666, unix.Mkdev(0, 0))
if err != nil {
if os.IsPermission(err) {
return false, nil
2 changes: 1 addition & 1 deletion pkg/fseval/fseval_default.go
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ func (fs osFsEval) RemoveAll(path string) error {

// Mknod is equivalent to unix.Mknod.
func (fs osFsEval) Mknod(path string, mode os.FileMode, dev uint64) error {
return unix.Mknod(path, uint32(mode), int(dev))
return system.Mknod(path, uint32(mode), dev)
}

// MkdirAll is equivalent to os.MkdirAll.
28 changes: 28 additions & 0 deletions pkg/system/mknod_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package system

import (
"golang.org/x/sys/unix"
)

// Mknod creates a filesystem node (file, device special file or named pipe) named path
// with attributes specified by mode and dev.
func Mknod(path string, mode uint32, dev uint64) error {
return unix.Mknod(path, mode, dev)
}
25 changes: 6 additions & 19 deletions pkg/system/mknod_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !freebsd

/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
@@ -18,26 +20,11 @@
package system

import (
"archive/tar"

"golang.org/x/sys/unix"
)

// Tarmode takes a Typeflag (from a tar.Header for example) and returns the
// corresponding os.Filemode bit. Unknown typeflags are treated like regular
// files.
func Tarmode(typeflag byte) uint32 {
switch typeflag {
case tar.TypeSymlink:
return unix.S_IFLNK
case tar.TypeChar:
return unix.S_IFCHR
case tar.TypeBlock:
return unix.S_IFBLK
case tar.TypeFifo:
return unix.S_IFIFO
case tar.TypeDir:
return unix.S_IFDIR
}
return 0
// Mknod creates a filesystem node (file, device special file or named pipe) named path
// with attributes specified by mode and dev.
func Mknod(path string, mode uint32, dev uint64) error {
return unix.Mknod(path, mode, int(dev))
}
43 changes: 43 additions & 0 deletions pkg/system/tarmode_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package system

import (
"archive/tar"

"golang.org/x/sys/unix"
)

// Tarmode takes a Typeflag (from a tar.Header for example) and returns the
// corresponding os.Filemode bit. Unknown typeflags are treated like regular
// files.
func Tarmode(typeflag byte) uint32 {
switch typeflag {
case tar.TypeSymlink:
return unix.S_IFLNK
case tar.TypeChar:
return unix.S_IFCHR
case tar.TypeBlock:
return unix.S_IFBLK
case tar.TypeFifo:
return unix.S_IFIFO
case tar.TypeDir:
return unix.S_IFDIR
}
return 0
}
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/unpriv/unpriv.go
Original file line number Diff line number Diff line change
@@ -451,7 +451,7 @@ func MkdirAll(path string, perm os.FileMode) error {
// required access bits to modify or resolve the path.
func Mknod(path string, mode os.FileMode, dev uint64) error {
return errors.Wrap(Wrap(path, func(path string) error {
return unix.Mknod(path, uint32(mode), int(dev))
return system.Mknod(path, uint32(mode), dev)
}), "unpriv.mknod")
}

0 comments on commit 5412ded

Please sign in to comment.