struct

(** A filename is a string. *)

type filename = string;;

(** A foldername is a string. *)

type foldername = string;;

(** A content is a string. *)

type content = string;;

(** The current value of umask. *)

let current_umask = 
 let old = Unix.umask 0 in 
 let   _ = Unix.umask old in 
 old
;;

(** Create a file if necessary with the given permissions (by default equal to 0o640). *)

let touch ?(perm=0o640) (fname:filename) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREAT] perm) in (Unix.close fd)
;;

(**

Copying

*)


(** Support for copying files. -- From Xavier Leroy and Didier Remy's OS course, Chapter 2. *)

module Copylib = struct

    open Unix;;
    
    let buffer_size = 8192;;
    let buffer = String.create buffer_size;;
    
    let file_copy ?(perm=0o666) ?(flag=O_TRUNC) input_name output_name =
      let fd_in = openfile input_name [O_RDONLY] 0 in
      let fd_out = openfile output_name [O_WRONLYO_CREAT; flag] perm in
      let rec copy_loop () =
        match read fd_in buffer 0 buffer_size with
          0 -> ()
        | r -> ignore (write fd_out buffer 0 r); copy_loop () in
      copy_loop ();
      close fd_in;
      close fd_out;;

end;; (* module Copylib *)

(** Copy a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_copy ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm x) y ;;

(** Append a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_append ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm ~flag:Unix.O_APPEND x) y ;;


(**

Saving strings

*)


(** Write or rewrite the file with the given content. If the file does not exists, it is created with the given permission (set by default to 0o640). *)

let put ?(perm=0o640) (fname:filename) (x:content) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_TRUNC] perm) in 
  let n = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(** Alias for put. *)

let rewrite = put;;

(** Similar to the function put described above, but the content is appended instead of rewrited. If the file doesn't exists, it is created with the given permissions (set by default to 0o640). *)

let append ?(perm=0o640) (fname:filename) (x:content) = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_APPEND] perm) in 
  let n  = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(**

Loading strings

*)


(** Return the whole content (caution!) of the file as a string. Use only for small files. Great for making pipelines. For instance, the following pipeline catches the first line of /etc/fstab containing the substring "hda1":
# "/etc/fstab" => ( cat || String.to_list || Str.grep ".*hda1.*" || hd ) 
*)

let rec cat (fname:filename) = 
  let fd = (Unix.openfile fname [Unix.O_RDONLY] 0o640) in 
  let len = 16*1024 in
  let buff = String.create len in
  let rec boucle () = 
    begin
    let n = (Unix.read fd buff 0 len) in
    let s = String.sub buff 0 n in
    if (n<len) then s
    else s^(boucle ())
    end in
  boucle ()
;;


(**

Temporary files

*)


(** Support for this section. *)

module Templib = struct

(** General function for creating temporary files or directories in a parent directory with some permissions and with a prefix and suffix for the name. The function returns the name of the created file or directory. *)

let rec temp_name ~(dir:bool) ~(perm:Unix.file_perm) ~(parent:string) ~(prefix:string) ~(suffix:string) () = 
  begin
   let rnd = Random.int (1024*1024*1023)&nra" rel="Chapter" href="UnixExtra.html"> UnixExtra.Extra struct
in
   if (Sys.file_exists candidate) then (temp_name ~dir ~perm  ~parent ~prefix ~suffix ())
   else 
     begin
         if dir then (Unix.mkdir candidate perm) 
         else (touch candidate ~perm) ;
         candidate
     end
  end
;;
end
./usr/share/doc/libocamlbricks-ocaml-dev/html/api/code_UnixExtra.Extra.html0000644000000000000000000012651111027341643025633 0ustar rootroot UnixExtra.Extra struct

(** A filename is a string. *)

type filename = string;;

(** A foldername is a string. *)

type foldername = string;;

(** A content is a string. *)

type content = string;;

(** The current value of umask. *)

let current_umask = 
 let old = Unix.umask 0 in 
 let   _ = Unix.umask old in 
 old
;;

(** Create a file if necessary with the given permissions (by default equal to 0o640). *)

let touch ?(perm=0o640) (fname:filename) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREAT] perm) in (Unix.close fd)
;;

(**

Copying

*)


(** Support for copying files. -- From Xavier Leroy and Didier Remy's OS course, Chapter 2. *)

module Copylib = struct

    open Unix;;
    
    let buffer_size = 8192;;
    let buffer = String.create buffer_size;;
    
    let file_copy ?(perm=0o666) ?(flag=O_TRUNC) input_name output_name =
      let fd_in = openfile input_name [O_RDONLY] 0 in
      let fd_out = openfile output_name [O_WRONLYO_CREAT; flag] perm in
      let rec copy_loop () =
        match read fd_in buffer 0 buffer_size with
          0 -> ()
        | r -> ignore (write fd_out buffer 0 r); copy_loop () in
      copy_loop ();
      close fd_in;
      close fd_out;;

end;; (* module Copylib *)

(** Copy a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_copy ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm x) y ;;

(** Append a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_append ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm ~flag:Unix.O_APPEND x) y ;;


(**

Saving strings

*)


(** Write or rewrite the file with the given content. If the file does not exists, it is created with the given permission (set by default to 0o640). *)

let put ?(perm=0o640) (fname:filename) (x:content) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_TRUNC] perm) in 
  let n = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(** Alias for put. *)

