How to handle .tar.md5 files - android

I was wondering about how to create / extract / verify .tar.md5 files. These files are used when flashing images to android devices, see here for example.
As far as I can tell the checksum is appended to the file like this:
cp file.tar file.tar.md5
md5sum file.tar >> file.tar.md5
Firstly I would like to know how to extract the file. Can I simply use tar -xf on the file.tar.md5?
How can I verify the integrity of the file? I would like to remove the last bytes (containing the checksum) from the file to obtain the original file back. I guess you would have to use a regexp to match the checksum file.tar? Is something like this implemented somewhere already?

First of all tar -xf should work since tar continues while it matches its' packing algorithm. If the file stops matching so would tar.
Also most archive managers such as 7-zip or winrar will open it if you remove the ".md5".
They might print error regarding mismatch with the end of the file, ignore it.
As for verifying the file:
print out the stored md5sum: tail -z -n 1 [File name here].tar.md5
calculate the md5sum of the tar part of the file: head -z -n -1 [File name here].tar.md5 | md5sum

What works for me with Ubuntu 19.10 is:
download single-file 4 GiB zip from sammobile com
unzip to several *.tar.md5
run the below command-line
.
for F in *.tar.md5; do echo -n "$F " &&
EXP=($(tail --lines=1 "$F")) &&
ACT=($(head --lines=-1 "$F" | md5sum)) &&
if [ ${EXP[0]} = ${ACT[0]} ]; then echo -n "md5ok " &&
tar --extract --file "$F" && echo "done"
else echo "FAIL"; fi; done &&
unlz4 --multiple --verbose *.lz4
AP_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT_meta.tar.md5 md5ok done
BL_G965U1UEU3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CP_G965U1UEU3ARL1_CP11407818_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
HOME_CSC_OMC_OYM_G965U1OYM3ARL1_CL14745140_QB21029084_REV01_user_low_ship_MULTI_CERT.tar.md5 md5ok done
…
But we should all try to get away from bash

Related

Calculating hash of files in directory hierarchy in Android

I need to calculate md5 hash of files in a directory hierarchy. I am using the following case as a test. The Android device I have has a md5 binary, but needs absolute path of file (md5 <filename>).
I have the following directory hierarchy on a Android device:
/data/local/tmp/test1
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
To get list of absolute paths, I followed the answer mentioned here.
$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /data/local/tmp/' > filelist.txt
Now I have list of absolute paths of each file.
Next I want to read this file in a script line by line, and call md5 for each line. md5 will print a message if the input is a directory. I followed the example here.
#! /bin/bash
filename='filelist.txt'
cat $filename | while read LINE; do
adb shell 'md5 $LINE'
done
I get the following output:
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
could not read /data/local/tmp/test1, Is a directory
I expected it to print a directory warning for test1 and test2, and then md5 for test3, as test3 is a file. Can someone suggest how to fix this code ? I was expecting something like:
could not read /data/local/tmp/test1, Is a directory
could not read /data/local/tmp/test1/test2, Is a directory
<hash_value> /data/local/tmp/test1/test2/test3
You should use find:
adb shell find /data/local/tmp -type f -exec md5sum {} '\;'

Script that will transfer photos from phone camera using adb

