SparkleShare/SparkleLib/SparkleFetcherBase.cs

188 lines
6.1 KiB
C#
Raw Normal View History

// SparkleShare, a collaboration and sharing tool.
// 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;
using System.Threading;
2011-05-31 01:26:57 +00:00
using Mono.Unix;
namespace SparkleLib {
// Sets up a fetcher that can get remote folders
public abstract class SparkleFetcherBase {
public delegate void StartedEventHandler ();
public delegate void FinishedEventHandler ();
public delegate void FailedEventHandler ();
public event StartedEventHandler Started;
public event FinishedEventHandler Finished;
public event FailedEventHandler Failed;
protected string target_folder;
protected string remote_url;
private Thread thread;
public abstract bool Fetch ();
2011-05-31 01:26:57 +00:00
public SparkleFetcherBase (string server, string remote_folder, string target_folder)
{
this.target_folder = target_folder;
2011-05-31 01:26:57 +00:00
this.remote_url = server + "/" + remote_folder;
}
// 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);
if (Started != null)
Started ();
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);
this.thread = new Thread (new ThreadStart (delegate {
if (Fetch ()) {
2011-05-31 01:26:57 +00:00
SparkleHelpers.DebugInfo ("Fetcher", "Finished");
EnableHostKeyCheckingForHost (host);
if (Finished != null)
Finished ();
2011-05-31 01:26:57 +00:00
} else {
2011-05-31 01:26:57 +00:00
SparkleHelpers.DebugInfo ("Fetcher", "Failed");
EnableHostKeyCheckingForHost (host);
if (Failed != null)
Failed ();
}
}));
this.thread.Start ();
}
2011-05-31 01:26:57 +00:00
public string RemoteUrl {
get {
return this.remote_url;
}
}
public void Dispose ()
{
if (this.thread != null) {
this.thread.Abort ();
this.thread.Join ();
}
}
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;
}
}
}