remove unused TypeBanned, add sort-by-name step to directory listing

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
master
kim (grufwub) 4 years ago
parent 7ab50bfc86
commit 64f05141e5

@ -86,6 +86,5 @@ const (
/* Gophor specific types */
TypeExec = ItemType('$') /* Execute command and insert stdout here */
TypeInfoNotStated = ItemType('z') /* INTERNAL USE. We use this in a switch case, a line never starts with this */
TypeBanned = ItemType('@') /* INTERNAL USE. We use this in a switch case, a line never starts with this */
TypeUnknown = ItemType('?') /* INTERNAL USE. We use this in a switch case, a line never starts with this */
)

35
fs.go

@ -7,6 +7,7 @@ import (
"bytes"
"time"
"io"
"sort"
"bufio"
)
@ -204,23 +205,26 @@ func _listDir(dirPath string, hidden map[string]bool) ([]byte, *GophorError) {
return nil, &GophorError{ FileOpenErr, err }
}
/* Open directory stream for reading */
/* Read files in directory */
files, err := fd.Readdir(-1)
if err != nil {
logSystemError("failed to enumerate dir %s: %s\n", dirPath, err.Error())
return nil, &GophorError{ DirListErr, err }
}
/* Walk through directory */
/* Sort the files by name */
sort.Sort(byName(files))
/* Create directory content slice, ready */
dirContents := make([]byte, 0)
/* First add a 'back' entry. GoLang Readdir() seems to miss this */
line := buildLine(TypeDirectory, "..", path.Join(fd.Name(), ".."), *ServerHostname, *ServerPort)
dirContents = append(dirContents, line...)
/* Iterate through files :) */
/* Walk through files :D */
for _, file := range files {
/* Skip dotfiles + gophermap file + requested hidden */
/* If requested hidden */
if _, ok := hidden[file.Name()]; ok {
continue
}
@ -237,10 +241,6 @@ func _listDir(dirPath string, hidden map[string]bool) ([]byte, *GophorError) {
/* Regular file -- find item type and creating listing */
itemPath := path.Join(fd.Name(), file.Name())
itemType := getItemType(itemPath)
if itemType == TypeBanned {
/* Banned file extension, skipping! */
break
}
line = buildLine(itemType, file.Name(), itemPath, *ServerHostname, *ServerPort)
dirContents = append(dirContents, line...)
@ -259,21 +259,24 @@ func _listDirRegexMatch(dirPath string, hidden map[string]bool) ([]byte, *Gophor
return nil, &GophorError{ FileOpenErr, err }
}
/* Open directory stream for reading */
/* Read files in directory */
files, err := fd.Readdir(-1)
if err != nil {
logSystemError("failed to enumerate dir %s: %s\n", dirPath, err.Error())
return nil, &GophorError{ DirListErr, err }
}
/* Walk through directory */
/* Sort the files by name */
sort.Sort(byName(files))
/* Create directory content slice, ready */
dirContents := make([]byte, 0)
/* First add a 'back' entry. GoLang Readdir() seems to miss this */
line := buildLine(TypeDirectory, "..", path.Join(fd.Name(), ".."), *ServerHostname, *ServerPort)
dirContents = append(dirContents, line...)
/* Iterate through files :) */
/* Walk through files :D */
for _, file := range files {
/* If regex match in restricted files || requested hidden */
if isRestrictedFile(file.Name()) {
@ -294,10 +297,6 @@ func _listDirRegexMatch(dirPath string, hidden map[string]bool) ([]byte, *Gophor
/* Regular file -- find item type and creating listing */
itemPath := path.Join(fd.Name(), file.Name())
itemType := getItemType(itemPath)
if itemType == TypeBanned {
/* Banned file extension, skipping! */
break
}
line = buildLine(itemType, file.Name(), itemPath, *ServerHostname, *ServerPort)
dirContents = append(dirContents, line...)
@ -308,3 +307,9 @@ func _listDirRegexMatch(dirPath string, hidden map[string]bool) ([]byte, *Gophor
return dirContents, nil
}
/* Took a leaf out of go-gopher's book here. */
type byName []os.FileInfo
func (s byName) Len() int { return len(s) }
func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

Loading…
Cancel
Save