<commit_before>// Copyright (C) 2011 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.ssh.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.Vector;
import java.util.NoSuchElementException;

import org.vngx.jsch.Session;
import org.vngx.jsch.ChannelType;
import org.vngx.jsch.ChannelSftp;
import org.vngx.jsch.exception.JSchException;

import org.joval.intf.io.IFile;
import org.joval.intf.io.IFilesystem;
import org.joval.intf.io.IRandomAccess;
import org.joval.intf.system.IBaseSession;
import org.joval.intf.system.IEnvironment;
import org.joval.intf.system.IProcess;
import org.joval.intf.unix.system.IUnixSession;
import org.joval.intf.util.IPathRedirector;
import org.joval.intf.util.tree.INode;
import org.joval.intf.util.tree.ITreeBuilder;
import org.joval.intf.system.IEnvironment;
import org.joval.util.tree.CachingTree;
import org.joval.util.tree.Tree;
import org.joval.util.JOVALMsg;
import org.joval.util.JOVALSystem;

/**
 * An implementation of the IFilesystem interface for SSH-connected sessions.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class SftpFilesystem extends CachingTree implements IFilesystem {
    final static char DELIM_CH = '/';
    final static String DELIM_STR = "/";

    private Session jschSession;
    private IBaseSession session;
    private IEnvironment env;
    private boolean autoExpand = true;
    private boolean preloaded = false;

    ChannelSftp cs;

    public SftpFilesystem(Session jschSession, IBaseSession session, IEnvironment env) {
	super();
	this.jschSession = jschSession;
	this.session = session;
	this.env = env;
    }

    public void setJschSession(Session jschSession) {
	this.jschSession = jschSession;
    }

    public void setAutoExpand(boolean autoExpand) {
	this.autoExpand = autoExpand;
    }

    // Implement IPathRedirector

    public String getRedirect(String path) {
	return path;
    }

    // Implement methods left abstract in CachingTree

    public boolean preload() {
	if (preloaded) {
	    return true;
	}

	ITreeBuilder tree = cache.getTreeBuilder("");
	if (tree == null) {
	    tree = new Tree("", DELIM_STR);
	    cache.addTree(tree);
	}
	try {
	    String command = null;
	    switch(session.getType()) {
	      case UNIX:
		switch(((IUnixSession)session).getFlavor()) {
		  //
		  // On Linux, we do not search under the /proc directory.  It's just a big mess under there.
		  //
		  case LINUX:
		    IFile[] roots = getFile(DELIM_STR).listFiles();
		    Collection<String> filtered = new Vector<String>();
		    for (int i=0; i < roots.length; i++) {
			if ("proc".equals(roots[i].getName())) {
			    filtered.add(roots[i].getPath());
			}
		    }
		    StringBuffer sb = new StringBuffer("find -L ");
		    for (String s : filtered) {
			sb.append(s).append(" ");
		    }
		    command = sb.toString().trim();
		    break;<commit_msg>An important little fix for Linux file searches.<commit_after>// Copyright (C) 2011 jOVAL.org.  All rights reserved.
// This software is licensed under the AGPL 3.0 license available at http://www.joval.org/agpl_v3.txt

package org.joval.ssh.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.Vector;
import java.util.NoSuchElementException;

import org.vngx.jsch.Session;
import org.vngx.jsch.ChannelType;
import org.vngx.jsch.ChannelSftp;
import org.vngx.jsch.exception.JSchException;

import org.joval.intf.io.IFile;
import org.joval.intf.io.IFilesystem;
import org.joval.intf.io.IRandomAccess;
import org.joval.intf.system.IBaseSession;
import org.joval.intf.system.IEnvironment;
import org.joval.intf.system.IProcess;
import org.joval.intf.unix.system.IUnixSession;
import org.joval.intf.util.IPathRedirector;
import org.joval.intf.util.tree.INode;
import org.joval.intf.util.tree.ITreeBuilder;
import org.joval.intf.system.IEnvironment;
import org.joval.util.tree.CachingTree;
import org.joval.util.tree.Tree;
import org.joval.util.JOVALMsg;
import org.joval.util.JOVALSystem;

/**
 * An implementation of the IFilesystem interface for SSH-connected sessions.
 *
 * @author <NAME>
 * @version %I% %G%
 */
public class SftpFilesystem extends CachingTree implements IFilesystem {
    final static char DELIM_CH = '/';
    final static String DELIM_STR = "/";

    private Session jschSession;
    private IBaseSession session;
    private IEnvironment env;
    private boolean autoExpand = true;
    private boolean preloaded = false;

    ChannelSftp cs;

    public SftpFilesystem(Session jschSession, IBaseSession session, IEnvironment env) {
	super();
	this.jschSession = jschSession;
	this.session = session;
	this.env = env;
    }

    public void setJschSession(Session jschSession) {
	this.jschSession = jschSession;
    }

    public void setAutoExpand(boolean autoExpand) {
	this.autoExpand = autoExpand;
    }

    // Implement IPathRedirector

    public String getRedirect(String path) {
	return path;
    }

    // Implement methods left abstract in CachingTree

    public boolean preload() {
	if (preloaded) {
	    return true;
	}

	ITreeBuilder tree = cache.getTreeBuilder("");
	if (tree == null) {
	    tree = new Tree("", DELIM_STR);
	    cache.addTree(tree);
	}
	try {
	    String command = null;
	    switch(session.getType()) {
	      case UNIX:
		switch(((IUnixSession)session).getFlavor()) {
		  //
		  // On Linux, we do not search under the /proc directory.  It's just a big mess under there.
		  //
		  case LINUX: {
		    IFile[] roots = getFile(DELIM_STR).listFiles();
		    Collection<String> filtered = new Vector<String>();
		    for (int i=0; i < roots.length; i++) {
			if (!"proc".equals(roots[i].getName())) {
			    filtered.add(roots[i].getPath());
			}
		    }
		    StringBuffer sb = new StringBuffer("find -L ");
		    for (String s : filtered) {
			sb.append(s).append(" ");
		    }
		    command = sb.toString().trim();
		    break;
		  }
{'fraction_non_alphanumeric': 0.11777707609725292, 'fraction_numerical': 0.0025260498894853173, 'mean_word_length': 3.6072727272727274, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '963566', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2041, 'n_words': 490}