Story
I take photos and record videos with my phone camera and keep all of them on my internal storage/sdcard. I periodically back them up on my PC, so I keep these camera photos on PC storage in sync with phone storage.
For years, I've been backing up my phone camera photos to my PC in the following way:
Plug in phone into PC and allow access to phone data
Browse phone storage → DCIM → Camera
Wait several minutes for the system to load a list of ALL photos
Copy only several latest photos which haven't been backed up yet
I figured that waiting several minutes for all photos to load is an unnecessary drag so I downloaded adb platform tools. I've added the folder bin to my Path environment variable (i.e. %USERPROFILE%\Tools\adb-platform-tools_r28.0.3) so that I can seamlessly use adb and not write its full path each time.
The script
I wrote the following script for Git Bash for Windows. It is also compatible with Unix if you change the $userprofile variable. Essentially, the script pulls camera photos between two dates from phone storage to PC.
# Attach device and start deamon process
adb devices
# Initialize needed variables
userprofile=$(echo "$USERPROFILE" | tr "\\" "/") # Windows adjustments
srcFolder="//storage/06CB-C9CE/DCIM/Camera" # Remote folder
dstFolder="$userprofile/Desktop/CameraPhotos" # Local folder
lsFile="$dstFolder/camera-ls.txt"
filenameRegex="2019061[5-9]_.*" # Date from 20190615 to 20190619
# Create dst folder if it doesn't exist
mkdir -p "$dstFolder"
# 1. List contents from src folder
# 2. Filter out file names matching regex
# 3. Write these file names line by line into a ls file
adb shell ls "$srcFolder" | grep -E "$filenameRegex" > "$lsFile"
# Pull files listed in ls file from src to dst folder
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder" # adb: error: ...
done < "$lsFile"
# Clean up
rm "$lsFile"
# Inform the user
echo "Done pulling files to $dstFolder"
The problem
When I run the script (bash adb-pull-camera-photos.sh), everything runs smoothly except for the adb pull command in the while-loop. It gives the following error:
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
I am not sure why the output is broken. Sometimes when I resize the Git Bash window some of the text goes haywire. This is the actual error text:
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg': No such file or directory
adb: error: failed to stat remote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg': No such file or directory
I am sure that these files exist in the specified directory on the phone. When I manually execute the failing command in bash, it succeeds with the following output:
$ adb pull "//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg" "C:/Users/User/Desktop/CameraPhotos/"
//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg: 1 file pulled. 15.4 MB/s (1854453 bytes in 0.115s)
The question
I can't figure out what's wrong with the script. I thought the Windows system might be causing a commotion, because I don't see the reason why the same code works when entered manually, but doesn't work when run in a script. How do I fix this error?
Additional info
Note that I had to use // in the beginning of an absolute path on Windows because Git Bash would interpret / as its own root directory (C:\Program Files\Git).
I've echoed all variables inside the script and got all the correct paths that otherwise work via manual method.
camera-ls.txt file contents
20190618_124656.jpg
20190618_204522.jpg
20190619_225739.jpg
Additional questions
Is it possible to navigate to external sdcard without using its name? I had to use /storage/06CB-C9CE/ because /sdcard/ navigates to internal storage.
Why does tr "\\" "/" give me this error: tr: warning: an unescaped backslash at end of string is not portable?
Windows batch script
Here's a .bat script that can be run by Windows Command Prompt or Windows PowerShell. No Git Bash required.
:: Start deamon of the device attached
adb devices
:: Pull camera files starting from date
set srcFolder=/storage/06CB-C9CE/DCIM/Camera
set dstFolder=%USERPROFILE%\Desktop\CameraPhotos
set lsFile=%USERPROFILE%\Desktop\CameraPhotos\camera-ls.txt
set dateRegex=2019061[5-9]_.*
mkdir %dstFolder%
adb shell ls %srcFolder% | adb shell grep %dateRegex% > %lsFile%
for /F "tokens=*" %%A in (%lsFile%) do adb pull %srcFolder%/%%A %dstFolder%
del %lsFile%
echo Done pulling files to %dstFolder%
Just edit the srcFolder to point to your phone camera folder,
plug a pattern into the dateRegex for matching the date interval and
save it as a file with .bat extension, i.e: adb-pull-camera-photos.bat.
Double-click the file and it will pull filtered photos into CameraPhotos folder on Desktop.
Keep in mind that you still need have adb for Windows on your PC.
The problem was with Windows line delimiters.
Easy fix
Just add the IFS=$'\r\n' above the loop so that the read command knows the actual line delimiter.
IFS=$'\r\n'
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Explanation
I tried plugging the whole while-loop into the console and it failed with the same error:
$ bash adb-pull-camera-photos.sh
List of devices attached
9889db343047534336 device
tr: warning: an unescaped backslash at end of string is not portable
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_124656.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190618_204522.jpg
': No such file or directoryemote object '//storage/06CB-C9CE/DCIM/Camera/20190619_225739.jpg
Done pulling files to C:/Users/User/Desktop/CameraPhotos
This time I started investigating why the output was broken. I remembered that windows uses \r\n as newline, which means Carriage Return + Line Feed, (CR+LF), so some text must have been overwritten.
It was because of broken values stored inside the $filename variable.
This is the loop from the script:
while read filename; do
if [ -z "$filename" ]; then continue; fi
adb pull "$srcFolder/$filename" "$dstFolder"
done < "$lsFile"
Since each iteration of the while-loop reads a line from $lsFile in the following form:
exampleFilename.jpg\r\n
It misinterprets the newline symbols as part of the file name, so adb pull tries to read files with these whitespaces in their names, but fails and it additionally writes a broken output.
Adb Photo Sync
This might not be the answer but might be useful for others looking for android photo/files backup solution.
I use this script on my Windows with git bash. This can be easily used for Linux. A common issue with a long backup process is that it might get interrupted and you might have to restart the entire copy process from start.
This script saves you from this trouble. You can restart the script or interrupt in between but it will resume copy operation from the point it left.
Just change the rfolder => android folder, lfolder => local folder
#!/bin/sh
rfolder=sdcard/DCIM/Camera
lfolder=/f/mylocal/s8-backup/Camera
adb shell ls "$rfolder" > android.files
ls -1 "$lfolder" > local.files
rm -f update.files
touch update.files
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
# Populate files to update
if ! grep -q "$l" local.files; then
echo "$l" >> update.files
fi
done < android.files
script_dir=$(pwd)
cd $lfolder
while IFS= read -r q; do
# Remove non-printable characters (are not visible on console)
l=$(echo ${q} | sed 's/[^[:print:]]//')
echo "Get file: $l"
adb pull "$rfolder/$l"
done < "${script_dir}"/update.files

