Tarantool development patches archive
 help / color / mirror / Atom feed
* [PATCH v2] fio: introduce utime function
@ 2019-07-05 19:07 Oleg Babin
  2019-07-08 14:51 ` Vladimir Davydov
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Babin @ 2019-07-05 19:07 UTC (permalink / raw)
  To: tarantool-patches, vdavydov.dev; +Cc: Oleg Babin

From: Oleg Babin <babinoleg@mail.ru>

Closes #4323

@TarantoolBot document
Title: fio.utime

fio.utime (filepath [, atime [, mtime]])
Set access and modification times of a file.
The first argument is the filename, the second argument (atime) is
the access time, and the third argument (mtime) is
the modification time. Both times are provided in seconds since the epoch.
If the modification time is omitted, the access time provided is used;
if both times are omitted, the current time is used.
---
Issue: https://github.com/tarantool/tarantool/issues/4323

Changes in v2:
  - Reworked test 

 src/lib/core/coio_file.c |   8 +++
 src/lib/core/coio_file.h |   1 +
 src/lua/fio.c            |  10 +++-
 src/lua/fio.lua          |  22 +++++++++
 test/app/fio.result      | 104 +++++++++++++++++++++++++++++++++++++--
 test/app/fio.test.lua    |  30 ++++++++++-
 6 files changed, 169 insertions(+), 6 deletions(-)

diff --git a/src/lib/core/coio_file.c b/src/lib/core/coio_file.c
index 3359f42bc..ba4b6d691 100644
--- a/src/lib/core/coio_file.c
+++ b/src/lib/core/coio_file.c
@@ -631,3 +631,11 @@ coio_copyfile(const char *source, const char *dest)
 	eio_req *req = eio_custom(coio_do_copyfile, 0, coio_complete, &eio);
 	return coio_wait_done(req, &eio);
 }
+
+int
+coio_utime(const char *pathname, double mtime, double atime)
+{
+	INIT_COEIO_FILE(eio);
+	eio_req *req = eio_utime(pathname, atime, mtime, 0, coio_complete, &eio);
+	return coio_wait_done(req, &eio);
+}
diff --git a/src/lib/core/coio_file.h b/src/lib/core/coio_file.h
index f2112ceed..5ff30ff7f 100644
--- a/src/lib/core/coio_file.h
+++ b/src/lib/core/coio_file.h
@@ -84,6 +84,7 @@ int	coio_tempdir(char *path, size_t path_len);
 
 int	coio_readdir(const char *path, char **buf);
 int	coio_copyfile(const char *source, const char *dest);
+int	coio_utime(const char *pathname, double mtime, double atime);
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif /* defined(__cplusplus) */
diff --git a/src/lua/fio.c b/src/lua/fio.c
index 806f4256b..55fa66762 100644
--- a/src/lua/fio.c
+++ b/src/lua/fio.c
@@ -703,7 +703,14 @@ lbox_fio_copyfile(struct lua_State *L)
 	return lbox_fio_pushbool(L, coio_copyfile(source, dest) == 0);
 }
 
-
+static int
+lbox_fio_utime(struct lua_State *L)
+{
+	const char *pathname = lua_tostring(L, 1);
+	double atime = lua_tonumber(L, 2);
+	double mtime = lua_tonumber(L, 3);
+	return lbox_fio_pushbool(L, coio_utime(pathname, atime, mtime) == 0);
+}
 
 void
 tarantool_lua_fio_init(struct lua_State *L)
@@ -747,6 +754,7 @@ tarantool_lua_fio_init(struct lua_State *L)
 		{ "listdir",		lbox_fio_listdir		},
 		{ "fstat",		lbox_fio_fstat			},
 		{ "copyfile",		lbox_fio_copyfile,		},
+		{ "utime",		lbox_fio_utime			},
 		{ NULL,			NULL				}
 	};
 	luaL_register(L, NULL, internal_methods);
diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index ba8c47ec0..321ae8b2d 100644
--- a/src/lua/fio.lua
+++ b/src/lua/fio.lua
@@ -3,6 +3,7 @@
 local fio = require('fio')
 local ffi = require('ffi')
 local buffer = require('buffer')
+local fiber = require('fiber')
 
 ffi.cdef[[
     int umask(int mask);
@@ -468,6 +469,27 @@ fio.copytree = function(from, to)
     return true
 end
 
+local function check_time(time, name)
+    if time ~= nil and type(time) ~= 'number' then
+        error('fio.utime: ' .. name .. ' should be a number', 2)
+    end
+end
+
+fio.utime = function(path, atime, mtime)
+    if type(path) ~= 'string' then
+        error('Usage: fio.utime(filepath[, atime[, mtime]])')
+    end
+
+    check_time(atime, 'atime')
+    check_time(mtime, 'mtime')
+
+    local current_time = fiber.time()
+    atime = atime or current_time
+    mtime = mtime or atime
+
+    return internal.utime(path, atime, mtime)
+end
+
 fio.path = {}
 fio.path.is_file = function(filename)
     local fs = fio.stat(filename)
diff --git a/test/app/fio.result b/test/app/fio.result
index 8819e39fa..3663560ae 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -4,6 +4,9 @@ fio = require 'fio'
 ffi = require 'ffi'
 ---
 ...
+fiber = require 'fiber'
+---
+...
 buffer = require 'buffer'
 ---
 ...
@@ -1045,6 +1048,103 @@ fh:close()
 ---
 - true
 ...
+-- test utime
+test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
+---
+- true
+...
+fh = fio.open('newfile', {'O_RDWR','O_CREAT'})
+---
+...
+current_time = math.floor(fiber.time())
+---
+...
+fiber_time = fiber.time
+---
+...
+fiber.time = function() return current_time end
+---
+...
+fio.utime('newfile', 0, 0)
+---
+- true
+...
+fh:stat().atime == 0
+---
+- true
+...
+fh:stat().mtime == 0
+---
+- true
+...
+fio.utime('newfile', 1, 2)
+---
+- true
+...
+fh:stat().atime == 1
+---
+- false
+...
+fh:stat().mtime == 2
+---
+- false
+...
+fio.utime('newfile', 3)
+---
+- true
+...
+fh:stat().atime == 3
+---
+- true
+...
+fh:stat().mtime == 3
+---
+- true
+...
+fio.utime('newfile')
+---
+- true
+...
+fh:stat().atime == current_time
+---
+- true
+...
+fh:stat().mtime == current_time
+---
+- true
+...
+fio.utime(nil)
+---
+- error: 'builtin/fio.lua: Usage: fio.utime(filepath[, atime[, mtime]])'
+...
+fio.utime('newfile', 'string')
+---
+- error: 'builtin/fio.lua: fio.utime: atime should be a number'
+...
+fio.utime('newfile', 1, 'string')
+---
+- error: 'builtin/fio.lua: fio.utime: mtime should be a number'
+...
+fh:close()
+---
+- true
+...
+fio.unlink('newfile')
+---
+- true
+...
+fh = nil
+---
+...
+current_time = nil
+---
+...
+fiber.time = fiber_time
+---
+...
+fiber_time = nil
+---
+...
 -- gh-2924
 -- fio.path.exists lexists is_file, etc
 --
@@ -1279,10 +1379,6 @@ fio.rmdir(tmpdir)
 --
 -- gh-3580: Check that error messages are descriptive enough.
 --
-test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
----
-- true
-...
 fh1:seek(nil, 'a')
 ---
 - error: 'builtin/fio.lua: fio.seek(): unknown whence: a'
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 6c0d2f478..be735e4f3 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -1,5 +1,6 @@
 fio = require 'fio'
 ffi = require 'ffi'
+fiber = require 'fiber'
 buffer = require 'buffer'
 test_run = require('test_run').new()
 -- umask
@@ -341,6 +342,34 @@ fh:read(64)
 buf:recycle()
 fh:close()
 
+-- test utime
+test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
+fh = fio.open('newfile', {'O_RDWR','O_CREAT'})
+current_time = math.floor(fiber.time())
+fiber_time = fiber.time
+fiber.time = function() return current_time end
+fio.utime('newfile', 0, 0)
+fh:stat().atime == 0
+fh:stat().mtime == 0
+fio.utime('newfile', 1, 2)
+fh:stat().atime == 1
+fh:stat().mtime == 2
+fio.utime('newfile', 3)
+fh:stat().atime == 3
+fh:stat().mtime == 3
+fio.utime('newfile')
+fh:stat().atime == current_time
+fh:stat().mtime == current_time
+fio.utime(nil)
+fio.utime('newfile', 'string')
+fio.utime('newfile', 1, 'string')
+fh:close()
+fio.unlink('newfile')
+fh = nil
+current_time = nil
+fiber.time = fiber_time
+fiber_time = nil
+
 -- gh-2924
 -- fio.path.exists lexists is_file, etc
 --
@@ -423,7 +452,6 @@ fio.rmdir(tmpdir)
 --
 -- gh-3580: Check that error messages are descriptive enough.
 --
-test_run:cmd("push filter '(.builtin/.*.lua):[0-9]+' to '\\1'")
 fh1:seek(nil, 'a')
 fio.open(nil)
 fio.open(tmp1, {'A'}, tonumber('0777', 8))
-- 
2.21.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] fio: introduce utime function
  2019-07-05 19:07 [PATCH v2] fio: introduce utime function Oleg Babin
@ 2019-07-08 14:51 ` Vladimir Davydov
  2019-07-08 15:52   ` [tarantool-patches] " Олег
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir Davydov @ 2019-07-08 14:51 UTC (permalink / raw)
  To: Oleg Babin; +Cc: tarantool-patches, Oleg Babin

On Fri, Jul 05, 2019 at 10:07:03PM +0300, Oleg Babin wrote:
> +fio.utime('newfile', 0, 0)
> +---
> +- true
> +...
> +fh:stat().atime == 0
> +---
> +- true
> +...
> +fh:stat().mtime == 0
> +---
> +- true
> +...
> +fio.utime('newfile', 1, 2)
> +---
> +- true
> +...
> +fh:stat().atime == 1
> +---
> +- false
> +...
> +fh:stat().mtime == 2
> +---
> +- false
> +...

???

Looks like you mixed atime with mtime. Please fix.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tarantool-patches] Re: [PATCH v2] fio: introduce utime function
  2019-07-08 14:51 ` Vladimir Davydov
