2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2010-05-06 00:17:00 +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
|
2010-11-21 12:33:24 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-05-06 00:17:00 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
|
2010-05-06 00:17:00 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2010-08-02 15:42:42 +00:00
|
|
|
namespace SparkleLib {
|
2011-05-11 19:29:29 +00:00
|
|
|
|
|
|
|
public static class SparkleHelpers {
|
2010-07-04 22:41:47 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
public static bool ShowDebugInfo = true;
|
2012-01-13 23:46:32 +00:00
|
|
|
private static object debug_lock = new object ();
|
2010-07-04 22:41:47 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
// Show debug info if needed
|
|
|
|
public static void DebugInfo (string type, string message)
|
|
|
|
{
|
|
|
|
if (ShowDebugInfo) {
|
|
|
|
string timestamp = DateTime.Now.ToString ("HH:mm:ss");
|
2010-08-16 11:12:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
if (!message.StartsWith ("["))
|
|
|
|
message = " " + message;
|
2010-08-16 11:12:20 +00:00
|
|
|
|
2012-01-11 23:02:22 +00:00
|
|
|
string line = timestamp + " " + "[" + type + "]" + message;
|
|
|
|
|
|
|
|
if (SparkleConfig.DefaultConfig.DebugMode)
|
|
|
|
Console.WriteLine (line);
|
|
|
|
|
2012-01-13 23:46:32 +00:00
|
|
|
lock (debug_lock) {
|
|
|
|
File.AppendAllText (
|
|
|
|
SparkleConfig.DefaultConfig.LogFilePath,
|
|
|
|
line + Environment.NewLine
|
|
|
|
);
|
|
|
|
}
|
2011-05-11 19:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-16 11:12:20 +00:00
|
|
|
|
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
// Makes it possible to combine more than
|
|
|
|
// two paths at once
|
|
|
|
public static string CombineMore (params string [] parts)
|
|
|
|
{
|
|
|
|
string new_path = "";
|
2010-08-16 11:12:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
foreach (string part in parts)
|
|
|
|
new_path = Path.Combine (new_path, part);
|
2010-08-16 11:12:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
return new_path;
|
|
|
|
}
|
2010-08-16 11:12:20 +00:00
|
|
|
|
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
// Recursively sets access rights of a folder to 'Normal'
|
|
|
|
public static void ClearAttributes (string path)
|
|
|
|
{
|
|
|
|
if (Directory.Exists (path)) {
|
|
|
|
string [] folders = Directory .GetDirectories (path);
|
2010-09-04 15:23:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
foreach (string folder in folders)
|
|
|
|
ClearAttributes (folder);
|
2010-09-04 15:23:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
string [] files = Directory .GetFiles(path);
|
2010-09-04 15:23:20 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
foreach (string file in files)
|
2011-10-01 18:08:05 +00:00
|
|
|
if (!IsSymlink (file))
|
|
|
|
File.SetAttributes (file, FileAttributes.Normal);
|
2011-05-11 19:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-04 15:23:20 +00:00
|
|
|
|
2011-10-23 12:48:13 +00:00
|
|
|
|
2011-10-01 18:08:05 +00:00
|
|
|
// Check if a file is a symbolic link
|
|
|
|
public static bool IsSymlink (string file)
|
|
|
|
{
|
|
|
|
FileAttributes attr = File.GetAttributes (file);
|
|
|
|
|
2011-10-23 12:48:13 +00:00
|
|
|
return ((attr & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
|
2011-10-01 18:08:05 +00:00
|
|
|
}
|
2010-05-06 00:17:00 +00:00
|
|
|
|
2011-10-23 12:48:13 +00:00
|
|
|
|
2011-05-11 19:29:29 +00:00
|
|
|
// Converts a UNIX timestamp to a more usable time object
|
|
|
|
public static DateTime UnixTimestampToDateTime (int timestamp)
|
|
|
|
{
|
|
|
|
DateTime unix_epoch = new DateTime (1970, 1, 1, 0, 0, 0, 0);
|
|
|
|
return unix_epoch.AddSeconds (timestamp);
|
|
|
|
}
|
2011-10-23 12:48:13 +00:00
|
|
|
|
2011-10-24 14:33:37 +00:00
|
|
|
|
|
|
|
// Gets the relative path of two hierarchical absolute paths
|
|
|
|
public static string DiffPaths (string target, string source)
|
|
|
|
{
|
|
|
|
return target.Replace (source + Path.DirectorySeparatorChar, "");
|
|
|
|
}
|
2011-10-23 12:48:13 +00:00
|
|
|
}
|
2010-05-06 00:17:00 +00:00
|
|
|
}
|
2011-10-23 12:48:13 +00:00
|
|
|
|