123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215(******************************************************************************)(* ocaml-fileutils: files and filenames common operations *)(* *)(* Copyright (C) 2003-2014, Sylvain Le Gall *)(* *)(* This library is free software; you can redistribute it and/or modify it *)(* under the terms of the GNU Lesser General Public License as published by *)(* the Free Software Foundation; either version 2.1 of the License, or (at *)(* your option) any later version, with the OCaml static compilation *)(* exception. *)(* *)(* This library is distributed in the hope that it will be useful, but *)(* WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *)(* COPYING for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this library; if not, write to the Free Software Foundation, *)(* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *)(******************************************************************************)openFileUtilTypesopenFilePathopenFileUtilMiscopenFileUtilSizeopenFileUtilSTATletcompile_filter?(match_compile=(funsfn->s=fn))flt=letcflt=letreccc=function|True->`Valtrue|False->`Valfalse|Is_dev_block->`Stat(`KindDev_block)|Is_dev_char->`Stat(`KindDev_char)|Is_dir->`Stat(`KindDir)|Is_file->`Stat(`KindFile)|Is_socket->`Stat(`KindSocket)|Is_pipe->`Stat(`KindFifo)|Is_link->`Is_link|Is_set_group_ID->`Stat`Is_set_group_ID|Has_sticky_bit->`Stat`Has_sticky_bit|Has_set_user_ID->`Stat`Has_set_user_ID|Is_readable->`Stat`Is_readable|Is_writeable->`Stat`Is_writeable|Is_exec->`Stat`Is_exec|Size_not_null->`Stat(`Size(`Bigger,B0L))|Size_bigger_thansz->`Stat(`Size(`Bigger,sz))|Size_smaller_thansz->`Stat(`Size(`Smaller,sz))|Size_equal_tosz->`Stat(`Size(`Equal,sz))|Size_fuzzy_equal_tosz->`Stat(`Size(`FuzzyEqual,sz))|Is_owned_by_user_ID->`Stat(`Is_owned_by_user_ID(Unix.geteuid()))|Is_owned_by_group_ID->`Stat(`Is_owned_by_group_ID(Unix.getegid()))|Exists->`Stat`Exists|Is_newer_thanfn1->`Stat(`Newer(statfn1).modification_time)|Is_older_thanfn1->`Stat(`Older(statfn1).modification_time)|Is_newer_than_date(dt)->`Stat(`Newerdt)|Is_older_than_date(dt)->`Stat(`Olderdt)|Has_extensionext->`Has_extensionext|Has_no_extension->`Has_no_extension|Is_current_dir->`Is_current_dir|Is_parent_dir->`Is_parent_dir|Basename_iss->`Basename_iss|Dirname_iss->`Dirname_iss|Customf->`Customf|Matchstr->`Custom(match_compilestr)|And(flt1,flt2)->beginmatchccflt1,ccflt2with|`Valtrue,cflt|cflt,`Valtrue->cflt|`Valfalse,_|_,`Valfalse->`Valfalse|cflt1,cflt2->`And(cflt1,cflt2)end|Or(flt1,flt2)->beginmatchccflt1,ccflt2with|`Valtrue,_|_,`Valtrue->`Valtrue|`Valfalse,cflt|cflt,`Valfalse->cflt|cflt1,cflt2->`Or(cflt1,cflt2)end|Notflt->beginmatchccfltwith|`Valb->`Val(notb)|cflt->`Notcfltendinccfltinletneed_statL,need_stat=letrecdfs=function|`Val_|`Has_extension_|`Has_no_extension|`Is_current_dir|`Is_parent_dir|`Basename_is_|`Dirname_is_|`Custom_->false,false|`Stat_->true,false|`Is_link->false,true|`And(cflt1,cflt2)|`Or(cflt1,cflt2)->letneed_stat1,need_statL1=dfscflt1inletneed_stat2,need_statL2=dfscflt2inneed_stat1||need_stat2,need_statL1||need_statL2|`Notcflt->dfscfltindfscfltin(* Compiled function to return. *)fun?st_opt?stL_optfn->letst_opt=ifneed_stat&&st_opt=NonethenbegintrymatchstL_optwith|Somestwhennotst.is_link->stL_opt|_->Some(statfn)withFileDoesntExist_->Noneendelsest_optinletstL_opt=ifneed_statL&&stL_opt=Nonethenbegintrymatchst_optwith|Somestwhennotst.is_link->st_opt|_->Some(stat~dereference:truefn)withFileDoesntExist_->NoneendelsestL_optinletreceval=function|`Valb->b|`Has_extensionext->begintrycheck_extensionfnextwithFilePath.NoExtension_->falseend|`Has_no_extension->begintrylet_str:filename=chop_extensionfninfalsewithFilePath.NoExtension_->trueend|`Is_current_dir->is_current(basenamefn)|`Is_parent_dir->is_parent(basenamefn)|`Basename_isbn->(FilePath.compare(basenamefn)bn)=0|`Dirname_isdn->(FilePath.compare(dirnamefn)dn)=0|`Customf->ffn|`State->beginmatchstL_opt,ewith|Some_,`Exists->true|SomestL,`Kindknd->stL.kind=knd|SomestL,`Is_set_group_ID->stL.permission.group.sticky|SomestL,`Has_sticky_bit->stL.permission.other.sticky|SomestL,`Has_set_user_ID->stL.permission.user.sticky|SomestL,`Size(cmp,sz)->beginletdiff=size_comparestL.sizeszinmatchcmpwith|`Bigger->diff>0|`Smaller->diff<0|`Equal->diff=0|`FuzzyEqual->(size_compare~fuzzy:truestL.sizesz)=0end|SomestL,`Is_owned_by_user_IDuid->uid=stL.owner|SomestL,`Is_owned_by_group_IDgid->gid=stL.group_owner|SomestL,`Is_readable->letperm=stL.permissioninperm.user.read||perm.group.read||perm.other.read|SomestL,`Is_writeable->letperm=stL.permissioninperm.user.write||perm.group.write||perm.other.write|SomestL,`Is_exec->letperm=stL.permissioninperm.user.exec||perm.group.exec||perm.other.exec|SomestL,`Newerdt->stL.modification_time>dt|SomestL,`Olderdt->stL.modification_time<dt|None,_->falseend|`Is_link->beginmatchst_optwith|Somest->st.is_link|None->falseend|`And(cflt1,cflt2)->(evalcflt1)&&(evalcflt2)|`Or(cflt1,cflt2)->(evalcflt1)||(evalcflt2)|`Notcflt->not(evalcflt)inevalcfltlettest?match_compiletst=letctst=compile_filter?match_compiletstinfunfln->ctst(solve_dirnamefln)letfilterfltlst=List.filter(testflt)lstlettest_exists=test(Or(Exists,Is_link))