copyfile

copyfile

function copyfile( src, dst ) --> res, err

Description

This function copy the file in the source path in the destination path.

Parameters

srcPath

Path to the source file.

dstPath

Path where to copy the file.

Return Values

res

true if the copy successes of nil if an error occurse.

err

A message if an error occurse, otherwise nil

Code

--ZFUNC-copyfile-v1
local function copyfile( src, dst ) --> ok, err

   local function checkerror( ... )
      local msg = ""
      for m = 1, select( "#", ... ) do
         local p = select( m, ... )
         if p ~= nil then
         msg = msg..p..". "
         end
      end
      if msg == "" then return true end
      return nil, msg
   end

   local s, serr = io.open( src, "rb" )
   if serr then
      return checkerror( "Can not open source file", serr )
   end

   local d, derr = io.open( dst, "wb" )
   if not d then
      s, serr = s:close()
      return checkerror( "Can not create destination file" , derr, serr )
   end

   -- Copy loop
   while true do
      buf, serr = s:read( 1024 )
      if serr or not buf then break end
      ok, derr = d:write( buf )
      if derr then break end
   end
   if serr or derr then
      return checkerror( "Error while copying", serr, derr )
   end

   s, serr = s:close()
   d, derr = d:close()
   return checkerror( serr, derr )
end

return copyfile

Examples

local t = require "taptest"
local copyfile = require "copyfile"
local readfile = require "readfile"
local writefile = require "writefile"

local inpath = "intmp.txt"
local outpath = "outtmp.txt"
local data = ( "01f" ):rep( 512 )

writefile( inpath, data )
t( copyfile( inpath, outpath ), true )
t( readfile( outpath ), data )

os.remove( inpath )
os.remove( outpath )

t()

See also