src/Pure/General/ssh.scala
changeset 76117 531248fd8952
parent 76116 c4dc343fdbcb
child 76118 e8e3b60d8ecd
equal deleted inserted replaced
76116:c4dc343fdbcb 76117:531248fd8952
   154       catch { case exn: Throwable => fw.close(); proxy.close(); throw exn }
   154       catch { case exn: Throwable => fw.close(); proxy.close(); throw exn }
   155     }
   155     }
   156   }
   156   }
   157 
   157 
   158 
   158 
   159   /* logging */
       
   160 
       
   161   def logging(verbose: Boolean = true, debug: Boolean = false): Unit = {
       
   162     JSch.setLogger(if (verbose) new Logger(debug) else null)
       
   163   }
       
   164 
       
   165   private class Logger(debug: Boolean) extends JSch_Logger {
       
   166     def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug
       
   167 
       
   168     def log(level: Int, msg: String): Unit = {
       
   169       level match {
       
   170         case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg)
       
   171         case JSch_Logger.WARN => Output.warning(msg)
       
   172         case _ => Output.writeln(msg)
       
   173       }
       
   174     }
       
   175   }
       
   176 
       
   177 
       
   178   /* user info */
   159   /* user info */
   179 
   160 
   180   object No_User_Info extends UserInfo {
   161   object No_User_Info extends UserInfo {
   181     def getPassphrase: String = null
   162     def getPassphrase: String = null
   182     def getPassword: String = null
   163     def getPassword: String = null
   330     override def expand_path(path: Path): Path = path.expand_env(settings)
   311     override def expand_path(path: Path): Path = path.expand_env(settings)
   331     def remote_path(path: Path): String = expand_path(path).implode
   312     def remote_path(path: Path): String = expand_path(path).implode
   332     override def bash_path(path: Path): String = Bash.string(remote_path(path))
   313     override def bash_path(path: Path): String = Bash.string(remote_path(path))
   333 
   314 
   334     def chmod(permissions: Int, path: Path): Unit = sftp.chmod(permissions, remote_path(path))
   315     def chmod(permissions: Int, path: Path): Unit = sftp.chmod(permissions, remote_path(path))
   335     def mv(path1: Path, path2: Path): Unit = sftp.rename(remote_path(path1), remote_path(path2))
       
   336     def rm(path: Path): Unit = sftp.rm(remote_path(path))
   316     def rm(path: Path): Unit = sftp.rm(remote_path(path))
   337     def mkdir(path: Path): Unit = sftp.mkdir(remote_path(path))
   317     def mkdir(path: Path): Unit = sftp.mkdir(remote_path(path))
   338     def rmdir(path: Path): Unit = sftp.rmdir(remote_path(path))
       
   339 
   318 
   340     private def test_entry(path: Path, as_dir: Boolean): Boolean =
   319     private def test_entry(path: Path, as_dir: Boolean): Boolean =
   341       try {
   320       try {
   342         val is_dir = sftp.stat(remote_path(path)).isDir
   321         val is_dir = sftp.stat(remote_path(path)).isDir
   343         if (as_dir) is_dir else !is_dir
   322         if (as_dir) is_dir else !is_dir
   344       }
   323       }
   345       catch { case _: SftpException => false }
   324       catch { case _: SftpException => false }
   346 
   325 
   347     override def is_dir(path: Path): Boolean = test_entry(path, true)
   326     override def is_dir(path: Path): Boolean = test_entry(path, true)
   348     override def is_file(path: Path): Boolean = test_entry(path, false)
   327     override def is_file(path: Path): Boolean = test_entry(path, false)
   349 
       
   350     def is_link(path: Path): Boolean =
       
   351       try { sftp.lstat(remote_path(path)).isLink }
       
   352       catch { case _: SftpException => false }
       
   353 
   328 
   354     override def make_directory(path: Path): Path = {
   329     override def make_directory(path: Path): Path = {
   355       if (!is_dir(path)) {
   330       if (!is_dir(path)) {
   356         execute(
   331         execute(
   357           "perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"")
   332           "perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"")
   378             try { sftp.stat(dir_name + "/" + name).isDir }
   353             try { sftp.stat(dir_name + "/" + name).isDir }
   379             catch { case _: SftpException => false }
   354             catch { case _: SftpException => false }
   380           }
   355           }
   381           else attrs.isDir)
   356           else attrs.isDir)
   382       }).toList.sortBy(_.name)
   357       }).toList.sortBy(_.name)
   383     }
       
   384 
       
   385     def find_files(
       
   386       start: Path,
       
   387       pred: Path => Boolean = _ => true,
       
   388       include_dirs: Boolean = false,
       
   389       follow_links: Boolean = false
       
   390     ): List[Path] = {
       
   391       val result = new mutable.ListBuffer[Path]
       
   392       def check(path: Path): Unit = { if (pred(path)) result += path }
       
   393 
       
   394       def find(dir: Path): Unit = {
       
   395         if (include_dirs) check(dir)
       
   396         if (follow_links || !is_link(dir)) {
       
   397           for (entry <- read_dir(dir)) {
       
   398             val path = dir + Path.basic(entry.name)
       
   399             if (entry.is_file) check(path) else find(path)
       
   400           }
       
   401         }
       
   402       }
       
   403       if (is_file(start)) check(start) else find(start)
       
   404 
       
   405       result.toList
       
   406     }
   358     }
   407 
   359 
   408     def open_input(path: Path): InputStream = sftp.get(remote_path(path))
   360     def open_input(path: Path): InputStream = sftp.get(remote_path(path))
   409     def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
   361     def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
   410 
   362