Skip to content

Commit

Permalink
Merge pull request #39 from eXist-db/checksum
Browse files Browse the repository at this point in the history
Add SHA256 checksum to package info
  • Loading branch information
joewiz authored May 13, 2019
2 parents d8e8d02 + 20f7389 commit 000a461
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions expath-pkg.xml.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://expath.org/ns/pkg" name="http://exist-db.org/apps/public-repo" abbrev="@project.name@" version="@project.version@" spec="1.0">
<title>eXist-db Public Application Repository</title>
<dependency processor="http://exist-db.org" semver-min="3.5.0"/>
<dependency package="http://expath.org/ns/crypto" semver-min="0.5"/>
</package>
4 changes: 3 additions & 1 deletion modules/find.xql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ return
"public/"
return
if ($info) then
<found>{$compatible-xar}</found>
let $app := collection($config:app-root || "/public")//(app|version)[@path eq $compatible-xar]
return
<found>{$app/@sha256,($app/version,$app/@version)[1] ! attribute version {.},$compatible-xar}</found>
else if ($zip) then
response:redirect-to(xs:anyURI($rel-public || $compatible-xar || ".zip"))
else
Expand Down
24 changes: 21 additions & 3 deletions modules/scan.xql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ xquery version "3.0";

module namespace scanrepo="http://exist-db.org/xquery/admin/scanrepo";

import module namespace config="http://exist-db.org/xquery/apps/config" at "config.xqm";
import module namespace config = "http://exist-db.org/xquery/apps/config" at "config.xqm";
import module namespace crypto = "http://expath.org/ns/crypto";
import module namespace util = "http://exist-db.org/xquery/util";

declare namespace repo="http://exist-db.org/xquery/repo";
declare namespace expath="http://expath.org/ns/pkg";
Expand Down Expand Up @@ -44,10 +46,21 @@ declare function scanrepo:process($apps as element(app)*) {
{
reverse(
for $older in $app[version != $newest/version]
let $xar := concat($config:public, "/", $older/@path)
let $hash := crypto:hash(
util:binary-doc($xar),
"sha256",
"hex"
)
let $n := tokenize($older/version, "\.") ! xs:int(analyze-string(., "(\d+)")//fn:group[1])
order by $n[1], $n[2], $n[3]
return
<version version="{$older/version}">{$older/@path, $older/requires}</version>
<version version="{$older/version}">{
$older/@path,
attribute size { xmldb:size($config:public, $older/@path) },
attribute sha256 { $hash },
$older/requires
}</version>
)
}
</other>
Expand Down Expand Up @@ -196,8 +209,13 @@ declare function scanrepo:entry-filter($path as xs:anyURI, $type as xs:string, $

declare function scanrepo:extract-metadata($resource as xs:string) {
let $xar := concat($config:public, "/", $resource)
let $hash := crypto:hash(
util:binary-doc($xar),
"sha256",
"hex"
)
return
<app path="{$resource}" size="{xmldb:size($config:public, $resource)}">
<app path="{$resource}" size="{xmldb:size($config:public, $resource)}" sha256="{$hash}">
{
compression:unzip(util:binary-doc($xar), util:function(xs:QName("scanrepo:entry-filter"), 3), (),
util:function(xs:QName("scanrepo:entry-data"), 4), $resource)
Expand Down
2 changes: 1 addition & 1 deletion modules/upload.xql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare function local:upload($collection, $path, $data) {
<files json:array="true">
<name>{$path}</name>
<type>{xmldb:get-mime-type($path)}</type>
<size>93928</size>
<size>{xmldb:size($collection, $path)}</size>
</files>
</result>
return
Expand Down
20 changes: 13 additions & 7 deletions post-install.xql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
xquery version "3.0";
xquery version "3.1";

import module namespace scanrepo="http://exist-db.org/xquery/admin/scanrepo" at "modules/scan.xql";
import module namespace system="http://exist-db.org/xquery/system";
import module namespace util="http://exist-db.org/xquery/util";
import module namespace xdb="http://exist-db.org/xquery/xmldb";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";

(: The following external variables are set by the repo:deploy function :)

Expand All @@ -14,11 +14,17 @@ declare variable $dir external;
(: the target collection into which the app is deployed :)
declare variable $target external;

(: Handle difference between 4.x.x and 5.x.x releases of eXist :)
declare variable $local:copy-collection :=
let $fnNew := function-lookup(xs:QName("xmldb:copy-collection"), 2)
return
if (exists($fnNew)) then $fnNew else function-lookup(xs:QName("xmldb:copy"), 2);

declare function local:mkcol-recursive($collection, $components) {
if (exists($components)) then
let $newColl := concat($collection, "/", $components[1])
return (
xdb:create-collection($collection, $components[1]),
xmldb:create-collection($collection, $components[1]),
local:mkcol-recursive($newColl, subsequence($components, 2))
)
else
Expand All @@ -43,15 +49,15 @@ declare function local:get-repo-dir() {
};

declare function local:copy-previous-public-from-temp-or-create() {
if (xdb:collection-available("/db/temp/public")) then
let $copy-dummy := xdb:copy("/db/temp/public", $target)
return xdb:remove("/db/temp/public")
if (xmldb:collection-available("/db/temp/public")) then
let $copy-dummy := $local:copy-collection("/db/temp/public", $target)
return xmldb:remove("/db/temp/public")
else
local:mkcol($target, "public")
};

system:as-user("repo", "repo", (
local:copy-previous-public-from-temp-or-create(),
xdb:store-files-from-pattern(concat($target, "/public"), local:get-repo-dir(), "*.xar"),
xmldb:store-files-from-pattern(concat($target, "/public"), local:get-repo-dir(), "*.xar"),
scanrepo:scan()
))
16 changes: 11 additions & 5 deletions pre-install.xql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
xquery version "1.0";
xquery version "3.1";

import module namespace xdb="http://exist-db.org/xquery/xmldb";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";

(: The following external variables are set by the repo:deploy function :)

Expand All @@ -11,11 +11,17 @@ declare variable $dir external;
(: the target collection into which the app is deployed :)
declare variable $target external;

(: Handle difference between 4.x.x and 5.x.x releases of eXist :)
declare variable $local:copy-collection :=
let $fnNew := function-lookup(xs:QName("xmldb:copy-collection"), 2)
return
if (exists($fnNew)) then $fnNew else function-lookup(xs:QName("xmldb:copy"), 2);

declare function local:mkcol-recursive($collection, $components) {
if (exists($components)) then
let $newColl := concat($collection, "/", $components[1])
return (
xdb:create-collection($collection, $components[1]),
xmldb:create-collection($collection, $components[1]),
local:mkcol-recursive($newColl, subsequence($components, 2))
)
else
Expand All @@ -29,8 +35,8 @@ declare function local:mkcol($collection, $path) {

declare function local:copy-current-public-to-temp() {
local:mkcol("/db/", "temp"),
if (xdb:collection-available(concat($target, "/public"))) then
xdb:copy(concat($target, "/public"), "/db/temp/")
if (xmldb:collection-available(concat($target, "/public"))) then
$local:copy-collection(concat($target, "/public"), "/db/temp/")
else
()
};
Expand Down

0 comments on commit 000a461

Please sign in to comment.