Since the permissions policy on my device is a bit paranoid (cannot adb pull application data), I wrote a script to recursively copy files.
Note: this recursive copy of the script file / folder is for Android!
copy-p:
#! /system/bin/sh src="$1" dst="$2" dir0=`pwd` myfind() { local fpath=$1 if [ -e "$fpath" ] then echo $fpath if [ -d "$fpath" ] then for fn in $fpath/* do myfind $fn done fi else : echo "$fpath not found" fi } if [ ! -z "$dst" ] then if [ -d "$src" ] then echo 'the source is a directory' mkdir -p $dst if [[ "$dst" = /* ]] then :
Here I provide the source of the easy find for Android, because on some devices this utility is missing. Instead of myfind you can use find if it is defined on the device.
Installation:
$ adb push copy-r /sdcard/
Running inside adb shell (root):
or
# source /sdcard/copy-r files/ /sdcard/files3
(Hash # above is a su prompt, and . Is a command that forces the shell to run the specified file, almost the same as source ).
After copying, I can adb pull files from the SD card.
Writing files to the application directory was more difficult, I tried to set r / w permissions on files and its subdirectories, this did not work (well, it allowed me to read, but not write, which is strange), so I had to do:
String[] cmdline = { "sh", "-c", "source /sdcard/copy-r /sdcard/files4 /data/data/com.example.myapp/files" }; try { Runtime.getRuntime().exec(cmdline); } catch (IOException e) { e.printStackTrace(); }
in the onCreate () application.
PS in case someone needs a code to remove protection from application directories to enable access to the adb shell on a non-root phone,
setRW(appContext.getFilesDir().getParentFile()); public static void setRW(File... files) { for (File file : files) { if (file.isDirectory()) { setRW(file.listFiles()); // Calls same method again. } else { } file.setReadable(true, false); file.setWritable(true, false); } }
although for some unknown reason I could read, but not write.