2020-04-13 12:15:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-13 21:48:49 +00:00
|
|
|
"os"
|
2020-04-13 12:15:12 +00:00
|
|
|
"sync"
|
2020-04-30 09:27:05 +00:00
|
|
|
"time"
|
2020-04-13 12:15:12 +00:00
|
|
|
)
|
|
|
|
|
2020-04-30 09:48:24 +00:00
|
|
|
const (
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Help converting file size stat to supplied size in megabytes */
|
|
|
|
BytesInMegaByte = 1048576.0
|
|
|
|
|
|
|
|
/* Filename constants */
|
|
|
|
CgiBinDirStr = "cgi-bin"
|
|
|
|
GophermapFileStr = "gophermap"
|
2020-04-30 09:48:24 +00:00
|
|
|
)
|
|
|
|
|
2020-04-29 17:28:51 +00:00
|
|
|
type FileSystem struct {
|
2020-05-04 17:19:56 +00:00
|
|
|
/* Holds and helps manage our file cache, as well as managing
|
|
|
|
* access and responding to filesystem requests submitted by
|
|
|
|
* a worker instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CacheMap *FixedMap /* Fixed size cache map */
|
|
|
|
CacheMutex sync.RWMutex /* RWMutex for safe cachemap access */
|
|
|
|
CacheFileMax int64 /* Cache file size max */
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:28:51 +00:00
|
|
|
func (fs *FileSystem) Init(size int, fileSizeMax float64) {
|
|
|
|
fs.CacheMap = NewFixedMap(size)
|
|
|
|
fs.CacheMutex = sync.RWMutex{}
|
|
|
|
fs.CacheFileMax = int64(BytesInMegaByte * fileSizeMax)
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 17:19:56 +00:00
|
|
|
func (fs *FileSystem) HandleRequest(request *FileSystemRequest) ([]byte, *GophorError) {
|
2020-05-01 14:08:47 +00:00
|
|
|
/* Get filesystem stat, check it exists! */
|
2020-05-04 17:19:56 +00:00
|
|
|
stat, err := os.Stat(request.AbsPath())
|
2020-05-01 14:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
/* Check file isn't in cache before throwing in the towel */
|
|
|
|
fs.CacheMutex.RLock()
|
2020-05-04 17:19:56 +00:00
|
|
|
file := fs.CacheMap.Get(request.AbsPath())
|
2020-05-01 14:08:47 +00:00
|
|
|
if file == nil {
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-05-01 14:08:47 +00:00
|
|
|
return nil, &GophorError{ FileStatErr, err }
|
2020-04-25 17:38:26 +00:00
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
|
2020-05-01 14:08:47 +00:00
|
|
|
/* It's there! Get contents, unlock and return */
|
|
|
|
file.Mutex.RLock()
|
2020-05-04 17:19:56 +00:00
|
|
|
b := file.Contents(request)
|
2020-05-01 14:08:47 +00:00
|
|
|
file.Mutex.RUnlock()
|
2020-04-29 22:21:20 +00:00
|
|
|
|
2020-05-01 14:08:47 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
|
|
|
return b, nil
|
|
|
|
}
|
2020-04-29 22:21:20 +00:00
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Handle file type */
|
2020-05-01 14:08:47 +00:00
|
|
|
switch {
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Directory */
|
2020-05-01 14:08:47 +00:00
|
|
|
case stat.Mode() & os.ModeDir != 0:
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Ignore cgi-bin directory */
|
2020-05-04 17:19:56 +00:00
|
|
|
if request.HasRelPathPrefix(CgiBinDirStr) {
|
2020-05-03 21:42:51 +00:00
|
|
|
return nil, &GophorError{ IllegalPathErr, nil }
|
|
|
|
}
|
2020-04-29 17:28:51 +00:00
|
|
|
|
2020-04-30 12:58:29 +00:00
|
|
|
/* Check Gophermap exists */
|
2020-05-04 17:19:56 +00:00
|
|
|
gophermapRequest := NewFileSystemRequest(request.Host, request.Client, request.RootDir, request.JoinRelPath(GophermapFileStr), request.Parameters)
|
|
|
|
stat, err = os.Stat(gophermapRequest.AbsPath())
|
2020-04-30 12:58:29 +00:00
|
|
|
|
|
|
|
var output []byte
|
|
|
|
var gophorErr *GophorError
|
2020-04-29 17:28:51 +00:00
|
|
|
if err == nil {
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Gophermap exists! If executable execute, else serve. */
|
|
|
|
if stat.Mode().Perm() & 0100 != 0 {
|
2020-05-04 17:19:56 +00:00
|
|
|
output, gophorErr = executeFile(gophermapRequest)
|
2020-05-03 21:42:51 +00:00
|
|
|
} else {
|
2020-05-04 17:19:56 +00:00
|
|
|
output, gophorErr = fs.FetchFile(gophermapRequest)
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
2020-04-29 17:28:51 +00:00
|
|
|
} else {
|
|
|
|
/* No gophermap, serve directory listing */
|
2020-05-04 17:19:56 +00:00
|
|
|
output, gophorErr = listDir(request, map[string]bool{})
|
2020-04-30 12:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if gophorErr != nil {
|
|
|
|
/* Fail out! */
|
|
|
|
return nil, gophorErr
|
2020-04-29 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:58:29 +00:00
|
|
|
/* Append footer text (contains last line) and return */
|
|
|
|
output = append(output, Config.FooterText...)
|
|
|
|
return output, nil
|
|
|
|
|
2020-04-29 17:28:51 +00:00
|
|
|
/* Regular file */
|
2020-05-03 21:42:51 +00:00
|
|
|
case stat.Mode() & os.ModeType == 0:
|
|
|
|
/* If cgi-bin, return executed contents. Else, fetch */
|
2020-05-04 17:19:56 +00:00
|
|
|
if request.HasRelPathPrefix(CgiBinDirStr) {
|
|
|
|
return executeCgi(request)
|
2020-05-03 21:42:51 +00:00
|
|
|
} else {
|
2020-05-04 17:19:56 +00:00
|
|
|
return fs.FetchFile(request)
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
2020-04-29 17:28:51 +00:00
|
|
|
|
|
|
|
/* Unsupported type */
|
|
|
|
default:
|
|
|
|
return nil, &GophorError{ FileTypeErr, nil }
|
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 17:28:51 +00:00
|
|
|
func (fs *FileSystem) FetchFile(request *FileSystemRequest) ([]byte, *GophorError) {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Get cache map read lock then check if file in cache map */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RLock()
|
2020-05-04 17:19:56 +00:00
|
|
|
file := fs.CacheMap.Get(request.AbsPath())
|
2020-04-24 11:09:54 +00:00
|
|
|
|
2020-04-19 19:44:48 +00:00
|
|
|
if file != nil {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* File in cache -- before doing anything get file read lock */
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.RLock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Check file is marked as fresh */
|
2020-04-25 18:56:03 +00:00
|
|
|
if !file.Fresh {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* File not fresh! Swap file read for write-lock */
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.RUnlock()
|
|
|
|
file.Mutex.Lock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Reload file contents from disk */
|
2020-04-19 19:44:48 +00:00
|
|
|
gophorErr := file.LoadContents()
|
2020-04-13 21:48:49 +00:00
|
|
|
if gophorErr != nil {
|
2020-04-14 10:49:37 +00:00
|
|
|
/* Error loading contents, unlock all mutex then return error */
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.Unlock()
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-04-13 21:48:49 +00:00
|
|
|
return nil, gophorErr
|
|
|
|
}
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Updated! Swap back file write for read lock */
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.Unlock()
|
|
|
|
file.Mutex.RLock()
|
2020-04-13 21:48:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Perform filesystem stat ready for checking file size later.
|
|
|
|
* Doing this now allows us to weed-out non-existent files early
|
|
|
|
*/
|
2020-05-04 17:19:56 +00:00
|
|
|
stat, err := os.Stat(request.AbsPath())
|
2020-04-15 11:43:02 +00:00
|
|
|
if err != nil {
|
2020-04-17 17:12:36 +00:00
|
|
|
/* Error stat'ing file, unlock read mutex then return error */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-04-15 11:43:02 +00:00
|
|
|
return nil, &GophorError{ FileStatErr, err }
|
|
|
|
}
|
2020-04-13 21:48:49 +00:00
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Create new file contents */
|
2020-04-29 17:28:51 +00:00
|
|
|
var contents FileContents
|
2020-05-04 17:19:56 +00:00
|
|
|
if request.HasAbsPathSuffix("/"+GophermapFileStr) {
|
|
|
|
contents = &GophermapContents{ request, nil }
|
2020-04-29 17:28:51 +00:00
|
|
|
} else {
|
2020-05-04 17:19:56 +00:00
|
|
|
contents = &RegularFileContents{ request, nil }
|
2020-04-29 17:28:51 +00:00
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
|
|
|
|
/* Create new file wrapper around contents */
|
2020-05-04 17:19:56 +00:00
|
|
|
file = &File{ contents, sync.RWMutex{}, true, time.Now().UnixNano() }
|
2020-04-13 12:15:12 +00:00
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* File isn't in cache yet so no need to get file lock mutex */
|
2020-04-13 21:48:49 +00:00
|
|
|
gophorErr := file.LoadContents()
|
|
|
|
if gophorErr != nil {
|
2020-04-17 17:12:36 +00:00
|
|
|
/* Error loading contents, unlock read mutex then return error */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-04-13 21:48:49 +00:00
|
|
|
return nil, gophorErr
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Compare file size (in MB) to CacheFileSizeMax, if larger just get file
|
|
|
|
* contents, unlock all mutex and don't bother caching.
|
|
|
|
*/
|
2020-04-29 17:28:51 +00:00
|
|
|
if stat.Size() > fs.CacheFileMax {
|
2020-04-24 10:21:46 +00:00
|
|
|
b := file.Contents(request)
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-04-15 11:43:02 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:14:36 +00:00
|
|
|
/* File not in cache -- Swap cache map read for write lock. */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
|
|
|
fs.CacheMutex.Lock()
|
2020-04-15 11:43:02 +00:00
|
|
|
|
2020-04-19 19:44:48 +00:00
|
|
|
/* Put file in the FixedMap */
|
2020-05-04 17:19:56 +00:00
|
|
|
fs.CacheMap.Put(request.AbsPath(), file)
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Before unlocking cache mutex, lock file read for upcoming call to .Contents() */
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.RLock()
|
2020-04-15 11:43:02 +00:00
|
|
|
|
2020-04-14 10:49:37 +00:00
|
|
|
/* Swap cache lock back to read */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.Unlock()
|
|
|
|
fs.CacheMutex.RLock()
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Read file contents into new variable for return, then unlock file read lock */
|
2020-04-24 10:21:46 +00:00
|
|
|
b := file.Contents(request)
|
2020-04-25 18:56:03 +00:00
|
|
|
file.Mutex.RUnlock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Finally we can unlock the cache map read lock, we are done :) */
|
2020-04-29 17:28:51 +00:00
|
|
|
fs.CacheMutex.RUnlock()
|
2020-04-13 12:15:12 +00:00
|
|
|
|
2020-04-13 21:48:49 +00:00
|
|
|
return b, nil
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
2020-04-30 09:27:05 +00:00
|
|
|
|
|
|
|
type File struct {
|
2020-05-04 17:19:56 +00:00
|
|
|
/* Wraps around the cached contents of a file
|
|
|
|
* helping with management of this content by
|
|
|
|
* a FileSystem instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Content FileContents
|
2020-04-30 09:27:05 +00:00
|
|
|
Mutex sync.RWMutex
|
|
|
|
Fresh bool
|
|
|
|
LastRefresh int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Contents(request *FileSystemRequest) []byte {
|
2020-05-04 17:19:56 +00:00
|
|
|
return f.Content.Render(request)
|
2020-04-30 09:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) LoadContents() *GophorError {
|
|
|
|
/* Clear current file contents */
|
2020-05-04 17:19:56 +00:00
|
|
|
f.Content.Clear()
|
2020-04-30 09:27:05 +00:00
|
|
|
|
|
|
|
/* Reload the file */
|
2020-05-04 17:19:56 +00:00
|
|
|
gophorErr := f.Content.Load()
|
2020-04-30 09:27:05 +00:00
|
|
|
if gophorErr != nil {
|
|
|
|
return gophorErr
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update lastRefresh, set fresh, unset deletion (not likely set) */
|
|
|
|
f.LastRefresh = time.Now().UnixNano()
|
|
|
|
f.Fresh = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startFileMonitor(sleepTime time.Duration) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
/* Sleep so we don't take up all the precious CPU time :) */
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
|
|
|
|
/* Check global file cache freshness */
|
|
|
|
checkCacheFreshness()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We shouldn't have reached here */
|
2020-05-02 21:34:49 +00:00
|
|
|
Config.SysLog.Fatal("", "FileCache monitor escaped run loop!\n")
|
2020-04-30 09:27:05 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCacheFreshness() {
|
|
|
|
/* Before anything, get cache write lock (in case we have to delete) */
|
|
|
|
Config.FileSystem.CacheMutex.Lock()
|
|
|
|
|
|
|
|
/* Iterate through paths in cache map to query file last modified times */
|
|
|
|
for path := range Config.FileSystem.CacheMap.Map {
|
|
|
|
/* Get file pointer, no need for lock as we have write lock */
|
|
|
|
file := Config.FileSystem.CacheMap.Get(path)
|
|
|
|
|
|
|
|
/* If this is a generated file, we skip */
|
|
|
|
if isGeneratedType(file) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
/* Log file as not in cache, then delete */
|
2020-05-02 21:34:49 +00:00
|
|
|
Config.SysLog.Error("", "Failed to stat file in cache: %s\n", path)
|
2020-04-30 09:27:05 +00:00
|
|
|
Config.FileSystem.CacheMap.Remove(path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
timeModified := stat.ModTime().UnixNano()
|
|
|
|
|
|
|
|
/* If the file is marked as fresh, but file on disk newer, mark as unfresh */
|
|
|
|
if file.Fresh && file.LastRefresh < timeModified {
|
|
|
|
file.Fresh = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done! We can release cache read lock */
|
|
|
|
Config.FileSystem.CacheMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGeneratedType(file *File) bool {
|
|
|
|
/* Just a helper function to neaten-up checking if file contents is of generated type */
|
2020-05-04 17:19:56 +00:00
|
|
|
switch file.Content.(type) {
|
2020-04-30 09:27:05 +00:00
|
|
|
case *GeneratedFileContents:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|