Commit 953ba7d3b42b7ddf8f98c9a572b8f97e8aa5a4a6

Authored by David René
1 parent 011bff2a

copy_file in anubis

Showing 1 changed file with 73 additions and 0 deletions   Show diff stats
anubis_dev/library/system/files.anubis 0 → 100644
  1 +
  2 + *Project* Anubis
  3 +
  4 + *Title* generic files functions.
  5 +
  6 + *Copyright* Copyright (c) David René 2006.
  7 +
  8 +
  9 +
  10 +
  11 +
  12 + *Author* David René
  13 + *Created* May 2005
  14 + *Status* Released
  15 + *Overview*
  16 +
  17 +read tools/streams.anubis
  18 +read tools/basis.anubis
  19 +
  20 +public type ResultCopy:
  21 + cant_read_file,
  22 + cant_create_file,
  23 + copy_error,
  24 + copy_file_mode_error,
  25 + copy_ok.
  26 +
  27 +define ResultCopy copy_file(RAddr(Int8)source , WAddr(Int8) target, Int32 length) =
  28 + with read_length = min(length, 65536),
  29 + if read_length = 0 then
  30 + copy_ok
  31 + else if read(source, read_length, 10) is
  32 + {
  33 + failure then copy_error,
  34 + success(buffer) then
  35 + if write( target , buffer) is
  36 + {
  37 + failure then copy_error,
  38 + success(how_many) then
  39 + if read_length = how_many then
  40 + copy_file(source, target, length - how_many)
  41 + else
  42 + copy_error
  43 + }
  44 + }
  45 +.
  46 +
  47 +public define ResultCopy copy_file( String source_file, String target_file) =
  48 + //open the source file
  49 + if (Maybe(RAddr(Int8)))file(source_file, read) is
  50 + {
  51 + failure then cant_read_file,
  52 + success(source) then
  53 + //open the target file
  54 + if (Maybe(RWAddr(Int8)))file(target_file, new) is
  55 + {
  56 + failure then cant_create_file, //nothing to write
  57 + success(target) then
  58 + if copy_file(source, weaken(target), file_size(source)) = copy_ok then
  59 + //we finalize the copy by attributes
  60 + if get_file_mode(source_file) is
  61 + {
  62 + failure then copy_file_mode_error,
  63 + success (file_mode) then
  64 + if set_file_mode(target_file, file_mode) is
  65 + {
  66 + failure then copy_file_mode_error,
  67 + success(_) then copy_ok
  68 + }
  69 + }
  70 + else
  71 + copy_error
  72 + }
  73 + }.
... ...