Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH] fio: introduce utime function
@ 2019-07-05  9:05 Oleg Babin
  2019-07-05 13:09 ` Vladimir Davydov
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg Babin @ 2019-07-05  9:05 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Oleg Babin

From: Oleg Babin <babinoleg@mail.ru>

Closes #4323

Issue: https://github.com/tarantool/tarantool/issues/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. If the modification time is omitted, the access time provided is used; if both times are omitted, the current time is used.
---
 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      | 90 ++++++++++++++++++++++++++++++++++++++++
 test/app/fio.test.lua    | 27 ++++++++++++
 6 files changed, 157 insertions(+), 1 deletion(-)

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..1b69d7700 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', 3)
+    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..ef21faffc 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,93 @@ fh:close()
 ---
 - true
 ...
+-- test utime
+fh = fio.open('newfile', {'O_RDWR','O_CREAT'})
+---
+...
+eps = 0.01
+---
+...
+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
+...
+current_time = fiber.time()
+---
+...
+fio.utime('newfile')
+---
+- true
+...
+fh:stat().atime - current_time < eps
+---
+- true
+...
+fh:stat().mtime - current_time < eps
+---
+- true
+...
+fio.utime(nil)
+---
+- error: 'builtin/fio.lua:480: Usage: fio.utime(filepath[, atime[, mtime]])'
+...
+fio.utime('newfile', 'string')
+---
+- error: 'fio.utime: atime should be a number'
+...
+fio.utime('newfile', 1, 'string')
+---
+- error: 'fio.utime: mtime should be a number'
+...
+fh:close()
+---
+- true
+...
+fio.unlink('newfile')
+---
+- true
+...
+fh = nil
+---
+...
+eps = nil
+---
+...
+current_time = nil
+---
+...
 -- gh-2924
 -- fio.path.exists lexists is_file, etc
 --
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 6c0d2f478..474b38b05 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,32 @@ fh:read(64)
 buf:recycle()
 fh:close()
 
+-- test utime
+fh = fio.open('newfile', {'O_RDWR','O_CREAT'})
+eps = 0.01
+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
+current_time = fiber.time()
+fio.utime('newfile')
+fh:stat().atime - current_time < eps
+fh:stat().mtime - current_time < eps
+fio.utime(nil)
+fio.utime('newfile', 'string')
+fio.utime('newfile', 1, 'string')
+fh:close()
+fio.unlink('newfile')
+fh = nil
+eps = nil
+current_time = nil
+
+
 -- gh-2924
 -- fio.path.exists lexists is_file, etc
 --
-- 
2.21.0

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-05  9:05 [tarantool-patches] [PATCH] fio: introduce utime function Oleg Babin
2019-07-05 13:09 ` Vladimir Davydov

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