From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Oleg Babin Subject: [PATCH v2] fio: introduce utime function Date: Fri, 5 Jul 2019 22:07:03 +0300 Message-Id: <20190705190703.22908-1-olegrok@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com Cc: Oleg Babin List-ID: From: Oleg Babin 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