GOFILES=\
codewalk.go\
dirtrees.go\
+ filesystem.go\
format.go\
godoc.go\
index.go\
main.go\
mapping.go\
+ parser.go\
snippet.go\
spec.go\
utils.go\
"fmt"
"http"
"io"
- "io/ioutil"
"log"
"os"
"regexp"
}
// If directory exists, serve list of code walks.
- dir, err := os.Lstat(abspath)
+ dir, err := fs.Lstat(abspath)
if err == nil && dir.IsDirectory() {
codewalkDir(w, r, relpath, abspath)
return
// loadCodewalk reads a codewalk from the named XML file.
-func loadCodewalk(file string) (*Codewalk, os.Error) {
- f, err := os.Open(file)
+func loadCodewalk(filename string) (*Codewalk, os.Error) {
+ f, err := fs.Open(filename)
if err != nil {
return nil, err
}
p.Entity = xml.HTMLEntity
err = p.Unmarshal(cw, nil)
if err != nil {
- return nil, &os.PathError{"parsing", file, err}
+ return nil, &os.PathError{"parsing", filename, err}
}
// Compute file list, evaluate line numbers for addresses.
if i < 0 {
i = len(st.Src)
}
- file := st.Src[0:i]
- data, err := ioutil.ReadFile(absolutePath(file, *goroot))
+ filename := st.Src[0:i]
+ data, err := fs.ReadFile(absolutePath(filename, *goroot))
if err != nil {
st.Err = err
continue
st.Hi = byteToLine(data, hi-1)
}
st.Data = data
- st.File = file
- m[file] = true
+ st.File = filename
+ m[filename] = true
}
// Make list of files
Title string
}
- dir, err := ioutil.ReadDir(abspath)
+ dir, err := fs.ReadDir(abspath)
if err != nil {
log.Print(err)
serveError(w, r, relpath, err)
}
var v vector.Vector
for _, fi := range dir {
+ name := fi.Name()
if fi.IsDirectory() {
- v.Push(&elem{fi.Name + "/", ""})
- } else if strings.HasSuffix(fi.Name, ".xml") {
- cw, err := loadCodewalk(abspath + "/" + fi.Name)
+ v.Push(&elem{name + "/", ""})
+ } else if strings.HasSuffix(name, ".xml") {
+ cw, err := loadCodewalk(abspath + "/" + name)
if err != nil {
continue
}
- v.Push(&elem{fi.Name[0 : len(fi.Name)-len(".xml")], cw.Title})
+ v.Push(&elem{name[0 : len(name)-len(".xml")], cw.Title})
}
}
// the usual godoc HTML wrapper.
func codewalkFileprint(w http.ResponseWriter, r *http.Request, f string) {
abspath := absolutePath(f, *goroot)
- data, err := ioutil.ReadFile(abspath)
+ data, err := fs.ReadFile(abspath)
if err != nil {
log.Print(err)
serveError(w, r, f, err)
"go/doc"
"go/parser"
"go/token"
- "io/ioutil"
"log"
- "os"
"path/filepath"
"strings"
"unicode"
}
-func isGoFile(f *os.FileInfo) bool {
- return f.IsRegular() &&
- !strings.HasPrefix(f.Name, ".") && // ignore .files
- filepath.Ext(f.Name) == ".go"
+func isGoFile(fi FileInfo) bool {
+ name := fi.Name()
+ return fi.IsRegular() &&
+ !strings.HasPrefix(name, ".") && // ignore .files
+ filepath.Ext(name) == ".go"
}
-func isPkgFile(f *os.FileInfo) bool {
- return isGoFile(f) &&
- !strings.HasSuffix(f.Name, "_test.go") // ignore test files
+func isPkgFile(fi FileInfo) bool {
+ return isGoFile(fi) &&
+ !strings.HasSuffix(fi.Name(), "_test.go") // ignore test files
}
-func isPkgDir(f *os.FileInfo) bool {
- return f.IsDirectory() && len(f.Name) > 0 && f.Name[0] != '_'
+func isPkgDir(fi FileInfo) bool {
+ name := fi.Name()
+ return fi.IsDirectory() && len(name) > 0 && name[0] != '_'
}
return &Directory{depth, path, name, "", nil}
}
- list, err := ioutil.ReadDir(path)
+ list, err := fs.ReadDir(path)
if err != nil {
// newDirTree is called with a path that should be a package
// directory; errors here should not happen, but if they do,
// we want to know about them
- log.Printf("ioutil.ReadDir(%s): %s", path, err)
+ log.Printf("ReadDir(%s): %s", path, err)
}
// determine number of subdirectories and if there are package files
// though the directory doesn't contain any real package files - was bug)
if synopses[0] == "" {
// no "optimal" package synopsis yet; continue to collect synopses
- file, err := parser.ParseFile(fset, filepath.Join(path, d.Name), nil,
+ file, err := parser.ParseFile(fset, filepath.Join(path, d.Name()), nil,
parser.ParseComments|parser.PackageClauseOnly)
if err == nil {
hasPkgFiles = true
i := 0
for _, d := range list {
if isPkgDir(d) {
- dd := b.newDirTree(fset, filepath.Join(path, d.Name), d.Name, depth+1)
+ name := d.Name()
+ dd := b.newDirTree(fset, filepath.Join(path, name), name, depth+1)
if dd != nil {
dirs[i] = dd
i++
// (i.e., in this case the tree may contain directories w/o any package files).
//
func newDirectory(root string, pathFilter func(string) bool, maxDepth int) *Directory {
- // The root could be a symbolic link so use os.Stat not os.Lstat.
- d, err := os.Stat(root)
+ // The root could be a symbolic link so use Stat not Lstat.
+ d, err := fs.Stat(root)
// If we fail here, report detailed error messages; otherwise
// is is hard to see why a directory tree was not built.
switch {
b := treeBuilder{pathFilter, maxDepth}
// the file set provided is only for local parsing, no position
// information escapes and thus we don't need to save the set
- return b.newDirTree(token.NewFileSet(), root, d.Name, 0)
+ return b.newDirTree(token.NewFileSet(), root, d.Name(), 0)
}
--- /dev/null
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file defines abstract file system access.
+
+package main
+
+import (
+ "io"
+ "io/ioutil"
+ "os"
+)
+
+
+// The FileInfo interface provides access to file information.
+type FileInfo interface {
+ Name() string
+ Size() int64
+ IsRegular() bool
+ IsDirectory() bool
+}
+
+
+// The FileSystem interface specifies the methods godoc is using
+// to access the file system for which it serves documentation.
+type FileSystem interface {
+ Open(path string) (io.ReadCloser, os.Error)
+ Lstat(path string) (FileInfo, os.Error)
+ Stat(path string) (FileInfo, os.Error)
+ ReadDir(path string) ([]FileInfo, os.Error)
+ ReadFile(path string) ([]byte, os.Error)
+}
+
+
+// ----------------------------------------------------------------------------
+// OS-specific FileSystem implementation
+
+var OS FileSystem = osFS{}
+
+
+// osFI is the OS-specific implementation of FileInfo.
+type osFI struct {
+ *os.FileInfo
+}
+
+
+func (fi osFI) Name() string {
+ return fi.FileInfo.Name
+}
+
+
+func (fi osFI) Size() int64 {
+ if fi.IsDirectory() {
+ return 0
+ }
+ return fi.FileInfo.Size
+}
+
+
+// osFS is the OS-specific implementation of FileSystem
+type osFS struct{}
+
+func (osFS) Open(path string) (io.ReadCloser, os.Error) {
+ return os.Open(path)
+}
+
+
+func (osFS) Lstat(path string) (FileInfo, os.Error) {
+ fi, err := os.Lstat(path)
+ return osFI{fi}, err
+}
+
+
+func (osFS) Stat(path string) (FileInfo, os.Error) {
+ fi, err := os.Stat(path)
+ return osFI{fi}, err
+}
+
+
+func (osFS) ReadDir(path string) ([]FileInfo, os.Error) {
+ l0, err := ioutil.ReadDir(path)
+ if err != nil {
+ return nil, err
+ }
+ l1 := make([]FileInfo, len(l0))
+ for i, e := range l0 {
+ l1[i] = osFI{e}
+ }
+ return l1, nil
+}
+
+
+func (osFS) ReadFile(path string) ([]byte, os.Error) {
+ return ioutil.ReadFile(path)
+}
"fmt"
"go/ast"
"go/doc"
- "go/parser"
"go/printer"
"go/token"
"http"
"io"
- "io/ioutil"
"log"
"os"
"path"
maxResults = flag.Int("maxresults", 10000, "maximum number of full text search results shown")
// file system mapping
- fsMap Mapping // user-defined mapping
- fsTree RWValue // *Directory tree of packages, updated with each sync
- pathFilter RWValue // filter used when building fsMap directory trees
- fsModified RWValue // timestamp of last call to invalidateIndex
+ fs FileSystem // the underlying file system
+ fsMap Mapping // user-defined mapping
+ fsTree RWValue // *Directory tree of packages, updated with each sync
+ pathFilter RWValue // filter used when building fsMap directory trees
+ fsModified RWValue // timestamp of last call to invalidateIndex
// http handlers
fileServer http.Handler // default file server
// readDirList reads a file containing a newline-separated list
// of directory paths and returns the list of paths.
func readDirList(filename string) ([]string, os.Error) {
- contents, err := ioutil.ReadFile(filename)
+ contents, err := fs.ReadFile(filename)
if err != nil {
return nil, err
}
// create a sorted list of valid directory names
filter := func(path string) bool {
- d, e := os.Lstat(path)
+ d, e := fs.Lstat(path)
if e != nil && err == nil {
// remember first error and return it from readDirList
// so we have at least some information if things go bad
// Template formatter for "dir/" format.
func dirslashFmt(w io.Writer, format string, x ...interface{}) {
- if x[0].(*os.FileInfo).IsDirectory() {
+ if x[0].(FileInfo).IsDirectory() {
w.Write([]byte{'/'})
}
}
if *templateDir != "" {
defaultpath := path
path = filepath.Join(*templateDir, name)
- if _, err := os.Stat(path); err != nil {
+ if _, err := fs.Stat(path); err != nil {
log.Print("readTemplate:", err)
path = defaultpath
}
}
- data, err := ioutil.ReadFile(path)
+ data, err := fs.ReadFile(path)
if err != nil {
log.Fatalf("ReadFile %s: %v", path, err)
}
func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath string) {
// get HTML body contents
- src, err := ioutil.ReadFile(abspath)
+ src, err := fs.ReadFile(abspath)
if err != nil {
- log.Printf("ioutil.ReadFile: %s", err)
+ log.Printf("ReadFile: %s", err)
serveError(w, r, relpath, err)
return
}
}
func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, title string) {
- src, err := ioutil.ReadFile(abspath)
+ src, err := fs.ReadFile(abspath)
if err != nil {
- log.Printf("ioutil.ReadFile: %s", err)
+ log.Printf("ReadFile: %s", err)
serveError(w, r, relpath, err)
return
}
return
}
- list, err := ioutil.ReadDir(abspath)
+ list, err := fs.ReadDir(abspath)
if err != nil {
- log.Printf("ioutil.ReadDir: %s", err)
+ log.Printf("ReadDir: %s", err)
serveError(w, r, relpath, err)
return
}
- for _, d := range list {
- if d.IsDirectory() {
- d.Size = 0
- }
- }
-
contents := applyTemplate(dirlistHTML, "dirlistHTML", list)
servePage(w, "Directory "+relpath, "", "", contents)
}
return
}
- dir, err := os.Lstat(abspath)
+ dir, err := fs.Lstat(abspath)
if err != nil {
log.Print(err)
serveError(w, r, relpath, err)
//
func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInfoMode) PageInfo {
// filter function to select the desired .go files
- filter := func(d *os.FileInfo) bool {
+ filter := func(d FileInfo) bool {
// If we are looking at cmd documentation, only accept
// the special fakePkgFile containing the documentation.
- return isPkgFile(d) && (h.isPkg || d.Name == fakePkgFile)
+ return isPkgFile(d) && (h.isPkg || d.Name() == fakePkgFile)
}
// get package ASTs
fset := token.NewFileSet()
- pkgs, err := parser.ParseDir(fset, abspath, filter, parser.ParseComments)
+ pkgs, err := parseDir(fset, abspath, filter)
if err != nil && pkgs == nil {
// only report directory read errors, ignore parse errors
// (may be able to extract partial package information)
"go/token"
"go/scanner"
"index/suffixarray"
- "io/ioutil"
"os"
"path/filepath"
"regexp"
// failed (that is, if the file was not added), it returns file == nil.
func (x *Indexer) addFile(filename string, goFile bool) (file *token.File, ast *ast.File) {
// open file
- f, err := os.Open(filename)
+ f, err := fs.Open(filename)
if err != nil {
return
}
}
-func (x *Indexer) visitFile(dirname string, f *os.FileInfo, fulltextIndex bool) {
+func (x *Indexer) visitFile(dirname string, f FileInfo, fulltextIndex bool) {
if !f.IsRegular() {
return
}
- filename := filepath.Join(dirname, f.Name)
+ filename := filepath.Join(dirname, f.Name())
goFile := false
switch {
}
goFile = true
- case !fulltextIndex || !isWhitelisted(f.Name):
+ case !fulltextIndex || !isWhitelisted(f.Name()):
return
}
// index all files in the directories given by dirnames
for dirname := range dirnames {
- list, err := ioutil.ReadDir(dirname)
+ list, err := fs.ReadDir(dirname)
if err != nil {
continue // ignore this directory
}
flag.Usage = usage
flag.Parse()
+ // Determine file system to use.
+ // TODO(gri) Complete this - for now we only have one.
+ fs = OS
+
// Clean goroot: normalize path separator.
*goroot = filepath.Clean(*goroot)
import (
"fmt"
"io"
- "os"
"path"
"path/filepath"
"sort"
continue // no match
}
abspath := filepath.Join(e.path, tail)
- if _, err := os.Stat(abspath); err == nil {
+ if _, err := fs.Stat(abspath); err == nil {
return abspath
}
}
--- /dev/null
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains support functions for parsing .go files.
+// Similar functionality is found in package go/parser but the
+// functions here operate using godoc's file system fs instead
+// of calling the OS's file operations directly.
+
+package main
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "os"
+ "path/filepath"
+)
+
+func parseFiles(fset *token.FileSet, filenames []string) (pkgs map[string]*ast.Package, first os.Error) {
+ pkgs = make(map[string]*ast.Package)
+ for _, filename := range filenames {
+ src, err := fs.ReadFile(filename)
+ if err != nil {
+ if first == nil {
+ first = err
+ }
+ continue
+ }
+
+ file, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
+ if err != nil {
+ if first == nil {
+ first = err
+ }
+ continue
+ }
+
+ name := file.Name.Name
+ pkg, found := pkgs[name]
+ if !found {
+ // TODO(gri) Use NewPackage here; reconsider ParseFiles API.
+ pkg = &ast.Package{name, nil, nil, make(map[string]*ast.File)}
+ pkgs[name] = pkg
+ }
+ pkg.Files[filename] = file
+ }
+ return
+}
+
+
+func parseDir(fset *token.FileSet, path string, filter func(FileInfo) bool) (map[string]*ast.Package, os.Error) {
+ list, err := fs.ReadDir(path)
+ if err != nil {
+ return nil, err
+ }
+
+ filenames := make([]string, len(list))
+ i := 0
+ for _, d := range list {
+ if filter == nil || filter(d) {
+ filenames[i] = filepath.Join(path, d.Name())
+ i++
+ }
+ }
+ filenames = filenames[0:i]
+
+ return parseFiles(fset, filenames)
+}
}
+// TODO(gri) For now, using os.Getwd() is ok here since the functionality
+// based on this code is not invoked for the appengine version,
+// but this is fragile. Determine what the right thing to do is,
+// here (possibly have some Getwd-equivalent in FileSystem).
var cwd, _ = os.Getwd() // ignore errors
// canonicalizePaths takes a list of (directory/file) paths and returns
// atomically renames that file to the file named by filename.
//
func writeFileAtomically(filename string, data []byte) os.Error {
+ // TODO(gri) this won't work on appengine
f, err := ioutil.TempFile(filepath.Split(filename))
if err != nil {
return err
// the extension is not known; read an initial chunk
// of the file and check if it looks like text
- f, err := os.Open(filename)
+ f, err := fs.Open(filename)
if err != nil {
return false
}