From 1a59a5478f8c8bdb0c50043a2cfccaa597ee8374 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Sat, 24 Jan 2015 21:56:12 +0000 Subject: [PATCH 1/7] Expose hash, flags, options in Request --- common_test.go | 8 +++++++- nativemodel_darwin.go | 4 ++-- nativemodel_unix.go | 4 ++-- nativemodel_windows.go | 4 ++-- protocol.go | 19 +++++++++++-------- protocol_test.go | 2 +- wireformat.go | 4 ++-- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/common_test.go b/common_test.go index f67fb481..0f3795d5 100644 --- a/common_test.go +++ b/common_test.go @@ -13,6 +13,9 @@ type TestModel struct { name string offset int64 size int + hash []byte + flags uint32 + options []Option closedCh chan bool } @@ -28,11 +31,14 @@ func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) { func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) { } -func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int) ([]byte, error) { +func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { t.folder = folder t.name = name t.offset = offset t.size = size + t.hash = hash + t.flags = flags + t.options = options return t.data, nil } diff --git a/nativemodel_darwin.go b/nativemodel_darwin.go index 5b4b9be6..6001af69 100644 --- a/nativemodel_darwin.go +++ b/nativemodel_darwin.go @@ -26,9 +26,9 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI m.next.IndexUpdate(deviceID, folder, files) } -func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int) ([]byte, error) { +func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { name = norm.NFD.String(name) - return m.next.Request(deviceID, folder, name, offset, size) + return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) } func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { diff --git a/nativemodel_unix.go b/nativemodel_unix.go index 2fb1654c..5f7b2e2c 100644 --- a/nativemodel_unix.go +++ b/nativemodel_unix.go @@ -18,8 +18,8 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI m.next.IndexUpdate(deviceID, folder, files) } -func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int) ([]byte, error) { - return m.next.Request(deviceID, folder, name, offset, size) +func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { + return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) } func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { diff --git a/nativemodel_windows.go b/nativemodel_windows.go index d4feea32..4859cbaa 100644 --- a/nativemodel_windows.go +++ b/nativemodel_windows.go @@ -56,9 +56,9 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI m.next.IndexUpdate(deviceID, folder, files) } -func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int) ([]byte, error) { +func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { name = filepath.FromSlash(name) - return m.next.Request(deviceID, folder, name, offset, size) + return m.next.Request(deviceID, folder, name, offset, size, hash, flags, options) } func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { diff --git a/protocol.go b/protocol.go index e7b6fe27..7e265877 100644 --- a/protocol.go +++ b/protocol.go @@ -70,7 +70,7 @@ type Model interface { // An index update was received from the peer device IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) // A request was made by the peer device - Request(deviceID DeviceID, folder string, name string, offset int64, size int) ([]byte, error) + Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) // A cluster configuration message was received ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) // The peer device closed the connection @@ -82,7 +82,7 @@ type Connection interface { Name() string Index(folder string, files []FileInfo) error IndexUpdate(folder string, files []FileInfo) error - Request(folder string, name string, offset int64, size int) ([]byte, error) + Request(folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) ClusterConfig(config ClusterConfigMessage) Statistics() Statistics } @@ -201,7 +201,7 @@ func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo) error { } // Request returns the bytes for the specified block after fetching them from the connected peer. -func (c *rawConnection) Request(folder string, name string, offset int64, size int) ([]byte, error) { +func (c *rawConnection) Request(folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { var id int select { case id = <-c.nextID: @@ -218,10 +218,13 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i c.awaitingMut.Unlock() ok := c.send(id, messageTypeRequest, RequestMessage{ - Folder: folder, - Name: name, - Offset: offset, - Size: int32(size), + Folder: folder, + Name: name, + Offset: offset, + Size: int32(size), + Hash: hash, + Flags: flags, + Options: options, }) if !ok { return nil, ErrClosed @@ -499,7 +502,7 @@ func filterIndexMessageFiles(fs []FileInfo) []FileInfo { } func (c *rawConnection) handleRequest(msgID int, req RequestMessage) { - data, _ := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), int(req.Size)) + data, _ := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), int(req.Size), req.Hash, req.Flags, req.Options) c.send(msgID, messageTypeResponse, ResponseMessage{ Data: data, diff --git a/protocol_test.go b/protocol_test.go index c1048cdc..c660ba32 100644 --- a/protocol_test.go +++ b/protocol_test.go @@ -232,7 +232,7 @@ func TestClose(t *testing.T) { c0.Index("default", nil) c0.Index("default", nil) - if _, err := c0.Request("default", "foo", 0, 0); err == nil { + if _, err := c0.Request("default", "foo", 0, 0, nil, 0, nil); err == nil { t.Error("Request should return an error") } } diff --git a/wireformat.go b/wireformat.go index 4eab3d37..23d347e1 100644 --- a/wireformat.go +++ b/wireformat.go @@ -42,9 +42,9 @@ func (c wireFormatConnection) IndexUpdate(folder string, fs []FileInfo) error { return c.next.IndexUpdate(folder, myFs) } -func (c wireFormatConnection) Request(folder, name string, offset int64, size int) ([]byte, error) { +func (c wireFormatConnection) Request(folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { name = norm.NFC.String(filepath.ToSlash(name)) - return c.next.Request(folder, name, offset, size) + return c.next.Request(folder, name, offset, size, hash, flags, options) } func (c wireFormatConnection) ClusterConfig(config ClusterConfigMessage) { From 1cb5875b200f07f0a768198c2425d59905957bae Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Wed, 4 Feb 2015 22:15:17 +0000 Subject: [PATCH 2/7] Expose flags, options in Index{,Update} --- common_test.go | 4 ++-- nativemodel_darwin.go | 8 ++++---- nativemodel_unix.go | 8 ++++---- nativemodel_windows.go | 8 ++++---- protocol.go | 32 ++++++++++++++++++-------------- protocol_test.go | 4 ++-- wireformat.go | 8 ++++---- 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/common_test.go b/common_test.go index 0f3795d5..f46b6a8d 100644 --- a/common_test.go +++ b/common_test.go @@ -25,10 +25,10 @@ func newTestModel() *TestModel { } } -func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) { +func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { } -func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) { +func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { } func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { diff --git a/nativemodel_darwin.go b/nativemodel_darwin.go index 6001af69..502a71f2 100644 --- a/nativemodel_darwin.go +++ b/nativemodel_darwin.go @@ -12,18 +12,18 @@ type nativeModel struct { next Model } -func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) { +func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { for i := range files { files[i].Name = norm.NFD.String(files[i].Name) } - m.next.Index(deviceID, folder, files) + m.next.Index(deviceID, folder, files, flags, options) } -func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) { +func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { for i := range files { files[i].Name = norm.NFD.String(files[i].Name) } - m.next.IndexUpdate(deviceID, folder, files) + m.next.IndexUpdate(deviceID, folder, files, flags, options) } func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { diff --git a/nativemodel_unix.go b/nativemodel_unix.go index 5f7b2e2c..21585e30 100644 --- a/nativemodel_unix.go +++ b/nativemodel_unix.go @@ -10,12 +10,12 @@ type nativeModel struct { next Model } -func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) { - m.next.Index(deviceID, folder, files) +func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { + m.next.Index(deviceID, folder, files, flags, options) } -func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) { - m.next.IndexUpdate(deviceID, folder, files) +func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { + m.next.IndexUpdate(deviceID, folder, files, flags, options) } func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { diff --git a/nativemodel_windows.go b/nativemodel_windows.go index 4859cbaa..951f5b7e 100644 --- a/nativemodel_windows.go +++ b/nativemodel_windows.go @@ -24,7 +24,7 @@ type nativeModel struct { next Model } -func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) { +func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { for i, f := range files { if strings.ContainsAny(f.Name, disallowedCharacters) { if f.IsDeleted() { @@ -37,10 +37,10 @@ func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo) { } files[i].Name = filepath.FromSlash(f.Name) } - m.next.Index(deviceID, folder, files) + m.next.Index(deviceID, folder, files, flags, options) } -func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) { +func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { for i, f := range files { if strings.ContainsAny(f.Name, disallowedCharacters) { if f.IsDeleted() { @@ -53,7 +53,7 @@ func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileI } files[i].Name = filepath.FromSlash(files[i].Name) } - m.next.IndexUpdate(deviceID, folder, files) + m.next.IndexUpdate(deviceID, folder, files, flags, options) } func (m nativeModel) Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { diff --git a/protocol.go b/protocol.go index 7e265877..7d742a7f 100644 --- a/protocol.go +++ b/protocol.go @@ -66,9 +66,9 @@ type pongMessage struct{ EmptyMessage } type Model interface { // An index was received from the peer device - Index(deviceID DeviceID, folder string, files []FileInfo) + Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) // An index update was received from the peer device - IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) + IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) // A request was made by the peer device Request(deviceID DeviceID, folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) // A cluster configuration message was received @@ -80,8 +80,8 @@ type Model interface { type Connection interface { ID() DeviceID Name() string - Index(folder string, files []FileInfo) error - IndexUpdate(folder string, files []FileInfo) error + Index(folder string, files []FileInfo, flags uint32, options []Option) error + IndexUpdate(folder string, files []FileInfo, flags uint32, options []Option) error Request(folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) ClusterConfig(config ClusterConfigMessage) Statistics() Statistics @@ -169,7 +169,7 @@ func (c *rawConnection) Name() string { } // Index writes the list of file information to the connected peer device -func (c *rawConnection) Index(folder string, idx []FileInfo) error { +func (c *rawConnection) Index(folder string, idx []FileInfo, flags uint32, options []Option) error { select { case <-c.closed: return ErrClosed @@ -177,15 +177,17 @@ func (c *rawConnection) Index(folder string, idx []FileInfo) error { } c.idxMut.Lock() c.send(-1, messageTypeIndex, IndexMessage{ - Folder: folder, - Files: idx, + Folder: folder, + Files: idx, + Flags: flags, + Options: options, }) c.idxMut.Unlock() return nil } // IndexUpdate writes the list of file information to the connected peer device as an update -func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo) error { +func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo, flags uint32, options []Option) error { select { case <-c.closed: return ErrClosed @@ -193,8 +195,10 @@ func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo) error { } c.idxMut.Lock() c.send(-1, messageTypeIndexUpdate, IndexMessage{ - Folder: folder, - Files: idx, + Folder: folder, + Files: idx, + Flags: flags, + Options: options, }) c.idxMut.Unlock() return nil @@ -463,16 +467,16 @@ func (c *rawConnection) readMessage() (hdr header, msg encodable, err error) { func (c *rawConnection) handleIndex(im IndexMessage) { if debug { - l.Debugf("Index(%v, %v, %d files)", c.id, im.Folder, len(im.Files)) + l.Debugf("Index(%v, %v, %d file, flags %x, opts: %s)", c.id, im.Folder, len(im.Files), im.Flags, im.Options) } - c.receiver.Index(c.id, im.Folder, filterIndexMessageFiles(im.Files)) + c.receiver.Index(c.id, im.Folder, filterIndexMessageFiles(im.Files), im.Flags, im.Options) } func (c *rawConnection) handleIndexUpdate(im IndexMessage) { if debug { - l.Debugf("queueing IndexUpdate(%v, %v, %d files)", c.id, im.Folder, len(im.Files)) + l.Debugf("queueing IndexUpdate(%v, %v, %d files, flags %x, opts: %s)", c.id, im.Folder, len(im.Files), im.Flags, im.Options) } - c.receiver.IndexUpdate(c.id, im.Folder, filterIndexMessageFiles(im.Files)) + c.receiver.IndexUpdate(c.id, im.Folder, filterIndexMessageFiles(im.Files), im.Flags, im.Options) } func filterIndexMessageFiles(fs []FileInfo) []FileInfo { diff --git a/protocol_test.go b/protocol_test.go index c660ba32..3ff1042c 100644 --- a/protocol_test.go +++ b/protocol_test.go @@ -229,8 +229,8 @@ func TestClose(t *testing.T) { t.Error("Ping should not return true") } - c0.Index("default", nil) - c0.Index("default", nil) + c0.Index("default", nil, 0, nil) + c0.Index("default", nil, 0, nil) if _, err := c0.Request("default", "foo", 0, 0, nil, 0, nil); err == nil { t.Error("Request should return an error") diff --git a/wireformat.go b/wireformat.go index 23d347e1..9411955b 100644 --- a/wireformat.go +++ b/wireformat.go @@ -20,7 +20,7 @@ func (c wireFormatConnection) Name() string { return c.next.Name() } -func (c wireFormatConnection) Index(folder string, fs []FileInfo) error { +func (c wireFormatConnection) Index(folder string, fs []FileInfo, flags uint32, options []Option) error { var myFs = make([]FileInfo, len(fs)) copy(myFs, fs) @@ -28,10 +28,10 @@ func (c wireFormatConnection) Index(folder string, fs []FileInfo) error { myFs[i].Name = norm.NFC.String(filepath.ToSlash(myFs[i].Name)) } - return c.next.Index(folder, myFs) + return c.next.Index(folder, myFs, flags, options) } -func (c wireFormatConnection) IndexUpdate(folder string, fs []FileInfo) error { +func (c wireFormatConnection) IndexUpdate(folder string, fs []FileInfo, flags uint32, options []Option) error { var myFs = make([]FileInfo, len(fs)) copy(myFs, fs) @@ -39,7 +39,7 @@ func (c wireFormatConnection) IndexUpdate(folder string, fs []FileInfo) error { myFs[i].Name = norm.NFC.String(filepath.ToSlash(myFs[i].Name)) } - return c.next.IndexUpdate(folder, myFs) + return c.next.IndexUpdate(folder, myFs, flags, options) } func (c wireFormatConnection) Request(folder, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) { From fdf15f3ca323b647291e13b4970a0525926f9e4f Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Thu, 5 Feb 2015 23:01:17 +0000 Subject: [PATCH 3/7] Flag checking is now responsibility of the model --- protocol.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/protocol.go b/protocol.go index 7d742a7f..48d43995 100644 --- a/protocol.go +++ b/protocol.go @@ -287,11 +287,6 @@ func (c *rawConnection) readerLoop() (err error) { switch msg := msg.(type) { case IndexMessage: - if msg.Flags != 0 { - // We don't currently support or expect any flags. - return fmt.Errorf("protocol error: unknown flags 0x%x in Index(Update) message", msg.Flags) - } - switch hdr.msgType { case messageTypeIndex: if c.state < stateCCRcvd { @@ -308,10 +303,6 @@ func (c *rawConnection) readerLoop() (err error) { } case RequestMessage: - if msg.Flags != 0 { - // We don't currently support or expect any flags. - return fmt.Errorf("protocol error: unknown flags 0x%x in Request message", msg.Flags) - } if c.state < stateIdxRcvd { return fmt.Errorf("protocol error: request message in state %d", c.state) } From bf7fea9a0ac34db6c08db594e911b0dff2bd55e7 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Fri, 6 Feb 2015 21:34:51 +0000 Subject: [PATCH 4/7] Rename error to code, update xdr path --- message.go | 5 +++-- message_xdr.go | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/message.go b/message.go index 91c33190..c2d89894 100644 --- a/message.go +++ b/message.go @@ -1,5 +1,6 @@ // Copyright (C) 2014 The Protocol Authors. +//go:generate -command genxdr go run ../syncthing/Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go //go:generate genxdr -o message_xdr.go message.go package protocol @@ -78,8 +79,8 @@ type RequestMessage struct { } type ResponseMessage struct { - Data []byte - Error int32 + Data []byte + Code int32 } type ClusterConfigMessage struct { diff --git a/message_xdr.go b/message_xdr.go index 95d72eb1..c179de76 100644 --- a/message_xdr.go +++ b/message_xdr.go @@ -465,13 +465,13 @@ ResponseMessage Structure: \ Data (variable length) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Error | +| Code | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ struct ResponseMessage { opaque Data<>; - int Error; + int Code; } */ @@ -502,7 +502,7 @@ func (o ResponseMessage) AppendXDR(bs []byte) ([]byte, error) { func (o ResponseMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { xw.WriteBytes(o.Data) - xw.WriteUint32(uint32(o.Error)) + xw.WriteUint32(uint32(o.Code)) return xw.Tot(), xw.Error() } @@ -519,7 +519,7 @@ func (o *ResponseMessage) UnmarshalXDR(bs []byte) error { func (o *ResponseMessage) DecodeXDRFrom(xr *xdr.Reader) error { o.Data = xr.ReadBytes() - o.Error = int32(xr.ReadUint32()) + o.Code = int32(xr.ReadUint32()) return xr.Error() } From 34c2c1ec16729b4f1090db2a48893b129251e733 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Sun, 8 Feb 2015 11:04:01 +0000 Subject: [PATCH 5/7] Send and receive Request error codes --- errors.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ protocol.go | 5 +++-- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100755 errors.go diff --git a/errors.go b/errors.go new file mode 100755 index 00000000..31d27af0 --- /dev/null +++ b/errors.go @@ -0,0 +1,51 @@ +// Copyright (C) 2014 The Protocol Authors. + +package protocol + +import ( + "errors" +) + +const ( + ecNoError int32 = iota + ecGeneric + ecNoSuchFile + ecInvalid +) + +var ( + ErrNoError error = nil + ErrGeneric = errors.New("generic error") + ErrNoSuchFile = errors.New("no such file") + ErrInvalid = errors.New("file is invalid") +) + +var lookupError = map[int32]error{ + ecNoError: ErrNoError, + ecGeneric: ErrGeneric, + ecNoSuchFile: ErrNoSuchFile, + ecInvalid: ErrInvalid, +} + +var lookupCode = map[error]int32{ + ErrNoError: ecNoError, + ErrGeneric: ecGeneric, + ErrNoSuchFile: ecNoSuchFile, + ErrInvalid: ecInvalid, +} + +func codeToError(errcode int32) error { + err, ok := lookupError[errcode] + if !ok { + return ErrGeneric + } + return err +} + +func errorToCode(err error) int32 { + code, ok := lookupCode[err] + if !ok { + return ecGeneric + } + return code +} diff --git a/protocol.go b/protocol.go index 48d43995..d46d7003 100644 --- a/protocol.go +++ b/protocol.go @@ -497,10 +497,11 @@ func filterIndexMessageFiles(fs []FileInfo) []FileInfo { } func (c *rawConnection) handleRequest(msgID int, req RequestMessage) { - data, _ := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), int(req.Size), req.Hash, req.Flags, req.Options) + data, err := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), int(req.Size), req.Hash, req.Flags, req.Options) c.send(msgID, messageTypeResponse, ResponseMessage{ Data: data, + Code: errorToCode(err), }) } @@ -508,7 +509,7 @@ func (c *rawConnection) handleResponse(msgID int, resp ResponseMessage) { c.awaitingMut.Lock() if rc := c.awaiting[msgID]; rc != nil { c.awaiting[msgID] = nil - rc <- asyncResult{resp.Data, nil} + rc <- asyncResult{resp.Data, codeToError(resp.Code)} close(rc) } c.awaitingMut.Unlock() From 1d76efcbcd24b408f4cdcea3f305aa9291985ba1 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Wed, 11 Feb 2015 22:11:44 +0000 Subject: [PATCH 6/7] Remove duplication --- nativemodel_windows.go | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/nativemodel_windows.go b/nativemodel_windows.go index 951f5b7e..072e1278 100644 --- a/nativemodel_windows.go +++ b/nativemodel_windows.go @@ -25,34 +25,12 @@ type nativeModel struct { } func (m nativeModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { - for i, f := range files { - if strings.ContainsAny(f.Name, disallowedCharacters) { - if f.IsDeleted() { - // Don't complain if the file is marked as deleted, since it - // can't possibly exist here anyway. - continue - } - files[i].Flags |= FlagInvalid - l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name) - } - files[i].Name = filepath.FromSlash(f.Name) - } + fixupFiles(files) m.next.Index(deviceID, folder, files, flags, options) } func (m nativeModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) { - for i, f := range files { - if strings.ContainsAny(f.Name, disallowedCharacters) { - if f.IsDeleted() { - // Don't complain if the file is marked as deleted, since it - // can't possibly exist here anyway. - continue - } - files[i].Flags |= FlagInvalid - l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name) - } - files[i].Name = filepath.FromSlash(files[i].Name) - } + fixupFiles(files) m.next.IndexUpdate(deviceID, folder, files, flags, options) } @@ -68,3 +46,18 @@ func (m nativeModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessag func (m nativeModel) Close(deviceID DeviceID, err error) { m.next.Close(deviceID, err) } + +func fixupFiles(files []FileInfo) { + for i, f := range files { + if strings.ContainsAny(f.Name, disallowedCharacters) { + if f.IsDeleted() { + // Don't complain if the file is marked as deleted, since it + // can't possibly exist here anyway. + continue + } + files[i].Flags |= FlagInvalid + l.Warnf("File name %q contains invalid characters; marked as invalid.", f.Name) + } + files[i].Name = filepath.FromSlash(files[i].Name) + } +} From aa9eda197930d3c6f64a680f3e92f95763c1ed63 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Fri, 13 Feb 2015 23:27:01 +0000 Subject: [PATCH 7/7] Add IndexTemporary and RequestTemporary flags --- protocol.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/protocol.go b/protocol.go index d46d7003..b9859203 100644 --- a/protocol.go +++ b/protocol.go @@ -35,6 +35,7 @@ const ( stateIdxRcvd ) +// FileInfo flags const ( FlagDeleted uint32 = 1 << 12 FlagInvalid = 1 << 13 @@ -48,6 +49,17 @@ const ( SymlinkTypeMask = FlagDirectory | FlagSymlinkMissingTarget ) +// IndexMessage message flags (for IndexUpdate) +const ( + FlagIndexTemporary uint32 = 1 << iota +) + +// Request message flags +const ( + FlagRequestTemporary uint32 = 1 << iota +) + +// ClusterConfigMessage.Folders.Devices flags const ( FlagShareTrusted uint32 = 1 << 0 FlagShareReadOnly = 1 << 1