How to return 0 if a pattern is matched from a file?

I have a binary that repacks android kernel (not mkbootimg).
I'm making a script to automate the process.
I don't want any output on the screen. So I have redirected the output to a file named foo.log.
My current working folder is data/local/working/.
The command is - kernel_make "$zImage" "$ramdisk" "$cmdline" "$image" &> data/local/working/foo.log
What I've to do is, if the last line of the output (foo.log) is this -
"targed file $1 is patched sucesfully, enjoy new kernel"
Then return 0 and exit. Else return 1 and exit.
I'm trying to do with grep, but as I'm new to command line, do don't have any idea of doing it the right way.
Please be free to ask if anything more is required..
a common usage for that would be grep -q 'pattern', it doesn't generate any output, but you can check the return value of the command to decide if the pattern was found. See this example:
kent$ echo "foo"|grep -q bar
kent$ echo $?
1
kent$ echo "bar"|grep -q bar
kent$ echo $?
0
hope this is what you are looking for.
To extract the last line, you can use tail command.
In fact, you can also do it with awk in one shot:
..cmd with..output|awk 'END{if (!/pattern/)exit 1}'
then you can check the $?

Enumerate files and folders recursively

I am trying to run a bash script which will run on the current directory, find files and do some operation on them and when a directory is found do the same for it and over and over again.
The thing that got me confused was my operation, it is supposed to convert each graphic file, resize it, and put it in a parallel directory structure, doing that for four parallel directories, meaning it will mimic the original directory structure creating folders as it goes.
The problem is it doesn't work and also it keeps recursing into the newly created directories..
Can you help make it right?
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
mkdir "res-ldpi/$i"
mkdir "res-hdpi/$i"
mkdir "res-mdpi/$i"
mkdir "res-xhdpi/$i"
recurse "$i";
elif [ -f "$i" ]; then
convert ./"$i" -resize 38% -unsharp 0x1 res-ldpi/"$i"
fi
done
}
recurse .
Perhaps something along these lines (not tested...):
while read fn
do
dn=$(dirname "${fn}")
bn=$(basename "${fn}")
[[ -d "res-ldpi/${dn}" ]] || mkdir -p "res-ldpi/${dn}"
convert "${fn}" -resize 38% -unsharp 0x1 "res-ldpi/${dn}/${bn}"
done < <(find "${1}"/. -type f -print)
The idea is to 1) use find to just give you the name of every file in the directory and its subdirectories, 2) split that into a directory and file name, 3) check that the destination directory exists and create it if necessary, then 4) run convert as desired.
That didn't bother with the res-hdpi, res-mdpi and res-xhdpi directories, since your sample code didn't use them anyway - it shouldn't be too difficult to figure out how to add those in...

Android shell script to remove all files and folders in a directory except one

Right now I am using rm -r /blaa/* to delete all folders and files in the blaa directory. What I am looking for is a way to remove all folders and files in the blaa directory except if the folder is named abc.
Any ideas?
In Linux:
There are many ways to this; however I believe the best way to do this is to simply use the "find" tool.
find ! -iname "abc" -exec rm -rf {} \;
We can easily find and delete the every file and folder that is not named "abc".
find - to find files
! -iname - to filter files/folders, the "!" means not
-exec - to execute a command on every file
rm -rf - remove/delete files -r for folders as well and -f for force
"{} \;" - allows the commands to be used on every file
In Android:
Since you can't use "rm -rf" and when you use "rm -r" it will delete the folder "." which ends up deleting everything.
I am guessing you have "root" on your phone because you can use the "find" tool.
find ! -iname "abc" | sed 1d | xargs rm -r
find - to find files
! -iname - to filter files/folders, the "!" means not
| - pipe sends data to next command
sed - replace text/output
"1d" - removes first line when you do "find ! -iname" by itself
xargs - runs commands after pipe
rm -r - remove/delete files, "-r" for recursive for folders
Edit: Fixed and tested in Android
You can easily change this to suite your needs, please let me know if this helps!
The Adopted Solution
...and the final hoorah... This is what worked for the use case (helps to sum up the comments below as well):
find ! -iname "abc" -maxdepth 1 -depth -print0 | sed '$d' | xargs -0 rm -r;
Notes:
-depth — reverses the output (so you dont delete sub-dirs first
-maxdepth 1 — kind of voids the use of -depth, but hey... this says only output contents of the current directory and not sub-dirs (which are removed by the -r option anyway)
-print0 and -0 — splits on line feeds instead of white space (for dirs with space in the name)
sed "$d" — says to remove the last line (cause its now reversed). The last line is just a period which including would make the call delete everything in the directory (and subs!)
I am sure someone can tighten this up, but it works and was a great learning op!
Thanks again to Jared Burrows(and the Unix community in general — go team!) — MindWire

Categories

Resources