Skip to content

Commit eaf966b

Browse files
tareksanderGrimler91
authored andcommitted
Added: Storage Access Framework programs: termux-saf-*
1 parent cfd4689 commit eaf966b

10 files changed

Lines changed: 258 additions & 0 deletions

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ set(script_files
3838
scripts/termux-notification-channel
3939
scripts/termux-notification-list
4040
scripts/termux-notification-remove
41+
scripts/termux-saf-create
42+
scripts/termux-saf-dirs
43+
scripts/termux-saf-ls
44+
scripts/termux-saf-managedir
45+
scripts/termux-saf-mkdir
46+
scripts/termux-saf-read
47+
scripts/termux-saf-rm
48+
scripts/termux-saf-stat
49+
scripts/termux-saf-write
4150
scripts/termux-sensor
4251
scripts/termux-share
4352
scripts/termux-sms-inbox

scripts/termux-saf-create.in

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-create
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME [-t mime-type] folder-uri name"
7+
echo "Creates a file in a folder managed by Termux:API."
8+
echo "Returns the URI you can use to read and write the file with termux-saf-read and termux-saf-write."
9+
echo "You can specify the mime type explicitly or let it be guessed based on the file extension."
10+
echo "As the folder URI you can use the URI of a directory listed by termux-saf-dirs or by termux-saf-ls."
11+
echo "Android doesn't allow creating files with the same name, so the name may be changed if a file or folder with that name already exists."
12+
echo "You can use termux-saf-stat with the returned URI to get the name it was really given."
13+
echo " -h show this help"
14+
echo " -t specify the mime type of the file. The mime type is required for other apps to recognize the content as e.g. a video. If not specified, 'application/octet-stream' is assumed, that means raw binary data."
15+
exit 0
16+
}
17+
18+
mime=''
19+
20+
while getopts :ht: option
21+
do
22+
case "$option" in
23+
h) show_usage;;
24+
t) mime="$OPTARG";;
25+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
26+
esac
27+
done
28+
shift $((OPTIND-1))
29+
30+
if [ $# != 2 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
31+
32+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod createDocument --es treeuri "$1" --es filename "$2" --es mimetype "$mime"
33+

scripts/termux-saf-dirs.in

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-dirs
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME"
7+
echo "Lists all directories you gave Termux:API in the same format as termux-saf-ls."
8+
echo "That meas this lists the 'directory' that contains all directories you can access with the other termux-saf-* commands."
9+
echo "The URIs can be used with the other termux-saf-* commands to create files and folders and list the directory contents."
10+
echo "See termux-saf-managedir to give Termux:API access to a folder."
11+
echo " -h show this help"
12+
exit 0
13+
}
14+
15+
while getopts :h option
16+
do
17+
case "$option" in
18+
h) show_usage;;
19+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
20+
esac
21+
done
22+
shift $((OPTIND-1))
23+
24+
if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
25+
26+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod getManagedDocumentTrees
27+

scripts/termux-saf-ls.in

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-ls
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME folder-uri"
7+
echo "Lists the files and folders in a folder identified by a URI."
8+
echo "You can get a folder URI by:"
9+
echo "- Listing folders with termux-saf-ls"
10+
echo "- Giving Termux:API access to folders with termux-saf-managedir"
11+
echo "- Listing the folders you gave Termux:API access to with termux-saf-dirs"
12+
echo "- Creating a folder with termux-saf-mkdir"
13+
echo "The list is returned as a JSON array with one JSON object per entry."
14+
echo "The objects have the keys:"
15+
echo "- 'name' for the human-readable name"
16+
echo "- 'uri' for URI you can use with the other termux-saf-* commands"
17+
echo "- 'type' for the mime type"
18+
echo "- 'length' for size in bytes if it's a file"
19+
echo "You can recognize folders by the special mime type 'vnd.android.document/directory'."
20+
echo " -h show this help"
21+
exit 0
22+
}
23+
24+
while getopts :h option
25+
do
26+
case "$option" in
27+
h) show_usage;;
28+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
29+
esac
30+
done
31+
shift $((OPTIND-1))
32+
33+
if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
34+
35+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod listDirectory --es treeuri "$1"
36+

