Skip to content

Commit

Permalink
windows: Fix compilation issue
Browse files Browse the repository at this point in the history
This commit is to make sure there is no compilication issue
with windows.

Fixes vishvananda#23

Signed-off-by: Tam Mach <sayboras@yahoo.com>
  • Loading branch information
sayboras committed Oct 28, 2022
1 parent e414ad8 commit 29d7678
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 67 deletions.
63 changes: 0 additions & 63 deletions netns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,10 @@
// as root.
package netns

import (
"fmt"

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

// NsHandle is a handle to a network namespace. It can be cast directly
// to an int and used as a file descriptor.
type NsHandle int

// Equal determines if two network handles refer to the same network
// namespace. This is done by comparing the device and inode that the
// file descriptors point to.
func (ns NsHandle) Equal(other NsHandle) bool {
if ns == other {
return true
}
var s1, s2 unix.Stat_t
if err := unix.Fstat(int(ns), &s1); err != nil {
return false
}
if err := unix.Fstat(int(other), &s2); err != nil {
return false
}
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
}

// String shows the file descriptor number and its dev and inode.
func (ns NsHandle) String() string {
if ns == -1 {
return "NS(None)"
}
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns)
}
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
}

// UniqueId returns a string which uniquely identifies the namespace
// associated with the network handle.
func (ns NsHandle) UniqueId() string {
if ns == -1 {
return "NS(none)"
}
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return "NS(unknown)"
}
return fmt.Sprintf("NS(%d:%d)", s.Dev, s.Ino)
}

// IsOpen returns true if Close() has not been called.
func (ns NsHandle) IsOpen() bool {
return ns != -1
}

// Close closes the NsHandle and resets its file descriptor to -1.
// It is not safe to use an NsHandle after Close() is called.
func (ns *NsHandle) Close() error {
if err := unix.Close(int(*ns)); err != nil {
return err
}
(*ns) = -1
return nil
}

// None gets an empty (closed) NsHandle.
func None() NsHandle {
return NsHandle(-1)
Expand Down
57 changes: 57 additions & 0 deletions netns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,63 @@ const (
bindMountPath = "/run/netns" /* Bind mount path for named netns */
)

// Equal determines if two network handles refer to the same network
// namespace. This is done by comparing the device and inode that the
// file descriptors point to.
func (ns NsHandle) Equal(other NsHandle) bool {
if ns == other {
return true
}
var s1, s2 unix.Stat_t
if err := unix.Fstat(int(ns), &s1); err != nil {
return false
}
if err := unix.Fstat(int(other), &s2); err != nil {
return false
}
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
}

// String shows the file descriptor number and its dev and inode.
func (ns NsHandle) String() string {
if ns == -1 {
return "NS(None)"
}
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns)
}
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
}

// UniqueId returns a string which uniquely identifies the namespace
// associated with the network handle.
func (ns NsHandle) UniqueId() string {
if ns == -1 {
return "NS(none)"
}
var s unix.Stat_t
if err := unix.Fstat(int(ns), &s); err != nil {
return "NS(unknown)"
}
return fmt.Sprintf("NS(%d:%d)", s.Dev, s.Ino)
}

// IsOpen returns true if Close() has not been called.
func (ns NsHandle) IsOpen() bool {
return ns != -1
}

// Close closes the NsHandle and resets its file descriptor to -1.
// It is not safe to use an NsHandle after Close() is called.
func (ns *NsHandle) Close() error {
if err := unix.Close(int(*ns)); err != nil {
return err
}
(*ns) = -1
return nil
}

// Setns sets namespace using golang.org/x/sys/unix.Setns.
//
// Deprecated: Use golang.org/x/sys/unix.Setns instead.
Expand Down
6 changes: 2 additions & 4 deletions netns_unspecified.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//go:build !linux
// +build !linux

package netns

import (
"errors"
"fmt"
)

var (
ErrNotImplemented = errors.New("not implemented")
)

func Setns(ns NsHandle, nstype int) (err error) {
return ErrNotImplemented
}

func Set(ns NsHandle) (err error) {
return ErrNotImplemented
}
Expand Down

0 comments on commit 29d7678

Please sign in to comment.