bashmacosawkdebiancydia

Appending md5sums to end of file


I'm attempting to automate a debian repository (for cydia) to update by shell script when a new build is created, this has always worked until the latest version of cydia which now seems to require MD5 hashes of the Packages files to be included in the Release file. I've tried and failed to append the hashes programmatically, I can get the hash of the file:

echo -n | cat ./Packages | md5sum | awk '{print $1}'

and I can get the size of the file in bytes:

stat -f%z ./Packages

but I can't work out how to create one line in the form:

 c98fd649b21ebf3cc66d9e565f75284f 404 Packages

and to add it to the appropriate line of the release file

Release File:

Origin: Repo name
Label: label
Suite: stable
Version: 0.9
Codename: codename
Architectures: iphoneos-arm
Components: main
Description: Description.
MD5Sum:
 c98fd649b21ebf3cc66d9e565f75284f 404 Packages
 b361d77125813106377a48616c7c4a38 293 Packages.gz
 e2f125c1fa9ec8a183064d0b4fec3b3d 320 Packages.bz2

My question is, how can I replace the hash and the size in bytes of each Packages file in the Release?


Solution

  • You can create a function that prints the hash and size in the format you want:

    print_hash_and_size() {
      printf " %s %s %s\n" $(md5sum "$1" | awk '{print $1}') $(stat -c %s "$1") "$1"
    }
    

    And call the above function for each file you want to append to your release file.

    {
      printf "%s\n" "MD5Sum";
      print_hash_and_size Packages;
      print_hash_and_size Packages.gz;
      print_hash_and_size Packages.bz2;
    } >> release_file
    

    Which would append four lines to your file, somewhat like in your example:

    MD5Sum:
     c98fd649b21ebf3cc66d9e565f75284f 404 Packages
     b361d77125813106377a48616c7c4a38 293 Packages.gz
     e2f125c1fa9ec8a183064d0b4fec3b3d 320 Packages.bz2
    

    I see you are using this command sequence to get the hash:

    echo -n | cat ./Packages | md5sum | awk '{print $1}'
    

    Not sure why echo -n and cat are needed. The file name can be passed to md5sum directly as an argument, as I you see in the function above.