<commit_before>/*
   Copyright 2012 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package daemon.sshd

import notification.client._
import java.io.{OutputStream, InputStream}
import scala.actors.Actor
import org.eclipse.jgit.transport.{ReceivePack, UploadPack}
import net.liftweb.common._
import org.apache.sshd.server.{SessionAware, Environment, ExitCallback, Command => SshCommand}
import org.apache.sshd.server.session.ServerSession

import org.eclipse.jgit.lib.{Repository => JRepository, Constants}
import code.model._

trait CommandBase extends SshCommand with SessionAware with Loggable with daemon.Resolver {

  val repoPath: String

  protected var in: InputStream = _
  protected var out: OutputStream = _
  protected var err: OutputStream = _

  protected var callback: ExitCallback = _

  protected var onExit: Int = EXIT_SUCCESS

  protected var user: UserDoc = _
  protected var keys: Seq[SshKeyBase[_]] = _

  val EXIT_SUCCESS = 0
  val EXIT_ERROR = 127


  def setSession(session: ServerSession) {
    keys = session.getAttribute(DatabasePubKeyAuth.SSH_KEYS_KEY)
    user = session.getAttribute(DatabasePubKeyAuth.USER_KEY)
  }

  def setInputStream(in: InputStream) {
    this.in = in
  }

  def destroy() {}

  def setExitCallback(callback: ExitCallback) {
    this.callback = callback
  }

  def start(env: Environment) = {
    Actor.actor {
        try {
          run(env)
        } finally {
          in.close
          out.close
          err.close
          callback.onExit(onExit)
        }
      
    }
  }


  def run(env: Environment)

  def setErrorStream(err: OutputStream) {
    this.err = err
  }

  def setOutputStream(out: OutputStream) {
    this.out = out
  }

  protected def sendError(message: String) {
    err.write(Constants.encode(message + "\n"))
    err.flush
    onExit = EXIT_ERROR
  }

  def checkRepositoryAccess(r: RepositoryDoc) = {
    if(user.id.get == r.ownerId.get) { // user@server:repo 
      !keys.filter(_.acceptableFor(r)).isEmpty
    } else {// cuser@server:user/repo
      !r.collaborators.filter(_.login.get == user.login.get).isEmpty
    }
  }
}

class UploadPackCommand(val repoPath: String) extends CommandBase {
  logger.debug("Upload pack command executed")
  def run(env: Environment) = {
    for(proc <- packProcessing(repoByPath(repoPath, Some(user)), uploadPack, checkRepositoryAccess)) {
          proc(in, out, err)
    } 
  }
 
}

class ReceivePackCommand(val repoPath: String) extends CommandBase {
  logger.debug("Receive pack command executed")
  def run(env: Environment) = {
    for(proc <- packProcessing(repoByPath(repoPath, Some(user)), receivePack, checkRepositoryAccess)) {
          proc(in, out, err)
    } 
  }

  
}



class UnrecognizedCommand extends CommandBase {
  val repoPath = ""

  def run(env: Environment) = {
    sendError("This command doesn't supported by this server");
  }
}<commit_msg>Dont close mina streams, byt flush it, and close repo<commit_after>/*
   Copyright 2012 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package daemon.sshd

import notification.client._
import java.io.{OutputStream, InputStream}
import scala.actors.Actor
import org.eclipse.jgit.transport.{ReceivePack, UploadPack}
import net.liftweb.common._
import org.apache.sshd.server.{SessionAware, Environment, ExitCallback, Command => SshCommand}
import org.apache.sshd.server.session.ServerSession

import org.eclipse.jgit.lib.{Repository => JRepository, Constants}
import code.model._

trait CommandBase extends SshCommand with SessionAware with Loggable with daemon.Resolver {

  val repoPath: String

  protected var in: InputStream = _
  protected var out: OutputStream = _
  protected var err: OutputStream = _

  protected var callback: ExitCallback = _

  protected var onExit: Int = EXIT_SUCCESS

  protected var user: UserDoc = _
  protected var keys: Seq[SshKeyBase[_]] = _

  val EXIT_SUCCESS = 0
  val EXIT_ERROR = 127


  def setSession(session: ServerSession) {
    keys = session.getAttribute(DatabasePubKeyAuth.SSH_KEYS_KEY)
    user = session.getAttribute(DatabasePubKeyAuth.USER_KEY)
  }

  def setInputStream(in: InputStream) {
    this.in = in
  }

  def destroy() {}

  def setExitCallback(callback: ExitCallback) {
    this.callback = callback
  }

  def start(env: Environment) = {
    Actor.actor {
        try {
          run(env)
        } finally {
          try {
            out.flush
          } catch {
            case _ =>
          }
          try {
            err.flush
          } catch {
            case _ =>
          }
          callback.onExit(onExit)
        }
      
    }
  }


  def run(env: Environment)

  def setErrorStream(err: OutputStream) {
    this.err = err
  }

  def setOutputStream(out: OutputStream) {
    this.out = out
  }

  protected def sendError(message: String) {
    err.write(Constants.encode(message + "\n"))
    err.flush
    onExit = EXIT_ERROR
  }

  def checkRepositoryAccess(r: RepositoryDoc) = {
    if(user.id.get == r.ownerId.get) { // user@server:repo 
      !keys.filter(_.acceptableFor(r)).isEmpty
    } else {// cuser@server:user/repo
      !r.collaborators.filter(_.login.get == user.login.get).isEmpty
    }
  }
}

class UploadPackCommand(val repoPath: String) extends CommandBase {
  logger.debug("Upload pack command executed")
  def run(env: Environment) = {
    for {
      repo <- repoByPath(repoPath, Some(user))
      if checkRepositoryAccess(repo)
    } {
       val up = uploadPack(repo)
       up(in, out, err)
       repo.git.fs_repo_!.close
    } 
  }
 
}

class ReceivePackCommand(val repoPath: String) extends CommandBase {
  logger.debug("Receive pack command executed")
  def run(env: Environment) = {
    for {
      repo <- repoByPath(repoPath, Some(user))
      if checkRepositoryAccess(repo)
    } {
       val up = receivePack(repo)
       up(in, out, err)
       repo.git.fs_repo_!.close
    }
  }

  
}



class UnrecognizedCommand extends CommandBase {
  val repoPath = ""

  def run(env: Environment) = {
    sendError("This command doesn't supported by this server");
  }
}
{'fraction_non_alphanumeric': 0.09515083168875106, 'fraction_numerical': 0.0033831406822667043, 'mean_word_length': 3.075244112578978, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1874658', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2088, 'n_words': 582}