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

**URL:** <https://discuss.kakoune.com/t/kowsky-kak-a-backup-file-utility-for-the-kakoune-editor-v0-2-0-and-all-done/1428>\
**Category:** Plugins\
**Tags:** plugins\
**Created:** [December 12, 2020, 7:48am UTC](https://discuss.kakoune.com/t/kowsky-kak-a-backup-file-utility-for-the-kakoune-editor-v0-2-0-and-all-done/1428 "2020-12-12T07:48:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![duncan](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/duncan/32/95_2.png) [@duncan](https://discuss.kakoune.com/u/duncan)\
**Post date:** [December 12, 2020, 7:48am UTC](https://discuss.kakoune.com/t/kowsky-kak-a-backup-file-utility-for-the-kakoune-editor-v0-2-0-and-all-done/1428/1 "2020-12-12T07:48:02Z")

</div>

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 👋.

* * *

I’ve gone and done a Kowsky! A backup file utility with [Kakoune](https://kakoune.org/) editor integration.

```shell

#########
# 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

- Common Weakness Enumeration (CWE), 2020, _‘CWE CATEGORY: File Handling Issues’_, Category ID: 1219, viewed 05 December 2020, [CWE - CWE-1219: CWE CATEGORY: File Handling Issues (4.20)](https://cwe.mitre.org/data/definitions/1219.html)
- Kakoune 2020, _‘5. Configuration & Autoloading’_, viewed 04 December 2020, [GitHub - mawww/kakoune: mawww's experiment for a better code editor · GitHub](https://github.com/mawww/kakoune#5-configuration--autoloading)
- Kakoune 2020, _‘Debug Buffers’_, buffers.asciidoc, viewed 05 December 2020, [kakoune/doc/pages/buffers.asciidoc at master · mawww/kakoune · GitHub](https://github.com/mawww/kakoune/blob/master/doc/pages/buffers.asciidoc#debug-buffers)
- Oracle 2020, _‘Default Policy Implementation and Policy File Syntax’_, Java Documentation, viewed 27 November 2020, [Permissions in the JDK](https://docs.oracle.com/en/java/javase/15/security/permissions-jdk1.html)
- Oracle 2020, _‘Files.isHidden’_, java.nio.file.Files, viewed 05 December 2020, [Files (Java SE 15 & JDK 15)](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/nio/file/Files.html)

* * *

Inspired by dmerejkowsky original post: [Create backup of each edited file](https://discuss.kakoune.com/t/create-backup-of-each-edited-file/1016), with source file [kak-back.rs](https://github.com/dmerejkowsky/dotfiles/blob/master/dm-tools/src/bin/kak-back.rs). Thanks Dmere.

- Merejkowsky, D 2020, _‘kak-spell’_, PyEnchant wrapper for Kakoune, viewed 06 December 2020, [Client Challenge](https://pypi.org/project/kak-spell/)

```plaintext

@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_](https://sdkman.io/) for installation instructions.

```shell

$ 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/

```

- Deitel, P 2017, _‘Understanding Java 9 Modules’_, What they are and how to use them, Java 9 Excerpt, Oracle, viewed 05 August 2020, [https://www.oracle.com/corporate/features/understanding-java-9-modules.html](https://www.oracle.com/corporate/features/understanding-java-9-modules.html)

* * *

#### -Djava.security.policy

```java

# 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";
};

```

 ![](https://discuss.kakoune.com/uploads/default/original/1X/00b39aa3210f2d96cd1c9d652c78494010814b97.png)

[Mapping from Code to Domains and to Permissions](https://docs.oracle.com/en/java/javase/14/security/java-se-platform-security-architecture.html)

* * *

#### kowsky.kakoune

```shell

# 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

```shell

$ 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/

```

- Oracle 2020, _‘The jlink Command’_, Java SE 15, viewed 20 November 2020, [The jlink Command](https://docs.oracle.com/en/java/javase/15/docs/specs/man/jlink.html)
- OpenJDK 2017, _‘JEP 220: Modular Run-Time Images’_, viewed 05 August 2020, [JEP 220: Modular Run-Time Images](https://openjdk.java.net/jeps/220)

* * *

#### gradle

```shell

$ 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

```

- Gradle 2020, _‘The Gradle Wrapper’_, viewed 13 August 2020, [Gradle Wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html)
- Sdkman 2020, _‘Gradle’_, The Software Development Kit Manager, viewed 06 December 2020, [SDK Installation Candidates | SDKMAN! the Software Development Kit Manager](https://sdkman.io/sdks#gradle)

* * *

#### File Handling Issues

- CWE Category: File Handling Issues, [CWE - CWE-1219: CWE CATEGORY: File Handling Issues (4.20)](https://cwe.mitre.org/data/definitions/1219.html)
- CWE-23: Relative Path Traversal, [CWE - CWE-23: Relative Path Traversal (4.20)](https://cwe.mitre.org/data/definitions/23.html)
- CWE-378: Creation of Temporary File With Insecure Permissions, [CWE - CWE-378: Creation of Temporary File With Insecure Permissions (4.20)](https://cwe.mitre.org/data/definitions/378.html)
- CWE-379: Creation of Temporary File in Directory with Insecure Permissions, [CWE - CWE-379: Creation of Temporary File in Directory with Insecure Permissions (4.20)](https://cwe.mitre.org/data/definitions/379.html)
- CWE-426: Untrusted Search Path, [CWE - CWE-426: Untrusted Search Path (4.20)](https://cwe.mitre.org/data/definitions/426.html)
- CWE-427: Uncontrolled Search Path Element, [CWE - CWE-427: Uncontrolled Search Path Element (4.20)](https://cwe.mitre.org/data/definitions/427.html)

* * *

#### Kakoune General Information

Work done? Have some fun. Share any improvements, ideas or thoughts with the community [discuss.kakoune](https://discuss.kakoune.com/).

Kakoune is an open source modal editor. The source code lives on github [mawww/kakoune](https://github.com/mawww/kakoune#-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](https://github.com/mawww/kakoune/issues/2402).  
Read the Kakoune wiki for information on install options via a [Plugin Manager](https://github.com/mawww/kakoune/wiki/Plugin-Managers).

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_](https://getpolarized.io/). Thanks all. Bye.

---

<div class="post-metadata">

**Author:** ![duncan](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/duncan/32/95_2.png) [@duncan](https://discuss.kakoune.com/u/duncan)\
**Post date:** [March 2, 2021, 8:23am UTC](https://discuss.kakoune.com/t/kowsky-kak-a-backup-file-utility-for-the-kakoune-editor-v0-2-0-and-all-done/1428/2 "2021-03-02T08:23:42Z")

</div>

#### 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’_

```auto

$ 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"

```

- ilya-g 2020, build.gradle.kts, kotlin-jlink-examples, gradle, viewed 01 March 2021, [kotlin-jlink-examples/build.gradle.kts at master · ilya-g/kotlin-jlink-examples · GitHub](https://github.com/ilya-g/kotlin-jlink-examples/blob/master/gradle/app/build.gradle.kts)

* * *
