Skip to content

Commit

Permalink
Allow to specify allowed <custom/> keys
Browse files Browse the repository at this point in the history
If no <custom/> tags are set to be propagated to the output, we will
simply ignore them all.
  • Loading branch information
ximion committed Jan 19, 2017
1 parent 022ba96 commit a28b844
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/asgen-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Oldsuites | This key exists to support migration from an alternative appstream g
Suites | Suites which should be recognized by the generator. Each suite has the components and architectures which should be searched for metadata as children. See below for more information.
Features | Disable or enable selected generator features. For a detailed description see below.
CAInfo | Set the CA certificate bundle file to use for SSL peer verification. If this is not set, the generator will use the system default.
AllowedCustomKeys | Set which keys of the <custom/> tag are allowed to be propagated to the collection metadata output. This key takes a list of custom-key strings as value.


### Suite fields
Expand Down
6 changes: 6 additions & 0 deletions src/asgen/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Config
DataType metadataType;
uint enabledFeatures; // bitfield

bool[string] allowedCustomKeys; // set of allowed keys in <custom/> tags

string workspaceDir;

string caInfo;
Expand Down Expand Up @@ -338,6 +340,10 @@ class Config
oldsuites = map!"a.str"(root["Oldsuites"].array).array;
}

if ("AllowedCustomKeys" in root.object)
foreach (ref key; root["AllowedCustomKeys"].array)
allowedCustomKeys[key.str] = true;

// Enable features which are default-enabled
setFeature (GeneratorFeature.PROCESS_DESKTOP, true);
setFeature (GeneratorFeature.VALIDATE, true);
Expand Down
37 changes: 36 additions & 1 deletion src/asgen/result.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,25 @@ import appstream.Component;
import asgen.hint;
import asgen.utils : buildCptGlobalID;
import asgen.backends.interfaces;
import asgen.config : Config;


/**
* Helper function for GeneratorResult.finalize()
*/
extern(C)
int evaluateCustomEntry (void *keyPtr, void *value, void *userData)
{
auto key = (cast(const(char)*) keyPtr).fromStringz;
auto conf = *cast(Config*) userData;

if (key in conf.allowedCustomKeys)
return false; // FALSE, do not delete

// remove invalid key
return true;
}

class GeneratorResult
{

Expand Down Expand Up @@ -208,6 +225,8 @@ public:
*/
void finalize ()
{
auto conf = Config.get ();

// we need to duplicate the associative array, because the addHint() function
// may remove entries from "cpts", breaking our foreach loop.
foreach (cpt; cpts.dup.byValue ()) {
Expand All @@ -230,8 +249,8 @@ public:
addHint (cpt.getId (), "metainfo-no-summary");
}

// inject package descriptions, if needed
foreach (cpt; cpts.byValue ()) {
// inject package descriptions, if needed
if (cpt.getKind () == ComponentKind.DESKTOP_APP) {
auto flags = cpt.getValueFlags;
cpt.setValueFlags (flags | AsValueFlags.NO_TRANSLATION_FALLBACK);
Expand All @@ -250,6 +269,22 @@ public:
}

}

// filter custom tags
auto customHashTable = cpt.getCustom ();
auto noCustomKeysAllowed = conf.allowedCustomKeys.length == 0;
if (customHashTable.size () > 0) {
import gi.glibtypes;

if (noCustomKeysAllowed) {
// if we don't allow any custom keys, we can delete them faster
customHashTable.removeAll ();
continue;
}

// filter the custom values
customHashTable.foreachRemove (&evaluateCustomEntry, &conf);
}
}
}

Expand Down

0 comments on commit a28b844

Please sign in to comment.