files.anubis 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

 *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
read system/data_io.anubis
read system/string.anubis

public define String      normalize_path(String given_path).
public define Maybe(One)  flush(ByteArray buffer, WStream target).

public type ResultCopy:
  cant_read_file,
  cant_create_file,
  copy_error,
  copy_file_mode_error,
  copy_file_times_error, 
  copy_ok(Int written).         //copy ok of 'written' bytes

define ResultCopy 
  _copy_file
  (
    RStream source, 
    WStream target, 
    Int   so_far
  ) =

  if read(source, 65536, 10) is
  {
    error     then copy_error,
    timeout   then copy_error,
    ok(buffer)then
      with len = length(buffer),
      if len = 0 then 
        copy_ok(so_far)
      else
        if flush(buffer, target) is
        {
          failure    then copy_error,
          success(_) then _copy_file(source, target, so_far + len)
        }
  }.

define ResultCopy 
  _copy_file
  (
    RStream source, 
    WStream target,
    Int   remaining,
    Int   so_far
  ) =
  with read_length = min(remaining, 65536),
  if read_length = 0 then 
    copy_ok(so_far)
  else if read(source, read_length, 10) is
  {
    error     then copy_error,
    timeout   then copy_error,
    ok(buffer)then
      if flush(buffer, target) is
      {
        failure    then copy_error,
        success(_) then _copy_file(source, target, remaining - read_length, so_far + read_length)
      }
  }.
  
public define ResultCopy 
  copy_file
  (
    RStream source, 
    WStream target, 
  ) =
  _copy_file(source, target, 0).

public define ResultCopy 
  copy_file
  (
    RStream source, 
    WStream target,
    Int   length
  ) =
  _copy_file(source, target, length, 0).
  
define ResultCopy 
  copy_file_data
  ( 
    String source_file, 
    String target_file
  ) =
  //open the source file
  if (Maybe(RStream))file(source_file, read) is
  {
     failure          then cant_read_file,
     success(source)  then 
    //open the target file
    if (Maybe(RWStream))file(target_file, new) is
    {
      failure         then println("can't create target file "+target_file);cant_create_file, //nothing to write 
      success(target) then copy_file(source, weaken(target))
    }
  }.
  
public define ResultCopy 
  append_file_to_file
  ( 
    String source_file, 
    String target_file
  ) =
  //open the source file
  if (Maybe(RStream))file(source_file, read) is
  {
     failure          then cant_read_file,
     success(source)  then 
    //open the target file
    if (Maybe(RWStream))file(target_file, append) is
    {
      failure         then println("can't create target file"+target_file);cant_create_file, //nothing to write 
      success(target) then copy_file(source, weaken(target))
    }
  }.

  
public define ResultCopy 
  copy_file
  ( 
    String the_source_file, 
    String the_target_file
  ) =

  // copy only the data of the file
  with source_file = normalize_path(the_source_file),
       target_file = normalize_path(the_target_file),
  with result = copy_file_data(source_file, target_file),
  if result is copy_ok(size) 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 print("get get_file_mode error\n");copy_file_mode_error,
                success (file_mode) then 
                if set_file_mode(target_file, file_mode) is
                {
                  failure     then print("set set_file_mode error\n");copy_file_mode_error,
                  success(_)  then copy_ok(size)
                } 
              }
          }
    }
  else
    result .

    
public define Bool
  move_file
  (
    String  source_file,
    String  target_file
  )=
  
  with result = copy_file(source_file, target_file),
  if result is copy_ok(_) then
    remove(source_file)
  else
    false.
  
/** copy file into the socket
 */ 
