fileset

git mirror of https://ccx.te2000.cz/bzr/fileset
git clone https://ccx.te2000.cz/git/fileset
Log | Files | Refs | README

commit 7f239e85db1b6c64f054a9597718ab049c42a365
parent bf09029e9c7786d70c38eb074a8a427c7d1005fb
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date:   Mon,  4 Jan 2016 12:27:15 +0100

some basic functions for zsh implementation
Diffstat:
Azsh-funcions/confz_fileset_init | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 101 insertions(+), 0 deletions(-)

diff --git a/zsh-funcions/confz_fileset_init b/zsh-funcions/confz_fileset_init @@ -0,0 +1,101 @@ +# vim: ft=zsh noet ts=4 sts=4 sw=4 + +# +# confz functions for file hierarchies and fileset miniformat +# + +zmodload -F zsh/stat b:zstat + +fileset_ftypes=( # convert hex type to a word + C S # unix socket + c S + A L # symlink, could also be 'h' + a L + 8 f # plain file + 6 b # block device file + 4 d # directory + 2 c # character device file + 1 p # FIFO pipe +) + +global -A fileset_stat_cache +global fileset_stat_cur fileset_stat_next_id fileset_stat_cur_type + +fileset_reset_cache(){ + # omit unsetting used variables for now + # probably no particular gain in freeing them + fileset_stat=() + fileset_stat_cur= + fileset_stat_next_id=1 +} + +# runs zstat on $1 if not already in cache +# the result is available as ${(P)fileset_stat_cur} +fileset_stat() { + local id ret ftype + id=${fileset_stat_cache[$1]:-missing} + if [[ $id == missing ]]; then + zstat -L -H fileset_stat_$fileset_stat_next_id $1 + ret=$? + if (($ret == 0)); then + fileset_stat_cur=fileset_stat_$fileset_stat_next_id + fileset_stat[$1]=$fileset_stat_next_id + fileset_stat_next_id=$[ $fileset_stat_next_id + 1 ] + ftype=$(( [##16] ${${(P)fileset_stat_cur}[mode]} >> 12 )) + fileset_stat_cur_type=$fileset_ftypes[$ftype] + else + fileset_stat[$1]=-$ret + fi + return $ret + elif (( $id >= 0 )); then + fileset_stat_cur=fileset_stat_$id + ftype=$(( [##16] ${${(P)fileset_stat_cur}[mode]} >> 12 )) + fileset_stat_cur_type=$fileset_ftypes[$ftype] + return 0 + else + return $[ -$id ] + fi +} + +fileset_resetcmd() { + fileset_reset_cache + "$@" +} + +confz_fs_p_check() { + checkvars filename + local parent + parent=${${vars[filename]}:h} + do_command=( fileset_resetcmd mkdir -p $parent ) + fileset_stat $parent && [[ $fileset_stat_cur_type == d ]] +} + + +confz_fs_type_or_missing_check() { + checkvars filename flags filetype +} + +confz_fs_type_or_missing_do() { + local is_dir + if fileset_stat $vars_filename && [[ $fileset_stat_cur_type == d ]]; then + is_dir=1 + else + is_dir=0 + fi + + if [[ $vars[flags] == *r* ]]; then + if [[ $vars[flags] == *f* ]]; then + rm -rf $vars[filename] + else + rm -r $vars[filename] + fi + elif (($is_dir)); then + rmdir $vars[filename] + else + if [[ $vars[flags] == *f* ]]; then + rm -f $vars[filename] + else + rm $vars[filename] + fi + fi +}