Skip to content

Commit

Permalink
Merge pull request #12 from zimmski/additions
Browse files Browse the repository at this point in the history
Cleanups, file resource limit helper, environment variable with default value helper, query files of path and subpaths helper
  • Loading branch information
zimmski committed Aug 4, 2018
2 parents 057b83a + f52be9c commit 61951ef
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 40 deletions.
37 changes: 12 additions & 25 deletions capture_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
package osutil

import (
"syscall"
"testing"

"github.com/stretchr/testify/assert"
)

func setAndGetSmallRLimit(t *testing.T) syscall.Rlimit {
var limit syscall.Rlimit

limit.Max = 10
limit.Cur = 10

assert.NoError(t, syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit))
assert.NoError(t, syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit))

return limit
}

func TestCapture(t *testing.T) {
limit := setAndGetSmallRLimit(t)

// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks.
for i := 0; i <= int(limit.Cur); i++ {
testCapture(t)
}
assert.NoError(t, SetRLimitFiles(10, func(limit uint64) {
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks.
for i := 0; i <= int(limit); i++ {
testCapture(t)
}
}))
}

func TestCaptureWithCGo(t *testing.T) {
limit := setAndGetSmallRLimit(t)

// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks.
for i := 0; i <= int(limit.Cur); i++ {
testCaptureWithCGo(t)
}
assert.NoError(t, SetRLimitFiles(10, func(limit uint64) {
// Use at least one more file descriptor than our current limit, so we make sure that there are no file descriptor leaks.
for i := 0; i <= int(limit); i++ {
testCaptureWithCGo(t)
}
}))
}
2 changes: 1 addition & 1 deletion copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
)

// CopyFile copies a file from src to dst
// CopyFile copies a file from src to dst.
func CopyFile(src string, dst string) (err error) {
s, err := os.Open(src)
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ func TestCopyFile(t *testing.T) {
src := "copy.go"
dst := "copy.go.tmp"

err := CopyFile(src, dst)
assert.Nil(t, err)
assert.NoError(t, CopyFile(src, dst))

s, err := ioutil.ReadFile(src)
assert.Nil(t, err)
assert.NoError(t, err)

d, err := ioutil.ReadFile(dst)
assert.Nil(t, err)
assert.NoError(t, err)

assert.Equal(t, s, d)

err = os.Remove(dst)
assert.Nil(t, err)
assert.NoError(t, os.Remove(dst))
}
14 changes: 14 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package osutil

import (
"os"
)

// EnvOrDefault returns the environment variable with the given key, or the default value if the key is not defined.
func EnvOrDefault(key string, defaultValue string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}

return defaultValue
}
10 changes: 5 additions & 5 deletions exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
)

var (
// ErrNotADirectory the given directory does not exist
// ErrNotADirectory indicates that the given directory does not exist.
ErrNotADirectory = errors.New("not a directory")
// ErrNotAFile the given file does not exist
// ErrNotAFile indicates thate the given file does not exist.
ErrNotAFile = errors.New("not a file")
)

// Stat retuns a FileInfo structure describing the given file
// Stat retuns a FileInfo structure describing the given file.
func Stat(filepath string) (os.FileInfo, error) {
f, err := os.Open(filepath)
if err != nil {
Expand All @@ -32,7 +32,7 @@ func Stat(filepath string) (os.FileInfo, error) {
return fi, nil
}

// DirExists checks if a directory exists
// DirExists checks if a directory exists.
func DirExists(path string) error {
fi, err := Stat(path)
if err != nil {
Expand All @@ -46,7 +46,7 @@ func DirExists(path string) error {
return nil
}

// FileExists checks if a file exists
// FileExists checks if a file exists.
func FileExists(filepath string) error {
fi, err := Stat(filepath)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

func TestDirExists(t *testing.T) {
assert.Nil(t, DirExists("/"))
assert.Nil(t, DirExists("../osutil"))
assert.NoError(t, DirExists("/"))
assert.NoError(t, DirExists("../osutil"))

assert.NotNil(t, DirExists("hey"))
assert.Error(t, DirExists("hey"))

assert.Equal(t, ErrNotADirectory, DirExists("exists.go"))
}
26 changes: 26 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package osutil

import (
"os"
"path/filepath"
)

// FilesRecursive returns all files in a given path and its subpaths.
func FilesRecursive(path string) (files []string, err error) {
var fs []string

err = filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}

fs = append(fs, path)

return err
})
if err != nil {
return nil, err
}

return fs, nil
}
35 changes: 35 additions & 0 deletions files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package osutil

import (
"io/ioutil"
"os"
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilesRecursive(t *testing.T) {
path, err := ioutil.TempDir("", "os-util")
assert.NoError(t, err)

assert.NoError(t, os.MkdirAll(path+"/a/b", 0750))
assert.NoError(t, ioutil.WriteFile(path+"/c.txt", []byte("foobar"), 0640))
assert.NoError(t, ioutil.WriteFile(path+"/a/d.txt", []byte("foobar"), 0640))
assert.NoError(t, ioutil.WriteFile(path+"/a/b/e.txt", []byte("foobar"), 0640))

fs, err := FilesRecursive(path)
assert.NoError(t, err)

sort.Strings(fs)

assert.Equal(
t,
[]string{
path + "/a/b/e.txt",
path + "/a/d.txt",
path + "/c.txt",
},
fs,
)
}
29 changes: 29 additions & 0 deletions limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package osutil

import (
"syscall"
)

// SetRLimitFiles temporarily changes the file descriptor resource limit while calling the given function.
func SetRLimitFiles(limit uint64, call func(limit uint64)) (err error) {
var tmp syscall.Rlimit
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &tmp); err != nil {
return nil
}
defer func() {
if err == nil {
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &tmp)
}
}()

if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{
Cur: limit,
Max: tmp.Max,
}); err != nil {
return err
}

call(limit)

return nil
}

0 comments on commit 61951ef

Please sign in to comment.