Skip to content

Commit

Permalink
go: compile on darwin
Browse files Browse the repository at this point in the history
Unit tests for recon fail, since they expect /proc.
We can fix that later.
  • Loading branch information
redbo committed Feb 3, 2016
1 parent 45e48e0 commit 307612b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 37 deletions.
18 changes: 3 additions & 15 deletions go/objectserver/backend.go
Expand Up @@ -27,20 +27,17 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/openstack/swift/go/hummingbird"
)

const METADATA_CHUNK_SIZE = 65536

func GetXAttr(fileNameOrFd interface{}, attr string, value []byte) (int, error) {
switch v := fileNameOrFd.(type) {
case string:
return syscall.Getxattr(v, attr, value)
return Getxattr(v, attr, value)
case uintptr:
return hummingbird.FGetXattr(v, attr, value)
return FGetxattr(v, attr, value)
}
return 0, &hummingbird.BackendError{Err: errors.New("Invalid fileNameOrFd"), Code: hummingbird.UnhandledError}
}
Expand Down Expand Up @@ -111,7 +108,7 @@ func RawWriteMetadata(fd uintptr, buf []byte) error {
if len(buf) < writelen {
writelen = len(buf)
}
if _, err := hummingbird.FSetXattr(fd, metadataName, buf[0:writelen]); err != nil {
if _, err := FSetxattr(fd, metadataName, buf[0:writelen]); err != nil {
return err
}
buf = buf[writelen:len(buf)]
Expand Down Expand Up @@ -407,12 +404,3 @@ func ObjectMetadata(dataFile string, metaFile string) (map[string]string, error)
}
return datafileMetadata, nil
}

func FreeDiskSpace(fd uintptr) (int64, error) {
var st syscall.Statfs_t
if err := syscall.Fstatfs(int(fd), &st); err != nil {
return 0, err
} else {
return int64(st.Frsize) * int64(st.Bavail), nil
}
}
3 changes: 1 addition & 2 deletions go/objectserver/backend_test.go
Expand Up @@ -26,7 +26,6 @@ import (
)