public define Maybe(One)
  copy_file_to_stream
  (
    RStream   source_file, 
    RWStream  socket, 
    Int     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
  {
    error     then failure,
    timeout   then  failure,
    ok(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
    }
  }.

/** copy file into the socket
 */ 
public define Maybe(One)
  copy_file_to_Connection
  (
    Connection  source_file, 
    Connection  socket, 
    Int       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
  {
    error     then failure,
    timeout   then  failure,
    ok(buffer)then
    if write(socket, buffer) is
    {
      failure           then failure,
      success(how_many) then 
        if read_length = how_many then 
          copy_file_to_Connection(source_file, socket, length - how_many)
        else 
          failure
    }
  }.

define Maybe(One)
  flush
  (
    ByteArray buffer,
    WStream   target
  )=
  if write( target , buffer) is
  {
    failure           then failure,
    success(nb_write) then
      with buffer_size = length(buffer),      
      if nb_write = buffer_size then
        success(unique)
      else
        with new_buffer = extract(buffer, nb_write, buffer_size),
        flush(new_buffer, target)
  }.
  
public define ResultCopy 
  copy_Data_IO_to_Stream
  (
    Data_IO source, 
    WStream target,
    Int   so_far
  ) =
  if read_bytes(source, 65536) is
  {
    failure         then copy_error,
    time_out        then copy_error, 
    success(buffer) then 
      if flush( buffer, target ) is
      {
        failure     then copy_error,
        success(_)  then copy_Data_IO_to_Stream(source, target, so_far + 65536)
      },
      
    truncated(buffer)       then
      with len = length(buffer),
      if len = 0 then
        copy_ok(so_far)
      else 
        if flush( buffer, target ) is
        {
          failure     then copy_error,
          success(_)  then copy_ok(so_far + len)
        }
  }.

define ResultCopy
  copy_Data_IO_List_to_Stream
  (
    List(Data_IO) io_list,
    WStream       target,
    Int         so_far
  )=
  if io_list is
  {
    []        then copy_ok(so_far),
    [ h . t ]  then
      if rewind(h)(unique) then
        with result = copy_Data_IO_to_Stream(h, target, 0),
        if result is copy_ok(written) then 
          copy_Data_IO_List_to_Stream(t, target, so_far + written)
        else
          result
      else
        copy_error
  }.
  
public define ResultCopy
  copy_Data_IO_List_to_Stream
  (
    List(Data_IO) io_list,
    WStream       target
  )=
  if io_list is
  {
    []        then copy_ok(0),
    [ h . t ]  then copy_Data_IO_List_to_Stream(io_list, target, 0)
  }.

public define ResultCopy
  copy_Data_IO_List_to_Stream
  (
    List(Data_IO) io_list,
    String        target_file
  )=
  with norm_target_file = normalize_path(target_file),
  if (Maybe(RWStream))file(norm_target_file, new) is
  {
      failure         then print("copy_Data_IO_to_file: can't create '"+norm_target_file+"' file\n");cant_create_file, //nothing to write 
      success(target) then copy_Data_IO_List_to_Stream(io_list, weaken(target))
  }.
  
public define ResultCopy
  copy_Data_IO_to_file
  (
    Data_IO io,
    String  target_file
  )=
  with norm_target_file = normalize_path(target_file),
  if (Maybe(RWStream))file(norm_target_file, new) is
  {
      failure         then print("copy_Data_IO_to_file: can't create '"+norm_target_file+"' file\n");cant_create_file, //nothing to write 
      success(target) then copy_Data_IO_to_Stream(io, weaken(target), 0)
  }.
  
define String
  normalize_path
  (
    List(Word8)  path,
    List(Word8)  so_far,
    Bool        in_slash
  )=
  if path is
  {
    []        then implode(reverse(so_far)),
    [h . t ]  then
      if h = '\\'  & in_slash then
        normalize_path(t, so_far, true)
      else if h = '\\' & in_slash = false then
        normalize_path(t, [ '/' . so_far], true)
      else if h = '/' & in_slash then
        normalize_path(t, so_far, true)
      else if h = '/' & in_slash = false then
        normalize_path(t, [h . so_far], true)
      else
        normalize_path(t, [h . so_far], false)
  }.


  /** Normalize path, change the anti-slash in the file_path string by slash. Also
   * the double slash are remove to be only slash at the end
   * For example the given path to normalize is:
   *          "\general_dir\/my_dir/foo.foo"
   * 
   * result of normalize path will be   "/general_dir/my_dir/foo.foo"
   */ 
public define String 
  normalize_path
  (
    String given_path
  )=
  normalize_path(explode(given_path),[], false).


define Maybe(String)
  normalize_path
  (
    List(String)  folders,
    List(String)  so_far,
  )=
  if folders is
  {
    []    then success(join("/", reverse(so_far))),
    [h . t] then
      if h = "" | h = "." then
        if so_far is
        {
          [] then normalize_path(t, [h . so_far]),   // the first '/'or '.' have to be added to keep absolute or relative path
          [_ . _] then
            if t is 
            {
              []    then if h = "" then normalize_path(t, [h . so_far]) // the last '/' should be keeped too
                                   else normalize_path(t, so_far),      // but not the '.'
              [_ . _] then normalize_path(t, so_far)
            }
        }
      else if h = ".." then 
        if so_far is
        {
          []    then failure,
          [_ . h2] then normalize_path(t, h2)
        }
      else
        normalize_path(t, [h . so_far])
  }.

public define Maybe(String)
  normalize_path_ex
  (
    String given_path
  )=
  normalize_path(split_by_tokens(given_path, ['/', '\\']),[]).
  
define Int 
  last_char
  (
    List(Word8) path,
    Word8       token,
    Int         last_token,
    Int         current_pos
  ) = 
  if path is
  {
    []        then last_token,
    [h . t ]  then
      if h = token then
        last_char(t, token, current_pos, current_pos + 1)
      else  
        last_char(t, token, last_token, current_pos + 1)
  }.
  
  /** extract dir, extract the directory path from the given path. The given path
   * can have file in the tail of it.
   * the result of extract_dir apply on "/general_dir/my_dir/foo.foo" is
   * "/general_dir/my_dir/"
   */
   
public define String
  extract_dir
  (
    String given_path
  )=
  with path = normalize_path(given_path),
  with pos = last_char(explode(path),'/', 0, 0),
  if sub_string(path, 0, pos+1) is
  {
    failure           then given_path,
    success(new_path) then new_path
  } .

public define List(String)
  extract_dir
  (
    String given_path
  )=
  split_by_token(extract_dir(given_path), '/').
  
public define One
   make_directories
     (
       String dir
     ) =
     with list_dir = (List(String))extract_dir(dir),
     //println("try to make dir "+dir);
     make_directories("", list_dir).
    
public define String 
  extract_file_name
  (
    String  given_path
  )=
  with path = normalize_path(given_path),
        len = length(path),
        pos = last_char(explode(path), '/', -1, 0),
   file_len = len - (pos+1),
  if sub_string(path, pos + 1, file_len) is
  {
    failure             then "",
    success(file_name)  then file_name
  }.

public define String 
  remove_extension
  (
    String  given_path
  )=
  with path = normalize_path(given_path),
       len = length(path),
       pos = last_char(explode(path), '.', -1, 0),
  //pos = -1 mean no dot found in the path, hence no extension need to remove
  if pos = -1 then
    given_path
  else
    if sub_string(path, 0, pos) is
    {
      failure             then "",
      success(file_name)  then file_name
    }.
  
public define Bool
  is_dir
  (
    String  given_path
  ) =
  with path = normalize_path(given_path),
        len = length(path),
  if sub_string(path, len - 1, 1) is
  {
    failure     then false,
    success(c)  then
      if c = "/" then 
        true
      else
        false
  }.
  
define Bool
  remove_files
  (
    String       path,
    List(String) files
  )=
  if files is
  {
    []        then true,  //we have finished
    [file_name . t ]  then
      if file_name = "." | file_name = ".." then 
        remove_files(path, t)
      else if remove(path + file_name) then
//        println("removing "+path+file_name);
        remove_files(path, t)
      else
        println("can't remove file ["+path+file_name+"]"); false
  }.

  /**
   * Emptying directory, remove all files matching with the given mask
   * If something wrong happend during the deletion of files the function
   * may return false otherwise true
   */
public define Bool
  emptying_directory
  (
    String  path,
    String  mask
  )=
  with norm_path = normalize_path(path),
  remove_files(norm_path +"/", directory_list(norm_path, mask)).


/**
 * Check if the specified path is a relative or an absolute path
 * Returns true if the path is relative.
 */
public define Bool
  is_relative
  (
    String path
  ) =
  if nth(0, path) is
  {
    failure then true,    // empty path
    success(c1) then
      if c1 = '/' | c1 = '\\' then false
      else if (c1 >=+ 'A' & c1 +=< 'Z') | (c1 >=+ 'a' & c1 +=< 'z') then
        if nth(1, path) is
        {
          failure then true,    // one letter path ('c')
          success(c2) then
            if c2 = ':' then
              if nth(2, path) is
              {
                failure then true,
                success(c3) then
                  if c3 = '/' | c3 = '\\' then false
                                          else true    // strange path 'C:xxxxx'
              }
            else true
        }
      else true
  }.
  
/**
 * Check if the specified path is a relative or an absolute path
 * Returns true if the path is relative.
 */
public define inline Bool
  is_absolute
  (
    String path
  ) =
  if is_relative(path) then false else true.


public define Maybe(String)
  make_relative_path
  (
    String root,
    String path,
  ) =
  if is_relative(path) & is_absolute(root) then failure
  else
    if normalize_path_ex(root) is 
    {
      failure then failure,
      success(root_norm) then
        if normalize_path_ex(path) is 
        {
          failure then failure,
          success(path_norm) then
            with root_folders = map_select((String s) |-> if s = "" then failure else success(s), split(root_norm, '/')),
                 path_folders = map_select((String s) |-> if s = "" then failure else success(s), split(path_norm, '/')),
                 compare = (List(String) fold1, List(String) fold2, List(String) so_far) |-compare-> 
                            if fold1 is
                            {
                              [] then success(join("/", reverse(so_far) + fold2)),
                              [h . t] then
                                if fold2 is
                                {
                                  [] then compare(t, [], [".." . so_far]),
                                  [h2 . t2] then 
                                    if h = h2 then
                                      compare(t, t2, so_far)
                                    else
                                      failure
                                }
                            },
            compare(root_folders, path_folders, [])
        }
    }.