Introduce file locking and fs abstraction

This commit is contained in:
Benedikt Kulmann
2020-04-21 12:00:34 +02:00
parent d3412c965a
commit 4617e7c6cc
6 changed files with 137 additions and 100 deletions
+53 -44
View File
@@ -741,6 +741,7 @@ type ListOption struct {
// *ListOption_IntValue
Option isListOption_Option `protobuf_oneof:"option"`
Selected bool `protobuf:"varint,3,opt,name=selected,proto3" json:"selected,omitempty"`
DisplayValue string `protobuf:"bytes,4,opt,name=displayValue,proto3" json:"displayValue,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@@ -815,6 +816,13 @@ func (m *ListOption) GetSelected() bool {
return false
}
func (m *ListOption) GetDisplayValue() string {
if m != nil {
return m.DisplayValue
}
return ""
}
// XXX_OneofWrappers is for the internal use of the proto package.
func (*ListOption) XXX_OneofWrappers() []interface{} {
return []interface{}{
@@ -845,48 +853,49 @@ func init() {
}
var fileDescriptor_199e73d7d5fd837b = []byte{
// 684 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x55, 0x5d, 0x6f, 0xd3, 0x4a,
0x10, 0xad, 0xeb, 0xa4, 0x71, 0x26, 0x6a, 0x6f, 0xbb, 0xb7, 0xf7, 0x5e, 0xdf, 0xb4, 0x40, 0x58,
0x1e, 0x40, 0x20, 0x35, 0xa8, 0xbc, 0x20, 0x24, 0x84, 0x94, 0x0a, 0x28, 0x50, 0x8a, 0xb4, 0x95,
0xfa, 0x50, 0x15, 0x90, 0x9b, 0x0c, 0xe9, 0xaa, 0x8e, 0xed, 0x7a, 0x37, 0x55, 0xfa, 0xc6, 0x2f,
0xe0, 0x4f, 0x80, 0xf8, 0x9d, 0xc8, 0x63, 0x3b, 0xfe, 0x88, 0x53, 0xb5, 0x82, 0xa7, 0xac, 0x67,
0xce, 0x39, 0x73, 0x32, 0xb3, 0x1f, 0xb0, 0x11, 0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0xe1, 0xef, 0x8a, 0x9b, 0x9b, 0xdd, 0xcd, 0xf5, 0xbc, 0xfa, 0x45, 0x68, 0xf3,
0xab, 0x20, 0xb1, 0x7a, 0xaf, 0x71, 0x14, 0xbf, 0xe0, 0x27, 0x4b, 0xf4, 0xf3, 0xe4, 0x57, 0x00,
0x00, 0x00, 0xff, 0xff, 0x2b, 0x5c, 0xdc, 0x93, 0xee, 0x07, 0x00, 0x00,
// 695 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x55, 0x4f, 0x6f, 0xd3, 0x4e,
0x10, 0xad, 0x9b, 0xa4, 0x71, 0x26, 0xbf, 0xf6, 0xd7, 0x2e, 0x05, 0x4c, 0x5a, 0x20, 0x2c, 0x07,
0x10, 0x48, 0x0d, 0x2a, 0x17, 0x84, 0x84, 0x90, 0x52, 0x01, 0x05, 0x4a, 0x91, 0xb6, 0x52, 0x0f,
0x55, 0x01, 0xb9, 0xc9, 0x90, 0xae, 0xea, 0xd8, 0xae, 0x77, 0x53, 0xa5, 0x37, 0x3e, 0x01, 0x67,
0xee, 0x20, 0x3e, 0x27, 0xf2, 0xd8, 0x8e, 0xff, 0xc4, 0xa9, 0x5a, 0xc1, 0xc9, 0xbb, 0x6f, 0xe6,
0xbd, 0x7d, 0x9e, 0xd9, 0x3f, 0xb0, 0xe6, 0x9f, 0x0c, 0x3a, 0x7e, 0xe0, 0x69, 0xaf, 0x73, 0xf6,
0xa4, 0xa3, 0x50, 0x6b, 0xe9, 0x0e, 0xd4, 0x06, 0x21, 0xac, 0x46, 0x1f, 0x7e, 0x08, 0x6b, 0x5b,
0x01, 0xda, 0x1a, 0xf7, 0xe2, 0x70, 0x77, 0xe4, 0xf6, 0x1d, 0x14, 0x78, 0x3a, 0x42, 0xa5, 0xd9,
0x0b, 0x58, 0x52, 0xb9, 0x80, 0x65, 0xb4, 0x8d, 0x87, 0xcd, 0xcd, 0xe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
View File
@@ -90,4 +90,5 @@ message ListOption {
int64 intValue = 2;
}
bool selected = 3;
string displayValue = 4;
}
+1 -1
View File
@@ -1,4 +1,4 @@
// package http
package http
// import (
// svc "github.com/owncloud/ocis-settings/pkg/service/v0"
+42
View File
@@ -0,0 +1,42 @@
package store
import (
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"os"
)
// Unmarshal file into record
func (s Store) parseRecordFromFile(record proto.Message, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
s.Logger.Err(err).Msgf("error reading file %v: file not found", filePath)
return err
}
defer file.Close()
decoder := jsonpb.Unmarshaler{}
if err = decoder.Unmarshal(file, record); err != nil {
s.Logger.Err(err).Msgf("error reading file %v: unmarshalling failed", filePath)
return err
}
return nil
}
// Marshal record into file
func (s Store) writeRecordToFile(record proto.Message, filePath string) error {
file, err := os.Create(filePath)
if err != nil {
s.Logger.Err(err).Msgf("error writing file %v: opening failed", filePath)
return err
}
defer file.Close()
encoder := jsonpb.Marshaler{}
if err = encoder.Marshal(file, record); err != nil {
s.Logger.Err(err).Msgf("error writing file %v: marshalling failed", filePath)
return err
}
return nil
}
+25
View File
@@ -0,0 +1,25 @@
package store
import (
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"strings"
)
// Builds a unique file name from the given bundle
func buildFileNameFromBundle(bundle *proto.SettingsBundle) string {
return buildFileNameFromBundleArgs(bundle.Extension, bundle.Key)
}
// Builds a unique file name from the given params
func buildFileNameFromBundleArgs(extension string, key string) string {
return extension + "__" + key + ".json"
}
// Extracts extension and key from the given fileName and builds a (minimalistic) bundle from it
func parseBundleFromFileName(fileName string) *proto.SettingsBundle {
parts := strings.Split(strings.Replace(fileName, ".json", "", 1), "__")
return &proto.SettingsBundle{
Key: parts[1],
Extension: parts[0],
}
}
+15 -55
View File
@@ -6,9 +6,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"github.com/golang/protobuf/jsonpb"
olog "github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
@@ -56,7 +54,7 @@ func (s Store) ListAll() ([]*proto.SettingsBundle, error) {
s.Logger.Info().Msg("listing bundles")
for _, v := range bundles {
records = append(records, parseFileName(v.Name()))
records = append(records, parseBundleFromFileName(v.Name()))
}
return records, nil
@@ -73,7 +71,7 @@ func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error
s.Logger.Info().Msgf("listing bundles by extension %v", extension)
for _, v := range bundles {
record := parseFileName(v.Name())
record := parseBundleFromFileName(v.Name())
if record.Extension == extension {
records = append(records, record)
}
@@ -84,73 +82,35 @@ func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error
// Read tries to find a bundle by the given extension and key within the mountPath
func (s Store) Read(extension string, key string) (*proto.SettingsBundle, error) {
fileName := buildFileNameFromData(extension, key)
contents, err := os.Open(path.Join(s.mountPath, fileName))
if err != nil {
s.Logger.Err(err).Msgf("error reading contents for extension %v and key %v: file not found", extension, key)
return nil, err
if len(extension) < 1 || len(key) < 1 {
s.Logger.Error().Msg("extension and key cannot be empty")
return nil, fmt.Errorf(emptyKeyError)
}
filePath := path.Join(s.mountPath, buildFileNameFromBundleArgs(extension, key))
record := proto.SettingsBundle{}
if err = jsonpb.Unmarshal(contents, &record); err != nil {
s.Logger.Err(err).Msg("error unmarshalling record")
if err := s.parseRecordFromFile(&record, filePath); err != nil {
return nil, err
}
s.Logger.Debug().Msgf("read contents from file: %v", filePath)
return &record, nil
}
// Write writes the given record into a file within the mountPath
func (s Store) Write(rec *proto.SettingsBundle) (*proto.SettingsBundle, error) {
if len(rec.Key) < 1 {
s.Logger.Error().Msg("key cannot be empty")
func (s Store) Write(record *proto.SettingsBundle) (*proto.SettingsBundle, error) {
if len(record.Extension) < 1 || len(record.Key) < 1 {
s.Logger.Error().Msg("extension and key cannot be empty")
return nil, fmt.Errorf(emptyKeyError)
}
marshaler := jsonpb.Marshaler{}
recordPath := path.Join(s.mountPath, buildFileNameFromBundle(rec))
if err := ioutil.WriteFile(recordPath, []byte{}, 0644); err != nil {
filePath := path.Join(s.mountPath, buildFileNameFromBundle(record))
if err := s.writeRecordToFile(record, filePath); err != nil {
return nil, err
}
fd, err := os.OpenFile(recordPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
s.Logger.Err(err).
Str(
"finding file",
fmt.Sprintf("file `%v` not found on store: `%v`", recordPath, s.mountPath),
)
}
if err = marshaler.Marshal(fd, rec); err != nil {
s.Logger.Err(err).
Str(
"marshaling record",
fmt.Sprintf("error marshaling record: %+v", rec),
)
}
s.Logger.Info().Msgf("request contents written to file: %v", recordPath)
return rec, nil
}
// Builds a unique file name from the given bundle
func buildFileNameFromBundle(bundle *proto.SettingsBundle) string {
return buildFileNameFromData(bundle.Extension, bundle.Key)
}
// Builds a unique file name from the given params
func buildFileNameFromData(extension string, key string) string {
return extension + "__" + key + ".json"
}
// Extracts extension and key from the given fileName and builds a (minimalistic) bundle from it
func parseFileName(fileName string) *proto.SettingsBundle {
parts := strings.Split(strings.Replace(fileName, ".json", "", 1), "__")
return &proto.SettingsBundle{
Key: parts[1],
Extension: parts[0],
}
s.Logger.Debug().Msgf("request contents written to file: %v", filePath)
return record, nil
}
func init() {