2011-06-17 21:13:01 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
// Package fileembed provides access to static data resources (images,
|
|
|
|
// HTML, css, etc) embedded into the binary with genfileembed.
|
|
|
|
//
|
|
|
|
// Most of the package contains internal details used by genfileembed.
|
|
|
|
// Normal applications will simply make a global Files variable.
|
2011-06-17 21:13:01 +00:00
|
|
|
package fileembed
|
|
|
|
|
|
|
|
import (
|
2012-11-28 23:54:56 +00:00
|
|
|
"compress/zlib"
|
2013-06-09 23:11:03 +00:00
|
|
|
"encoding/base64"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"errors"
|
2012-11-28 23:54:56 +00:00
|
|
|
"fmt"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"io"
|
2011-10-18 18:10:02 +00:00
|
|
|
"io/ioutil"
|
2012-11-28 23:54:56 +00:00
|
|
|
"log"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net/http"
|
2011-06-17 21:13:01 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2012-02-29 16:01:59 +00:00
|
|
|
"strings"
|
2011-06-17 21:13:01 +00:00
|
|
|
"sync"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"time"
|
2011-06-17 21:13:01 +00:00
|
|
|
)
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
// Files contains all the embedded resources.
|
2011-06-17 21:13:01 +00:00
|
|
|
type Files struct {
|
|
|
|
// Optional environment variable key to override
|
|
|
|
OverrideEnv string
|
|
|
|
|
2011-10-18 18:10:02 +00:00
|
|
|
// Optional fallback directory to check, if not in memory.
|
|
|
|
DirFallback string
|
|
|
|
|
|
|
|
// SlurpToMemory controls whether on first access the file is
|
|
|
|
// slurped into memory. It's intended for use with DirFallback.
|
|
|
|
SlurpToMemory bool
|
|
|
|
|
2013-06-04 23:57:18 +00:00
|
|
|
// Listable controls whether requests for the http file "/" return
|
|
|
|
// a directory of available files. Must be set to true for
|
|
|
|
// http.FileServer to correctly handle requests for index.html.
|
|
|
|
Listable bool
|
|
|
|
|
2011-06-17 21:13:01 +00:00
|
|
|
lk sync.Mutex
|
2012-02-29 06:49:52 +00:00
|
|
|
file map[string]*staticFile
|
|
|
|
}
|
|
|
|
|
|
|
|
type staticFile struct {
|
|
|
|
name string
|
2012-11-28 23:54:56 +00:00
|
|
|
contents []byte
|
2012-02-29 06:49:52 +00:00
|
|
|
modtime time.Time
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 23:54:56 +00:00
|
|
|
type Opener interface {
|
|
|
|
Open() (io.Reader, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type String string
|
|
|
|
|
|
|
|
func (s String) Open() (io.Reader, error) {
|
|
|
|
return strings.NewReader(string(s)), nil
|
|
|
|
}
|
|
|
|
|
2013-06-09 23:11:03 +00:00
|
|
|
// ZlibCompressed is used to store a compressed file.
|
2012-11-28 23:54:56 +00:00
|
|
|
type ZlibCompressed string
|
|
|
|
|
|
|
|
func (zb ZlibCompressed) Open() (io.Reader, error) {
|
|
|
|
rz, err := zlib.NewReader(strings.NewReader(string(zb)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not open ZlibCompressed: %v", err)
|
|
|
|
}
|
|
|
|
return rz, nil
|
|
|
|
}
|
|
|
|
|
2013-06-09 23:11:03 +00:00
|
|
|
// ZlibCompressedBase64 is used to store a compressed file.
|
|
|
|
// Unlike ZlibCompressed, the string is base64 encoded,
|
|
|
|
// in standard base64 encoding.
|
|
|
|
type ZlibCompressedBase64 string
|
|
|
|
|
|
|
|
func (zb ZlibCompressedBase64) Open() (io.Reader, error) {
|
|
|
|
rz, err := zlib.NewReader(base64.NewDecoder(base64.StdEncoding, strings.NewReader(string(zb))))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not open ZlibCompressedBase64: %v", err)
|
|
|
|
}
|
|
|
|
return rz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multi concatenates multiple Openers into one, like io.MultiReader.
|
|
|
|
func Multi(openers ...Opener) Opener {
|
|
|
|
return multi(openers)
|
|
|
|
}
|
|
|
|
|
|
|
|
type multi []Opener
|
|
|
|
|
|
|
|
func (m multi) Open() (io.Reader, error) {
|
|
|
|
rs := make([]io.Reader, 0, len(m))
|
|
|
|
for _, o := range m {
|
|
|
|
r, err := o.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rs = append(rs, r)
|
|
|
|
}
|
|
|
|
return io.MultiReader(rs...), nil
|
|
|
|
}
|
|
|
|
|
2011-06-17 21:13:01 +00:00
|
|
|
// Add adds a file to the file set.
|
2013-06-09 23:11:03 +00:00
|
|
|
func (f *Files) Add(filename string, size int64, modtime time.Time, o Opener) {
|
2011-06-17 21:13:01 +00:00
|
|
|
f.lk.Lock()
|
|
|
|
defer f.lk.Unlock()
|
2012-11-28 23:54:56 +00:00
|
|
|
|
|
|
|
r, err := o.Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not add file %v: %v", filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contents, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not read contents of file %v: %v", filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
f.add(filename, &staticFile{
|
|
|
|
name: filename,
|
|
|
|
contents: contents,
|
|
|
|
modtime: modtime,
|
|
|
|
})
|
2011-10-18 18:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// f.lk must be locked
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *Files) add(filename string, sf *staticFile) {
|
2011-06-17 21:13:01 +00:00
|
|
|
if f.file == nil {
|
2012-02-29 06:49:52 +00:00
|
|
|
f.file = make(map[string]*staticFile)
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
2012-02-29 06:49:52 +00:00
|
|
|
f.file[filename] = sf
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 16:01:59 +00:00
|
|
|
var _ http.FileSystem = (*Files)(nil)
|
|
|
|
|
|
|
|
func (f *Files) Open(filename string) (hf http.File, err error) {
|
2013-06-04 23:57:18 +00:00
|
|
|
// don't bother locking f.lk here, because Listable will normally be set on initialization
|
|
|
|
if filename == "/" && f.Listable {
|
|
|
|
return openDir(f)
|
2012-02-29 16:01:59 +00:00
|
|
|
}
|
2013-06-04 23:57:18 +00:00
|
|
|
filename = strings.TrimLeft(filename, "/")
|
2011-06-17 21:13:01 +00:00
|
|
|
if e := f.OverrideEnv; e != "" && os.Getenv(e) != "" {
|
2012-02-29 16:01:59 +00:00
|
|
|
diskPath := filepath.Join(os.Getenv(e), filename)
|
|
|
|
return os.Open(diskPath)
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
f.lk.Lock()
|
|
|
|
defer f.lk.Unlock()
|
2012-02-29 06:49:52 +00:00
|
|
|
sf, ok := f.file[filename]
|
2011-06-17 21:13:01 +00:00
|
|
|
if !ok {
|
2011-10-18 18:10:02 +00:00
|
|
|
return f.openFallback(filename)
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
2012-02-29 06:49:52 +00:00
|
|
|
return &fileHandle{sf: sf}, nil
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 18:10:02 +00:00
|
|
|
// f.lk is held
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (f *Files) openFallback(filename string) (http.File, error) {
|
2011-10-18 18:10:02 +00:00
|
|
|
if f.DirFallback == "" {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return nil, os.ErrNotExist
|
2011-10-18 18:10:02 +00:00
|
|
|
}
|
|
|
|
of, err := os.Open(filepath.Join(f.DirFallback, filename))
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
case f.SlurpToMemory:
|
|
|
|
defer of.Close()
|
|
|
|
bs, err := ioutil.ReadAll(of)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-02-29 06:49:52 +00:00
|
|
|
fi, err := of.Stat()
|
|
|
|
|
|
|
|
sf := &staticFile{
|
|
|
|
name: filename,
|
2012-11-28 23:54:56 +00:00
|
|
|
contents: bs,
|
2012-02-29 06:49:52 +00:00
|
|
|
modtime: fi.ModTime(),
|
|
|
|
}
|
|
|
|
f.add(filename, sf)
|
|
|
|
return &fileHandle{sf: sf}, nil
|
2011-10-18 18:10:02 +00:00
|
|
|
}
|
|
|
|
return of, nil
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
type fileHandle struct {
|
|
|
|
sf *staticFile
|
2011-06-17 21:13:01 +00:00
|
|
|
off int64
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
var _ http.File = (*fileHandle)(nil)
|
|
|
|
|
|
|
|
func (f *fileHandle) Close() error {
|
2011-06-17 21:13:01 +00:00
|
|
|
if f.closed {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return os.ErrInvalid
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
f.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *fileHandle) Read(p []byte) (n int, err error) {
|
|
|
|
if f.off >= int64(len(f.sf.contents)) {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return 0, io.EOF
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
2012-02-29 06:49:52 +00:00
|
|
|
n = copy(p, f.sf.contents[f.off:])
|
2011-06-17 21:13:01 +00:00
|
|
|
f.off += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *fileHandle) Readdir(int) ([]os.FileInfo, error) {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return nil, errors.New("not directory")
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *fileHandle) Seek(offset int64, whence int) (int64, error) {
|
2011-06-17 21:13:01 +00:00
|
|
|
switch whence {
|
|
|
|
case os.SEEK_SET:
|
|
|
|
f.off = offset
|
|
|
|
case os.SEEK_CUR:
|
|
|
|
f.off += offset
|
|
|
|
case os.SEEK_END:
|
2012-11-28 23:54:56 +00:00
|
|
|
f.off = f.sf.Size() + offset
|
2011-06-17 21:13:01 +00:00
|
|
|
default:
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return 0, os.ErrInvalid
|
2011-06-17 21:13:01 +00:00
|
|
|
}
|
|
|
|
if f.off < 0 {
|
|
|
|
f.off = 0
|
|
|
|
}
|
|
|
|
return f.off, nil
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *fileHandle) Stat() (os.FileInfo, error) {
|
|
|
|
return f.sf, nil
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
var _ os.FileInfo = (*staticFile)(nil)
|
2011-06-17 21:13:01 +00:00
|
|
|
|
2012-02-29 06:49:52 +00:00
|
|
|
func (f *staticFile) Name() string { return f.name }
|
|
|
|
func (f *staticFile) Size() int64 { return int64(len(f.contents)) }
|
|
|
|
func (f *staticFile) Mode() os.FileMode { return 0444 }
|
|
|
|
func (f *staticFile) ModTime() time.Time { return f.modtime }
|
|
|
|
func (f *staticFile) IsDir() bool { return false }
|
|
|
|
func (f *staticFile) Sys() interface{} { return nil }
|
2013-06-04 23:57:18 +00:00
|
|
|
|
|
|
|
func openDir(f *Files) (hf http.File, err error) {
|
|
|
|
f.lk.Lock()
|
|
|
|
defer f.lk.Unlock()
|
|
|
|
|
|
|
|
allFiles := make([]os.FileInfo, 0, len(f.file))
|
|
|
|
var dirModtime time.Time
|
|
|
|
|
|
|
|
for filename, sfile := range f.file {
|
|
|
|
if strings.Contains(filename, "/") {
|
|
|
|
continue // skip child directories; we only support readdir on the rootdir for now
|
|
|
|
}
|
|
|
|
allFiles = append(allFiles, sfile)
|
|
|
|
// a directory's modtime is the maximum contained modtime
|
|
|
|
if sfile.modtime.After(dirModtime) {
|
|
|
|
dirModtime = sfile.modtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dirHandle{
|
|
|
|
sd: &staticDir{name: "/", modtime: dirModtime},
|
|
|
|
files: allFiles,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type dirHandle struct {
|
|
|
|
sd *staticDir
|
|
|
|
files []os.FileInfo
|
|
|
|
off int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dirHandle) Readdir(n int) ([]os.FileInfo, error) {
|
|
|
|
if n <= 0 {
|
|
|
|
return d.files, nil
|
|
|
|
}
|
|
|
|
if d.off >= len(d.files) {
|
|
|
|
return []os.FileInfo{}, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.off+n > len(d.files) {
|
|
|
|
n = len(d.files) - d.off
|
|
|
|
}
|
|
|
|
matches := d.files[d.off : d.off+n]
|
|
|
|
d.off += n
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if d.off > len(d.files) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dirHandle) Close() error { return nil }
|
|
|
|
func (d *dirHandle) Read(p []byte) (int, error) { return 0, errors.New("not file") }
|
|
|
|
func (d *dirHandle) Seek(int64, int) (int64, error) { return 0, os.ErrInvalid }
|
|
|
|
func (d *dirHandle) Stat() (os.FileInfo, error) { return d.sd, nil }
|
|
|
|
|
|
|
|
type staticDir struct {
|
|
|
|
name string
|
|
|
|
modtime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *staticDir) Name() string { return d.name }
|
|
|
|
func (d *staticDir) Size() int64 { return 0 }
|
|
|
|
func (d *staticDir) Mode() os.FileMode { return 0444 | os.ModeDir }
|
|
|
|
func (d *staticDir) ModTime() time.Time { return d.modtime }
|
|
|
|
func (d *staticDir) IsDir() bool { return true }
|
|
|
|
func (d *staticDir) Sys() interface{} { return nil }
|