summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-12 23:43:21 +0800
committerruki <[email protected]>2019-10-12 23:43:25 +0800
commit18468a7582fa6b277380a752ff3b7a2a37bc77bd (patch)
treec3d69c0ff3ff7dc5f4b1832f7ee1dc1c13d1ac7f
parent02e8be01f8e0311464302aff6b92a739098c7258 (diff)
improve file module
-rw-r--r--xmake/core/base/io/file.lua108
-rw-r--r--xmake/core/base/io/filelock.lua58
-rw-r--r--xmake/core/base/io/socket.lua19
3 files changed, 141 insertions, 44 deletions
diff --git a/xmake/core/base/io/file.lua b/xmake/core/base/io/file.lua
index 26c914dea..1e1c777c1 100644
--- a/xmake/core/base/io/file.lua
+++ b/xmake/core/base/io/file.lua
@@ -52,19 +52,36 @@ end
-- close file
function _file:close()
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok, errors = io.file_close(self._FILE)
+
+ -- close it
+ ok, errors = io.file_close(self._FILE)
if ok then
self._FILE = nil
end
return ok, errors
end
+-- ensure the file is opened
+function _file:_ensure_opened()
+ if not self._FILE then
+ return false, string.format("%s: has been closed!", self)
+ end
+ return true
+end
+
-- tostring(file)
function _file:__tostring()
- return "<file: " .. self:name() .. ">"
+ local name = self:path()
+ if not name or #name > 16 then
+ name = self:name()
+ end
+ return "<file: " .. name .. ">"
end
-- gc(file)
@@ -81,85 +98,120 @@ end
-- get file rawfd
function _file:rawfd()
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return nil, errors
end
+
+ -- get rawfd
local result, errors = io.file_rawfd(self._FILE)
if not result and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return result, errors
end
-- get file size
function _file:size()
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return nil, errors
end
+
+ -- get file size
local result, errors = io.file_size(self._FILE)
if not result and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return result, errors
end
-- read data from file
function _file:read(fmt, opt)
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return nil, errors
end
+
+ -- read it
opt = opt or {}
local result, errors = io.file_read(self._FILE, fmt, opt.continuation)
if errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return result, errors
end
-- write data to file
function _file:write(...)
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok, errors = io.file_write(self._FILE, ...)
+
+ -- write it
+ ok, errors = io.file_write(self._FILE, ...)
if not ok and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return ok, errors
end
-- seek offset at file
function _file:seek(whence, offset)
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return nil, errors
end
+
+ -- seek it
local result, errors = io.file_seek(self._FILE, whence, offset)
if not result and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return result, errors
end
-- flush data to file
function _file:flush()
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok, errors = io.file_flush(self._FILE)
+
+ -- flush it
+ ok, errors = io.file_flush(self._FILE)
if not ok and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return ok, errors
end
-- this file is a tty?
function _file:isatty()
- if not self._FILE then
- return false, string.format("file(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok, errors = io.file_isatty(self._FILE)
+
+ -- is a tty?
+ ok, errors = io.file_isatty(self._FILE)
if ok == nil and errors then
- errors = string.format("file(%s): %s", self:name(), errors)
+ errors = string.format("%s: %s", self, errors)
end
return ok, errors
end
diff --git a/xmake/core/base/io/filelock.lua b/xmake/core/base/io/filelock.lua
index f68da33c1..b4e66c7bb 100644
--- a/xmake/core/base/io/filelock.lua
+++ b/xmake/core/base/io/filelock.lua
@@ -60,14 +60,19 @@ end
-- @return ok, errors
--
function _filelock:lock(opt)
- if not self._LOCK then
- return false, string.format("filelock(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
+
+ -- lock it
if self._LOCKED_NUM > 0 or io.filelock_lock(self._LOCK, opt) then
self._LOCKED_NUM = self._LOCKED_NUM + 1
return true
else
- return false, string.format("filelock(%s): lock %s failed!", self:name(), self:path())
+ return false, string.format("%s: lock failed!", self)
end
end
@@ -78,22 +83,32 @@ end
-- @return ok, errors
--
function _filelock:trylock(opt)
- if not self._LOCK then
- return false, string.format("filelock(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
+
+ -- unlock it
if self._LOCKED_NUM > 0 or io.filelock_trylock(self._LOCK, opt) then
self._LOCKED_NUM = self._LOCKED_NUM + 1
return true
else
- return false, string.format("filelock(%s): trylock %s failed!", self:name(), self:path())
+ return false, string.format("%s: trylock failed!", self)
end
end
-- unlock file
function _filelock:unlock(opt)
- if not self._LOCK then
- return false, string.format("filelock(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
+
+ -- unlock it
if self._LOCKED_NUM > 1 or (self._LOCKED_NUM > 0 and io.filelock_unlock(self._LOCK)) then
if self._LOCKED_NUM > 0 then
self._LOCKED_NUM = self._LOCKED_NUM - 1
@@ -102,16 +117,21 @@ function _filelock:unlock(opt)
end
return true
else
- return false, string.format("filelock(%s): unlock %s failed!", self:name(), self:path())
+ return false, string.format("%s: unlock failed!", self)
end
end
-- close filelock
function _filelock:close()
- if not self._LOCK then
- return false, string.format("filelock(%s) has been closed!", self:name())
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok = io.filelock_close(self._LOCK)
+
+ -- close filelock
+ ok = io.filelock_close(self._LOCK)
if ok then
self._LOCK = nil
self._LOCKED_NUM = 0
@@ -119,9 +139,21 @@ function _filelock:close()
return ok
end
+-- ensure the filelock is opened
+function _filelock:_ensure_opened()
+ if not self._LOCK then
+ return false, string.format("%s: has been closed!", self)
+ end
+ return true
+end
+
-- tostring(filelock)
function _filelock:__tostring()
- return "<filelock: " .. self:name() .. ">"
+ local name = self:path()
+ if not name or #name > 16 then
+ name = self:name()
+ end
+ return "<filelock: " .. name .. ">"
end
-- gc(filelock)
diff --git a/xmake/core/base/io/socket.lua b/xmake/core/base/io/socket.lua
index 79be64cf7..9f26d82ed 100644
--- a/xmake/core/base/io/socket.lua
+++ b/xmake/core/base/io/socket.lua
@@ -48,16 +48,29 @@ end
-- close socket
function _socket:close()
- if not self._SOCK then
- return false, string.format("%s has been closed!", tostring(self))
+
+ -- ensure opened
+ local ok, errors = self:_ensure_opened()
+ if not ok then
+ return false, errors
end
- local ok = io.socket_close(self._SOCK)
+
+ -- close it
+ ok = io.socket_close(self._SOCK)
if ok then
self._SOCK = nil
end
return ok
end
+-- ensure the socket is opened
+function _socket:_ensure_opened()
+ if not self._SOCK then
+ return false, string.format("%s: has been closed!", self)
+ end
+ return true
+end
+
-- tostring(socket)
function _socket:__tostring()
return string.format("<socket: %s/%s>", self:type(), self:family())