{"action":"created","comment":{"url":"https://api.github.com/repos/amplab/tachyon/pulls/comments/47947088","id":47947088,"diff_hunk":"@@ -0,0 +1,246 @@\n+/*\n+ * Licensed to the University of California, Berkeley under one or more contributor license\n+ * agreements. See the NOTICE file distributed with this work for additional information regarding\n+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License. You may obtain a\n+ * copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software distributed under the License\n+ * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n+ * or implied. See the License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package tachyon.util;\n+\n+import java.io.BufferedReader;\n+import java.io.IOException;\n+import java.io.InputStream;\n+import java.io.InputStreamReader;\n+import java.nio.charset.Charset;\n+\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import tachyon.Constants;\n+\n+/** A base class for running a Unix command */\n+public abstract class ShellUtils {\n+  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);\n+  /** a Unix command to set permission */\n+  public static final String SET_PERMISSION_COMMAND = \"chmod\";\n+\n+  /** a Unix command to get a given user's groups list */\n+  public static String[] getGroupsForUserCommand(final String user) {\n+    return new String[] {\"bash\", \"-c\", \"id -gn \" + user + \"; id -Gn \" + user};\n+  }\n+\n+  /** Return a command to set permission */\n+  public static String[] getSetPermissionCommand(String perm, String filePath) {\n+    return new String[] {SET_PERMISSION_COMMAND, perm, filePath};\n+  }\n+\n+  /** Token separator regex used to parse Shell tool outputs */\n+  public static final String TOKEN_SEPARATOR_REGEX = \"[ \\t\\n\\r\\f]\";\n+\n+  private Process mProcess; // sub process used to execute the command\n+  private int mExitCode;\n+\n+  private ShellUtils() {}\n+\n+  /** check to see if a command needs to be executed and execute if needed */\n+  protected void run() throws IOException {\n+    mExitCode = 0; // reset for next run\n+    runCommand();\n+  }\n+\n+  /** Run a command */\n+  private void runCommand() throws IOException {\n+    ProcessBuilder builder = new ProcessBuilder(getExecString());\n+\n+    mProcess = builder.start();\n+\n+    BufferedReader inReader =\n+        new BufferedReader(new InputStreamReader(mProcess.getInputStream(),\n+            Charset.defaultCharset()));\n+    final StringBuffer errMsg = new StringBuffer();\n+\n+    // read input streams as this would free up the buffers\n+    try {\n+      parseExecResult(inReader); // parse the output\n+      // clear the input stream buffer\n+      String line = inReader.readLine();\n+      while (line != null) {\n+        line = inReader.readLine();\n+      }\n+      // wait for the process to finish and check the exit code\n+      mExitCode = mProcess.waitFor();\n+      if (mExitCode != 0) {\n+        throw new ExitCodeException(mExitCode, errMsg.toString());\n+      }\n+    } catch (InterruptedException ie) {\n+      throw new IOException(ie.toString());\n+    } finally {\n+      // close the input stream\n+      try {\n+        // JDK 7 tries to automatically drain the input streams for us\n+        // when the process exits, but since close is not synchronized,\n+        // it creates a race if we close the stream first and the same\n+        // fd is recycled. the stream draining thread will attempt to\n+        // drain that fd!! it may block, OOM, or cause bizarre behavior\n+        // see: https://bugs.openjdk.java.net/browse/JDK-8024521\n+        // issue is fixed in build 7u60\n+        InputStream stdout = mProcess.getInputStream();\n+        synchronized (stdout) {\n+          inReader.close();\n+        }\n+      } catch (IOException ioe) {\n+        LOG.warn(\"Error while closing the input stream\", ioe);\n+      }\n+      mProcess.destroy();\n+    }\n+  }\n+\n+  /** return an array containing the command name & its parameters */\n+  protected abstract String[] getExecString();\n+\n+  /** Parse the execution result */\n+  protected abstract void parseExecResult(BufferedReader lines) throws IOException;\n+\n+  /**\n+   * get the current sub-process executing the given command\n+   *\n+   * @return process executing the command\n+   */\n+  public Process getProcess() {\n+    return mProcess;\n+  }\n+\n+  /**\n+   * get the exit code\n+   *\n+   * @return the exit code of the process\n+   */\n+  public int getExitCode() {\n+    return mExitCode;\n+  }\n+\n+  /**\n+   * This is an IOException with exit code added.\n+   */\n+  public static class ExitCodeException extends IOException {\n+\n+    private static final long serialVersionUID = -6520494427049734809L;\n+\n+    private final int mExitCode;\n+\n+    public ExitCodeException(int exitCode, String message) {\n+      super(message);\n+      mExitCode = exitCode;\n+    }\n+\n+    public int getExitCode() {\n+      return mExitCode;\n+    }\n+\n+    @Override\n+    public String toString() {\n+      final StringBuilder sb = new StringBuilder(\"ExitCodeException \");\n+      sb.append(\"exitCode=\").append(mExitCode).append(\": \");\n+      sb.append(super.getMessage());\n+      return sb.toString();\n+    }\n+  }\n+\n+  public interface CommandExecutor {\n+\n+    void execute() throws IOException;\n+\n+    int getExitCode() throws IOException;\n+\n+    String getOutput() throws IOException;\n+\n+    void close();\n+\n+  }\n+\n+  /**\n+   * A simple shell command executor.\n+   *\n+   * <code>ShellCommandExecutor</code>should be used in cases where the output of the command needs\n+   * no explicit parsing and where the command, working directory and the environment remains\n+   * unchanged. The output of the command is stored as-is and is expected to be small.\n+   */\n+  public static class ShellCommandExecutor extends ShellUtils implements CommandExecutor {\n+\n+    private String[] mCommand;\n+    private StringBuffer mOutput;\n+","path":"common/src/main/java/tachyon/util/ShellUtils.java","position":180,"original_position":180,"commit_id":"03e51ae39c77e26271500fea187c16c1b17d8b0c","original_commit_id":"03e51ae39c77e26271500fea187c16c1b17d8b0c","user":{"login":"yupeng9","id":13425258,"avatar_url":"https://avatars.githubusercontent.com/u/13425258?v=3","gravatar_id":"","url":"https://api.github.com/users/yupeng9","html_url":"https://github.com/yupeng9","followers_url":"https://api.github.com/users/yupeng9/followers","following_url":"https://api.github.com/users/yupeng9/following{/other_user}","gists_url":"https://api.github.com/users/yupeng9/gists{/gist_id}","starred_url":"https://api.github.com/users/yupeng9/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yupeng9/subscriptions","organizations_url":"https://api.github.com/users/yupeng9/orgs","repos_url":"https://api.github.com/users/yupeng9/repos","events_url":"https://api.github.com/users/yupeng9/events{/privacy}","received_events_url":"https://api.github.com/users/yupeng9/received_events","type":"User","site_admin":false},"body":"Please add javadoc","created_at":"2015-12-17T19:09:48Z","updated_at":"2015-12-17T19:09:48Z","html_url":"https://github.com/amplab/tachyon/pull/2276#discussion_r47947088","pull_request_url":"https://api.github.com/repos/amplab/tachyon/pulls/2276","_links":{"self":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/comments/47947088"},"html":{"href":"https://github.com/amplab/tachyon/pull/2276#discussion_r47947088"},"pull_request":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/2276"}}},"pull_request":{"url":"https://api.github.com/repos/amplab/tachyon/pulls/2276","id":53809465,"html_url":"https://github.com/amplab/tachyon/pull/2276","diff_url":"https://github.com/amplab/tachyon/pull/2276.diff","patch_url":"https://github.com/amplab/tachyon/pull/2276.patch","issue_url":"https://api.github.com/repos/amplab/tachyon/issues/2276","number":2276,"state":"open","locked":false,"title":"[TACHYON-792] Group - Add User Group mapping service","user":{"login":"sundapeng","id":3818001,"avatar_url":"https://avatars.githubusercontent.com/u/3818001?v=3","gravatar_id":"","url":"https://api.github.com/users/sundapeng","html_url":"https://github.com/sundapeng","followers_url":"https://api.github.com/users/sundapeng/followers","following_url":"https://api.github.com/users/sundapeng/following{/other_user}","gists_url":"https://api.github.com/users/sundapeng/gists{/gist_id}","starred_url":"https://api.github.com/users/sundapeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sundapeng/subscriptions","organizations_url":"https://api.github.com/users/sundapeng/orgs","repos_url":"https://api.github.com/users/sundapeng/repos","events_url":"https://api.github.com/users/sundapeng/events{/privacy}","received_events_url":"https://api.github.com/users/sundapeng/received_events","type":"User","site_admin":false},"body":"https://tachyon.atlassian.net/browse/TACHYON-792","created_at":"2015-12-16T07:58:29Z","updated_at":"2015-12-17T19:09:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"92128bdc2c0e205519c8a725ffa109e69a216913","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/amplab/tachyon/pulls/2276/commits","review_comments_url":"https://api.github.com/repos/amplab/tachyon/pulls/2276/comments","review_comment_url":"https://api.github.com/repos/amplab/tachyon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/amplab/tachyon/issues/2276/comments","statuses_url":"https://api.github.com/repos/amplab/tachyon/statuses/03e51ae39c77e26271500fea187c16c1b17d8b0c","head":{"label":"sundapeng:TACHYON-792","ref":"TACHYON-792","sha":"03e51ae39c77e26271500fea187c16c1b17d8b0c","user":{"login":"sundapeng","id":3818001,"avatar_url":"https://avatars.githubusercontent.com/u/3818001?v=3","gravatar_id":"","url":"https://api.github.com/users/sundapeng","html_url":"https://github.com/sundapeng","followers_url":"https://api.github.com/users/sundapeng/followers","following_url":"https://api.github.com/users/sundapeng/following{/other_user}","gists_url":"https://api.github.com/users/sundapeng/gists{/gist_id}","starred_url":"https://api.github.com/users/sundapeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sundapeng/subscriptions","organizations_url":"https://api.github.com/users/sundapeng/orgs","repos_url":"https://api.github.com/users/sundapeng/repos","events_url":"https://api.github.com/users/sundapeng/events{/privacy}","received_events_url":"https://api.github.com/users/sundapeng/received_events","type":"User","site_admin":false},"repo":{"id":39428718,"name":"tachyon","full_name":"sundapeng/tachyon","owner":{"login":"sundapeng","id":3818001,"avatar_url":"https://avatars.githubusercontent.com/u/3818001?v=3","gravatar_id":"","url":"https://api.github.com/users/sundapeng","html_url":"https://github.com/sundapeng","followers_url":"https://api.github.com/users/sundapeng/followers","following_url":"https://api.github.com/users/sundapeng/following{/other_user}","gists_url":"https://api.github.com/users/sundapeng/gists{/gist_id}","starred_url":"https://api.github.com/users/sundapeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sundapeng/subscriptions","organizations_url":"https://api.github.com/users/sundapeng/orgs","repos_url":"https://api.github.com/users/sundapeng/repos","events_url":"https://api.github.com/users/sundapeng/events{/privacy}","received_events_url":"https://api.github.com/users/sundapeng/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sundapeng/tachyon","description":" A Reliable Memory Centric Distributed Storage System","fork":true,"url":"https://api.github.com/repos/sundapeng/tachyon","forks_url":"https://api.github.com/repos/sundapeng/tachyon/forks","keys_url":"https://api.github.com/repos/sundapeng/tachyon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sundapeng/tachyon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sundapeng/tachyon/teams","hooks_url":"https://api.github.com/repos/sundapeng/tachyon/hooks","issue_events_url":"https://api.github.com/repos/sundapeng/tachyon/issues/events{/number}","events_url":"https://api.github.com/repos/sundapeng/tachyon/events","assignees_url":"https://api.github.com/repos/sundapeng/tachyon/assignees{/user}","branches_url":"https://api.github.com/repos/sundapeng/tachyon/branches{/branch}","tags_url":"https://api.github.com/repos/sundapeng/tachyon/tags","blobs_url":"https://api.github.com/repos/sundapeng/tachyon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sundapeng/tachyon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sundapeng/tachyon/git/refs{/sha}","trees_url":"https://api.github.com/repos/sundapeng/tachyon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sundapeng/tachyon/statuses/{sha}","languages_url":"https://api.github.com/repos/sundapeng/tachyon/languages","stargazers_url":"https://api.github.com/repos/sundapeng/tachyon/stargazers","contributors_url":"https://api.github.com/repos/sundapeng/tachyon/contributors","subscribers_url":"https://api.github.com/repos/sundapeng/tachyon/subscribers","subscription_url":"https://api.github.com/repos/sundapeng/tachyon/subscription","commits_url":"https://api.github.com/repos/sundapeng/tachyon/commits{/sha}","git_commits_url":"https://api.github.com/repos/sundapeng/tachyon/git/commits{/sha}","comments_url":"https://api.github.com/repos/sundapeng/tachyon/comments{/number}","issue_comment_url":"https://api.github.com/repos/sundapeng/tachyon/issues/comments{/number}","contents_url":"https://api.github.com/repos/sundapeng/tachyon/contents/{+path}","compare_url":"https://api.github.com/repos/sundapeng/tachyon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sundapeng/tachyon/merges","archive_url":"https://api.github.com/repos/sundapeng/tachyon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sundapeng/tachyon/downloads","issues_url":"https://api.github.com/repos/sundapeng/tachyon/issues{/number}","pulls_url":"https://api.github.com/repos/sundapeng/tachyon/pulls{/number}","milestones_url":"https://api.github.com/repos/sundapeng/tachyon/milestones{/number}","notifications_url":"https://api.github.com/repos/sundapeng/tachyon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sundapeng/tachyon/labels{/name}","releases_url":"https://api.github.com/repos/sundapeng/tachyon/releases{/id}","created_at":"2015-07-21T06:40:32Z","updated_at":"2015-07-21T06:40:34Z","pushed_at":"2015-12-17T05:59:21Z","git_url":"git://github.com/sundapeng/tachyon.git","ssh_url":"git@github.com:sundapeng/tachyon.git","clone_url":"https://github.com/sundapeng/tachyon.git","svn_url":"https://github.com/sundapeng/tachyon","homepage":"http://tachyon-project.org","size":32442,"stargazers_count":0,"watchers_count":0,"language":"Java","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":"master"}},"base":{"label":"amplab:master","ref":"master","sha":"5c10a96522c69b1c5a3f6e20339b9db3d3ca940f","user":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"repo":{"id":7276954,"name":"tachyon","full_name":"amplab/tachyon","owner":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/amplab/tachyon","description":"Memory Centric Reliable Distributed Storage","fork":false,"url":"https://api.github.com/repos/amplab/tachyon","forks_url":"https://api.github.com/repos/amplab/tachyon/forks","keys_url":"https://api.github.com/repos/amplab/tachyon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/amplab/tachyon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/amplab/tachyon/teams","hooks_url":"https://api.github.com/repos/amplab/tachyon/hooks","issue_events_url":"https://api.github.com/repos/amplab/tachyon/issues/events{/number}","events_url":"https://api.github.com/repos/amplab/tachyon/events","assignees_url":"https://api.github.com/repos/amplab/tachyon/assignees{/user}","branches_url":"https://api.github.com/repos/amplab/tachyon/branches{/branch}","tags_url":"https://api.github.com/repos/amplab/tachyon/tags","blobs_url":"https://api.github.com/repos/amplab/tachyon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/amplab/tachyon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/amplab/tachyon/git/refs{/sha}","trees_url":"https://api.github.com/repos/amplab/tachyon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/amplab/tachyon/statuses/{sha}","languages_url":"https://api.github.com/repos/amplab/tachyon/languages","stargazers_url":"https://api.github.com/repos/amplab/tachyon/stargazers","contributors_url":"https://api.github.com/repos/amplab/tachyon/contributors","subscribers_url":"https://api.github.com/repos/amplab/tachyon/subscribers","subscription_url":"https://api.github.com/repos/amplab/tachyon/subscription","commits_url":"https://api.github.com/repos/amplab/tachyon/commits{/sha}","git_commits_url":"https://api.github.com/repos/amplab/tachyon/git/commits{/sha}","comments_url":"https://api.github.com/repos/amplab/tachyon/comments{/number}","issue_comment_url":"https://api.github.com/repos/amplab/tachyon/issues/comments{/number}","contents_url":"https://api.github.com/repos/amplab/tachyon/contents/{+path}","compare_url":"https://api.github.com/repos/amplab/tachyon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/amplab/tachyon/merges","archive_url":"https://api.github.com/repos/amplab/tachyon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/amplab/tachyon/downloads","issues_url":"https://api.github.com/repos/amplab/tachyon/issues{/number}","pulls_url":"https://api.github.com/repos/amplab/tachyon/pulls{/number}","milestones_url":"https://api.github.com/repos/amplab/tachyon/milestones{/number}","notifications_url":"https://api.github.com/repos/amplab/tachyon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/amplab/tachyon/labels{/name}","releases_url":"https://api.github.com/repos/amplab/tachyon/releases{/id}","created_at":"2012-12-21T17:43:46Z","updated_at":"2015-12-16T14:18:59Z","pushed_at":"2015-12-17T17:07:33Z","git_url":"git://github.com/amplab/tachyon.git","ssh_url":"git@github.com:amplab/tachyon.git","clone_url":"https://github.com/amplab/tachyon.git","svn_url":"https://github.com/amplab/tachyon","homepage":"http://tachyon-project.org","size":41497,"stargazers_count":1811,"watchers_count":1811,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":717,"mirror_url":null,"open_issues_count":28,"forks":717,"open_issues":28,"watchers":1811,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/2276"},"html":{"href":"https://github.com/amplab/tachyon/pull/2276"},"issue":{"href":"https://api.github.com/repos/amplab/tachyon/issues/2276"},"comments":{"href":"https://api.github.com/repos/amplab/tachyon/issues/2276/comments"},"review_comments":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/2276/comments"},"review_comment":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/2276/commits"},"statuses":{"href":"https://api.github.com/repos/amplab/tachyon/statuses/03e51ae39c77e26271500fea187c16c1b17d8b0c"}}}}