@ 2019-07-08 15:52   ` Олег
  2019-07-09  8:55     ` Vladimir Davydov
  0 siblings, 1 reply; 4+ messages in thread
From: Олег @ 2019-07-08 15:52 UTC (permalink / raw)
  To: tarantool-patches


Thanks! My bad. I really missed it.

Please apply following diff to my patch.

diff --git a/src/lua/fio.c b/src/lua/fio.c
index 55fa66762..48e2f8eb3 100644
--- a/src/lua/fio.c
+++ b/src/lua/fio.c
@@ -709,7 +709,7 @@ lbox_fio_utime(struct lua_State *L)
      const char *pathname = lua_tostring(L, 1);
      double atime = lua_tonumber(L, 2);
      double mtime = lua_tonumber(L, 3);
-    return lbox_fio_pushbool(L, coio_utime(pathname, atime, mtime) == 0);
+    return lbox_fio_pushbool(L, coio_utime(pathname, mtime, atime) == 0);
  }

  void
diff --git a/test/app/fio.result b/test/app/fio.result
index 3663560ae..34f58d6bc 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -1083,11 +1083,11 @@ fio.utime('newfile', 1, 2)
  ...
  fh:stat().atime == 1
  ---
-- false
+- true
  ...
  fh:stat().mtime == 2
  ---
