files.anubis 3.42 KB

 *Project*                     Anubis
   
 *Title*                  generic files functions. 
   
 *Copyright*       Copyright (c) David René 2006-2007. 

   
   
   
   
 *Author*    David René
 *Created*   May 2005
 *Status*    Released  
 *Overview* 
 
read tools/connections.anubis
read tools/streams.anubis
read tools/basis.anubis

public type ResultCopy:
  cant_read_file,
  cant_create_file,
  copy_error,
  copy_file_mode_error,
  copy_file_times_error, 
  copy_ok.

define ResultCopy 
  copy_file
  (
    RAddr(Int8) source, 
    WAddr(Int8) target, 
    Int32 length
  ) =
  with read_length = min(length, 65536),
  if read_length = 0 then 
    copy_ok
  else if read(source, read_length, 10) is
  {
    failure         then copy_error,
    success(buffer) then
    if write( target , buffer) is
    {
      failure then copy_error,
      success(how_many) then
      if read_length = how_many then 
        copy_file(source, target, length - how_many)
      else 
        copy_error
    }
  }
.

define ResultCopy 
  copy_file_data
  ( 
    String source_file, 
    String target_file
  ) =
  //open the source file
  if (Maybe(RAddr(Int8)))file(source_file, read) is
  {
     failure          then cant_read_file,
     success(source)  then 
    //open the target file
    if (Maybe(RWAddr(Int8)))file(target_file, new) is
    {
      failure         then print("can't create target file\n");cant_create_file, //nothing to write 
      success(target) then copy_file(source, weaken(target), file_size(source))
    }
  }.
  
public define ResultCopy 
  copy_file
  ( 
    String source_file, 
    String target_file
  ) =

  // copy only the data of the file
  with result = copy_file_data(source_file, target_file),
  if result = copy_ok then
  // copy date and times
    if get_file_times(source_file) is 
    {
      failure then print("get copy_file_times_error\n");copy_file_times_error,
      success(ftimes) then 
        if ftimes is times(modif, access) then
          if set_file_times(target_file, ftimes) is 
          {
            failure     then print("set copy_file_times_error\n");copy_file_times_error, 
            success(_)  then 
              //copy file_mode
              if get_file_mode(source_file) is
              {
                failure             then copy_file_mode_error,
                success (file_mode) then 
                if set_file_mode(target_file, file_mode) is
                {
                  failure     then copy_file_mode_error,
                  success(_)  then copy_ok
                } 
              }
          }
    }
   else
    result
  .

public define Bool
  move_file
  (
    String  source_file,
    String  target_file
  )=
  if copy_file(source_file, target_file) is copy_ok then
    remove(source_file)
  else
    false.
  
/** copy file into the socket
 */ 
public define Maybe(One)
  copy_file_to_stream
  (
    RAddr(Int8)   source_file, 
    RWAddr(Int8)  socket, 
    Int32         length
  ) =
  //we send the mail content by 16KB buffer
  with read_length = min(length, 16384),
  if read_length = 0 then 
    success(unique)
  else if read(source_file, read_length, 10) is
  {
    failure         then failure,
    success(buffer) then
    if reliable_write(tcp(socket), buffer) is
    {
      failure           then failure,
      success(how_many) then 
        if read_length = how_many then 
          copy_file_to_stream(source_file, socket, length - how_many)
        else 
          failure
    }
  }.