scripts/termux-saf-managedir.in

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-managedir
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME"
7+
echo "Opens the system file explorer and lets you specify a folder Termux:API get access to."
8+
echo "You can then use the termux-saf-* utilities to manage the contents in that folder."
9+
echo "Returns a URI identifying the directory, this URI can be used with the other termux-saf-* commands to create files and folders and list the directory contents."
10+
echo "If you close the file manager instead, an empty string will be returned."
11+
echo "You can use termux-saf-dirs to list out all directories you gave Termux:API access to like this."
12+
echo " -h show this help"
13+
exit 0
14+
}
15+
16+
while getopts :h option
17+
do
18+
case "$option" in
19+
h) show_usage;;
20+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
21+
esac
22+
done
23+
shift $((OPTIND-1))
24+
25+
if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
26+
27+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod manageDocumentTree
28+

scripts/termux-saf-mkdir.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-mkdir
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME parent-uri name"
7+
echo "Creates a directory via SAF."
8+
echo "This behaves like termux-saf-create, just for directories."
9+
echo " -h show this help"
10+
exit 0
11+
}
12+
13+
while getopts :h option
14+
do
15+
case "$option" in
16+
h) show_usage;;
17+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
18+
esac
19+
done
20+
shift $((OPTIND-1))
21+
22+
if [ $# != 2 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
23+
24+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod createDocument --es treeuri "$1" --es filename "$2" --es mimetype 'vnd.android.document/directory'
25+

scripts/termux-saf-read.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-read
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME uri"
7+
echo "Reads from a file identified by a URI and writes the data to sstandard output."
8+
echo "You can use pipes to process the data or redirect it into a file to make a local copy."
9+
echo "See termux-saf-list to get the URIs to files."
10+
echo " -h show this help"
11+
exit 0
12+
}
13+
14+
while getopts :h option
15+
do
16+
case "$option" in
17+
h) show_usage;;
18+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
19+
esac
20+
done
21+
shift $((OPTIND-1))
22+
23+
if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
24+
25+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod readDocument --es uri "$1"
26+

scripts/termux-saf-rm.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-rm
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME uri"
7+
echo "Removes the file or folder at the given URI. See other termux-saf-* commands for more info."
8+
echo "Returns 0 on success, 1 if the file or folder couldn't be deleted and 2 if another error occurred."
9+
echo " -h show this help"
10+
exit 0
11+
}
12+
13+
while getopts :h option
14+
do
15+
case "$option" in
16+
h) show_usage;;
17+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
18+
esac
19+
done
20+
shift $((OPTIND-1))
21+
22+
if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
23+
24+
exit "$(@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod removeDocument --es uri "$1")"
25+

scripts/termux-saf-stat.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-stat
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME uri"
7+
echo "Returns the file or folder info as a JSON object."
8+
echo "The format is the same as an entry from termux-saf-ls."
9+
echo " -h show this help"
10+
exit 0
11+
}
12+
13+
while getopts :h option
14+
do
15+
case "$option" in
16+
h) show_usage;;
17+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
18+
esac
19+
done
20+
shift $((OPTIND-1))
21+
22+
if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
23+
24+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod statURI --es uri "$1"
25+

scripts/termux-saf-write.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!@TERMUX_PREFIX@/bin/sh
2+
set -e -u
3+
4+
SCRIPTNAME=termux-saf-write
5+
show_usage () {
6+
echo "Usage: $SCRIPTNAME uri"
7+
echo "Writes the standard input into an existing file identified by a URI. The previous contents are erased. To create a new file, use termux-saf-create."
8+
echo " -h show this help"
9+
exit 0
10+
}
11+
12+
while getopts :h option
13+
do
14+
case "$option" in
15+
h) show_usage;;
16+
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
17+
esac
18+
done
19+
shift $((OPTIND-1))
20+
21+
if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi
22+
23+
@TERMUX_PREFIX@/libexec/termux-api SAF --es safmethod writeDocument --es uri "$1"
24+

0 commit comments

Comments
 (0)