let rewrite = put;;

(** Similar to the function put described above, but the content is appended instead of rewrited. If the file doesn't exists, it is created with the given permissions (set by default to 0o640). *)

let append ?(perm=0o640) (fname:filename) (x:content) = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_APPEND] perm) in 
  let n  = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(**

Loading strings

*)


(** Return the whole content (caution!) of the file as a string. Use only for small files. Great for making pipelines. For instance, the following pipeline catches the first line of /etc/fstab containing the substring "hda1":
# "/etc/fstab" => ( cat || String.to_list || Str.grep ".*hda1.*" || hd ) 
*)

let rec cat (fname:filename) = 
  let fd = (Unix.openfile fname [Unix.O_RDONLY] 0o640) in 
  let len = 16*1024 in
  let buff = String.create len in
  let rec boucle () = 
    begin
    let n = (Unix.read fd buff 0 len) in
    let s = String.sub buff 0 n in
    if (n<len) then s
    else s^(boucle ())
    end in
  boucle ()
;;


(**

Temporary files

*)


(** Support for this section. *)

module Templib = struct

(** General function for creating temporary files or directories in a parent directory with some permissions and with a prefix and suffix for the name. The function returns the name of the created file or directory. *)

let rec temp_name ~(dir:bool) ~(perm:Unix.file_perm) ~(parent:string) ~(prefix:string) ~(suffix:string) () = 
  begin
   let rnd = Random.int (1024*1024*1023)&nra" rel="Chapter" href="UnixExtra.html"> UnixExtra.Extra struct
in
   if (Sys.file_exists candidate) then (temp_name ~dir ~perm  ~parent ~prefix ~suffix ())
   else 
     begin
         if dir then (Unix.mkdir candidate perm) 
         else (touch candidate ~perm) ;
         candidate
     end
  end
;;
end
./usr/share/doc/libocamlbricks-ocaml-dev/html/api/code_UnixExtra.Extra.html0000644000000000000000000012651111027341643025633 0ustar rootroot UnixExtra.Extra struct

(** A filename is a string. *)

type filename = string;;

(** A foldername is a string. *)

type foldername = string;;

(** A content is a string. *)

type content = string;;

(** The current value of umask. *)

let current_umask = 
 let old = Unix.umask 0 in 
 let   _ = Unix.umask old in 
 old
;;

(** Create a file if necessary with the given permissions (by default equal to 0o640). *)

let touch ?(perm=0o640) (fname:filename) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREAT] perm) in (Unix.close fd)
;;

(**

Copying

*)


(** Support for copying files. -- From Xavier Leroy and Didier Remy's OS course, Chapter 2. *)

module Copylib = struct

    open Unix;;
    
    let buffer_size = 8192;;
    let buffer = String.create buffer_size;;
    
    let file_copy ?(perm=0o666) ?(flag=O_TRUNC) input_name output_name =
      let fd_in = openfile input_name [O_RDONLY] 0 in
      let fd_out = openfile output_name [O_WRONLYO_CREAT; flag] perm in
      let rec copy_loop () =
        match read fd_in buffer 0 buffer_size with
          0 -> ()
        | r -> ignore (write fd_out buffer 0 r); copy_loop () in
      copy_loop ();
      close fd_in;
      close fd_out;;

end;; (* module Copylib *)

(** Copy a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_copy ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm x) y ;;

(** Append a file into another. Optional permissions (by default 0o640) concern of course the target. *)

let file_append ?(perm=0o640) (x:filename) (y:filename) = Unix.handle_unix_error (Copylib.file_copy ~perm ~flag:Unix.O_APPEND x) y ;;


(**

Saving strings

*)


(** Write or rewrite the file with the given content. If the file does not exists, it is created with the given permission (set by default to 0o640). *)

let put ?(perm=0o640) (fname:filename) (x:content) : unit = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_TRUNC] perm) in 
  let n = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(** Alias for put. *)

let rewrite = put;;

(** Similar to the function put described above, but the content is appended instead of rewrited. If the file doesn't exists, it is created with the given permissions (set by default to 0o640). *)

let append ?(perm=0o640) (fname:filename) (x:content) = 
  let fd = (Unix.openfile fname [Unix.O_CREATUnix.O_WRONLYUnix.O_APPEND] perm) in 
  let n  = String.length x in
  ignore (Unix.write fd x 0 n);
  (Unix.close fd)
;;

(**

Loading strings

*)


(** Return the whole content (caution!) of the file as a string. Use only for small files. Great for making pipelines. For instance, the following pipeline catches the first line of /etc/fstab containing the substring "hda1":
# "/etc/fstab" => ( cat || String.to_list || Str.grep ".*hda1.*" || hd ) 
*)

let rec cat (fname:filename) = 
  let fd = (Unix.openfile fname