Skip to content

Commit

Permalink
Merge pull request #3 from moul/dev/moul/darwin
Browse files Browse the repository at this point in the history
feat: add darwin support
  • Loading branch information
moul authored Jul 3, 2020
2 parents 211a53f + 4a88842 commit f066182
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Docker Metrics](https://images.microbadger.com/badges/image/moul/openfiles.svg)](https://microbadger.com/images/moul/openfiles)
[![Made by Manfred Touron](https://img.shields.io/badge/made%20by-Manfred%20Touron-blue.svg?style=flat)](https://manfred.life/)

[![CI](https://github.com/moul/openfiles/workflows/CI/badge.svg)](https://github.com/moul/openfiles/actions?query=workflow%3ACI)
[![Go](https://github.com/moul/openfiles/workflows/Go/badge.svg)](https://github.com/moul/openfiles/actions?query=workflow%3AGo)
[![Release](https://github.com/moul/openfiles/workflows/Release/badge.svg)](https://github.com/moul/openfiles/actions?query=workflow%3ARelease)
[![GolangCI](https://golangci.com/badges/github.com/moul/openfiles.svg)](https://golangci.com/r/github.com/moul/openfiles)
[![codecov](https://codecov.io/gh/moul/openfiles/branch/master/graph/badge.svg)](https://codecov.io/gh/moul/openfiles)
Expand Down
19 changes: 19 additions & 0 deletions openfiles_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build darwin

package openfiles

import (
"fmt"
"os"
"os/exec"
"strings"
)

func Count() (int64, error) {
out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v", os.Getpid())).Output()
if err != nil {
return -1, err
}
lines := strings.Split(string(out), "\n")
return int64(len(lines) - 1), nil
}
4 changes: 2 additions & 2 deletions openfiles_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"path/filepath"
)

func Count() (int, error) {
func Count() (int64, error) {
pid := os.Getpid()
m, err := filepath.Glob(fmt.Sprintf("/proc/%d/fd/*", pid))
if err != nil {
return -1, err
}
return len(m), nil
return int64(len(m)), nil
}
4 changes: 2 additions & 2 deletions openfiles_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// +build !linux
// +build !linux,!darwin

package openfiles

func Count() (int, error) {
func Count() (int64, error) {
return -1, ErrNotSupported
}

0 comments on commit f066182

Please sign in to comment.