From 4852177de4d69f3eb7a5a84629a7722a2b633f19 Mon Sep 17 00:00:00 2001 From: TAV2BJ Date: Fri, 13 Oct 2023 10:51:30 +0200 Subject: [PATCH] Allows to disable making parent collection in createParentCollection --- client.go | 22 +++++++++++++--------- client_test.go | 31 +++++++++++++++++++++++++++++++ requests.go | 9 ++++++--- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 656e003..40dbe2b 100644 --- a/client.go +++ b/client.go @@ -386,7 +386,7 @@ func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadClos } // Write writes data to a given path -func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) { +func (c *Client) Write(path string, data []byte, _ os.FileMode, doParents ...bool) (err error) { s, err := c.put(path, bytes.NewReader(data)) if err != nil { return @@ -398,7 +398,7 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) { return nil case 404, 409: - err = c.createParentCollection(path) + err = c.createParentCollection(path, c.parseParentOption(doParents...)) if err != nil { return } @@ -416,23 +416,27 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) { } // WriteStream writes a stream -func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) { - - err = c.createParentCollection(path) - if err != nil { +func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode, doParents ...bool) (err error) { + if err := c.createParentCollection(path, c.parseParentOption(doParents...)); err != nil { return err } - s, err := c.put(path, stream) if err != nil { return err } - switch s { case 200, 201, 204: return nil - default: return NewPathError("WriteStream", path, s) } } + +// parseParentOption parses variadic bool argument. If present, first element is returned. +func (c *Client) parseParentOption(b ...bool) bool { + create := true + if len(b) > 0 { + create = b[0] + } + return create +} diff --git a/client_test.go b/client_test.go index 65724fb..5473aa8 100644 --- a/client_test.go +++ b/client_test.go @@ -543,6 +543,22 @@ func TestWriteStream(t *testing.T) { if info, err := fs.Stat(ctx, "/404/works.txt"); err != nil { t.Fatalf("got: %v, want file info: %v", err, info) } + + if err := cli.WriteStream("/test/newFile.txt", strings.NewReader("foo bar\n"), 0660, false); err != nil { + t.Fatalf("got: %v, want nil", err) + } + + if info, err := fs.Stat(ctx, "/test/newFile.txt"); err != nil { + t.Fatalf("got: %v, want file info: %v", err, info) + } + + if err := cli.WriteStream("/test/fld1/fld2/newFile.txt", strings.NewReader("foo bar\n"), 0660, true); err != nil { + t.Fatalf("got: %v, want nil", err) + } + + if info, err := fs.Stat(ctx, "/test/fld1/fld2/newFile.txt"); err != nil { + t.Fatalf("got: %v, want file info: %v", err, info) + } } func TestWriteStreamFromPipe(t *testing.T) { @@ -572,3 +588,18 @@ func TestWriteStreamFromPipe(t *testing.T) { t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 8) } } + +func TestClient_createParentCollection(t *testing.T) { + cli, srv, _, _ := newServer(t) + defer srv.Close() + + err := cli.createParentCollection("/test/folder1/folder2/") + if err != nil { + t.Fatalf("got: %v, want no err", err) + } + + err = cli.createParentCollection("/some/folder/", false) + if err != nil { + t.Fatalf("got: %v, want no err", err) + } +} diff --git a/requests.go b/requests.go index 8e362e8..f71ddb8 100644 --- a/requests.go +++ b/requests.go @@ -131,7 +131,7 @@ func (c *Client) doCopyMove( return } -func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) { +func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool, doParents ...bool) (err error) { s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite) if err != nil { return @@ -149,7 +149,7 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri log.Printf("TODO handle %s - %s multistatus result %s\n", method, oldpath, String(data)) case 409: - err := c.createParentCollection(newpath) + err := c.createParentCollection(newpath, c.parseParentOption(doParents...)) if err != nil { return err } @@ -171,7 +171,10 @@ func (c *Client) put(path string, stream io.Reader) (status int, err error) { return } -func (c *Client) createParentCollection(itemPath string) (err error) { +func (c *Client) createParentCollection(itemPath string, doParents ...bool) (err error) { + if len(doParents) > 0 && !doParents[0] { + return nil + } parentPath := path.Dir(itemPath) if parentPath == "." || parentPath == "/" { return nil