-- false
+- true
  ...
  fio.utime('newfile', 3)
  ---


08.07.2019 17:51, Vladimir Davydov wrote:
 > On Fri, Jul 05, 2019 at 10:07:03PM +0300, Oleg Babin wrote:
 >> +fio.utime('newfile', 0, 0)
 >> +---
 >> +- true
 >> +...
 >> +fh:stat().atime == 0
 >> +---
 >> +- true
 >> +...
 >> +fh:stat().mtime == 0
 >> +---
 >> +- true
 >> +...
 >> +fio.utime('newfile', 1, 2)
 >> +---
 >> +- true
 >> +...
 >> +fh:stat().atime == 1
 >> +---
 >> +- false
 >> +...
 >> +fh:stat().mtime == 2
 >> +---
 >> +- false
 >> +...
 >
 > ???
 >
 > Looks like you mixed atime with mtime. Please fix.
 >

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [tarantool-patches] Re: [PATCH v2] fio: introduce utime function
  2019-07-08 15:52   ` [tarantool-patches] " Олег
@ 2019-07-09  8:55     ` Vladimir Davydov
  0 siblings, 0 replies; 4+ messages in thread
From: Vladimir Davydov @ 2019-07-09  8:55 UTC (permalink / raw)
  To: Олег; +Cc: tarantool-patches

On Mon, Jul 08, 2019 at 06:52:24PM +0300, Олег wrote:
> 
> Thanks! My bad. I really missed it.
> 
> Please apply following diff to my patch.
> 
> diff --git a/src/lua/fio.c b/src/lua/fio.c
> index 55fa66762..48e2f8eb3 100644
> --- a/src/lua/fio.c
> +++ b/src/lua/fio.c
> @@ -709,7 +709,7 @@ lbox_fio_utime(struct lua_State *L)
>      const char *pathname = lua_tostring(L, 1);
>      double atime = lua_tonumber(L, 2);
>      double mtime = lua_tonumber(L, 3);
> -    return lbox_fio_pushbool(L, coio_utime(pathname, atime, mtime) == 0);
> +    return lbox_fio_pushbool(L, coio_utime(pathname, mtime, atime) == 0);
>  }

So, now we have:

  eio_utime(path, atime, mtime)
  coio_utime(path, mtime, atime)
  fio.utime(path, atime, mtime)

I think that for the sake of consistency coio_utime should take atime as
the second argument and mtime as the third.

Please fix and send v3 in a separate email.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-07-09  8:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-05 19:07 [PATCH v2] fio: introduce utime function Oleg Babin
2019-07-08 14:51 ` Vladimir Davydov
2019-07-08 15:52   ` [tarantool-patches] " Олег
2019-07-09  8:55     ` Vladimir Davydov

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