Ticket #41734: patch-bin-bitpocket.diff

File patch-bin-bitpocket.diff, 9.2 KB (added by cedric.simillion@…, 10 years ago)

patch file

  • bin/bitpocket

    old new  
    1818[ -f "$CFG_FILE" ] && . "$CFG_FILE"
    1919
    2020# Test for GNU versions of core utils. Bail if non-GNU.
    21 sed --version >/dev/null 2>/dev/null
     21gsed --version >/dev/null 2>/dev/null
    2222if [ $? -ne 0 ]; then
    2323    echo "fatal: It seems like you are running non-GNU versions of coreutils."
    2424    echo "       It is currently unsafe to use bitpocket with this setup,"
     
    7070    exit 128
    7171  fi
    7272
    73   mkdir "$DOT_DIR"
     73  gmkdir "$DOT_DIR"
    7474
    75   cat <<EOF > "$CFG_FILE"
     75  gcat <<EOF > "$CFG_FILE"
    7676## Host and path of central storage
    7777REMOTE_HOST=$1
    7878REMOTE_PATH="$2"
     
    8989# SLOW_SYNC_STOP_CMD="notify-send 'BitPocket sync finished'"
    9090EOF
    9191
    92   echo "Initialized bitpocket directory at `pwd`"
     92  echo "Initialized bitpocket directory at `gpwd`"
    9393  echo "Please have a look at the config file ($DOT_DIR/config)"
    9494}
    9595
     
    112112  on_slow_sync_start
    113113
    114114  # Check what has changed
    115   touch "$STATE_DIR/tree-prev"
    116   touch "$STATE_DIR/added-prev"
     115  gtouch "$STATE_DIR/tree-prev"
     116  gtouch "$STATE_DIR/added-prev"
    117117
    118118  # Save before-sync state
    119119  # Must be done with rsync itself (rather than find) to respect includes/excludes
    120120  # Order of includes/excludes/filters is EXTREMELY important
    121121  echo "# Saving current state and backing up files (if needed)"
    122   echo "  | Root dir: $(pwd)"   
     122  echo "  | Root dir: $(gpwd)"   
    123123  rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \
    124       | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort > "$STATE_DIR/tree-current"
     124      | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort > "$STATE_DIR/tree-current"
    125125
    126126  # Prevent bringing back locally deleted files or removing new local files
    127   cp -f "$STATE_DIR/added-prev" "$TMP_DIR/fetch-exclude"
    128   sort "$STATE_DIR/tree-prev" "$STATE_DIR/tree-current" | uniq -u >> "$TMP_DIR/fetch-exclude"
     127  gcp -f "$STATE_DIR/added-prev" "$TMP_DIR/fetch-exclude"
     128  gsort "$STATE_DIR/tree-prev" "$STATE_DIR/tree-current" | guniq -u >> "$TMP_DIR/fetch-exclude"
    129129
    130130  # It is difficult to only create the backup directory if needed; instead
    131131  # we always create it, but remove it if it is empty afterwards.
    132   mkdir --parents $DOT_DIR/backups/$TIMESTAMP
     132  gmkdir --parents $DOT_DIR/backups/$TIMESTAMP
    133133
    134134  # Determine what will be fetched from server and make backup
    135135  # copies of any local files to be deleted or overwritten.
    136136  # Order of includes/excludes/filters is EXTREMELY important
    137137  rsync --dry-run \
    138138        -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . \
    139       | grep "^[ch<>\.\*][f]\|\*deleting" | sed "s:^\S*\s*::" | sed "s:\(.*\):if [ -f \"\1\" ]; then cp --parents \"\1\" $DOT_DIR/backups/$TIMESTAMP; fi:" | sh || die "BACKUP"
    140   [ "$(ls -A $DOT_DIR/backups/$TIMESTAMP)" ] && echo "  | Some files were backed up to $DOT_DIR/backups/$TIMESTAMP"
    141   [ "$(ls -A $DOT_DIR/backups/$TIMESTAMP)" ] || rmdir $DOT_DIR/backups/$TIMESTAMP
     139      | grep "^[ch<>\.\*][f]\|\*deleting" | gsed "s:^\S*\s*::" | gsed "s:\(.*\):if [ -f \"\1\" ]; then gcp --parents \"\1\" $DOT_DIR/backups/$TIMESTAMP; fi:" | sh || die "BACKUP"
     140  [ "$(gls -A $DOT_DIR/backups/$TIMESTAMP)" ] && echo "  | Some files were backed up to $DOT_DIR/backups/$TIMESTAMP"
     141  [ "$(gls -A $DOT_DIR/backups/$TIMESTAMP)" ] || grmdir $DOT_DIR/backups/$TIMESTAMP
    142142
    143143  # Actual fetch
    144144  # Pulling changes from server
    145145  # Order of includes/excludes/filters is EXTREMELY important
    146146  echo
    147147  echo "# Pulling changes from server"
    148   rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | sed "s/^/  | /"  || die "PULL"
     148  rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | gsed "s/^/  | /"  || die "PULL"
    149149
    150150  # Actual push
    151151  # Send new and updated, remotely remove files deleted locally
    152152  # Order of includes/excludes/filters is EXTREMELY important
    153153  echo
    154154  echo "# Pushing changes to server"
    155   rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | sed "s/^/  | /" || die "PUSH"
     155  rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | gsed "s/^/  | /" || die "PUSH"
    156156
    157157  # Save after-sync state
    158158  # Must be done with rsync itself (rather than find) to respect includes/excludes
     
    160160  echo
    161161  echo "# Saving after-sync state and cleaning up"
    162162  rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \
    163       | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort > "$TMP_DIR/tree-after"
     163      | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort > "$TMP_DIR/tree-after"
    164164
    165165  # Save all newly created files for next run (to prevent deletion of them)
    166166  # This includes files created by user in parallel to sync and files fetched from remote
    167167  comm -23 "$TMP_DIR/tree-after" "$STATE_DIR/tree-current" >"$STATE_DIR/added-prev"
    168168
    169169  # Save new tree state for next run
    170   cat "$STATE_DIR/tree-current" "$STATE_DIR/added-prev" >"$STATE_DIR/tree-prev"
     170  gcat "$STATE_DIR/tree-current" "$STATE_DIR/added-prev" >"$STATE_DIR/tree-prev"
    171171
    172172  # Fire off slow sync stop notifier in background
    173173  on_slow_sync_stop
     
    193193    # If pack directory is missing, create it and prepare git repo
    194194    if [ ! -d "$DOT_DIR/pack" ]
    195195    then
    196         mkdir    $DOT_DIR/pack
     196        gmkdir    $DOT_DIR/pack
    197197        git init $DOT_DIR/pack
    198         touch    $DOT_DIR/pack/.git-init-marker
     198        gtouch    $DOT_DIR/pack/.git-init-marker
    199199        (cd $DOT_DIR/pack && git add .)
    200200        (cd $DOT_DIR/pack && git commit -a -q -m "INIT")
    201201    fi
    202202
    203203    # If any backups exist, pack them into the repo
    204     if [ -d "$DOT_DIR/backups" ] && [ "$(ls -A $DOT_DIR/backups)" ]
     204    if [ -d "$DOT_DIR/backups" ] && [ "$(gls -A $DOT_DIR/backups)" ]
    205205    then
    206206        for DIR in $DOT_DIR/backups/*
    207207        do
    208             TSTAMP=$(echo $DIR | sed "s|.*/||")
    209             if [ "$(ls -A $DIR)" ]
     208            TSTAMP=$(echo $DIR | gsed "s|.*/||")
     209            if [ "$(gls -A $DIR)" ]
    210210            then
    211211                echo -n "Processing: $TSTAMP ... "
    212212                echo -n "Moving ... "
    213                 (cp -rfl $DIR/* $DOT_DIR/pack && rm -rf $DIR)        || die MV
     213                (gcp -rfl $DIR/* $DOT_DIR/pack && grm -rf $DIR)        || die MV
    214214                echo -n "Adding ... "
    215215                (cd $DOT_DIR/pack && git add .)                      || die ADD
    216216                echo -n "Committing ... "
     
    221221                echo "Done."
    222222            else
    223223                echo "Removing empty dir $DIR  ..."
    224                 rmdir $DIR
     224                grmdir $DIR
    225225            fi
    226226        done
    227227        echo "Running 'git gc' on pack dir"
     
    237237
    238238function on_slow_sync_start {
    239239  if [ -n "$SLOW_SYNC_START_CMD" ]; then
    240     rm -rf "$SLOW_SYNC_FILE"
    241     (sleep $SLOW_SYNC_TIME && touch "$SLOW_SYNC_FILE" && eval "$SLOW_SYNC_START_CMD" ; wait) &
     240    grm -rf "$SLOW_SYNC_FILE"
     241    (sleep $SLOW_SYNC_TIME && gtouch "$SLOW_SYNC_FILE" && eval "$SLOW_SYNC_START_CMD" ; wait) &
    242242    disown
    243243    shell_pid=$!
    244244  fi
     
    246246
    247247function on_slow_sync_stop {
    248248  if [ -n "$shell_pid" ]; then
    249     kill $shell_pid &>/dev/null
     249    gkill $shell_pid &>/dev/null
    250250
    251251    if [[ -n "$SLOW_SYNC_STOP_CMD" && -f "$SLOW_SYNC_FILE" ]]; then
    252252      (eval "$SLOW_SYNC_STOP_CMD") &
     
    267267
    268268function acquire_lock {
    269269  if ! mkdir "$LOCK_DIR" 2>/dev/null ; then
    270     kill -0 $(cat "$LOCK_DIR/pid") &>/dev/null
     270    gkill -0 $(gcat "$LOCK_DIR/pid") &>/dev/null
    271271
    272272    if [[ $? == 0 ]]; then
    273273      echo "There's already an instance of BitPocket syncing this directory. Exiting."
    274274      exit 1
    275275    else
    276276      echo -e "\x1b\x5b1;31mbitpocket error:\x1b\x5b0m Bitpocket found a stale lock directory:"
    277       echo "  | Root dir: $(pwd)"   
     277      echo "  | Root dir: $(gpwd)"   
    278278      echo "  | Lock dir: $LOCK_DIR"   
    279       echo "  | Command:  LOCK_PATH=$(pwd)/$LOCK_DIR && rm \$LOCK_PATH/pid && rmdir \$LOCK_PATH"
     279      echo "  | Command:  LOCK_PATH=$(gpwd)/$LOCK_DIR && grm \$LOCK_PATH/pid && grmdir \$LOCK_PATH"
    280280      echo "Please remove the lock directory and try again."
    281281      exit 2
    282282    fi
     
    286286}
    287287
    288288function release_lock {
    289   rm "$LOCK_DIR/pid" &>/dev/null && rmdir "$LOCK_DIR" &>/dev/null
     289  rm "$LOCK_DIR/pid" &>/dev/null && grmdir "$LOCK_DIR" &>/dev/null
    290290}
    291291
    292292function acquire_remote_lock {
     
    308308    echo "fatal: Not a bitpocket directory. Try 'bitpocket help' for usage."
    309309    exit 128
    310310  fi
    311   mkdir -p "$TMP_DIR"
    312   mkdir -p "$STATE_DIR"
     311  gmkdir -p "$TMP_DIR"
     312  gmkdir -p "$STATE_DIR"
    313313}
    314314
    315315function cleanup {
     
    320320function bring_the_children_let_me_kill_them {
    321321  if [ -n "$shell_pid" ]; then
    322322    pkill -P $shell_pid &>/dev/null
    323     kill $shell_pid &>/dev/null
     323    gkill $shell_pid &>/dev/null
    324324  fi
    325325}
    326326
     
    336336function list {
    337337  echo -e "\x1b\x5b1;32mbitpocket\x1b\x5b0m will sync the following files:"
    338338  rsync -av --list-only --exclude "/$DOT_DIR"  $USER_RULES . | grep "^-\|^d" \
    339       | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort
     339      | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort
    340340}
    341341
    342342function usage {