func TestWriteReadMetadata(t *testing.T) {

data := map[string]string{
strings.Repeat("la", 5): strings.Repeat("la", 30),
strings.Repeat("moo", 500): strings.Repeat("moo", 300),
Expand All @@ -35,7 +34,7 @@ func TestWriteReadMetadata(t *testing.T) {
defer testFile.Close()
defer os.Remove(testFile.Name())
assert.Equal(t, err, nil)
WriteMetadata(testFile.Fd(), data)
err = WriteMetadata(testFile.Fd(), data)
checkData := map[string]string{
strings.Repeat("la", 5): strings.Repeat("la", 30),
strings.Repeat("moo", 500): strings.Repeat("moo", 300),
Expand Down
17 changes: 8 additions & 9 deletions go/objectserver/main.go
Expand Up @@ -33,7 +33,6 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/justinas/alice"
Expand Down Expand Up @@ -287,7 +286,7 @@ func (server *ObjectServer) ObjPutHandler(writer http.ResponseWriter, request *h
return
}
if !server.disableFallocate && request.ContentLength > 0 {
syscall.Fallocate(int(tempFile.Fd()), 1, 0, request.ContentLength)
Fallocate(int(tempFile.Fd()), 1, 0, request.ContentLength)
}
hash := md5.New()
totalSize, err := hummingbird.Copy(request.Body, tempFile, hash)
Expand Down Expand Up @@ -330,9 +329,9 @@ func (server *ObjectServer) ObjPutHandler(writer http.ResponseWriter, request *h
}
go func() {
HashCleanupListDir(hashDir, hummingbird.GetLogger(request))
if fd, err := syscall.Open(hashDir, syscall.O_DIRECTORY|os.O_RDONLY, 0666); err == nil {
syscall.Fsync(fd)
syscall.Close(fd)
if dir, err := os.OpenFile(hashDir, os.O_RDONLY, 0666); err == nil {
dir.Sync()
dir.Close()
}
InvalidateHash(hashDir)
}()
Expand Down Expand Up @@ -442,9 +441,9 @@ func (server *ObjectServer) ObjDeleteHandler(writer http.ResponseWriter, request
}
go func() {
HashCleanupListDir(hashDir, hummingbird.GetLogger(request))
if fd, err := syscall.Open(hashDir, syscall.O_DIRECTORY|os.O_RDONLY, 0666); err == nil {
syscall.Fsync(fd)
syscall.Close(fd)
if dir, err := os.OpenFile(hashDir, os.O_RDONLY, 0666); err == nil {
dir.Sync()
dir.Close()
}
InvalidateHash(hashDir)
}()
Expand Down Expand Up @@ -558,7 +557,7 @@ func (server *ObjectServer) ObjRepConnHandler(writer http.ResponseWriter, reques
return err
}
if !server.disableFallocate && sfr.Size > 0 {
syscall.Fallocate(int(tempFile.Fd()), 1, 0, sfr.Size)
Fallocate(int(tempFile.Fd()), 1, 0, sfr.Size)
}
if err := rc.SendMessage(SyncFileResponse{GoAhead: true, Msg: "go ahead"}); err != nil {
return err
Expand Down
94 changes: 94 additions & 0 deletions go/objectserver/sys_darwin.go
@@ -0,0 +1,94 @@
// Copyright (c) 2015 Rackspace
//
// 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 objectserver

import (
"syscall"
"unsafe"
)

const METADATA_CHUNK_SIZE = 65536
const XATTR_CREATE = 0x0002
const XATTR_REPLACE = 0x0004

func FGetxattr(fd uintptr, attr string, value []byte) (int, error) {
attrp, err := syscall.BytePtrFromString(attr)
if err != nil {
return 0, err
}
if len(value) == 0 {
if r0, _, e1 := syscall.Syscall6(syscall.SYS_FGETXATTR, fd, uintptr(unsafe.Pointer(attrp)), 0, 0, 0, 0); e1 == 0 {
return int(r0), nil
} else {
return 0, e1
}
} else {
valuep := unsafe.Pointer(&value[0])
if r0, _, e1 := syscall.Syscall6(syscall.SYS_FGETXATTR, fd, uintptr(unsafe.Pointer(attrp)), uintptr(valuep), uintptr(len(value)), 0, 0); e1 == 0 {
return int(r0), nil
} else {
return int(r0), e1
}
}
}

func FSetxattr(fd uintptr, attr string, value []byte) (int, error) {
attrp, err := syscall.BytePtrFromString(attr)
if err != nil {
return 0, err
}
valuep := unsafe.Pointer(&value[0])
r0, _, e1 := syscall.Syscall6(syscall.SYS_FSETXATTR, fd, uintptr(unsafe.Pointer(attrp)), uintptr(valuep), uintptr(len(value)), 0, 0)
if e1 != 0 {
err = e1
}
return int(r0), err
}

func Getxattr(filename string, attr string, value []byte) (int, error) {
filep, err := syscall.BytePtrFromString(filename)
attrp, err := syscall.BytePtrFromString(attr)
if err != nil {
return 0, err
}
if len(value) == 0 {
if r0, _, e1 := syscall.Syscall6(syscall.SYS_GETXATTR, uintptr(unsafe.Pointer(filep)), uintptr(unsafe.Pointer(attrp)), 0, 0, 0, 0); e1 == 0 {
return int(r0), nil
} else {
return 0, e1
}
} else {
valuep := unsafe.Pointer(&value[0])
if r0, _, e1 := syscall.Syscall6(syscall.SYS_GETXATTR, uintptr(unsafe.Pointer(filep)), uintptr(unsafe.Pointer(attrp)), uintptr(valuep), uintptr(len(value)), 0, 0); e1 == 0 {
return int(r0), nil
} else {
return int(r0), e1
}
}
}

func Fallocate(fd int, mode uint32, off int64, len int64) (err error) {
return nil
}

func FreeDiskSpace(fd uintptr) (int64, error) {
var st syscall.Statfs_t
if err := syscall.Fstatfs(int(fd), &st); err != nil {
return 0, err
} else {
return int64(st.Bsize) * int64(st.Bavail), nil
}
}
27 changes: 23 additions & 4 deletions go/hummingbird/xattr.go → go/objectserver/sys_linux.go
Expand Up @@ -13,14 +13,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hummingbird
package objectserver

import (
"syscall"
"unsafe"
)

func FGetXattr(fd uintptr, attr string, value []byte) (int, error) {
const METADATA_CHUNK_SIZE = 65536

func FGetxattr(fd uintptr, attr string, value []byte) (int, error) {
attrp, err := syscall.BytePtrFromString(attr)
if err != nil {
return 0, err
Expand All @@ -41,7 +43,7 @@ func FGetXattr(fd uintptr, attr string, value []byte) (int, error) {
}
}

func FSetXattr(fd uintptr, attr string, value []byte) (int, error) {
func FSetxattr(fd uintptr, attr string, value []byte) (int, error) {
attrp, err := syscall.BytePtrFromString(attr)
if err != nil {
return 0, err
Expand All @@ -51,5 +53,22 @@ func FSetXattr(fd uintptr, attr string, value []byte) (int, error) {
if e1 != 0 {
err = e1
}
return int(r0), nil
return int(r0), err
}

func Getxattr(filename string, attr string, value []byte) (int, error) {
return syscall.Getxattr(filename, attr, value)
}

func Fallocate(fd int, mode uint32, off int64, len int64) (err error) {
return syscall.Fallocate(fd, mode, off, len)
}

func FreeDiskSpace(fd uintptr) (int64, error) {
var st syscall.Statfs_t
if err := syscall.Fstatfs(int(fd), &st); err != nil {
return 0, err
} else {
return int64(st.Frsize) * int64(st.Bavail), nil
}
}
11 changes: 4 additions & 7 deletions go/hummingbird/xattr_test.go → go/objectserver/sys_test.go
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hummingbird
package objectserver

import (
"io/ioutil"
Expand All @@ -23,22 +23,19 @@ import (
"github.com/stretchr/testify/require"
)

// func FGetXattr(fd uintptr, attr string, value []byte) (int, error)
// func FSetXattr(fd uintptr, attr string, value []byte) (int, error)

func TestXattr(t *testing.T) {
fp, err := ioutil.TempFile("", "")
require.Nil(t, err)
defer fp.Close()
defer os.RemoveAll(fp.Name())

_, err = FSetXattr(fp.Fd(), "user.swift.metadata", []byte("somevalue"))
_, err = FSetxattr(fp.Fd(), "user.swift.metadata", []byte("somevalue"))
require.Nil(t, err)

count, err := FGetXattr(fp.Fd(), "user.swift.metadata", nil)
count, err := FGetxattr(fp.Fd(), "user.swift.metadata", nil)
require.Nil(t, err)
value := make([]byte, count)
count, err = FGetXattr(fp.Fd(), "user.swift.metadata", value)
count, err = FGetxattr(fp.Fd(), "user.swift.metadata", value)
require.Nil(t, err)
require.Equal(t, "somevalue", string(value))
}

0 comments on commit 307612b

Please sign in to comment.