summaryrefslogtreecommitdiff
path: root/src/pkg/io/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/io/utils.go')
-rw-r--r--src/pkg/io/utils.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/pkg/io/utils.go b/src/pkg/io/utils.go
index 78b8320ec..bdf234874 100644
--- a/src/pkg/io/utils.go
+++ b/src/pkg/io/utils.go
@@ -9,6 +9,7 @@ package io
import (
"bytes";
"os";
+ "sort";
)
// ReadAll reads from r until an error or EOF and returns the data it read.
@@ -37,9 +38,36 @@ func WriteFile(filename string, data []byte, perm int) os.Error {
return err;
}
n, err := f.Write(data);
+ f.Close();
if err == nil && n < len(data) {
err = ErrShortWrite;
}
- f.Close();
return err;
}
+
+// A dirList implements sort.Interface.
+type dirList []*os.Dir
+
+func (d dirList) Len() int { return len(d); }
+func (d dirList) Less(i, j int) bool { return d[i].Name < d[j].Name; }
+func (d dirList) Swap(i, j int) { d[i], d[j] = d[j], d[i]; }
+
+// ReadDir reads the directory named by dirname and returns
+// a list of sorted directory entries.
+func ReadDir(dirname string) ([]*os.Dir, os.Error) {
+ f, err := os.Open(dirname, os.O_RDONLY, 0);
+ if err != nil {
+ return nil, err;
+ }
+ list, err := f.Readdir(-1);
+ f.Close();
+ if err != nil {
+ return nil, err;
+ }
+ dirs := make(dirList, len(list));
+ for i := range list {
+ dirs[i] = &list[i];
+ }
+ sort.Sort(dirs);
+ return dirs, nil;
+}