2013-07-16 22:44:18 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
2013-07-16 22:02:35 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-07-16 22:44:18 -06:00
|
|
|
// Package util contains utility types and functions for godoc.
|
|
|
|
package util
|
2013-07-16 22:02:35 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
pathpkg "path"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
2013-07-16 22:44:18 -06:00
|
|
|
|
|
|
|
"code.google.com/p/go.tools/godoc/vfs"
|
2013-07-16 22:02:35 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// An RWValue wraps a value and permits mutually exclusive
|
|
|
|
// access to it and records the time the value was last set.
|
|
|
|
type RWValue struct {
|
|
|
|
mutex sync.RWMutex
|
|
|
|
value interface{}
|
|
|
|
timestamp time.Time // time of last set()
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:44:18 -06:00
|
|
|
func (v *RWValue) Set(value interface{}) {
|
2013-07-16 22:02:35 -06:00
|
|
|
v.mutex.Lock()
|
|
|
|
v.value = value
|
|
|
|
v.timestamp = time.Now()
|
|
|
|
v.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:44:18 -06:00
|
|
|
func (v *RWValue) Get() (interface{}, time.Time) {
|
2013-07-16 22:02:35 -06:00
|
|
|
v.mutex.RLock()
|
|
|
|
defer v.mutex.RUnlock()
|
|
|
|
return v.value, v.timestamp
|
|
|
|
}
|
|
|
|
|
2013-07-22 18:37:43 -06:00
|
|
|
// IsText reports whether a significant prefix of s looks like correct UTF-8;
|
2013-07-16 22:02:35 -06:00
|
|
|
// that is, if it is likely that s is human-readable text.
|
2013-07-16 22:44:18 -06:00
|
|
|
func IsText(s []byte) bool {
|
2013-07-16 22:02:35 -06:00
|
|
|
const max = 1024 // at least utf8.UTFMax
|
|
|
|
if len(s) > max {
|
|
|
|
s = s[0:max]
|
|
|
|
}
|
|
|
|
for i, c := range string(s) {
|
|
|
|
if i+utf8.UTFMax > len(s) {
|
|
|
|
// last char may be incomplete - ignore
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
|
|
|
|
// decoding error or control character - not a text file
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// textExt[x] is true if the extension x indicates a text file, and false otherwise.
|
|
|
|
var textExt = map[string]bool{
|
|
|
|
".css": false, // must be served raw
|
|
|
|
".js": false, // must be served raw
|
|
|
|
}
|
|
|
|
|
2013-07-22 18:37:43 -06:00
|
|
|
// IsTextFile reports whether the file has a known extension indicating
|
2013-07-16 22:02:35 -06:00
|
|
|
// a text file, or if a significant chunk of the specified file looks like
|
|
|
|
// correct UTF-8; that is, if it is likely that the file contains human-
|
|
|
|
// readable text.
|
2013-07-16 22:44:18 -06:00
|
|
|
func IsTextFile(fs vfs.Opener, filename string) bool {
|
2013-07-16 22:02:35 -06:00
|
|
|
// if the extension is known, use it for decision making
|
|
|
|
if isText, found := textExt[pathpkg.Ext(filename)]; found {
|
|
|
|
return isText
|
|
|
|
}
|
|
|
|
|
|
|
|
// the extension is not known; read an initial chunk
|
|
|
|
// of the file and check if it looks like text
|
|
|
|
f, err := fs.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var buf [1024]byte
|
|
|
|
n, err := f.Read(buf[0:])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:44:18 -06:00
|
|
|
return IsText(buf[0:n])
|
2013-07-16 22:02:35 -06:00
|
|
|
}
|