Skip to content

Commit

Permalink
build local storage on windows, make grace os independent (#99)
Browse files Browse the repository at this point in the history
* build local storage on windows, make grace os independent

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>

* Fixed Windows build also for the owncloud storage package

The code for the Windows implementation is taken from fs/local/local_windows.go

* Compile on all non-Windows following @tboerger's suggestion

* build as unix on !windows

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic authored and labkode committed Sep 4, 2019
1 parent 9826d12 commit dccd3b8
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 72 deletions.
8 changes: 7 additions & 1 deletion cmd/revad/grace/grace.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ func (w *Watcher) GetListeners(servers map[string]Server) (map[string]net.Listen
// Do we abort running the forked child? Probably yes, as if the parent cannot be
// killed that means we run two version of the code indefinitely.
w.log.Info().Msgf("killing parent pid gracefully with SIGQUIT: %d", w.ppid)
err := syscall.Kill(w.ppid, syscall.SIGQUIT)
p, err := os.FindProcess(w.ppid)
if err != nil {
w.log.Error().Err(err).Msgf("error finding parent process with ppid:%d", w.ppid)
err = errors.Wrap(err, "error finding parent process")
return nil, err
}
err = p.Kill()
if err != nil {
w.log.Error().Err(err).Msgf("error killing parent process with ppid:%d", w.ppid)
err = errors.Wrap(err, "error killing parent process")
Expand Down
36 changes: 0 additions & 36 deletions pkg/storage/fs/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ package local

import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"syscall"

"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/storage/fs/registry"

"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/mime"
"github.com/cs3org/reva/pkg/storage"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -109,38 +105,6 @@ func (fs *localFS) removeRoot(np string) string {

type localFS struct{ root string }

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
stat, ok := fi.Sys().(*syscall.Stat_t)
if ok {
// take device and inode into account
err = binary.Write(h, binary.BigEndian, stat.Ino)
if err != nil {
log.Error().Err(err).Msg("error writing inode")
}
err = binary.Write(h, binary.BigEndian, stat.Dev)
if err != nil {
log.Error().Err(err).Msg("error writing device")
}
}
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}

func (fs *localFS) normalize(ctx context.Context, fi os.FileInfo, fn string) *storageproviderv0alphapb.ResourceInfo {
fn = fs.removeRoot(path.Join("/", fn))
md := &storageproviderv0alphapb.ResourceInfo{
Expand Down
64 changes: 64 additions & 0 deletions pkg/storage/fs/local/local_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2018-2019 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build !windows

package local

import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"os"
"syscall"

"github.com/cs3org/reva/pkg/appctx"
)

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
stat, ok := fi.Sys().(*syscall.Stat_t)
if ok {
// take device and inode into account
err = binary.Write(h, binary.BigEndian, stat.Ino)
if err != nil {
log.Error().Err(err).Msg("error writing inode")
}
err = binary.Write(h, binary.BigEndian, stat.Dev)
if err != nil {
log.Error().Err(err).Msg("error writing device")
}
}
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}
52 changes: 52 additions & 0 deletions pkg/storage/fs/local/local_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018-2019 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build windows

package local

import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"os"

"github.com/cs3org/reva/pkg/appctx"
)

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
// device and inode hale no meaning on windows
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}
35 changes: 0 additions & 35 deletions pkg/storage/fs/owncloud/owncloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package owncloud

import (
"context"
"crypto/md5"
"encoding/binary"
"encoding/csv"
"fmt"
"io"
Expand All @@ -31,7 +29,6 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha"
Expand Down Expand Up @@ -333,38 +330,6 @@ func (fs *ocFS) removeNamespace(ctx context.Context, np string) string {
}
}

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
stat, ok := fi.Sys().(*syscall.Stat_t)
if ok {
// take device and inode into account
err = binary.Write(h, binary.BigEndian, stat.Ino)
if err != nil {
log.Error().Err(err).Msg("error writing inode")
}
err = binary.Write(h, binary.BigEndian, stat.Dev)
if err != nil {
log.Error().Err(err).Msg("error writing device")
}
}
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}

func getOwner(fn string) string {
parts := strings.SplitN(fn, "/", 3)
// parts = "", "<username>", "files", "foo/bar.txt"
Expand Down
64 changes: 64 additions & 0 deletions pkg/storage/fs/owncloud/owncloud_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2018-2019 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build !windows

package owncloud

import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"os"
"syscall"

"github.com/cs3org/reva/pkg/appctx"
)

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
stat, ok := fi.Sys().(*syscall.Stat_t)
if ok {
// take device and inode into account
err = binary.Write(h, binary.BigEndian, stat.Ino)
if err != nil {
log.Error().Err(err).Msg("error writing inode")
}
err = binary.Write(h, binary.BigEndian, stat.Dev)
if err != nil {
log.Error().Err(err).Msg("error writing device")
}
}
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}
52 changes: 52 additions & 0 deletions pkg/storage/fs/owncloud/owncloud_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018-2019 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build windows

package owncloud

import (
"context"
"crypto/md5"
"encoding/binary"
"fmt"
"os"

"github.com/cs3org/reva/pkg/appctx"
)

// calcEtag will create an etag based on the md5 of
// - mtime,
// - inode (if available),
// - device (if available) and
// - size.
// errors are logged, but an etag will still be returned
func calcEtag(ctx context.Context, fi os.FileInfo) string {
log := appctx.GetLogger(ctx)
h := md5.New()
err := binary.Write(h, binary.BigEndian, fi.ModTime().Unix())
if err != nil {
log.Error().Err(err).Msg("error writing mtime")
}
// device and inode have no meaning on windows
err = binary.Write(h, binary.BigEndian, fi.Size())
if err != nil {
log.Error().Err(err).Msg("error writing size")
}
return fmt.Sprintf(`"%x"`, h.Sum(nil))
}

0 comments on commit dccd3b8

Please sign in to comment.