summaryrefslogtreecommitdiff
path: root/src/pkg/path
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/path')
-rw-r--r--src/pkg/path/path.go70
-rw-r--r--src/pkg/path/path_test.go74
2 files changed, 72 insertions, 72 deletions
diff --git a/src/pkg/path/path.go b/src/pkg/path/path.go
index 59deb5ce9..e03f2ecf6 100644
--- a/src/pkg/path/path.go
+++ b/src/pkg/path/path.go
@@ -7,9 +7,9 @@
package path
import (
- "io/ioutil";
- "os";
- "strings";
+ "io/ioutil"
+ "os"
+ "strings"
)
// Clean returns the shortest path name equivalent to path
@@ -34,16 +34,16 @@ func Clean(path string) string {
return "."
}
- rooted := path[0] == '/';
- n := len(path);
+ rooted := path[0] == '/'
+ n := len(path)
// Invariants:
// reading from path; r is index of next byte to process.
// writing to buf; w is index of next byte to write.
// dotdot is index in buf where .. must stop, either because
// it is the leading slash or it is a leading ../../.. prefix.
- buf := strings.Bytes(path);
- r, w, dotdot := 0, 0, 0;
+ buf := strings.Bytes(path)
+ r, w, dotdot := 0, 0, 0
if rooted {
r, w, dotdot = 1, 1, 1
}
@@ -58,48 +58,48 @@ func Clean(path string) string {
r++
case path[r] == '.' && path[r+1] == '.' && (r+2 == n || path[r+2] == '/'):
// .. element: remove to last /
- r += 2;
+ r += 2
switch {
case w > dotdot:
// can backtrack
- w--;
+ w--
for w > dotdot && buf[w] != '/' {
w--
}
case !rooted:
// cannot backtrack, but not rooted, so append .. element.
if w > 0 {
- buf[w] = '/';
- w++;
+ buf[w] = '/'
+ w++
}
- buf[w] = '.';
- w++;
- buf[w] = '.';
- w++;
- dotdot = w;
+ buf[w] = '.'
+ w++
+ buf[w] = '.'
+ w++
+ dotdot = w
}
default:
// real path element.
// add slash if needed
if rooted && w != 1 || !rooted && w != 0 {
- buf[w] = '/';
- w++;
+ buf[w] = '/'
+ w++
}
// copy element
for ; r < n && path[r] != '/'; r++ {
- buf[w] = path[r];
- w++;
+ buf[w] = path[r]
+ w++
}
}
}
// Turn empty string into "."
if w == 0 {
- buf[w] = '.';
- w++;
+ buf[w] = '.'
+ w++
}
- return string(buf[0:w]);
+ return string(buf[0:w])
}
// Split splits path immediately following the final slash,
@@ -112,7 +112,7 @@ func Split(path string) (dir, file string) {
return path[0 : i+1], path[i+1:]
}
}
- return "", path;
+ return "", path
}
// Join joins dir and file into a single path, adding a separating
@@ -121,7 +121,7 @@ func Join(dir, file string) string {
if dir == "" {
return file
}
- return Clean(dir + "/" + file);
+ return Clean(dir + "/" + file)
}
// Ext returns the file name extension used by path.
@@ -134,28 +134,28 @@ func Ext(path string) string {
return path[i:]
}
}
- return "";
+ return ""
}
// Visitor methods are invoked for corresponding file tree entries
// visited by Walk. The parameter path is the full path of d relative
// to root.
type Visitor interface {
- VisitDir(path string, d *os.Dir) bool;
- VisitFile(path string, d *os.Dir);
+ VisitDir(path string, d *os.Dir) bool
+ VisitFile(path string, d *os.Dir)
}
func walk(path string, d *os.Dir, v Visitor, errors chan<- os.Error) {
if !d.IsDirectory() {
- v.VisitFile(path, d);
- return;
+ v.VisitFile(path, d)
+ return
}
if !v.VisitDir(path, d) {
- return // skip directory entries
+ return // skip directory entries
}
- list, err := ioutil.ReadDir(path);
+ list, err := ioutil.ReadDir(path)
if err != nil {
if errors != nil {
errors <- err
@@ -175,12 +175,12 @@ func walk(path string, d *os.Dir, v Visitor, errors chan<- os.Error) {
// If errors != nil, Walk sends each directory read error
// to the channel. Otherwise Walk discards the error.
func Walk(root string, v Visitor, errors chan<- os.Error) {
- d, err := os.Lstat(root);
+ d, err := os.Lstat(root)
if err != nil {
if errors != nil {
errors <- err
}
- return; // can't progress
+ return // can't progress
}
- walk(root, d, v, errors);
+ walk(root, d, v, errors)
}
diff --git a/src/pkg/path/path_test.go b/src/pkg/path/path_test.go
index 74520fa31..296712e52 100644
--- a/src/pkg/path/path_test.go
+++ b/src/pkg/path/path_test.go
@@ -5,12 +5,12 @@
package path
import (
- "os";
- "testing";
+ "os"
+ "testing"
)
type CleanTest struct {
- path, clean string;
+ path, clean string
}
var cleantests = []CleanTest{
@@ -72,7 +72,7 @@ func TestClean(t *testing.T) {
}
type SplitTest struct {
- path, dir, file string;
+ path, dir, file string
}
var splittests = []SplitTest{
@@ -92,7 +92,7 @@ func TestSplit(t *testing.T) {
}
type JoinTest struct {
- dir, file, path string;
+ dir, file, path string
}
var jointests = []JoinTest{
@@ -114,7 +114,7 @@ func TestJoin(t *testing.T) {
}
type ExtTest struct {
- path, ext string;
+ path, ext string
}
var exttests = []ExtTest{
@@ -134,9 +134,9 @@ func TestExt(t *testing.T) {
}
type Node struct {
- name string;
- entries []*Node; // nil if the entry is a file
- mark int;
+ name string
+ entries []*Node // nil if the entry is a file
+ mark int
}
var tree = &Node{
@@ -166,7 +166,7 @@ var tree = &Node{
}
func walkTree(n *Node, path string, f func(path string, n *Node)) {
- f(path, n);
+ f(path, n)
for _, e := range n.entries {
walkTree(e, Join(path, e.name), f)
}
@@ -175,25 +175,25 @@ func walkTree(n *Node, path string, f func(path string, n *Node)) {
func makeTree(t *testing.T) {
walkTree(tree, tree.name, func(path string, n *Node) {
if n.entries == nil {
- fd, err := os.Open(path, os.O_CREAT, 0660);
+ fd, err := os.Open(path, os.O_CREAT, 0660)
if err != nil {
t.Errorf("makeTree: %v", err)
}
- fd.Close();
+ fd.Close()
} else {
os.Mkdir(path, 0770)
}
})
}
-func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
+func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
func checkMarks(t *testing.T) {
walkTree(tree, tree.name, func(path string, n *Node) {
if n.mark != 1 {
t.Errorf("node %s mark = %d; expected 1", path, n.mark)
}
- n.mark = 0;
+ n.mark = 0
})
}
@@ -209,8 +209,8 @@ func mark(name string) {
type TestVisitor struct{}
func (v *TestVisitor) VisitDir(path string, d *os.Dir) bool {
- mark(d.Name);
- return true;
+ mark(d.Name)
+ return true
}
func (v *TestVisitor) VisitFile(path string, d *os.Dir) {
@@ -218,52 +218,52 @@ func (v *TestVisitor) VisitFile(path string, d *os.Dir) {
}
func TestWalk(t *testing.T) {
- makeTree(t);
+ makeTree(t)
// 1) ignore error handling, expect none
- v := &TestVisitor{};
- Walk(tree.name, v, nil);
- checkMarks(t);
+ v := &TestVisitor{}
+ Walk(tree.name, v, nil)
+ checkMarks(t)
// 2) handle errors, expect none
- errors := make(chan os.Error, 64);
- Walk(tree.name, v, errors);
+ errors := make(chan os.Error, 64)
+ Walk(tree.name, v, errors)
if err, ok := <-errors; ok {
t.Errorf("no error expected, found: s", err)
}
- checkMarks(t);
+ checkMarks(t)
if os.Getuid() != 0 {
// introduce 2 errors: chmod top-level directories to 0
- os.Chmod(Join(tree.name, tree.entries[1].name), 0);
- os.Chmod(Join(tree.name, tree.entries[3].name), 0);
+ os.Chmod(Join(tree.name, tree.entries[1].name), 0)
+ os.Chmod(Join(tree.name, tree.entries[3].name), 0)
// mark respective subtrees manually
- markTree(tree.entries[1]);
- markTree(tree.entries[3]);
+ markTree(tree.entries[1])
+ markTree(tree.entries[3])
// correct double-marking of directory itself
- tree.entries[1].mark--;
- tree.entries[3].mark--;
+ tree.entries[1].mark--
+ tree.entries[3].mark--
// 3) handle errors, expect two
- errors = make(chan os.Error, 64);
- os.Chmod(Join(tree.name, tree.entries[1].name), 0);
- Walk(tree.name, v, errors);
+ errors = make(chan os.Error, 64)
+ os.Chmod(Join(tree.name, tree.entries[1].name), 0)
+ Walk(tree.name, v, errors)
for i := 1; i <= 2; i++ {
if _, ok := <-errors; !ok {
- t.Errorf("%d. error expected, none found", i);
- break;
+ t.Errorf("%d. error expected, none found", i)
+ break
}
}
if err, ok := <-errors; ok {
t.Errorf("only two errors expected, found 3rd: %v", err)
}
// the inaccessible subtrees were marked manually
- checkMarks(t);
+ checkMarks(t)
}
// cleanup
- os.Chmod(Join(tree.name, tree.entries[1].name), 0770);
- os.Chmod(Join(tree.name, tree.entries[3].name), 0770);
+ os.Chmod(Join(tree.name, tree.entries[1].name), 0770)
+ os.Chmod(Join(tree.name, tree.entries[3].name), 0770)
if err := os.RemoveAll(tree.name); err != nil {
t.Errorf("removeTree: %v", err)
}