Add some documentation to pkg/system

Partially addresses #11581

Signed-off-by: Ankush Agarwal <ankushagarwal11@gmail.com>
This commit is contained in:
Ankush Agarwal 2015-03-31 01:03:31 -07:00
parent dce6f8ba76
commit eea8abd59e
10 changed files with 24 additions and 2 deletions

View file

@ -6,6 +6,10 @@ import (
"syscall" "syscall"
) )
// Lstat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Lstat(path string) (*Stat_t, error) { func Lstat(path string) (*Stat_t, error) {
s := &syscall.Stat_t{} s := &syscall.Stat_t{}
err := syscall.Lstat(path, s) err := syscall.Lstat(path, s)

View file

@ -5,6 +5,7 @@ import (
"testing" "testing"
) )
// TestLstat tests Lstat for existing and non existing files
func TestLstat(t *testing.T) { func TestLstat(t *testing.T) {
file, invalid, _, dir := prepareFiles(t) file, invalid, _, dir := prepareFiles(t)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)

View file

@ -15,8 +15,8 @@ var (
ErrMalformed = errors.New("malformed file") ErrMalformed = errors.New("malformed file")
) )
// Retrieve memory statistics of the host system and parse them into a MemInfo // ReadMemInfo retrieves memory statistics of the host system and returns a
// type. // MemInfo type.
func ReadMemInfo() (*MemInfo, error) { func ReadMemInfo() (*MemInfo, error) {
file, err := os.Open("/proc/meminfo") file, err := os.Open("/proc/meminfo")
if err != nil { if err != nil {
@ -26,6 +26,10 @@ func ReadMemInfo() (*MemInfo, error) {
return parseMemInfo(file) return parseMemInfo(file)
} }
// parseMemInfo parses the /proc/meminfo file into
// a MemInfo object given a io.Reader to the file.
//
// Throws error if there are problems reading from the file
func parseMemInfo(reader io.Reader) (*MemInfo, error) { func parseMemInfo(reader io.Reader) (*MemInfo, error) {
meminfo := &MemInfo{} meminfo := &MemInfo{}
scanner := bufio.NewScanner(reader) scanner := bufio.NewScanner(reader)

View file

@ -7,6 +7,7 @@ import (
"github.com/docker/docker/pkg/units" "github.com/docker/docker/pkg/units"
) )
// TestMemInfo tests parseMemInfo with a static meminfo string
func TestMemInfo(t *testing.T) { func TestMemInfo(t *testing.T) {
const input = ` const input = `
MemTotal: 1 kB MemTotal: 1 kB

View file

@ -6,6 +6,8 @@ import (
"syscall" "syscall"
) )
// 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 int) error { func Mknod(path string, mode uint32, dev int) error {
return syscall.Mknod(path, mode, dev) return syscall.Mknod(path, mode, dev)
} }

View file

@ -4,6 +4,8 @@ import (
"syscall" "syscall"
) )
// Stat_t type contains status of a file. It contains metadata
// like permission, owner, group, size, etc about a file
type Stat_t struct { type Stat_t struct {
mode uint32 mode uint32
uid uint32 uid uint32

View file

@ -4,6 +4,7 @@ import (
"syscall" "syscall"
) )
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) { func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
return &Stat_t{size: s.Size, return &Stat_t{size: s.Size,
mode: s.Mode, mode: s.Mode,
@ -13,6 +14,10 @@ func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
mtim: s.Mtim}, nil mtim: s.Mtim}, nil
} }
// Stat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*Stat_t, error) { func Stat(path string) (*Stat_t, error) {
s := &syscall.Stat_t{} s := &syscall.Stat_t{}
err := syscall.Stat(path, s) err := syscall.Stat(path, s)

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
) )
// TestFromStatT tests fromStatT for a tempfile
func TestFromStatT(t *testing.T) { func TestFromStatT(t *testing.T) {
file, _, _, dir := prepareFiles(t) file, _, _, dir := prepareFiles(t)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)

View file

@ -6,6 +6,7 @@ import (
"syscall" "syscall"
) )
// fromStatT creates a system.Stat_t type from a syscall.Stat_t type
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) { func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
return &Stat_t{size: s.Size, return &Stat_t{size: s.Size,
mode: uint32(s.Mode), mode: uint32(s.Mode),

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
) )
// prepareFiles creates files for testing in the temp directory
func prepareFiles(t *testing.T) (string, string, string, string) { func prepareFiles(t *testing.T) (string, string, string, string) {
dir, err := ioutil.TempDir("", "docker-system-test") dir, err := ioutil.TempDir("", "docker-system-test")
if err != nil { if err != nil {