{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/osquery/pulls/comments/86681172","pull_request_review_id":7328579,"id":86681172,"diff_hunk":"@@ -0,0 +1,416 @@\n+/*\n+ *  Copyright (c) 2014-present, Facebook, Inc.\n+ *  All rights reserved.\n+ *\n+ *  This source code is licensed under the BSD-style license found in the\n+ *  LICENSE file in the root directory of this source tree. An additional grant\n+ *  of patent rights can be found in the PATENTS file in the same directory.\n+ *\n+ *  Portage support by J.O. Aho <trizt@aho.hk>\n+ */\n+\n+#include <string>\n+#include <unordered_map>\n+#include <vector>\n+\n+#include <boost/algorithm/string.hpp>\n+#include <boost/algorithm/string/join.hpp>\n+#include <boost/algorithm/string/predicate.hpp>\n+#include <boost/algorithm/string/trim_all.hpp>\n+#include <boost/concept_check.hpp>\n+#include <boost/filesystem/operations.hpp>\n+#include <boost/filesystem/path.hpp>\n+\n+#include <osquery/core.h>\n+#include <osquery/filesystem.h>\n+#include <osquery/logger.h>\n+#include <osquery/tables.h>\n+\n+#include \"osquery/core/conversions.h\"\n+\n+#define fs boost::filesystem\n+\n+namespace osquery {\n+namespace tables {\n+\n+/* File paths used by portage */\n+fs::path portagePackageDir = \"/var/db/pkg\";\n+fs::path portageKeywords = \"/etc/portage/package.keywords\";\n+fs::path portageEtcMake = \"/etc/make.conf\";\n+fs::path portageEtcPortageMake = \"/etc/portage/make.conf\";\n+fs::path portageMask = \"/etc/portage/package.mask\";\n+fs::path portageUnMask = \"/etc/portage/package.unmask\";\n+fs::path portageUse = \"/etc/portage/package.use\";\n+fs::path portageWorld = \"/var/lib/portage/world\";\n+\n+/* Shared functions and structs */\n+\n+/**\n+ * @brief container to hold data before we store it to QueryData\n+ */\n+struct portagePackageHolder {\n+ public:\n+  std::string package;\n+  std::string version;\n+  std::string keyword;\n+  std::string mask;\n+  std::string unmask;\n+};\n+\n+/**\n+ * @brief split a package string with version into package name and package\n+ * version.\n+ *\n+ * we need to split a package stiring which includes the version it affects into\n+ * a pair of package name (first) and package version (second).\n+ */\n+std::pair<std::string, std::string> portageSplitPackageVersion(\n+    std::string pkgStr) {\n+  std::pair<std::string, std::string> package = std::make_pair(\"\", \"\");\n+  while (boost::starts_with(pkgStr, \">\") || boost::starts_with(pkgStr, \"<\") ||\n+         boost::starts_with(pkgStr, \"=\")) {\n+    if (!(package.second.length() == 0 && boost::starts_with(pkgStr, \"=\"))) {\n+      package.second += pkgStr.substr(0, 1);\n+    }\n+    pkgStr.erase(0, 1);\n+  }\n+  auto dpos = pkgStr.find('/');\n+  auto vpos = pkgStr.find_last_of('-');\n+  if (vpos != std::string::npos && vpos > dpos && vpos < pkgStr.length() - 1 &&\n+      isdigit(pkgStr[vpos + 1])) {\n+    package.second += pkgStr.substr(vpos + 1);\n+    pkgStr.erase(vpos, pkgStr.length() - vpos);\n+  } else {\n+    vpos = pkgStr.find_last_of('-', vpos - 1);\n+    if (vpos != std::string::npos && vpos > dpos &&\n+        vpos < pkgStr.length() - 1 && isdigit(pkgStr[vpos + 1])) {\n+      package.second += pkgStr.substr(vpos + 1);\n+      pkgStr.erase(vpos, pkgStr.length() - vpos);\n+    }\n+  }\n+  package.first = pkgStr;\n+  return package;\n+}\n+\n+/* Functions to fetch content for database tables */\n+QueryData parsePortagePackages(const fs::path& pkgpath,\n+                               const std::string& worldContent) {\n+  QueryData results;\n+\n+  fs::path full_path(fs::initial_path());\n+  full_path = fs::system_complete(fs::path(pkgpath));\n+\n+  if (!fs::exists(full_path)) {\n+    return results;\n+  }\n+\n+  // store world in a map so we can use find to see if\n+  // a package is in the world file\n+  std::unordered_map<std::string, bool> world = {};\n+  if (worldContent.length() > 0) {\n+    for (const auto& i : osquery::split(worldContent, \"\\n\")) {\n+      auto line = split(i);\n+      if (line.size() == 0 || boost::starts_with(line[0], \"#\")) {\n+        continue;\n+      }\n+      std::pair<std::string, std::string> package =\n+          portageSplitPackageVersion(line[0]);\n+      world.emplace(std::make_pair(package.first, true));\n+    }\n+  }\n+\n+  // This is a bit slow, would benefit if it had been multithreaded here\n+  fs::directory_iterator end_iter;\n+  for (fs::directory_iterator dir_itr(full_path); dir_itr != end_iter;","path":"osquery/tables/system/linux/portage.cpp","position":124,"original_position":124,"commit_id":"31c8f16d6298a1cf4f1d2fe1d4c87f13d8a629b9","original_commit_id":"31c8f16d6298a1cf4f1d2fe1d4c87f13d8a629b9","user":{"login":"theopolis","id":981645,"avatar_url":"https://avatars.githubusercontent.com/u/981645?v=3","gravatar_id":"","url":"https://api.github.com/users/theopolis","html_url":"https://github.com/theopolis","followers_url":"https://api.github.com/users/theopolis/followers","following_url":"https://api.github.com/users/theopolis/following{/other_user}","gists_url":"https://api.github.com/users/theopolis/gists{/gist_id}","starred_url":"https://api.github.com/users/theopolis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theopolis/subscriptions","organizations_url":"https://api.github.com/users/theopolis/orgs","repos_url":"https://api.github.com/users/theopolis/repos","events_url":"https://api.github.com/users/theopolis/events{/privacy}","received_events_url":"https://api.github.com/users/theopolis/received_events","type":"User","site_admin":false},"body":"Again, be careful! Maybe use our globbing APIs in `./osquery/filesystem.h`?","created_at":"2016-11-06T09:22:53Z","updated_at":"2016-11-06T09:22:53Z","html_url":"https://github.com/facebook/osquery/pull/2638#discussion_r86681172","pull_request_url":"https://api.github.com/repos/facebook/osquery/pulls/2638","_links":{"self":{"href":"https://api.github.com/repos/facebook/osquery/pulls/comments/86681172"},"html":{"href":"https://github.com/facebook/osquery/pull/2638#discussion_r86681172"},"pull_request":{"href":"https://api.github.com/repos/facebook/osquery/pulls/2638"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/osquery/pulls/2638","id":89863741,"html_url":"https://github.com/facebook/osquery/pull/2638","diff_url":"https://github.com/facebook/osquery/pull/2638.diff","patch_url":"https://github.com/facebook/osquery/pull/2638.patch","issue_url":"https://api.github.com/repos/facebook/osquery/issues/2638","number":2638,"state":"open","locked":false,"title":"Gentoo/Funtoo portage support","user":{"login":"trizt","id":22536847,"avatar_url":"https://avatars.githubusercontent.com/u/22536847?v=3","gravatar_id":"","url":"https://api.github.com/users/trizt","html_url":"https://github.com/trizt","followers_url":"https://api.github.com/users/trizt/followers","following_url":"https://api.github.com/users/trizt/following{/other_user}","gists_url":"https://api.github.com/users/trizt/gists{/gist_id}","starred_url":"https://api.github.com/users/trizt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trizt/subscriptions","organizations_url":"https://api.github.com/users/trizt/orgs","repos_url":"https://api.github.com/users/trizt/repos","events_url":"https://api.github.com/users/trizt/events{/privacy}","received_events_url":"https://api.github.com/users/trizt/received_events","type":"User","site_admin":false},"body":"Converts some of the portage files into osquery tables.\nShows correct version when querying os_version.\nMakes it also possible to build on Gentoo/Funtoo\n","created_at":"2016-10-18T17:37:18Z","updated_at":"2016-11-06T09:26:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f231add77c1c85775922db70edbcbf56de91405b","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/facebook/osquery/pulls/2638/commits","review_comments_url":"https://api.github.com/repos/facebook/osquery/pulls/2638/comments","review_comment_url":"https://api.github.com/repos/facebook/osquery/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/osquery/issues/2638/comments","statuses_url":"https://api.github.com/repos/facebook/osquery/statuses/31c8f16d6298a1cf4f1d2fe1d4c87f13d8a629b9","head":{"label":"trizt:develop","ref":"develop","sha":"31c8f16d6298a1cf4f1d2fe1d4c87f13d8a629b9","user":{"login":"trizt","id":22536847,"avatar_url":"https://avatars.githubusercontent.com/u/22536847?v=3","gravatar_id":"","url":"https://api.github.com/users/trizt","html_url":"https://github.com/trizt","followers_url":"https://api.github.com/users/trizt/followers","following_url":"https://api.github.com/users/trizt/following{/other_user}","gists_url":"https://api.github.com/users/trizt/gists{/gist_id}","starred_url":"https://api.github.com/users/trizt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trizt/subscriptions","organizations_url":"https://api.github.com/users/trizt/orgs","repos_url":"https://api.github.com/users/trizt/repos","events_url":"https://api.github.com/users/trizt/events{/privacy}","received_events_url":"https://api.github.com/users/trizt/received_events","type":"User","site_admin":false},"repo":{"id":69798521,"name":"osquery","full_name":"trizt/osquery","owner":{"login":"trizt","id":22536847,"avatar_url":"https://avatars.githubusercontent.com/u/22536847?v=3","gravatar_id":"","url":"https://api.github.com/users/trizt","html_url":"https://github.com/trizt","followers_url":"https://api.github.com/users/trizt/followers","following_url":"https://api.github.com/users/trizt/following{/other_user}","gists_url":"https://api.github.com/users/trizt/gists{/gist_id}","starred_url":"https://api.github.com/users/trizt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trizt/subscriptions","organizations_url":"https://api.github.com/users/trizt/orgs","repos_url":"https://api.github.com/users/trizt/repos","events_url":"https://api.github.com/users/trizt/events{/privacy}","received_events_url":"https://api.github.com/users/trizt/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/trizt/osquery","description":"SQL powered operating system instrumentation, monitoring, and analytics.","fork":true,"url":"https://api.github.com/repos/trizt/osquery","forks_url":"https://api.github.com/repos/trizt/osquery/forks","keys_url":"https://api.github.com/repos/trizt/osquery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/trizt/osquery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/trizt/osquery/teams","hooks_url":"https://api.github.com/repos/trizt/osquery/hooks","issue_events_url":"https://api.github.com/repos/trizt/osquery/issues/events{/number}","events_url":"https://api.github.com/repos/trizt/osquery/events","assignees_url":"https://api.github.com/repos/trizt/osquery/assignees{/user}","branches_url":"https://api.github.com/repos/trizt/osquery/branches{/branch}","tags_url":"https://api.github.com/repos/trizt/osquery/tags","blobs_url":"https://api.github.com/repos/trizt/osquery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/trizt/osquery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/trizt/osquery/git/refs{/sha}","trees_url":"https://api.github.com/repos/trizt/osquery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/trizt/osquery/statuses/{sha}","languages_url":"https://api.github.com/repos/trizt/osquery/languages","stargazers_url":"https://api.github.com/repos/trizt/osquery/stargazers","contributors_url":"https://api.github.com/repos/trizt/osquery/contributors","subscribers_url":"https://api.github.com/repos/trizt/osquery/subscribers","subscription_url":"https://api.github.com/repos/trizt/osquery/subscription","commits_url":"https://api.github.com/repos/trizt/osquery/commits{/sha}","git_commits_url":"https://api.github.com/repos/trizt/osquery/git/commits{/sha}","comments_url":"https://api.github.com/repos/trizt/osquery/comments{/number}","issue_comment_url":"https://api.github.com/repos/trizt/osquery/issues/comments{/number}","contents_url":"https://api.github.com/repos/trizt/osquery/contents/{+path}","compare_url":"https://api.github.com/repos/trizt/osquery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/trizt/osquery/merges","archive_url":"https://api.github.com/repos/trizt/osquery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/trizt/osquery/downloads","issues_url":"https://api.github.com/repos/trizt/osquery/issues{/number}","pulls_url":"https://api.github.com/repos/trizt/osquery/pulls{/number}","milestones_url":"https://api.github.com/repos/trizt/osquery/milestones{/number}","notifications_url":"https://api.github.com/repos/trizt/osquery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/trizt/osquery/labels{/name}","releases_url":"https://api.github.com/repos/trizt/osquery/releases{/id}","deployments_url":"https://api.github.com/repos/trizt/osquery/deployments","created_at":"2016-10-02T13:26:33Z","updated_at":"2016-10-02T13:26:36Z","pushed_at":"2016-10-23T14:58:00Z","git_url":"git://github.com/trizt/osquery.git","ssh_url":"git@github.com:trizt/osquery.git","clone_url":"https://github.com/trizt/osquery.git","svn_url":"https://github.com/trizt/osquery","homepage":"https://osquery.io","size":9378,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"release/2.0.0-19-gentoo"}},"base":{"label":"facebook:master","ref":"master","sha":"df25f27efb463666cf77b69c3cfea3eb70775917","user":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"repo":{"id":22394357,"name":"osquery","full_name":"facebook/osquery","owner":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/facebook/osquery","description":"SQL powered operating system instrumentation, monitoring, and analytics.","fork":false,"url":"https://api.github.com/repos/facebook/osquery","forks_url":"https://api.github.com/repos/facebook/osquery/forks","keys_url":"https://api.github.com/repos/facebook/osquery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/facebook/osquery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/facebook/osquery/teams","hooks_url":"https://api.github.com/repos/facebook/osquery/hooks","issue_events_url":"https://api.github.com/repos/facebook/osquery/issues/events{/number}","events_url":"https://api.github.com/repos/facebook/osquery/events","assignees_url":"https://api.github.com/repos/facebook/osquery/assignees{/user}","branches_url":"https://api.github.com/repos/facebook/osquery/branches{/branch}","tags_url":"https://api.github.com/repos/facebook/osquery/tags","blobs_url":"https://api.github.com/repos/facebook/osquery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/facebook/osquery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/facebook/osquery/git/refs{/sha}","trees_url":"https://api.github.com/repos/facebook/osquery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/facebook/osquery/statuses/{sha}","languages_url":"https://api.github.com/repos/facebook/osquery/languages","stargazers_url":"https://api.github.com/repos/facebook/osquery/stargazers","contributors_url":"https://api.github.com/repos/facebook/osquery/contributors","subscribers_url":"https://api.github.com/repos/facebook/osquery/subscribers","subscription_url":"https://api.github.com/repos/facebook/osquery/subscription","commits_url":"https://api.github.com/repos/facebook/osquery/commits{/sha}","git_commits_url":"https://api.github.com/repos/facebook/osquery/git/commits{/sha}","comments_url":"https://api.github.com/repos/facebook/osquery/comments{/number}","issue_comment_url":"https://api.github.com/repos/facebook/osquery/issues/comments{/number}","contents_url":"https://api.github.com/repos/facebook/osquery/contents/{+path}","compare_url":"https://api.github.com/repos/facebook/osquery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/facebook/osquery/merges","archive_url":"https://api.github.com/repos/facebook/osquery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/facebook/osquery/downloads","issues_url":"https://api.github.com/repos/facebook/osquery/issues{/number}","pulls_url":"https://api.github.com/repos/facebook/osquery/pulls{/number}","milestones_url":"https://api.github.com/repos/facebook/osquery/milestones{/number}","notifications_url":"https://api.github.com/repos/facebook/osquery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/facebook/osquery/labels{/name}","releases_url":"https://api.github.com/repos/facebook/osquery/releases{/id}","deployments_url":"https://api.github.com/repos/facebook/osquery/deployments","created_at":"2014-07-29T20:27:33Z","updated_at":"2016-11-06T06:16:26Z","pushed_at":"2016-11-06T07:39:22Z","git_url":"git://github.com/facebook/osquery.git","ssh_url":"git@github.com:facebook/osquery.git","clone_url":"https://github.com/facebook/osquery.git","svn_url":"https://github.com/facebook/osquery","homepage":"https://osquery.io","size":13650,"stargazers_count":7369,"watchers_count":7369,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":832,"mirror_url":null,"open_issues_count":77,"forks":832,"open_issues":77,"watchers":7369,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/osquery/pulls/2638"},"html":{"href":"https://github.com/facebook/osquery/pull/2638"},"issue":{"href":"https://api.github.com/repos/facebook/osquery/issues/2638"},"comments":{"href":"https://api.github.com/repos/facebook/osquery/issues/2638/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/osquery/pulls/2638/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/osquery/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/osquery/pulls/2638/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/osquery/statuses/31c8f16d6298a1cf4f1d2fe1d4c87f13d8a629b9"}}}}
{"actor":{"display_login":"theopolis"}}