Kowsky.kak a backup file utility for the Kakoune editor (v0.2.0 and all done)

This project is now mature with non-blocking kakoune intergraion.

Recommendation: containerise with jlink, see instructions below.

I run this on a computer built prior to 2013. Maximum wait time on file backup in kakoune editor < 1 second. Honestly that wait time is on a bad day, it never stops me from working continuously in kakoune editor.

I have tried to make the below easy and thorough, so any non-programmer can make kowsky.kak work.

Repository: https://bitbucket.org/KJ_Duncan/kowsky.kak/src/master/

$ git clone https://bitbucket.org/KJ_Duncan/kowsky.kak.git

Cool guys, let me know if you have any problems. Bye :wave:.


I’ve gone and done a Kowsky! A backup file utility with Kakoune editor integration.


#########
# Usage #
#########
"
kowsky cp source_file

          Copies a source file from the present working directory to kowsky's backup directory
          Hidden /.paths/ and /.files are ignored (no backup is created).

kowsky mv target_file

          Restores a corrupt or deleted file from the kowsky backup directory to the present working directory.
          The backup file remains intact. If target file was deleted from the working directory use:
             $ touch target_file
          To create an empty file which kowsky restores into.

kowsky [rm | --dry-run] [one | thirty | sixty | ninety | onetwenty]

          Removes the catalogue of backup files > days old.

