New Upstream Release - golang-github-tonistiigi-fifo
Ready changes
Summary
Merged new upstream version: 1.1.0 (was: 1.0.0).
Resulting package
Built on 2023-04-30T21:25 (took 11m49s)
The resulting binary packages can be installed (if you have the apt repository enabled) by running one of:
apt install -t fresh-releases golang-github-tonistiigi-fifo-dev
Lintian Result
Diff
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..d207b18
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.go text eol=lf
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..78e3524
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,69 @@
+name: CI
+
+on:
+ push:
+ branches: [ main ]
+ pull_request:
+ branches: [ main ]
+
+jobs:
+
+ checks:
+ name: Project Checks
+ runs-on: ubuntu-22.04
+ timeout-minutes: 5
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ path: src/github.com/containerd/fifo
+ fetch-depth: 25
+ - uses: actions/setup-go@v3
+ with:
+ go-version: 1.20.x
+ - uses: containerd/project-checks@v1.1.0
+ with:
+ working-directory: src/github.com/containerd/fifo
+
+ linters:
+ name: Linters
+ runs-on: ${{ matrix.os }}
+ timeout-minutes: 10
+
+ strategy:
+ matrix:
+ go-version: [1.20.x]
+ os: [ubuntu-22.04, macos-12, windows-2022]
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ path: src/github.com/containerd/fifo
+
+ - name: Set env
+ shell: bash
+ run: |
+ echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
+ echo "${{ github.workspace }}/bin" >> $GITHUB_PATH
+
+ - uses: golangci/golangci-lint-action@v3
+ with:
+ version: v1.51.1
+ working-directory: src/github.com/containerd/fifo
+
+ tests:
+ name: Tests
+ runs-on: ubuntu-22.04
+ timeout-minutes: 5
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ path: src/github.com/containerd/fifo
+
+ - uses: actions/setup-go@v3
+ with:
+ go-version: 1.20.x
+
+ - run: make test
+ working-directory: src/github.com/containerd/fifo
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7f7bd6a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+coverage.txt
+vendor/
diff --git a/.golangci.yml b/.golangci.yml
new file mode 100644
index 0000000..c124d3e
--- /dev/null
+++ b/.golangci.yml
@@ -0,0 +1,36 @@
+linters:
+ enable:
+ - exportloopref # Checks for pointers to enclosing loop variables
+ - gofmt
+ - goimports
+ - gosec
+ - ineffassign
+ - misspell
+ - nolintlint
+ - revive
+ - staticcheck
+ - tenv # Detects using os.Setenv instead of t.Setenv since Go 1.17
+ - unconvert
+ - unused
+ - vet
+ - dupword # Checks for duplicate words in the source code
+ disable:
+ - errcheck
+
+linters-settings:
+ gosec:
+ # The following issues surfaced when `gosec` linter
+ # was enabled. They are temporarily excluded to unblock
+ # the existing workflow, but still to be addressed by
+ # future works.
+ excludes:
+ - G204
+ - G305
+ - G306
+ - G402
+ - G404
+
+run:
+ timeout: 3m
+ skip-dirs:
+ - vendor
diff --git a/LICENSE b/LICENSE
index 8d318c1..261eeb9 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,21 +1,201 @@
-MIT
-
-Copyright (C) 2016 Tõnis Tiigi <tonistiigi@gmail.com>
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
\ No newline at end of file
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ 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.
diff --git a/Makefile b/Makefile
index c1c1cd5..40c5046 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,24 @@
-.PHONY: fmt vet test deps
+# Copyright The containerd Authors.
+
+# 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.
+
+.PHONY: check test deps
test: deps
- go test -v ./...
+ go test -v -race ./...
deps:
- go get -d -t ./...
-
-fmt:
- gofmt -s -l .
+ go mod vendor
-vet:
- go vet ./...
+check:
+ GOGC=75 golangci-lint run
diff --git a/debian/changelog b/debian/changelog
index e2d24c8..025cc49 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,4 +1,4 @@
-golang-github-tonistiigi-fifo (0.0~git20161203.0.fe870cc-2) UNRELEASED; urgency=medium
+golang-github-tonistiigi-fifo (1.1.0-1) UNRELEASED; urgency=medium
[ Alexandre Viau ]
* Point Vcs-* urls to salsa.debian.org.
@@ -6,7 +6,11 @@ golang-github-tonistiigi-fifo (0.0~git20161203.0.fe870cc-2) UNRELEASED; urgency=
[ Jelmer Vernooij ]
* Change priority extra to priority optional.
- -- Alexandre Viau <aviau@debian.org> Mon, 02 Apr 2018 21:01:31 -0400
+ [ Debian Janitor ]
+ * New upstream release.
+ * New upstream release.
+
+ -- Alexandre Viau <aviau@debian.org> Sun, 30 Apr 2023 21:14:02 -0000
golang-github-tonistiigi-fifo (0.0~git20161203.0.fe870cc-1) unstable; urgency=medium
diff --git a/errors.go b/errors.go
new file mode 100644
index 0000000..50f73b2
--- /dev/null
+++ b/errors.go
@@ -0,0 +1,28 @@
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
+
+import "errors"
+
+var (
+ ErrClosed = errors.New("fifo closed")
+ ErrCtrlClosed = errors.New("control of closed fifo")
+ ErrRdFrmWRONLY = errors.New("reading from write-only fifo")
+ ErrReadClosed = errors.New("reading from a closed fifo")
+ ErrWrToRDONLY = errors.New("writing to read-only fifo")
+ ErrWriteClosed = errors.New("writing to a closed fifo")
+)
diff --git a/fifo.go b/fifo.go
index 355944e..173bce9 100644
--- a/fifo.go
+++ b/fifo.go
@@ -1,14 +1,33 @@
+//go:build !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
import (
+ "context"
+ "fmt"
"io"
"os"
"runtime"
"sync"
"syscall"
- "github.com/pkg/errors"
- "golang.org/x/net/context"
+ "golang.org/x/sys/unix"
)
type fifo struct {
@@ -25,21 +44,46 @@ type fifo struct {
var leakCheckWg *sync.WaitGroup
+// OpenFifoDup2 is same as OpenFifo, but additionally creates a copy of the FIFO file descriptor with dup2 syscall.
+func OpenFifoDup2(ctx context.Context, fn string, flag int, perm os.FileMode, fd int) (io.ReadWriteCloser, error) {
+ f, err := openFifo(ctx, fn, flag, perm)
+ if err != nil {
+ return nil, fmt.Errorf("fifo error: %w", err)
+ }
+
+ if err := unix.Dup2(int(f.file.Fd()), fd); err != nil {
+ _ = f.Close()
+ return nil, fmt.Errorf("dup2 error: %w", err)
+ }
+
+ return f, nil
+}
+
// OpenFifo opens a fifo. Returns io.ReadWriteCloser.
// Context can be used to cancel this function until open(2) has not returned.
// Accepted flags:
-// - syscall.O_CREAT - create new fifo if one doesn't exist
-// - syscall.O_RDONLY - open fifo only from reader side
-// - syscall.O_WRONLY - open fifo only from writer side
-// - syscall.O_RDWR - open fifo from both sides, never block on syscall level
-// - syscall.O_NONBLOCK - return io.ReadWriteCloser even if other side of the
+// - syscall.O_CREAT - create new fifo if one doesn't exist
+// - syscall.O_RDONLY - open fifo only from reader side
+// - syscall.O_WRONLY - open fifo only from writer side
+// - syscall.O_RDWR - open fifo from both sides, never block on syscall level
+// - syscall.O_NONBLOCK - return io.ReadWriteCloser even if other side of the
// fifo isn't open. read/write will be connected after the actual fifo is
// open or after fifo is closed.
func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.ReadWriteCloser, error) {
+ fifo, err := openFifo(ctx, fn, flag, perm)
+ if fifo == nil {
+ // Do not return a non-nil ReadWriteCloser((*fifo)(nil)) value
+ // as that can confuse callers.
+ return nil, err
+ }
+ return fifo, err
+}
+
+func openFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (*fifo, error) {
if _, err := os.Stat(fn); err != nil {
if os.IsNotExist(err) && flag&syscall.O_CREAT != 0 {
- if err := mkfifo(fn, uint32(perm&os.ModePerm)); err != nil && !os.IsExist(err) {
- return nil, errors.Wrapf(err, "error creating fifo %v", fn)
+ if err := syscall.Mkfifo(fn, uint32(perm&os.ModePerm)); err != nil && !os.IsExist(err) {
+ return nil, fmt.Errorf("error creating fifo %v: %w", fn, err)
}
} else {
return nil, err
@@ -75,7 +119,11 @@ func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.Re
}
select {
case <-ctx.Done():
- f.Close()
+ select {
+ case <-f.opened:
+ default:
+ f.Close()
+ }
case <-f.opened:
case <-f.closed:
}
@@ -96,7 +144,7 @@ func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.Re
case <-ctx.Done():
err = ctx.Err()
default:
- err = errors.Errorf("fifo %v was closed before opening", h.Name())
+ err = fmt.Errorf("fifo %v was closed before opening", h.Name())
}
if file != nil {
file.Close()
@@ -127,7 +175,7 @@ func OpenFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (io.Re
// Read from a fifo to a byte array.
func (f *fifo) Read(b []byte) (int, error) {
if f.flag&syscall.O_WRONLY > 0 {
- return 0, errors.New("reading from write-only fifo")
+ return 0, ErrRdFrmWRONLY
}
select {
case <-f.opened:
@@ -138,14 +186,14 @@ func (f *fifo) Read(b []byte) (int, error) {
case <-f.opened:
return f.file.Read(b)
case <-f.closed:
- return 0, errors.New("reading from a closed fifo")
+ return 0, ErrReadClosed
}
}
// Write from byte array to a fifo.
func (f *fifo) Write(b []byte) (int, error) {
if f.flag&(syscall.O_WRONLY|syscall.O_RDWR) == 0 {
- return 0, errors.New("writing to read-only fifo")
+ return 0, ErrWrToRDONLY
}
select {
case <-f.opened:
@@ -156,7 +204,7 @@ func (f *fifo) Write(b []byte) (int, error) {
case <-f.opened:
return f.file.Write(b)
case <-f.closed:
- return 0, errors.New("writing to a closed fifo")
+ return 0, ErrWriteClosed
}
}
@@ -164,6 +212,10 @@ func (f *fifo) Write(b []byte) (int, error) {
// before open(2) has returned and fifo was never opened.
func (f *fifo) Close() (retErr error) {
for {
+ if f == nil {
+ return
+ }
+
select {
case <-f.closed:
f.handle.Close()
diff --git a/fifo_linux_test.go b/fifo_linux_test.go
index 1a7cf49..9f36a2b 100644
--- a/fifo_linux_test.go
+++ b/fifo_linux_test.go
@@ -1,9 +1,25 @@
-// +build linux
+//go:build linux
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
import (
- "io/ioutil"
+ "context"
"os"
"path/filepath"
"sync"
@@ -12,11 +28,10 @@ import (
"time"
"github.com/stretchr/testify/assert"
- "golang.org/x/net/context"
)
func TestFifoCloseAfterRm(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
diff --git a/fifo_nolinux_test.go b/fifo_nolinux_test.go
index 8de8b1e..1b99922 100644
--- a/fifo_nolinux_test.go
+++ b/fifo_nolinux_test.go
@@ -1,9 +1,25 @@
-// +build !linux
+//go:build !linux && !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
import (
- "io/ioutil"
+ "context"
"os"
"path/filepath"
"syscall"
@@ -11,11 +27,10 @@ import (
"time"
"github.com/stretchr/testify/assert"
- "golang.org/x/net/context"
)
func TestFifoCloseAfterRm(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
diff --git a/fifo_test.go b/fifo_test.go
index 3759263..96d9b3e 100644
--- a/fifo_test.go
+++ b/fifo_test.go
@@ -1,8 +1,26 @@
+//go:build !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
import (
+ "context"
"io"
- "io/ioutil"
"os"
"path/filepath"
"sync"
@@ -11,11 +29,10 @@ import (
"time"
"github.com/stretchr/testify/assert"
- "golang.org/x/net/context"
)
func TestFifoCancel(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
@@ -24,7 +41,8 @@ func TestFifoCancel(t *testing.T) {
leakCheckWg = nil
}()
- _, err = OpenFifo(context.Background(), filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_NONBLOCK, 0600)
+ f, err := OpenFifo(context.Background(), filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_NONBLOCK, 0600)
+ assert.Exactly(t, nil, f)
assert.NotNil(t, err)
assert.NoError(t, checkWgDone(leakCheckWg))
@@ -32,13 +50,13 @@ func TestFifoCancel(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
- f, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ f, err = OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
assert.NoError(t, err)
b := make([]byte, 32)
n, err := f.Read(b)
assert.Equal(t, n, 0)
- assert.EqualError(t, err, "reading from a closed fifo")
+ assert.Equal(t, err, ErrReadClosed)
select {
case <-ctx.Done():
@@ -49,7 +67,7 @@ func TestFifoCancel(t *testing.T) {
}
func TestFifoReadWrite(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
@@ -127,7 +145,7 @@ func TestFifoReadWrite(t *testing.T) {
}
func TestFifoCancelOneSide(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
@@ -164,13 +182,13 @@ func TestFifoCancelOneSide(t *testing.T) {
t.Fatal("read should have unblocked")
}
- assert.EqualError(t, err, "reading from a closed fifo")
+ assert.Equal(t, err, ErrReadClosed)
assert.NoError(t, checkWgDone(leakCheckWg))
}
func TestFifoBlocking(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
@@ -182,7 +200,8 @@ func TestFifoBlocking(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
- _, err = OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT, 0600)
+ f, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT, 0600)
+ assert.Exactly(t, nil, f)
assert.EqualError(t, err, "context deadline exceeded")
select {
@@ -237,7 +256,7 @@ func TestFifoBlocking(t *testing.T) {
}
func TestFifoORDWR(t *testing.T) {
- tmpdir, err := ioutil.TempDir("", "fifos")
+ tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
@@ -306,12 +325,150 @@ func TestFifoORDWR(t *testing.T) {
err = f.Close()
assert.NoError(t, err)
- n, err = r2.Read(b)
+ _, err = r2.Read(b)
assert.EqualError(t, err, io.EOF.Error())
assert.NoError(t, checkWgDone(leakCheckWg))
}
+func TestFifoCloseError(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ w.Close()
+
+ data := []byte("hello world!")
+ _, err = w.Write(data)
+ assert.Equal(t, ErrWriteClosed, err)
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ r.Close()
+
+ buf := make([]byte, len(data))
+ _, err = r.Read(buf)
+ assert.Equal(t, ErrReadClosed, err)
+}
+
+func TestFifoCloseWhileReading(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+
+ read := make(chan struct{})
+ readErr := make(chan error)
+
+ go func() {
+ buf := make([]byte, 32)
+ _, err := r.Read(buf)
+
+ if err != nil {
+ readErr <- err
+ return
+ }
+
+ close(read)
+
+ }()
+
+ time.Sleep(500 * time.Millisecond)
+ r.Close()
+
+ select {
+ case <-read:
+ t.Fatal("Read should not succeed")
+ case err := <-readErr:
+ assert.Equal(t, ErrReadClosed, err)
+ case <-time.After(500 * time.Millisecond):
+ t.Fatal("Read should not be blocked")
+ }
+}
+
+func TestFifoCloseWhileReadingAndWriting(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0)
+ assert.NoError(t, err)
+
+ read := make(chan struct{})
+ readErr := make(chan error)
+ wBuffer := []byte("foo")
+
+ go func() {
+ buf := make([]byte, 32)
+ _, err := r.Read(buf)
+
+ if err != nil {
+ readErr <- err
+ return
+ }
+
+ close(read)
+ }()
+
+ time.Sleep(500 * time.Millisecond)
+
+ // Close the reader and then write in the writer.
+ // The reader thread should return an error.
+ r.Close()
+
+ // The write should fail, the reader end of the pipe is closed.
+ _, err = w.Write(wBuffer)
+ assert.Error(t, err)
+
+ select {
+ case <-read:
+ t.Fatal("Read should not succeed")
+ case err := <-readErr:
+ assert.Error(t, err)
+ case <-time.After(500 * time.Millisecond):
+ t.Fatal("Read should not be blocked")
+ }
+}
+
+func TestFifoWrongRdWrError(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+
+ data := []byte("hello world!")
+ _, err = r.Write(data)
+ assert.Equal(t, ErrWrToRDONLY, err)
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+
+ buf := make([]byte, len(data))
+ _, err = w.Read(buf)
+ assert.Equal(t, ErrRdFrmWRONLY, err)
+}
+
func checkWgDone(wg *sync.WaitGroup) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..bd36dd6
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,14 @@
+module github.com/containerd/fifo
+
+go 1.18
+
+require (
+ github.com/stretchr/testify v1.8.1
+ golang.org/x/sys v0.5.0
+)
+
+require (
+ github.com/davecgh/go-spew v1.1.1 // indirect
+ github.com/pmezard/go-difflib v1.0.0 // indirect
+ gopkg.in/yaml.v3 v3.0.1 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..689fc9b
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,19 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/handle_linux.go b/handle_linux.go
index 7bda64c..9710cca 100644
--- a/handle_linux.go
+++ b/handle_linux.go
@@ -1,4 +1,20 @@
-// +build linux
+//go:build linux
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
@@ -7,14 +23,14 @@ import (
"os"
"sync"
"syscall"
-
- "github.com/pkg/errors"
)
+//nolint:revive
const O_PATH = 010000000
type handle struct {
f *os.File
+ fd uintptr
dev uint64
ino uint64
closeOnce sync.Once
@@ -24,33 +40,38 @@ type handle struct {
func getHandle(fn string) (*handle, error) {
f, err := os.OpenFile(fn, O_PATH, 0)
if err != nil {
- return nil, errors.Wrapf(err, "failed to open %v with O_PATH", fn)
+ return nil, fmt.Errorf("failed to open %v with O_PATH: %w", fn, err)
}
- var stat syscall.Stat_t
- if err := syscall.Fstat(int(f.Fd()), &stat); err != nil {
+ var (
+ stat syscall.Stat_t
+ fd = f.Fd()
+ )
+ if err := syscall.Fstat(int(fd), &stat); err != nil {
f.Close()
- return nil, errors.Wrapf(err, "failed to stat handle %v", f.Fd())
+ return nil, fmt.Errorf("failed to stat handle %v: %w", fd, err)
}
h := &handle{
f: f,
name: fn,
- dev: stat.Dev,
- ino: stat.Ino,
+ //nolint:unconvert
+ dev: uint64(stat.Dev),
+ ino: stat.Ino,
+ fd: fd,
}
// check /proc just in case
if _, err := os.Stat(h.procPath()); err != nil {
f.Close()
- return nil, errors.Wrapf(err, "couldn't stat %v", h.procPath())
+ return nil, fmt.Errorf("couldn't stat %v: %w", h.procPath(), err)
}
return h, nil
}
func (h *handle) procPath() string {
- return fmt.Sprintf("/proc/self/fd/%d", h.f.Fd())
+ return fmt.Sprintf("/proc/self/fd/%d", h.fd)
}
func (h *handle) Name() string {
@@ -60,10 +81,11 @@ func (h *handle) Name() string {
func (h *handle) Path() (string, error) {
var stat syscall.Stat_t
if err := syscall.Stat(h.procPath(), &stat); err != nil {
- return "", errors.Wrapf(err, "path %v could not be statted", h.procPath())
+ return "", fmt.Errorf("path %v could not be statted: %w", h.procPath(), err)
}
- if stat.Dev != h.dev || stat.Ino != h.ino {
- return "", errors.Errorf("failed to verify handle %v/%v %v/%v", stat.Dev, h.dev, stat.Ino, h.ino)
+ //nolint:unconvert
+ if uint64(stat.Dev) != h.dev || stat.Ino != h.ino {
+ return "", fmt.Errorf("failed to verify handle %v/%v %v/%v", stat.Dev, h.dev, stat.Ino, h.ino)
}
return h.procPath(), nil
}
diff --git a/handle_nolinux.go b/handle_nolinux.go
index d9648d8..f6863cf 100644
--- a/handle_nolinux.go
+++ b/handle_nolinux.go
@@ -1,11 +1,26 @@
-// +build !linux
+//go:build !linux && !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
import (
+ "fmt"
"syscall"
-
- "github.com/pkg/errors"
)
type handle struct {
@@ -17,13 +32,13 @@ type handle struct {
func getHandle(fn string) (*handle, error) {
var stat syscall.Stat_t
if err := syscall.Stat(fn, &stat); err != nil {
- return nil, errors.Wrapf(err, "failed to stat %v", fn)
+ return nil, fmt.Errorf("failed to stat %v: %w", fn, err)
}
h := &handle{
fn: fn,
- dev: uint64(stat.Dev),
- ino: stat.Ino,
+ dev: uint64(stat.Dev), //nolint:unconvert,nolintlint
+ ino: uint64(stat.Ino), //nolint:unconvert,nolintlint
}
return h, nil
@@ -32,10 +47,10 @@ func getHandle(fn string) (*handle, error) {
func (h *handle) Path() (string, error) {
var stat syscall.Stat_t
if err := syscall.Stat(h.fn, &stat); err != nil {
- return "", errors.Wrapf(err, "path %v could not be statted", h.fn)
+ return "", fmt.Errorf("path %v could not be statted: %w", h.fn, err)
}
- if uint64(stat.Dev) != h.dev || stat.Ino != h.ino {
- return "", errors.Errorf("failed to verify handle %v/%v %v/%v for %v", stat.Dev, h.dev, stat.Ino, h.ino, h.fn)
+ if uint64(stat.Dev) != h.dev || uint64(stat.Ino) != h.ino { //nolint:unconvert,nolintlint
+ return "", fmt.Errorf("failed to verify handle %v/%v %v/%v for %v", stat.Dev, h.dev, stat.Ino, h.ino, h.fn)
}
return h.fn, nil
}
diff --git a/mkfifo_nosolaris.go b/mkfifo_nosolaris.go
deleted file mode 100644
index 8c6ea45..0000000
--- a/mkfifo_nosolaris.go
+++ /dev/null
@@ -1,9 +0,0 @@
-// +build !solaris
-
-package fifo
-
-import "syscall"
-
-func mkfifo(path string, mode uint32) (err error) {
- return syscall.Mkfifo(path, mode)
-}
diff --git a/mkfifo_solaris.go b/mkfifo_solaris.go
deleted file mode 100644
index 8d588a4..0000000
--- a/mkfifo_solaris.go
+++ /dev/null
@@ -1,11 +0,0 @@
-// +build solaris
-
-package fifo
-
-import (
- "golang.org/x/sys/unix"
-)
-
-func mkfifo(path string, mode uint32) (err error) {
- return unix.Mkfifo(path, mode)
-}
diff --git a/raw.go b/raw.go
new file mode 100644
index 0000000..9f18f76
--- /dev/null
+++ b/raw.go
@@ -0,0 +1,114 @@
+//go:build !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
+
+import (
+ "syscall"
+)
+
+// SyscallConn provides raw access to the fifo's underlying filedescrptor.
+// See syscall.Conn for guarantees provided by this interface.
+func (f *fifo) SyscallConn() (syscall.RawConn, error) {
+ // deterministic check for closed
+ select {
+ case <-f.closed:
+ return nil, ErrClosed
+ default:
+ }
+
+ select {
+ case <-f.closed:
+ return nil, ErrClosed
+ case <-f.opened:
+ return f.file.SyscallConn()
+ default:
+ }
+
+ // Not opened and not closed, this means open is non-blocking AND it's not open yet
+ // Use rawConn to deal with non-blocking open.
+ rc := &rawConn{f: f, ready: make(chan struct{})}
+ go func() {
+ select {
+ case <-f.closed:
+ return
+ case <-f.opened:
+ rc.raw, rc.err = f.file.SyscallConn()
+ close(rc.ready)
+ }
+ }()
+
+ return rc, nil
+}
+
+type rawConn struct {
+ f *fifo
+ ready chan struct{}
+ raw syscall.RawConn
+ err error
+}
+
+func (r *rawConn) Control(f func(fd uintptr)) error {
+ select {
+ case <-r.f.closed:
+ return ErrCtrlClosed
+ case <-r.ready:
+ }
+
+ if r.err != nil {
+ return r.err
+ }
+
+ return r.raw.Control(f)
+}
+
+func (r *rawConn) Read(f func(fd uintptr) (done bool)) error {
+ if r.f.flag&syscall.O_WRONLY > 0 {
+ return ErrRdFrmWRONLY
+ }
+
+ select {
+ case <-r.f.closed:
+ return ErrReadClosed
+ case <-r.ready:
+ }
+
+ if r.err != nil {
+ return r.err
+ }
+
+ return r.raw.Read(f)
+}
+
+func (r *rawConn) Write(f func(fd uintptr) (done bool)) error {
+ if r.f.flag&(syscall.O_WRONLY|syscall.O_RDWR) == 0 {
+ return ErrWrToRDONLY
+ }
+
+ select {
+ case <-r.f.closed:
+ return ErrWriteClosed
+ case <-r.ready:
+ }
+
+ if r.err != nil {
+ return r.err
+ }
+
+ return r.raw.Write(f)
+}
diff --git a/raw_test.go b/raw_test.go
new file mode 100644
index 0000000..06371f7
--- /dev/null
+++ b/raw_test.go
@@ -0,0 +1,249 @@
+//go:build !windows
+
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "os"
+ "path"
+ "path/filepath"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestRawReadWrite(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer r.Close()
+ rawR := makeRawConn(t, r, false)
+ assert.Error(t, rawR.Write(func(uintptr) bool { return true }))
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_NONBLOCK, 0)
+ assert.NoError(t, err)
+ defer w.Close()
+ rawW := makeRawConn(t, w, false)
+ assert.Error(t, rawW.Read(func(uintptr) bool { return true }))
+
+ data := []byte("hello world")
+ rawWrite(t, rawW, data)
+
+ dataR := make([]byte, len(data))
+ rawRead(t, rawR, dataR)
+ assert.True(t, bytes.Equal(data, dataR))
+}
+
+func TestRawWriteUserRead(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer w.Close()
+ rawW := makeRawConn(t, w, false)
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer r.Close()
+
+ data := []byte("hello world!")
+ rawWrite(t, rawW, data)
+ w.Close()
+
+ buf := make([]byte, len(data))
+ n, err := io.ReadFull(r, buf)
+ assert.NoError(t, err)
+ assert.True(t, bytes.Equal(data, buf[:n]))
+}
+
+func TestUserWriteRawRead(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer w.Close()
+
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer r.Close()
+ rawR := makeRawConn(t, r, false)
+
+ data := []byte("hello world!")
+ n, err := w.Write(data)
+ assert.NoError(t, err)
+ assert.Equal(t, n, len(data))
+ w.Close()
+
+ buf := make([]byte, len(data))
+ rawRead(t, rawR, buf)
+ assert.True(t, bytes.Equal(data, buf[:n]))
+}
+
+func TestRawCloseError(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ t.Run("SyscallConnAfterClose", func(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+ defer cancel()
+
+ f, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_RDWR|syscall.O_CREAT, 0600)
+ assert.NoError(t, err)
+
+ f.Close()
+
+ makeRawConn(t, f, true)
+ })
+
+ t.Run("RawOpsAfterClose", func(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+ defer cancel()
+ f, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_RDWR|syscall.O_CREAT, 0600)
+ assert.NoError(t, err)
+ defer f.Close()
+
+ raw := makeRawConn(t, f, false)
+
+ f.Close()
+
+ assert.Error(t, raw.Control(func(uintptr) {}))
+ dummy := func(uintptr) bool { return true }
+ assert.Error(t, raw.Write(dummy))
+ assert.Error(t, raw.Read(dummy))
+ })
+
+ t.Run("NonBlockRawOpsAfterClose", func(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+ defer cancel()
+ dummy := func(uintptr) bool { return true }
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer r.Close()
+ rawR := makeRawConn(t, r, false)
+ r.Close()
+
+ assert.Equal(t, ErrCtrlClosed, rawR.Control(func(uintptr) {}))
+ assert.Equal(t, ErrReadClosed, rawR.Read(dummy))
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer w.Close()
+ rawW := makeRawConn(t, w, false)
+ w.Close()
+
+ assert.Equal(t, ErrCtrlClosed, rawW.Control(func(uintptr) {}))
+ assert.Equal(t, ErrWriteClosed, rawW.Write(dummy))
+ })
+}
+
+func TestRawWrongRdWrError(t *testing.T) {
+ tmpdir, err := os.MkdirTemp("", "fifos")
+ assert.NoError(t, err)
+ defer os.RemoveAll(tmpdir)
+
+ ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+ defer cancel()
+ dummy := func(uintptr) bool { return true }
+ r, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer r.Close()
+ rawR := makeRawConn(t, r, false)
+
+ assert.Equal(t, ErrWrToRDONLY, rawR.Write(dummy))
+
+ w, err := OpenFifo(ctx, filepath.Join(tmpdir, path.Base(t.Name())), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
+ assert.NoError(t, err)
+ defer w.Close()
+ rawW := makeRawConn(t, w, false)
+
+ assert.Equal(t, ErrRdFrmWRONLY, rawW.Read(dummy))
+}
+
+func makeRawConn(t *testing.T, fifo io.ReadWriteCloser, expectError bool) syscall.RawConn {
+ sc, ok := fifo.(syscall.Conn)
+ assert.True(t, ok, "not a syscall.Conn")
+
+ raw, err := sc.SyscallConn()
+ if !expectError {
+ assert.NoError(t, err)
+ } else {
+ assert.Error(t, err)
+ }
+
+ return raw
+}
+
+func rawWrite(t *testing.T, rc syscall.RawConn, data []byte) {
+ var written int
+ var wErr error
+
+ err := rc.Write(func(fd uintptr) bool {
+ var n int
+ n, wErr = syscall.Write(int(fd), data[written:])
+ written += n
+ if wErr != nil || n == 0 || written == len(data) {
+ return true
+ }
+ return false
+ })
+ assert.NoError(t, err)
+ assert.NoError(t, wErr)
+ assert.Equal(t, written, len(data))
+}
+
+func rawRead(t *testing.T, rc syscall.RawConn, data []byte) {
+ var (
+ rErr error
+ read int
+ )
+
+ err := rc.Read(func(fd uintptr) bool {
+ var n int
+ n, rErr = syscall.Read(int(fd), data[read:])
+ read += n
+ if rErr != nil || n == 0 || read == len(data) {
+ return true
+ }
+ return false
+ })
+ assert.NoError(t, err)
+ assert.NoError(t, rErr)
+ assert.Equal(t, read, len(data))
+}
diff --git a/readme.md b/readme.md
index 1c82669..ad4727d 100644
--- a/readme.md
+++ b/readme.md
@@ -1,5 +1,10 @@
### fifo
+[![PkgGoDev](https://pkg.go.dev/badge/github.com/containerd/fifo)](https://pkg.go.dev/github.com/containerd/fifo)
+[![Build Status](https://github.com/containerd/fifo/workflows/CI/badge.svg)](https://github.com/containerd/fifo/actions?query=workflow%3ACI)
+[![codecov](https://codecov.io/gh/containerd/fifo/branch/master/graph/badge.svg)](https://codecov.io/gh/containerd/fifo)
+[![Go Report Card](https://goreportcard.com/badge/github.com/containerd/fifo)](https://goreportcard.com/report/github.com/containerd/fifo)
+
Go package for handling fifos in a sane way.
```
@@ -27,4 +32,15 @@ func (f *fifo) Write(b []byte) (int, error)
// Close the fifo. Next reads/writes will error. This method can also be used
// before open(2) has returned and fifo was never opened.
func (f *fifo) Close() error
-```
\ No newline at end of file
+```
+
+## Project details
+
+The fifo is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE).
+As a containerd sub-project, you will find the:
+
+ * [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md),
+ * [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS),
+ * and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md)
+
+information in our [`containerd/project`](https://github.com/containerd/project) repository.
diff --git a/utils.go b/utils.go
new file mode 100644
index 0000000..bbdf790
--- /dev/null
+++ b/utils.go
@@ -0,0 +1,35 @@
+/*
+ Copyright The containerd Authors.
+
+ 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 fifo
+
+import "os"
+
+// IsFifo checks if a file is a (named pipe) fifo
+// if the file does not exist then it returns false
+func IsFifo(path string) (bool, error) {
+ stat, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return false, nil
+ }
+ return false, err
+ }
+ if stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
+ return true, nil
+ }
+ return false, nil
+}
Debdiff
[The following lists of changes regard files as different if they have different names, permissions or owners.]
Files in second set of .debs but not in first
-rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/errors.go -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/go.mod -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/go.sum -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/raw.go -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/raw_test.go -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/utils.go
Files in first set of .debs but not in second
-rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/mkfifo_nosolaris.go -rw-r--r-- root/root /usr/share/gocode/src/github.com/tonistiigi/fifo/mkfifo_solaris.go
No differences were encountered in the control files