2011-02-28 00:18:17 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package localdisk
|
|
|
|
|
|
|
|
import (
|
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"
|
2011-05-09 18:08:14 +00:00
|
|
|
"fmt"
|
2011-02-28 00:18:17 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2012-04-23 00:17:51 +00:00
|
|
|
"path/filepath"
|
2011-05-09 18:08:14 +00:00
|
|
|
|
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
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
2011-02-28 00:18:17 +00:00
|
|
|
)
|
|
|
|
|
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 (ds *DiskStorage) ReceiveBlob(blobRef *blobref.BlobRef, source io.Reader) (blobGot blobref.SizedBlobRef, err error) {
|
2011-05-09 18:08:14 +00:00
|
|
|
pname := ds.partition
|
|
|
|
if pname != "" {
|
2011-05-10 23:13:37 +00:00
|
|
|
err = fmt.Errorf("refusing upload directly to queue partition %q", pname)
|
|
|
|
return
|
2011-05-09 18:08:14 +00:00
|
|
|
}
|
|
|
|
hashedDirectory := ds.blobDirectory(pname, blobRef)
|
2011-02-28 00:18:17 +00:00
|
|
|
err = os.MkdirAll(hashedDirectory, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-10-06 00:30:54 +00:00
|
|
|
tempFile, err := ioutil.TempFile(hashedDirectory, BlobFileBaseName(blobRef)+".tmp")
|
2011-02-28 00:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
success := false // set true later
|
|
|
|
defer func() {
|
|
|
|
if !success {
|
|
|
|
log.Println("Removing temp file: ", tempFile.Name())
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
hash := blobRef.Hash()
|
2011-10-06 00:30:54 +00:00
|
|
|
written, err := io.Copy(io.MultiWriter(hash, tempFile), source)
|
2011-02-28 00:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = tempFile.Sync(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = tempFile.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !blobRef.HashMatches(hash) {
|
2011-05-13 02:19:49 +00:00
|
|
|
err = blobserver.ErrCorruptBlob
|
2011-02-28 00:18:17 +00:00
|
|
|
return
|
|
|
|
}
|
2013-02-07 18:04:03 +00:00
|
|
|
stat, err := os.Lstat(tempFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if stat.Size() != written {
|
|
|
|
err = fmt.Errorf("temp file %q size %d didn't match written size %d", tempFile.Name(), stat.Size(), written)
|
|
|
|
return
|
|
|
|
}
|
2011-02-28 00:18:17 +00:00
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
fileName := ds.blobPath("", blobRef)
|
2011-02-28 00:18:17 +00:00
|
|
|
if err = os.Rename(tempFile.Name(), fileName); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-02-07 18:04:03 +00:00
|
|
|
stat, err = os.Lstat(fileName)
|
2011-02-28 00:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-02-07 18:04:03 +00:00
|
|
|
if stat.Size() != written {
|
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
|
|
|
err = errors.New("Written size didn't match.")
|
2011-02-28 00:18:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
for _, mirror := range ds.mirrorPartitions {
|
|
|
|
pname := mirror.partition
|
|
|
|
if pname == "" {
|
|
|
|
panic("expected partition name")
|
|
|
|
}
|
|
|
|
partitionDir := ds.blobDirectory(pname, blobRef)
|
2012-04-23 00:17:51 +00:00
|
|
|
|
|
|
|
// Prevent the directory from being unlinked by
|
|
|
|
// enumerate code, which cleans up.
|
|
|
|
defer keepDirectoryLock(partitionDir).Unlock()
|
|
|
|
defer keepDirectoryLock(filepath.Dir(partitionDir)).Unlock()
|
|
|
|
defer keepDirectoryLock(filepath.Dir(filepath.Dir(partitionDir))).Unlock()
|
|
|
|
|
2011-02-28 00:18:17 +00:00
|
|
|
if err = os.MkdirAll(partitionDir, 0700); err != nil {
|
2012-04-23 00:17:51 +00:00
|
|
|
return blobref.SizedBlobRef{}, fmt.Errorf("localdisk.receive: MkdirAll(%q) after lock on it: %v", partitionDir, err)
|
2011-02-28 00:18:17 +00:00
|
|
|
}
|
2011-05-09 18:08:14 +00:00
|
|
|
partitionFileName := ds.blobPath(pname, blobRef)
|
2011-06-01 01:58:58 +00:00
|
|
|
pfi, err := os.Stat(partitionFileName)
|
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
|
|
|
if err == nil && !pfi.IsDir() {
|
2011-06-01 01:58:58 +00:00
|
|
|
log.Printf("Skipped dup on partition %q", pname)
|
|
|
|
} else {
|
2012-10-28 14:18:57 +00:00
|
|
|
if err = linkOrCopy(fileName, partitionFileName); err != nil && !linkAlreadyExists(err) {
|
|
|
|
log.Fatalf("got link or copy error %T %#v", err, err)
|
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 blobref.SizedBlobRef{}, err
|
2011-06-01 01:58:58 +00:00
|
|
|
}
|
2012-04-23 00:56:52 +00:00
|
|
|
log.Printf("Mirrored blob %s to partition %q", blobRef, pname)
|
2011-02-28 00:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
blobGot = blobref.SizedBlobRef{BlobRef: blobRef, Size: stat.Size()}
|
2011-02-28 00:18:17 +00:00
|
|
|
success = true
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
hub := ds.GetBlobHub()
|
2011-02-28 00:18:17 +00:00
|
|
|
hub.NotifyBlobReceived(blobRef)
|
2011-05-09 18:08:14 +00:00
|
|
|
for _, mirror := range ds.mirrorPartitions {
|
|
|
|
mirror.GetBlobHub().NotifyBlobReceived(blobRef)
|
2011-02-28 00:18:17 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2011-09-21 01:28:49 +00:00
|
|
|
|
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 linkAlreadyExists(err error) bool {
|
2012-04-23 01:51:41 +00:00
|
|
|
if os.IsExist(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if le, ok := err.(*os.LinkError); ok && os.IsExist(le.Err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2011-09-21 01:28:49 +00:00
|
|
|
}
|
2013-02-14 00:48:28 +00:00
|
|
|
|
|
|
|
// Used by Windows (receive_windows.go) and when a posix filesystem doesn't
|
|
|
|
// support a link operation (e.g. Linux with an exfat external USB disk).
|
|
|
|
func copyFile(src, dst string) error {
|
|
|
|
srcFile, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
dstFile, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(dstFile, srcFile)
|
|
|
|
return err
|
|
|
|
}
|