/SparkleLib/SparkleHelpers.cs

http://github.com/hbons/SparkleShare · C# · 103 lines · 55 code · 26 blank · 22 comment · 5 complexity · 7be5c6bfa64c856a4ef1f355c2791dc1 MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.IO;
  18. namespace SparkleLib {
  19. public static class SparkleHelpers {
  20. private static object debug_lock = new object ();
  21. // Show debug info if needed
  22. public static void DebugInfo (string type, string message)
  23. {
  24. if (!message.StartsWith ("["))
  25. message = " " + message;
  26. string timestamp = DateTime.Now.ToString ("HH:mm:ss");
  27. string line = timestamp + " " + "[" + type + "]" + message;
  28. if (SparkleConfig.DebugMode)
  29. Console.WriteLine (line);
  30. lock (debug_lock) {
  31. File.AppendAllText (
  32. SparkleConfig.DefaultConfig.LogFilePath,
  33. line + Environment.NewLine
  34. );
  35. }
  36. }
  37. // Makes it possible to combine more than
  38. // two paths at once
  39. public static string CombineMore (params string [] parts)
  40. {
  41. string new_path = "";
  42. foreach (string part in parts)
  43. new_path = Path.Combine (new_path, part);
  44. return new_path;
  45. }
  46. // Recursively sets access rights of a folder to 'Normal'
  47. public static void ClearAttributes (string path)
  48. {
  49. if (Directory.Exists (path)) {
  50. string [] folders = Directory .GetDirectories (path);
  51. foreach (string folder in folders)
  52. ClearAttributes (folder);
  53. string [] files = Directory .GetFiles(path);
  54. foreach (string file in files)
  55. if (!IsSymlink (file))
  56. File.SetAttributes (file, FileAttributes.Normal);
  57. }
  58. }
  59. // Check if a file is a symbolic link
  60. public static bool IsSymlink (string file)
  61. {
  62. FileAttributes attr = File.GetAttributes (file);
  63. return ((attr & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
  64. }
  65. // Converts a UNIX timestamp to a more usable time object
  66. public static DateTime UnixTimestampToDateTime (int timestamp)
  67. {
  68. DateTime unix_epoch = new DateTime (1970, 1, 1, 0, 0, 0, 0);
  69. return unix_epoch.AddSeconds (timestamp);
  70. }
  71. // Gets the relative path of two hierarchical absolute paths
  72. public static string DiffPaths (string target, string source)
  73. {
  74. return target.Replace (source + Path.DirectorySeparatorChar, "");
  75. }
  76. }
  77. }