2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-16 20:19:58 +00:00
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Diagnostics;
|
2011-05-31 01:26:57 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2011-05-16 20:19:58 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
using Mono.Unix;
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
// Sets up a fetcher that can get remote folders
|
|
|
|
public abstract class SparkleFetcherBase {
|
|
|
|
|
2011-05-16 22:16:56 +00:00
|
|
|
public delegate void StartedEventHandler ();
|
|
|
|
public delegate void FinishedEventHandler ();
|
|
|
|
public delegate void FailedEventHandler ();
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
public event StartedEventHandler Started;
|
|
|
|
public event FinishedEventHandler Finished;
|
|
|
|
public event FailedEventHandler Failed;
|
|
|
|
|
|
|
|
protected string target_folder;
|
|
|
|
protected string remote_url;
|
|
|
|
private Thread thread;
|
|
|
|
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
public SparkleFetcherBase (string server, string remote_folder, string target_folder)
|
2011-05-16 20:19:58 +00:00
|
|
|
{
|
|
|
|
this.target_folder = target_folder;
|
2011-05-31 01:26:57 +00:00
|
|
|
this.remote_url = server + "/" + remote_folder;
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clones the remote repository
|
|
|
|
public void Start ()
|
|
|
|
{
|
2011-05-31 01:26:57 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder: " + this.remote_url);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
if (Started != null)
|
2011-05-16 22:16:56 +00:00
|
|
|
Started ();
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
if (Directory.Exists (this.target_folder))
|
|
|
|
Directory.Delete (this.target_folder, true);
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
string host = GetHost (this.remote_url);
|
|
|
|
|
|
|
|
if (String.IsNullOrEmpty (host)) {
|
|
|
|
if (Failed != null)
|
|
|
|
Failed ();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DisableHostKeyCheckingForHost (host);
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
this.thread = new Thread (new ThreadStart (delegate {
|
|
|
|
if (Fetch ()) {
|
2011-05-31 01:26:57 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Finished");
|
|
|
|
|
|
|
|
EnableHostKeyCheckingForHost (host);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
if (Finished != null)
|
2011-05-16 22:16:56 +00:00
|
|
|
Finished ();
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
} else {
|
2011-05-31 01:26:57 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Failed");
|
|
|
|
|
|
|
|
EnableHostKeyCheckingForHost (host);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
if (Failed != null)
|
2011-05-16 22:16:56 +00:00
|
|
|
Failed ();
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-31 01:26:57 +00:00
|
|
|
public string RemoteUrl {
|
|
|
|
get {
|
|
|
|
return this.remote_url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract bool Fetch ();
|
2011-05-31 01:26:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
private void DisableHostKeyCheckingForHost (string host)
|
|
|
|
{
|
|
|
|
string ssh_config_file_path = SparkleHelpers.CombineMore (
|
|
|
|
SparklePaths.HomePath, ".ssh", "config");
|
|
|
|
|
|
|
|
string ssh_config = Environment.NewLine + "Host " + host +
|
|
|
|
Environment.NewLine + "\tStrictHostKeyChecking no";
|
|
|
|
|
|
|
|
if (File.Exists (ssh_config_file_path)) {
|
|
|
|
TextWriter writer = File.AppendText (ssh_config_file_path);
|
|
|
|
writer.WriteLine (ssh_config);
|
|
|
|
writer.Close ();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
TextWriter writer = new StreamWriter (ssh_config_file_path);
|
|
|
|
writer.WriteLine (ssh_config);
|
|
|
|
writer.Close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path);
|
|
|
|
file_info.FileAccessPermissions = (FileAccessPermissions.UserRead |
|
|
|
|
FileAccessPermissions.UserWrite);
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Disabled host key checking");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void EnableHostKeyCheckingForHost (string host)
|
|
|
|
{
|
|
|
|
string ssh_config_file_path = SparkleHelpers.CombineMore (
|
|
|
|
SparklePaths.HomePath, ".ssh", "config");
|
|
|
|
|
|
|
|
string ssh_config = Environment.NewLine + "Host " + host +
|
|
|
|
Environment.NewLine + "\tStrictHostKeyChecking no";
|
|
|
|
|
|
|
|
if (File.Exists (ssh_config_file_path)) {
|
|
|
|
StreamReader reader = new StreamReader (ssh_config_file_path);
|
|
|
|
string current_ssh_config = reader.ReadToEnd ();
|
|
|
|
reader.Close ();
|
|
|
|
|
|
|
|
current_ssh_config = current_ssh_config.Remove (
|
|
|
|
current_ssh_config.IndexOf (ssh_config), ssh_config.Length);
|
|
|
|
|
|
|
|
bool has_some_ssh_config = new Regex (@"[a-z]").IsMatch (current_ssh_config);
|
|
|
|
if (!has_some_ssh_config) {
|
|
|
|
File.Delete (ssh_config_file_path);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
TextWriter writer = new StreamWriter (ssh_config_file_path);
|
|
|
|
writer.WriteLine (current_ssh_config);
|
|
|
|
writer.Close ();
|
|
|
|
|
|
|
|
UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path);
|
|
|
|
file_info.FileAccessPermissions = (FileAccessPermissions.UserRead |
|
|
|
|
FileAccessPermissions.UserWrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Enabled host key checking");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetHost (string url)
|
|
|
|
{
|
|
|
|
Regex regex = new Regex (@"(@|://)([a-z0-9\.]+)(/|:)");
|
|
|
|
Match match = regex.Match (url);
|
|
|
|
|
|
|
|
if (match.Success)
|
|
|
|
return match.Groups [2].Value;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
|
|
|
}
|