aboutsummaryrefslogtreecommitdiff
path: root/fs/os.go
blob: 0a4f61418d84e768521b5b9c8e73f8fa922417c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package fs

import (
	"io"
	"os"
	"strings"
	"time"
)

func pathJoin(parts ...string) string {
	return strings.Join(parts, string(os.PathSeparator))
}

func openOSFile(path string) (osFile, error) {
	fi, err := os.Lstat(path)
	if err != nil {
		return osFile{}, err
	}

	return osFile{
		fullpath: path,
		fi:       fi,
	}, nil
}

type osFile struct {
	fullpath string
	fi       os.FileInfo
}

func (f osFile) Type() FileType {
	m := f.fi.Mode()
	if m.IsDir() {
		return FDir
	}
	if m.IsRegular() {
		return FFile
	}
	if m&os.ModeSymlink != 0 {
		return FSymlink
	}
	return "unknown"
}

func (f osFile) Name() string {
	return f.fi.Name()
}

func (f osFile) Executable() bool {
	return f.fi.Mode()&0100 != 0 // x bit set for user?
}

func (f osFile) ModTime() time.Time {
	return f.fi.ModTime()
}

func (f osFile) Delete() error {
	return os.RemoveAll(f.fullpath)
}

func (f osFile) Open() (io.ReadWriteCloser, error) {
	fh, err := os.Open(f.fullpath)
	if err != nil {
		return nil, err
	}
	return fh, nil
}

func (f osFile) Readdir() (list []File, err error) {
	fh, err := os.Open(f.fullpath)
	if err != nil {
		return
	}
	defer fh.Close()

	infos, err := fh.Readdir(-1)
	if err != nil {
		return
	}

	for _, fi := range infos {
		if fi.Name() == "." || fi.Name() == ".." {
			continue
		}

		list = append(list, osFile{
			fullpath: pathJoin(f.fullpath, fi.Name()),
			fi:       fi,
		})
	}

	return
}

func (f osFile) GetChild(name string) (File, error) {
	path := pathJoin(f.fullpath, name)
	fi, err := os.Lstat(path)
	if err != nil {
		return nil, err
	}
	return osFile{path, fi}, nil
}

func perms(executable bool) os.FileMode {
	if executable {
		return 0755
	} else {
		return 0644
	}
}

func (f osFile) CreateChildFile(name string, exec bool) (RegularFile, error) {
	p := pathJoin(f.fullpath, name)

	fh, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE, perms(exec))
	if err != nil {
		return nil, err
	}
	fh.Close()

	return openOSFile(p)
}

func (f osFile) CreateChildDir(name string) (Dir, error) {
	p := pathJoin(f.fullpath, name)

	if err := os.Mkdir(p, perms(true)); err != nil {
		return nil, err
	}

	return openOSFile(p)
}

func (f osFile) CreateChildSymlink(name string, target string) (Symlink, error) {
	p := pathJoin(f.fullpath, name)

	err := os.Symlink(target, p)
	if err != nil {
		return nil, err
	}

	return openOSFile(p)
}

func (f osFile) RenameChild(oldname, newname string) error {
	return os.Rename(pathJoin(f.fullpath, oldname), pathJoin(f.fullpath, newname))
}

func (f osFile) Readlink() (string, error) {
	return os.Readlink(f.fullpath)
}