kowsky rmdir

          Recursively remove backup files and kowsky created directories:
            /kak-back-kowsky/backups/*.*
"
######################
# Command Line Usage #
######################

# backup a file:
$ cd /into/the/directory/where/file/is/located
$ kowsky cp source_file

# restore a file:
$ cd /into/the/directory/where/file/was/deleted/or/corrupt
$ touch fileName.ext     # necessary if file was deleted
$ kowsky mv target_file  # backup file remains intact

# remove a catalogue of backup files > thirty days old:
$ kowsky rm thirty

# list files for removal > thirty days old:
$ kowsky --dry-run thirty

# remove all backup files and directory /kak-back-kowsky/*:
$ kowsky rmdir

##############################
# Kakoune Editor Integration #
##############################

# repositories kowsky.kakoune file
$ mv kowsky.kakoune $XDG_CONFIG_HOME/kak/autoload/kowsky.kak
# or
$ mv kowsky.kakoune $HOME/.config/kak/autoload/kowsky.kak


permissions: drwx ---- ---- to paths /kak-back-kowsky/backups
permissions: rw- ---- ---- to files /kak-back-kowsky/backups/*.*

absolute path: $HOME/.local/share/kak-back-kowsky/backups/*.*

Kowsky.kak leverages Java’s security manager, when this application attempts to
interact with local system resources it must first have the equivalent
permission explicitly granting the access via a policy file.

‘[Whenever] a resource access is attempted, all code traversed by the execution
thread up to that point must have permission for that resource access…’

(Permissions in the JDK).

Kowsky logs security and i/o exceptions to standard error, the output
is consistent with kakoune’s debug buffer for example:

:buffer *debug*

*** This is the debug buffer, where debug info will be written ***

shell stderr: <<<
Dec 05, 2020 6:05:16 PM KakBackKowsky cp
SEVERE: Unable to copy: README.md access denied (“java.util.PropertyPermission” “user.home” “read”)

shell stderr: <<<
Dec 05, 2020 6:08:40 PM KakBackKowsky mkdir
SEVERE: Unable to create backup directory: access denied (“java.io.FilePermission” “$HOME/.local/share/kak-back-kowsky/-” “read,write,delete”)


References


Inspired by dmerejkowsky original post: Create backup of each edited file, with source file kak-back.rs. Thanks Dmere.


@author Duncan, K & Merejkowsky, D 2020, KakBackKowsky.java & kak-back.rs, 
        🍵 418 - I'm a teapot, viewed 27 November 2020,
        https://bitbucket.org/KJ_Duncan/kowsky.kak/src/master/src/main/java/KakBackKowsky.java
        https://github.com/dmerejkowsky/dotfiles/blob/master/dm-tools/src/bin/kak-back.rs

@since Dizzee, R 2017, Wot U Gonna Do?, Raskit, viewed 07 June 2020,
       https://open.spotify.com/track/0XMS9iWCSKLunggRUsOd4C?si=7Uj93yQSS7q1O1dQWU3RxA
       https://youtu.be/8O0Spn2eaSk 

+----------------+----------------------+
|    Object      |       Type           |
+----------------+----------------------+
| kowsky         | executable           |
| jre-kowsky     | custom runtime image |
| kowsky.kak     | repository / module  |
| kowsky.policy  | java security policy |
| kowsky.kak.jar | archive for classes  |
| kowsky.kakoune | kakoune intergration |
+----------------+----------------------+


–module-path

@see package manager sdkman for installation instructions.


$ mkdir -p build/classes build/libs 

$ javac --release 15 -d build/classes \
        src/main/java/au/kjd/Days.java \
        src/main/java/au/kjd/KakBackKowsky.java \
        src/main/java/au/kjd/kowsky.java \
        src/main/java/module-info.java

$ jar --create --file build/libs/kowsky.kak.jar \
      --main-class au.kjd.kowsky \
      --module-version 0.2.0 \
      -C build/classes/ .

$ echo 'java -Djava.security.manager -Djava.security.policy==<path-to>/kowsky.policy --module-path <path-to-kowsky-jar> --module kowsky.kak "${@:1}"' > kowsky

$ chmod u-wx,og-rwx build/libs/kowsky.kak.jar && \
  chmod u-wx,og-rwx kowsky.policy && \
  chmod u+rx,u-w,og-rwx kowsky

$ mv build/libs/kowsky.kak.jar $HOME/<path-to-kowsky-jar> && \ 
  mv kowsky $HOME/local/bin/ && \
  mv kowsky.policy /somewhere/you/want/


-Djava.security.policy


# kowsky.policy
// '*' pwd files (non-recursive), '-' recursively walk sub-directories
// jlink: grant codeBase "jrt:/kowsky.kak"

grant codeBase "file:/<path-to>/kowsky.kak.jar" {
  permission java.util.logging.LoggingPermission "control", "";
  
  permission java.util.PropertyPermission "user.home", "read";
  permission java.util.PropertyPermission "user.dir", "read";
  
  permission java.io.FilePermission "*", "read,write";
  permission java.io.FilePermission "${user.home}/.local/share/", "read,write";
  permission java.io.FilePermission "${user.home}/.local/share/kak-back-kowsky", "read,write,delete";
  permission java.io.FilePermission "${user.home}/.local/share/kak-back-kowsky/-", "read,write,delete";
  
  // Needed by file permissions restriction system:
  permission java.lang.RuntimePermission "accessUserInformation";
};

Mapping from Code to Domains and to Permissions


kowsky.kakoune


# does the heavy lifting
hook global BufWritePost .* %{
  nop %sh{ kowsky cp "${kak_hook_param}" }
}

# quick way to finish up at a foreign location
define-command kowsky-rmdir \
  -docstring "recursively remove all /kak-back-kowsky/backups/*.*" %{
    nop %sh{ kowsky rmdir }
}

# remove > days old = one|thirty|sixty|ninety|onetwenty
hook global KakEnd .* %{
 nop %sh{ kowsky rm thirty }
}


jlink


$ jlink --output jre-kowsky \
        --module-path $HOME/<path-to-jar>/kowsky:$JAVA_HOME/jmods \
        --add-modules kowsky.kak,java.base,java.logging \
        --compress=0 --no-man-pages

$ kak kowsky.policy
grant codeBase "jrt:/kowsky.kak" {
 [...]
};

$ du -hs jre-kowsky
>  41M   jre-kowsky

$ echo '<path-to>/jre-kowsky/bin/java -Djava.security.manager -Djava.security.policy==<path-to>/kowsky.policy --module kowsky.kak "${@:1}"' > kowsky

$ chmod -R og-rwx jre-kowsky && \
  chmod u+rx,u-w,og-rwx kowsky && \
  chmod u-wx,og-rwx kowsky.policy
  
$ mv jre-kowsky kowsky.policy kowsky /somewhere/you/want/


gradle


$ git clone https://bitbucket.org/KJ_Duncan/kowsky.kak.git
$ cd kowsky.kak

# bitbucket does not allow *.jar files in a git clone repo
$ gradle wrapper
$ ./gradlew build  # to build with tests
$ ./gradlew test --rerun-tasks

$ ls build/libs/
> kowsky.kak.jar

$ jar --update --file build/libs/kowsky.kak.jar \
      --main-class au.kjd.kowsky 

# in conjunction with --module-path or jlink setup


File Handling Issues


Kakoune General Information

Work done? Have some fun. Share any improvements, ideas or thoughts with the community discuss.kakoune.

Kakoune is an open source modal editor. The source code lives on github mawww/kakoune.
A discussion on the repository structure of community plugins for Kakoune can be reviewed here: Standardi(s|z)ation of plugin file-structure layout #2402.
Read the Kakoune wiki for information on install options via a Plugin Manager.

Thank you to the Kakoune community 230+ contributors for a great modal editor in the terminal. I use it daily for the keyboard shortcuts.


That’s it for the readme, anything else you may need to know just pick up a book and read it Polar Bookshelf. Thanks all. Bye.

1 Like

kowsky.kak v0.2.2 - ./gradlew jlink

Build with ‘jlink - assemble and optimize a set of modules and their dependencies into a custom runtime image’


$ git clone https://bitbucket.org/KJ_Duncan/kowsky.kak.git
$ cd kowsky.kak

# bitbucket does not allow *.jar files in a git clone repo
$ gradle wrapper --gradle-version 6.8.3 --distribution-type all
$ ./gradlew tasks --all
# Other tasks
# -----------
$ ./gradlew jlink  # to build/libs/kowsky.kak.jar and build/jrt-kowsky-kak
$ ./gradlew test --rerun-tasks  # to run test suite without the cache

$ build/jrt-kowsky-kak/bin/java --list-modules
> java.base@15.0.2
> java.logging@15.0.2
> kowsky.kak

$ ls build/libs/
> kowsky.kak.jar

$ jar --file build/libs/kowsky.kak.jar --describe-module
> kowsky.kak jar:file://.../kowsky.kak/build/libs/kowsky.kak.jar/!module-info.class
> requires java.base
> requires java.logging
> contains au.kjd
> main-class au.kjd.kowsky

# in conjunction with jlink setup from line: "$ kak kowsky.policy"