fs.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. --[[
  2. nixio - Linux I/O library for lua
  3. Copyright 2009 Steven Barth <steven@midlink.org>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. $Id$
  9. ]]--
  10. local table = require "table"
  11. local nixio = require "nixio"
  12. local type, ipairs, setmetatable = type, ipairs, setmetatable
  13. require "nixio.util"
  14. module ("nixio.fs", function(m) setmetatable(m, {__index = nixio.fs}) end)
  15. function readfile(path, limit)
  16. local fd, code, msg = nixio.open(path, "r")
  17. local data
  18. if not fd then
  19. return nil, code, msg
  20. end
  21. data, code, msg = fd:readall(limit)
  22. fd:close()
  23. return data, code, msg
  24. end
  25. function writefile(path, data)
  26. local fd, code, msg, stat = nixio.open(path, "w")
  27. if not fd then
  28. return nil, code, msg
  29. end
  30. stat, code, msg = fd:writeall(data)
  31. fd:close()
  32. return stat, code, msg
  33. end
  34. function datacopy(src, dest, size)
  35. local fdin, code, msg = nixio.open(src, "r")
  36. if not fdin then
  37. return nil, code, msg
  38. end
  39. local fdout, code, msg = nixio.open(dest, "w")
  40. if not fdout then
  41. return nil, code, msg
  42. end
  43. local stat, code, msg, sent = fdin:copy(fdout, size)
  44. fdin:close()
  45. fdout:close()
  46. return stat, code, msg, sent
  47. end
  48. function copy(src, dest)
  49. local stat, code, msg, res = nixio.fs.lstat(src)
  50. if not stat then
  51. return nil, code, msg
  52. end
  53. if stat.type == "dir" then
  54. if nixio.fs.stat(dest, type) ~= "dir" then
  55. res, code, msg = nixio.fs.mkdir(dest)
  56. else
  57. stat = true
  58. end
  59. elseif stat.type == "lnk" then
  60. res, code, msg = nixio.fs.symlink(nixio.fs.readlink(src), dest)
  61. elseif stat.type == "reg" then
  62. res, code, msg = datacopy(src, dest)
  63. end
  64. if not res then
  65. return nil, code, msg
  66. end
  67. nixio.fs.utimes(dest, stat.atime, stat.mtime)
  68. if nixio.fs.lchown then
  69. nixio.fs.lchown(dest, stat.uid, stat.gid)
  70. end
  71. if stat.type ~= "lnk" then
  72. nixio.fs.chmod(dest, stat.modedec)
  73. end
  74. return true
  75. end
  76. function move(src, dest)
  77. local stat, code, msg = nixio.fs.rename(src, dest)
  78. if not stat and code == nixio.const.EXDEV then
  79. stat, code, msg = copy(src, dest)
  80. if stat then
  81. stat, code, msg = nixio.fs.unlink(src)
  82. end
  83. end
  84. return stat, code, msg
  85. end
  86. function mkdirr(dest, mode)
  87. if nixio.fs.stat(dest, "type") == "dir" then
  88. return true
  89. else
  90. local stat, code, msg = nixio.fs.mkdir(dest, mode)
  91. if not stat and code == nixio.const.ENOENT then
  92. stat, code, msg = mkdirr(nixio.fs.dirname(dest), mode)
  93. if stat then
  94. stat, code, msg = nixio.fs.mkdir(dest, mode)
  95. end
  96. end
  97. return stat, code, msg
  98. end
  99. end
  100. local function _recurse(cb, src, dest)
  101. local type = nixio.fs.lstat(src, "type")
  102. if type ~= "dir" then
  103. return cb(src, dest)
  104. else
  105. local stat, se, code, msg, s, c, m = true, nixio.const.sep
  106. if dest then
  107. s, c, m = cb(src, dest)
  108. stat, code, msg = stat and s, c or code, m or msg
  109. end
  110. for e in nixio.fs.dir(src) do
  111. if dest then
  112. s, c, m = _recurse(cb, src .. se .. e, dest .. se .. e)
  113. else
  114. s, c, m = _recurse(cb, src .. se .. e)
  115. end
  116. stat, code, msg = stat and s, c or code, m or msg
  117. end
  118. if not dest then -- Postfix
  119. s, c, m = cb(src)
  120. stat, code, msg = stat and s, c or code, m or msg
  121. end
  122. return stat, code, msg
  123. end
  124. end
  125. function copyr(src, dest)
  126. return _recurse(copy, src, dest)
  127. end
  128. function mover(src, dest)
  129. local stat, code, msg = nixio.fs.rename(src, dest)
  130. if not stat and code == nixio.const.EXDEV then
  131. stat, code, msg = _recurse(copy, src, dest)
  132. if stat then
  133. stat, code, msg = _recurse(nixio.fs.remove, src)
  134. end
  135. end
  136. return stat, code, msg
  137. end
  138. function remover(src)
  139. return _recurse(nixio.fs.remove, src)
  140. end