Tarantool development patches archive
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@gmail.com>
To: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: tml <tarantool-patches@freelists.org>
Subject: [PATCH v2] core/coio_file: copyfile -- Make it behave as regular cp
Date: Mon, 6 May 2019 12:59:02 +0300	[thread overview]
Message-ID: <20190506095902.GM2488@uranus.lan> (raw)
In-Reply-To: <20190506083729.cfyyu7pag7hvlgif@esperanza>

Traditional cp utility opens destination with O_TRUNC
flag, iow it drops old content of the target file if
such exists.

Fixes #4181
---
 https://github.com/tarantool/tarantool/issues/4181

 v2: Update the test to compare file contents

 src/lib/core/coio_file.c |  2 +-
 test/app/fio.result      | 41 ++++++++++++++++++++++++++++++++++++++++
 test/app/fio.test.lua    | 13 +++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/src/lib/core/coio_file.c b/src/lib/core/coio_file.c
index 3caf185a5..3359f42bc 100644
--- a/src/lib/core/coio_file.c
+++ b/src/lib/core/coio_file.c
@@ -584,7 +584,7 @@ coio_do_copyfile(eio_req *req)
 		goto error;
 	}
 
-	int dest_fd = open(eio->copyfile.dest, O_WRONLY | O_CREAT,
+	int dest_fd = open(eio->copyfile.dest, O_WRONLY|O_CREAT|O_TRUNC,
 			   st.st_mode & 0777);
 	if (dest_fd < 0) {
 		goto error_dest;
diff --git a/test/app/fio.result b/test/app/fio.result
index 879e0a767..39b2ad0b5 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -721,6 +721,12 @@ file3 = fio.pathjoin(tree, 'file.3')
 file4 = fio.pathjoin(tree, 'file.4')
 ---
 ...
+file5 = fio.pathjoin(tree, 'file.5')
+---
+...
+file6 = fio.pathjoin(tree, 'file.6')
+---
+...
 fh1 = fio.open(file1, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
 ---
 ...
@@ -775,6 +781,41 @@ errinj.set('ERRINJ_COIO_SENDFILE_CHUNK', -1)
 ---
 - ok
 ...
+--- test the destination file is truncated
+fh5 = fio.open(file5, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 420)
+---
+...
+fh5:write("template data")
+---
+- true
+...
+fh5:close()
+---
+- true
+...
+fh6 = fio.open(file6, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 420)
+---
+...
+fh6:write("to be truncated")
+---
+- true
+...
+fio.copyfile(file5, file6)
+---
+- true
+...
+fh6:seek(0)
+---
+- 0
+...
+fh6:read()
+---
+- template data
+...
+fh6:close()
+---
+- true
+...
 res, err = fio.copyfile(fio.pathjoin(tmp1, 'not_exists.txt'), tmp1)
 ---
 ...
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 1255b2804..ffb15c057 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -231,6 +231,8 @@ file1 = fio.pathjoin(tmp1, 'file.1')
 file2 = fio.pathjoin(tmp2, 'file.2')
 file3 = fio.pathjoin(tree, 'file.3')
 file4 = fio.pathjoin(tree, 'file.4')
+file5 = fio.pathjoin(tree, 'file.5')
+file6 = fio.pathjoin(tree, 'file.6')
 
 fh1 = fio.open(file1, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
 fh1:write("gogo")
@@ -249,6 +251,17 @@ fio.copyfile(file1, file4)
 fio.stat(file1, file4) ~= nil
 errinj.set('ERRINJ_COIO_SENDFILE_CHUNK', -1)
 
+--- test the destination file is truncated
+fh5 = fio.open(file5, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 420)
+fh5:write("template data")
+fh5:close()
+fh6 = fio.open(file6, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 420)
+fh6:write("to be truncated")
+fio.copyfile(file5, file6)
+fh6:seek(0)
+fh6:read()
+fh6:close()
+
 res, err = fio.copyfile(fio.pathjoin(tmp1, 'not_exists.txt'), tmp1)
 res
 err:match("failed to copy") ~= nil
-- 
2.20.1

  parent reply	other threads:[~2019-05-06  9:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-04 15:49 [PATCH] " Cyrill Gorcunov
2019-05-06  8:37 ` Vladimir Davydov
2019-05-06  8:43   ` Cyrill Gorcunov
2019-05-06  9:59   ` Cyrill Gorcunov [this message]
2019-05-06 10:09     ` [PATCH v2] " Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190506095902.GM2488@uranus.lan \
    --to=gorcunov@gmail.com \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v2] core/coio_file: copyfile -- Make it behave as regular cp' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox