Exemple #1
0
func newDirTree(path, name string, depth int) *Directory {
	if depth <= 0 {
		// return a dummy directory so that the parent directory
		// doesn't get discarded just because we reached the max
		// directory depth
		return &Directory{path, name, nil}
	}

	list, _ := io.ReadDir(path) // ignore errors

	// determine number of subdirectories and package files
	ndirs := 0
	nfiles := 0
	for _, d := range list {
		switch {
		case isPkgDir(d):
			ndirs++
		case isPkgFile(d):
			nfiles++
		}
	}

	// create subdirectory tree
	var dirs []*Directory
	if ndirs > 0 {
		dirs = make([]*Directory, ndirs)
		i := 0
		for _, d := range list {
			if isPkgDir(d) {
				dd := newDirTree(pathutil.Join(path, d.Name), d.Name, depth-1)
				if dd != nil {
					dirs[i] = dd
					i++
				}
			}
		}
		dirs = dirs[0:i]
	}

	// if there are no package files and no subdirectories
	// (with package files), ignore the directory
	if nfiles == 0 && len(dirs) == 0 {
		return nil
	}

	return &Directory{path, name, dirs}
}
Exemple #2
0
func serveDirectory(c *http.Conn, r *http.Request, path string) {
	if redirect(c, r) {
		return
	}

	list, err := io.ReadDir(path)
	if err != nil {
		http.NotFound(c, r)
		return
	}

	var buf bytes.Buffer
	if err := dirlistHTML.Execute(list, &buf); err != nil {
		log.Stderrf("dirlistHTML.Execute: %s", err)
	}

	servePage(c, "Directory "+path, "", buf.Bytes())
}
Exemple #3
0
func walk(path string, d *os.Dir, v Visitor, errors chan<- os.Error) {
	if !d.IsDirectory() {
		v.VisitFile(path, d)
		return
	}

	if !v.VisitDir(path, d) {
		return // skip directory entries
	}

	list, err := io.ReadDir(path)
	if err != nil {
		if errors != nil {
			errors <- err
		}
	}

	for _, e := range list {
		walk(Join(path, e.Name), e, v, errors)
	}
}
Exemple #4
0
func allPaged(c *http.Conn, req *http.Request, page int) {
	files, ok := io.ReadDir(PATH)
	sort.Sort(pasteList(files))

	if ok != nil {
		c.Write(strings.Bytes("Error reading pastes directory.\n"))
		return
	}

	offset := PER_PAGE * (page - 1)

	limit := len(files) - offset
	if limit > PER_PAGE {
		limit = PER_PAGE
	}

	if limit < 0 {
		c.Write(strings.Bytes("Page too far.\n"))
		return
	}

	pastes := make([]string, limit)
	for i, j := 0, offset; j < offset+limit; j++ {
		if strings.HasPrefix(files[j].Name, "private:") {
			limit++
			continue
		}

		pastes[i] = files[j].Name
		i++
	}

	prev := ""
	if page > 1 {
		prev = "/all/page/" + strconv.Itoa(page-1)
	}

	next := ""
	if len(files)/PER_PAGE > page {
		next = "/all/page/" + strconv.Itoa(page+1)
	}

	codeList := make([]string, len(pastes))
	results := make(chan int)
	for i, paste := range pastes {
		go func(i int, paste string) {
			code, err := prettyPaste(paste, 10)
			if err != nil {
				code[0] = err.String()
			}

			codeList[i] =
				fmt.Sprintf(
					H2(
						"Paste ",
						A("#%s").Attrs(As{
							"href": "/view/%s",
						})).Out(),
					paste, paste) + code[0]

			results <- i
		}(i, paste)
	}

	for i := 0; i < len(pastes); i++ {
		<-results
	}

	theme := curTheme(req)
	allPage.Execute(allEnv{prev, next, codeList, theme, themeSelect(theme)}, c)
}
Exemple #5
0
func newDirTree(path, name string, depth, maxDepth int) *Directory {
	if depth >= maxDepth {
		// return a dummy directory so that the parent directory
		// doesn't get discarded just because we reached the max
		// directory depth
		return &Directory{depth, path, name, "", nil}
	}

	list, _ := io.ReadDir(path) // ignore errors

	// determine number of subdirectories and package files
	ndirs := 0
	nfiles := 0
	text := ""
	for _, d := range list {
		switch {
		case isPkgDir(d):
			ndirs++
		case isPkgFile(d):
			nfiles++
			if text == "" {
				// no package documentation yet; take the first found
				file, err := parser.ParseFile(pathutil.Join(path, d.Name), nil,
					parser.ParseComments|parser.PackageClauseOnly)
				if err == nil &&
					// Also accept fakePkgName, so we get synopses for commmands.
					// Note: This may lead to incorrect results if there is a
					// (left-over) "documentation" package somewhere in a package
					// directory of different name, but this is very unlikely and
					// against current conventions.
					(file.Name.Value == name || file.Name.Value == fakePkgName) &&
					file.Doc != nil {
					// found documentation; extract a synopsys
					text = firstSentence(doc.CommentText(file.Doc))
				}
			}
		}
	}

	// create subdirectory tree
	var dirs []*Directory
	if ndirs > 0 {
		dirs = make([]*Directory, ndirs)
		i := 0
		for _, d := range list {
			if isPkgDir(d) {
				dd := newDirTree(pathutil.Join(path, d.Name), d.Name, depth+1, maxDepth)
				if dd != nil {
					dirs[i] = dd
					i++
				}
			}
		}
		dirs = dirs[0:i]
	}

	// if there are no package files and no subdirectories
	// (with package files), ignore the directory
	if nfiles == 0 && len(dirs) == 0 {
		return nil
	}

	return &Directory{depth, path, name, text, dirs}
}