From f5adeadb458330b02bb251af3fbf19c6ebec91ef Mon Sep 17 00:00:00 2001 From: tokiclover Date: Thu, 25 Sep 2014 21:38:22 +0200 Subject: [PATCH] modules: added firefox-profile This module maintains firefox home profile '~/.mozilla/firefox/abcd1234.default' in a tmpfs or zram backed filesystem to get a very responsive browser, with a tarball back up saved in '$HOME/.mozilla/firefox'. --- modules/firefox-profile/README.md | 59 +++++++++++++++++ modules/firefox-profile/functions/fhp | 52 +++++++++++++++ modules/firefox-profile/functions/mktmp | 86 ++++++++++++++++++++++++ modules/firefox-profile/init.zsh | 87 +++++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 modules/firefox-profile/README.md create mode 100644 modules/firefox-profile/functions/fhp create mode 100644 modules/firefox-profile/functions/mktmp create mode 100644 modules/firefox-profile/init.zsh diff --git a/modules/firefox-profile/README.md b/modules/firefox-profile/README.md new file mode 100644 index 00000000..d61bc64b --- /dev/null +++ b/modules/firefox-profile/README.md @@ -0,0 +1,59 @@ +Firefox home profile (fhp) +====== + +Maintains firefox home profile '$HOME/.mozilla/firefox/abcd1234.default' on Unix +systems in a tmpfs or zram backed filesystem to get a very responsive browser, +with a tarball back up saved in '$HOME/.mozilla/firefox'. + +Using a compressor like lz4 or lzo(p) make compression/decompression seamless, +so no need to remove the autoload of the function. Although, it can be commented +out to have a empty profile to start with, a 'fhp' command at the prompt will +decompress the tarball instantenously with lz4/lzo(p). + +Settings +-------- + +This module can guess a profile to maintain by looking at the previous directory, +by looking at the previous directory, however a user can set the following: + + zstyle ':prezto:module:firefox-profile' profile 'abcd1234' + zstyle ':prezto:module:firefox-profile' compressor 'lzop -1' + zstyle ':prezto:module:firefox-profile' zsh-hook 'yes' + +The second setting select a compressor to be used instead of default is 'lz4 -1'. + +The last setting add fhp function to zsh-exit-hook, so the profile will be saved +or archived to be more precise before the shell exit. + +Optimizations +------- + +Just make sure to have at least '/tmp' or your system TMPDIR in a tmpfs for the +least to get any benefice of this script with something like: + + /etc/fstab: tmp /tmp tmpfs mode=1777,size=256M,noatime 0 0 + +This will ensure very low latency when browsing the intertubes and removing the +profile in the fly *really* remove any trace of your browsing history because +everything is memory because firing up a 'fhp' command. + +### Zram baccked File System + +Aleternatively, one can pass a root directory in zram backed filesystem: + + zstyle ':prezto:module:firefox-profile' zramdir 'directory' + +### Auto-saving/Auto-start profile + +Of course, one can save a profile at regular time interval using a cron job or +something similar. + +A profile can be auto-started by setting: + + zstyle ':prezto:module:firefox-profile' start-profile 'yes' + +Authors +------- + + - [tokiclover](https://github.com/tokiclover) + diff --git a/modules/firefox-profile/functions/fhp b/modules/firefox-profile/functions/fhp new file mode 100644 index 00000000..50740d2b --- /dev/null +++ b/modules/firefox-profile/functions/fhp @@ -0,0 +1,52 @@ +# +# firefox home profile (fhp) module to maintain the profile +# in a tmpfs or zram backed filesystem +# +# $Header: firefox-profile/functions/fhp Exp $ +# $Aythor: (c) 2011-2014 -tclover Exp $ +# $License: MIT (or 2-clause/new/simplified BSD) Exp $ +# $Version: 3.0 2014/09/25 21:09:26 Exp $ +# + +function die { + local ret=$? + print -P " %F{red}*%f ${(%):-%1x}: $@" >&2 + return $ret +} + +local compressor profile + +zstyle -s ':prezto:module:firefox-profile' profile 'profile' +zstyle -s ':prezto:module:firefox-profile' compressor 'compressor' + +local ext=.tar.$compressor[(w)1] + +pushd -q "$HOME"/.mozilla/firefox || return + +if [[ -f $profile/.unpacked ]] { + mv -f $profile{,.old}$ext + if (( $? )) { + die "failed to override the old tarball" + return + } + + tar -X $profile/.unpacked -Ocp $profile | $=compressor $profile$ext + (( $? )) && die "failed to repack a new tarball" + popd -q + return +} + +if [[ -f $profile$ext ]] { + $compressor -cd $profile$ext | tar -xp && touch $profile/.unpacked + (( $? )) && die "failed to unpack the profile" +} elif [[ -f $profile.old$ext ]] { + $compressor -cd $profile.old$ext | tar -xp && touch $profile/.unpacked + (( $? )) && die "failed to unpack the profile" +} else { + die "no tarball found" +} +popd -q + +# +# vim:fenc=utf-8:ft=zsh:ci:pi:sts=2:sw=2:ts=2: +# diff --git a/modules/firefox-profile/functions/mktmp b/modules/firefox-profile/functions/mktmp new file mode 100644 index 00000000..ef7dc289 --- /dev/null +++ b/modules/firefox-profile/functions/mktmp @@ -0,0 +1,86 @@ +# +# Provide a cheap helper for temporary directory/file creation +# +# $Header: firefox-profile/functions/fhp Exp $ +# $Aythor: (c) 2011-2014 -tclover Exp $ +# $License: MIT (or 2-clause/new/simplified BSD) Exp $ +# $Version: 0.2 2014/09/09 21:09:26 Exp $ +# + +function die { + local ret=$? + print -P " %F{red}*%f ${(%):-%1x}: $@" >&2 + return $ret +} + +function usage { + cat <<-EOH +usage: mktmp [-d|-f] [-m ] [-o ] [-g ] TEMPLATE + -d, --dir create a directory + -f, --file create a file + -o, --owner owner naame + -g, --group group name + -m, --mode <1700> octal mode + -h, --help help/exit +EOH +return +} + +test $# -ge 1 -a -n "$1" || return + +local type temp=-XXXXXX tmpdir=${TMPDIR:-/tmp} +for (( ; $# > 1; )) + case $1 { + (-d|--dir) + type=dir + shift;; + (-f|--file) + type=file + shift;; + (-h|--help) + usage;; + (-m|--mode) + local mode=$2 + shift 2;; + (-o|--owner) + local owner="$2" + shift 2;; + (-g|-group) + local group="$2" + shift 2;; + (*) + die "invalid argument $@" + return;; + } + +test -n "$1" -a "${1%$temp}" != "$1" +if (( $? )) { + die "invalid/null TEMPLATE" + return +} +(( $+commands[uuidgen] )) && temp=$($commands[uuidgen] --random) +local tmp="$tmpdir/${1%$temp}-$temp[1,6]" + +if [[ $type == "dir" ]] { + mkdir -p ${mode:+-m$mode} "$tmp" + if (( $? )) { + die "failed to make $tmp" + return + } +} else { + mkdir -p "$tmp:h" && touch "$tmp" + if (( $? )) { + die "failed to make $tmp" + return + } + (( $+mode )) && chmod $mode "$tmp" +} + +(( $+owner )) && chown $owner "$tmp" +(( $+group )) && chgrp $group "$tmp" + +print "$tmp" + +# +# vim:fenc=utf-8:ft=zsh:ci:pi:sts=2:sw=2:ts=2: +# diff --git a/modules/firefox-profile/init.zsh b/modules/firefox-profile/init.zsh new file mode 100644 index 00000000..1be129e4 --- /dev/null +++ b/modules/firefox-profile/init.zsh @@ -0,0 +1,87 @@ +# +# firefox home profile (fhp) module to maintain the profile +# in a tmpfs or zram backed filesystem +# +# $Header: firefox-profile/init.zsh Exp $ +# $Aythor: (c) 2011-2014 -tclover Exp $ +# $License: MIT (or 2-clause/new/simplified BSD) Exp $ +# $Version: 2.0 2014/09/25 21:09:26 Exp $ +# + +setopt EXTENDED_GLOB +setopt NULL_GLOB + +zstyle -s ':prezto:module:firefox-profile' profile 'profile' +zstyle -s ':prezto:module:firefox-profile' compressor 'compressor' +zstyle -b ':prezto:module:firefox-profile' zsh-hook 'zsh_hook' +zstyle -s ':prezto:module:firefox-profile' zramdir 'zramdir' + +if [[ -z "$compressor" ]] { +: ${compressor:=lz4 -1 -} + zstyle ':prezto:module:firefox-profile' compressor "$compressor" +} + +: ${profile:=${$(print $HOME/.mozilla/firefox/*.default(/)):t}} +if [[ -z $profile ]] { + unset compressor profile zramdir zsh_hook + die "null firefox home profile" + return +} + +[[ ${profile%.default} == $profile ]] && profile+=.default +zstyle ':prezto:module:firefox-profile' profile "$profile" + +if (( $zsh_hook )) { + autoload -Uz add-zsh-hook + add-zsh-hook zshexit fhp +} + +# Initialize the temporary directory with an anonymous function +function { + local ext=.tar.$compressor[(w)1] + local fhpdir="$HOME"/.mozilla/firefox/$profile +: ${TMPDIR:=/tmp/$USER} + + [[ -n $zramdir ]] || [[ -d "$TMPDIR" ]] || mkdir -p -m1700 "$TMPDIR" + if (( $? )) { + die "no suitable directory found" + return + } + + mount | grep -q "$fhpdir" && return + + if [[ ! -f "$fhpdir$ext" ]] || [[ ! -f "$fhpdir.old$ext" ]] { + pushd -q "$fhpdir:h" || return + tar -Ocp $profile | $=compressor $profile$ext + if (( $? )) { + die "failed to pack a new tarball" + return + } + popd -q + } + + local mktmp=mktmp mntdir + (( $+commands[mktemp] )) && mktmp=$commands[mktemp] + + [[ -n $zramdir ]] && + mntdir=$($mktmp -d "$zramdir"/fhp-XXXXXX) || + mntdir=$($mktmp -d "$TMPDIR"/fhp-XXXXXX) + + sudo mount --bind "$mntdir" "$fhpdir" + if (( $? )) { + die "failed to mount $mntdir" + return + } +} + +# Finaly, start the firefox home profile +#(( $+functions[fhp] )) || autoload -Uz fhp +if zstyle -t ':prezto:module:firefox-profile' start-profile; then + fhp +fi + +unset compressor profile zramdir zsh_hook + +# +# vim:fenc=utf-8:ft=zsh:ci:pi:sts=2:sw=2:ts=2: +#