Module FileUtil


module FileUtil: sig .. end
A module to provide the core system utilities to manipulate file and directory. All function nearly match common unix utilities ( but try to be more portable )


Types and exceptions


type filename = string 
The base type of the module
exception SizeInvalid
exception FileDoesntExist
exception RecursiveLink of filename
exception RmDirNotEmpty of filename
exception MkdirMissingComponentPath of filename
exception MkdirDirnameAlreadyUsed of filename
exception CpCannotCopy of filename
exception CpNoSourceFile of filename
exception CpCannotCopyFilesToFile of filename list * filename
exception CpCannotCopyDir of filename
exception MvNoSourceFile

type action_link =
| Follow (*We consider links as simple directory ( it is dangerous )*)
| Skip (*Just skip it*)
| SkipInform of (filename -> unit) (*Skip and execute an action*)
| AskFollow of (filename -> bool) (*Ask and wait for input : true means follow and false means skip*)
The policy concerning the links which are directory

type interactive =
| Force (*Do it anyway*)
| Ask of (filename -> bool) (*Promp the user*)
For certain command, you should need to ask the user wether or not he does want to do some action. Provide the function to Ask or Force the action

type size =
| TB of float (*Terra bytes*)
| GB of float (*Giga bytes*)
| MB of float (*Mega bytes*)
| KB of float (*Kilo bytes*)
| B of float (*Bytes*)
Size type

type kind =
| Dir
| File
| Dev_char
| Dev_block
| Link
| Fifo
| Socket
Kind of file. This set is a combination of all POSIX file, some of them doesn't exist at all on certain file system

type base_permission = {
   sticky : bool;
   exec : bool;
   write : bool;
   read : bool;
}
Base permission. This is the base type for one set of permission

type permission = {
   user : base_permission;
   group : base_permission;
   other : base_permission;
}
Permission. All the base permission of a file

type stat = {
   kind : kind;
   is_link : bool;
   permission : permission;
   size : size;
   owner : int;
   group_owner : int;
   access_time : float;
   modification_time : float;
   creation_time : float;
}
Information about a file. This type is derived from Unix.stat

type test_file =
| Is_dev_block (*FILE exists and is block special*)
| Is_dev_char (*FILE exists and is character special*)
| Is_dir (*FILE exists and is a directory*)
| Exists (*FILE exists*)
| Is_file (*FILE exists and is a regular file*)
| Is_set_group_ID (*FILE exists and is set-group-ID*)
| Has_sticky_bit (*FILE exists and has its sticky bit set*)
| Is_link (*FILE exists and is a symbolic link*)
| Is_pipe (*FILE exists and is a named pipe*)
| Is_readable (*FILE exists and is readable*)
| Is_writeable (*FILE exists and is writeable*)
| Size_not_null (*FILE exists and has a size greater than zero*)
| Size_bigger_than of size (*FILE exists and has a size greater than given size*)
| Size_smaller_than of size (*FILE exists and has a size smaller than given size*)
| Size_equal_to of size (*FILE exists and has the same size as given size*)
| Size_fuzzy_equal_to of size (*FILE exists and has approximatively the same size as given size*)
| Is_socket (*FILE exists and is a socket*)
| Has_set_user_ID (*FILE exists and its set-user-ID bit is set*)
| Is_exec (*FILE exists and is executable*)
| Is_owned_by_user_ID (*FILE exists and is owned by the effective user ID*)
| Is_owned_by_group_ID (*FILE exists and is owned by the effective group ID*)
| Is_newer_than of filename (*FILE1 is newer (modification date) than FILE2*)
| Is_older_than of filename (*FILE1 is older than FILE2*)
| Is_newer_than_date of float (*FILE is newer than given date*)
| Is_older_than_date of float (*FILE is older than given date*)
| And of test_file * test_file (*Result of TEST1 and TEST2*)
| Or of test_file * test_file (*Result of TEST1 or TEST2*)
| Not of test_file (*Result of not TEST*)
| Match of string (*Match regexp*)
| True (*Always true*)
| False (*Always false*)
| Has_extension of string (*Check extension*)
| Has_no_extension (*Check absence of extension*)
| Is_parent_dir (*Is it the parent dir*)
| Is_current_dir (*Is it the current dir*)
| Basename_is of filename (*Check the basename*)
| Dirname_is of filename (*Check the dirname*)
| Custom of (filename -> bool) (*Custom operation on filename*)
Pattern you can use to test file

File utils specification


module type OPERATION_REGEXP = sig .. end
Abstraction of regexp operation
module type FILE_UTILS = sig .. end
Operation available.
module type META_FILE_UTILS = functor (OperationRegexp : OPERATION_REGEXP) -> FILE_UTILS
Meta structure use to define regexp generic file operation

Implementation



Classical permission


val permission_of_int : int -> permission
Understand the POSIX permission integer norm
val int_of_permission : permission -> int
Return the POSIX integer permission associated to the permission

Size operation


val size_convert_down : size -> size
Convert to the upper unit a size
val size_convert_up : size -> size
Convert to the smaller unit a size
val size_compare_unit : size -> size -> int
Compare two units of size : classification of size is To, Go, Mo, Ko, O , with To being the bigger unit
val size_to_same_unit : size -> size -> size
size_to_same_unit sz1 sz2 : convert sz2 to the unit of sz1
val size_to_To : size -> size
Convert a size to To
val size_to_Go : size -> size
Convert a size to Go
val size_to_Mo : size -> size
Convert a size to Mo
val size_to_Ko : size -> size
Convert a size to Ko
val size_to_O : size -> size
Convert a size to O
val size_apply_operation : (float -> float -> float) -> size -> size -> size
Apply an operation to a size : the two size are converted * to the same unit and the function is applied to their value
val size_compare : ?fuzzy:bool -> size -> size -> int
Compare two size, using the classical compare function. The two size * are converted to the same unit before. If fuzzy is set to true, the * comparison is done on the floor value of the two size.
val size_add : size -> size -> size
size_add sz1 sz2 : add sz1 to sz2, result is in the unit of sz1
val size_sub : size -> size -> size
size_sub sz1 sz2 : substract sz1 to sz2, result is in the unit of sz1
val string_of_size : ?fuzzy:bool -> size -> string
Convert a value to a string representation. If fuzzy is set to true, only * consider the most significant unit
module GenericUtil: 
functor (OperationRegexp : OPERATION_REGEXP) -> sig .. end
Implementation of META_FILE_UTILS

Concrete file utilities


module StrUtil: GenericUtil(sig
type t = Str.regexp 
val compile : string -> Str.regexp
val test : Str.regexp -> string -> bool
end)
Implementation using regexp