<commit_before>/*
 * @package jsDAV
 * @subpackage DAV
 * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
 * @author <NAME> <luis AT ajax DOT org>
 * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
 */

var jsDAV_Tree          = require("../tree").jsDAV_Tree,
    jsDAV_Ftp_Directory = require("../ftp/directory").jsDAV_Ftp_Directory,
    jsDAV_Ftp_File      = require("../ftp/file").jsDAV_Ftp_File,
    
    EventEmitter        = require("events").EventEmitter,
    Fs                  = require("fs"),
    Ftp                 = require("../../../support/node-ftp/ftp"),
    Util                = require("../util"),
    Exc                 = require("../exceptions");

/**
 * jsDAV_Tree_Ftp
 *
 * Creates this tree
 * Supply the path you'd like to share among with the options for the ftp connection
 *
 * @param {String} basePath
 * @contructor
 */
function jsDAV_Tree_Ftp(options) {
    this.basePath = options.node || "";
    this.options = options.ftp;
    this.ftp = new Ftp(options.ftp);
    this.ftp.$cache = {};
}

exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;

(function() {
    
    // Called by jsDAV_Ftp_Plugin.
    this.initialize = function() {
        var conn = this.ftp,
            user = this.options.user,
            pass = this.options.pass,
            self = this;
            
        var tmpFile = this.getRealPath("io.c9.tmp");
        
        conn.addListener("connect", function() {
            conn.auth(user, pass, function(err) {
                if (err)
                    return conn.emit("ftp.error", err);
                conn.put(new Buffer("Cloud9 FTP connection test."), tmpFile, function(err) {
                    if (err)
                        return conn.emit("ftp.error", err);
                    conn.lastMod(tmpFile, function(err, date) {
                        if (err)
                            return conn.emit("ftp.error", err);
                        var GMTDate = date;
                        conn.stat(tmpFile, function(err, stat) {
                            if (err)
                                return conn.emit("ftp.error", err);
                            var localDate = stat.getLastMod();
                            // Both dates were retrieved as GMT +0000 to make the comparison.
                            var timeDiff = localDate.getUTCHours() - GMTDate.getUTCHours();
                            // Save FTP server LIST cmd difference in hours.
                            Ftp.TZHourDiff = timeDiff + 1;
                            conn["delete"](tmpFile, function(err) {
                                if (err)
                                    return conn.emit("ftp.error", err);
                                conn.emit("ftp.ready");
                            });
                        });
                    });
                });
            });
        });
        
        conn.on("timeout", onServerFailed);
        conn.on("error", onServerFailed);
        conn.on("close", conn.connect);
        
        // Failsafe connection.
        try { conn.connect(); } catch(e) { onServerFailed(e.message); }
        
        function onServerFailed(err) {
            conn.emit("ftp.error", err);
            if (err/*.message && err.message.indexOf('ECONNRESET') > -1*/) {
                //self.$executeNext();
                return conn.connect();
            }
        }
    };
    
    /**
     * Returns a new node for the given path
     *
     * @param string path
     * @return void
     */
    this.getNodeForPath = function(path, next) {
        var self = this;<commit_msg>* fix for multiple connections for ftp<commit_after>/*
 * @package jsDAV
 * @subpackage DAV
 * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
 * @author <NAME> <luis AT ajax DOT org>
 * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
 */

var jsDAV_Tree          = require("../tree").jsDAV_Tree,
    jsDAV_Ftp_Directory = require("../ftp/directory").jsDAV_Ftp_Directory,
    jsDAV_Ftp_File      = require("../ftp/file").jsDAV_Ftp_File,
    
    EventEmitter        = require("events").EventEmitter,
    Fs                  = require("fs"),
    Ftp                 = require("../../../support/node-ftp/ftp"),
    Util                = require("../util"),
    Exc                 = require("../exceptions");

/**
 * jsDAV_Tree_Ftp
 *
 * Creates this tree
 * Supply the path you'd like to share among with the options for the ftp connection
 *
 * @param {String} basePath
 * @contructor
 */
function jsDAV_Tree_Ftp(options) {
    this.basePath = options.node || "";
    this.options = options.ftp;
    this.ftp = new Ftp(options.ftp);
    this.ftp.$cache = {};
}

exports.jsDAV_Tree_Ftp = jsDAV_Tree_Ftp;

(function() {
    
    // Called by jsDAV_Ftp_Plugin.
    this.initialize = function() {
        var conn = this.ftp,
            user = this.options.user,
            pass = this.options.pass,
            self = this;
            
        var tmpFile = this.getRealPath("io.c9.tmp");
        
        conn.addListener("connect", function() {
            conn.auth(user, pass, function(err) {
                if (err)
                    return conn.emit("ftp.error", err);
                conn.put(new Buffer("Cloud9 FTP connection test."), tmpFile, function(err) {
                    if (err)
                        return conn.emit("ftp.error", err);
                    conn.lastMod(tmpFile, function(err, date) {
                        if (err)
                            return conn.emit("ftp.error", err);
                        var GMTDate = date;
                        conn.stat(tmpFile, function(err, stat) {
                            if (err)
                                return conn.emit("ftp.error", err);
                            var localDate = stat.getLastMod();
                            // Both dates were retrieved as GMT +0000 to make the comparison.
                            var timeDiff = localDate.getUTCHours() - GMTDate.getUTCHours();
                            // Save FTP server LIST cmd difference in hours.
                            Ftp.TZHourDiff = timeDiff + 1;
                            conn["delete"](tmpFile, function(err) {
                                if (err)
                                    return conn.emit("ftp.error", err);
                                conn.emit("ftp.ready");
                            });
                        });
                    });
                });
            });
        });
        
        conn.on("timeout", onServerFailed);
        conn.on("error", onServerFailed);
//        conn.on("close", conn.connect);
        
        // Failsafe connection.
        try { conn.connect(); } catch(e) { onServerFailed(e.message); }
        
        function onServerFailed(err) {
            conn.emit("ftp.error", err);
            if (err/*.message && err.message.indexOf('ECONNRESET') > -1*/) {
                //self.$executeNext();
                return conn.connect();
            }
        }
    };
    
    /**
     * Returns a new node for the given path
     *
     * @param string path
     * @return void
     */
    this.getNodeForPath = function(path, next) {
        var self = this;
{'fraction_non_alphanumeric': 0.12546379002336128, 'fraction_numerical': 0.003298062388346846, 'mean_word_length': 1.368369671330947, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2086913', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1985, 'n_words': 437}