PageRenderTime 115ms CodeModel.GetById 110ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17
 18using System;
 19using System.IO;
 20
 21namespace SparkleLib {
 22    
 23    public static class SparkleHelpers {
 24
 25        private static object debug_lock = new object ();
 26
 27        // Show debug info if needed
 28        public static void DebugInfo (string type, string message)
 29        {
 30            if (!message.StartsWith ("["))
 31                message = " " + message;
 32
 33            string timestamp = DateTime.Now.ToString ("HH:mm:ss");
 34            string line      = timestamp + " " + "[" + type + "]" + message;
 35
 36            if (SparkleConfig.DebugMode)
 37                Console.WriteLine (line);
 38
 39            lock (debug_lock) {
 40                File.AppendAllText (
 41                    SparkleConfig.DefaultConfig.LogFilePath,
 42                    line + Environment.NewLine
 43                );
 44            }
 45        }
 46
 47
 48        // Makes it possible to combine more than
 49        // two paths at once
 50        public static string CombineMore (params string [] parts)
 51        {
 52            string new_path = "";
 53
 54            foreach (string part in parts)
 55                new_path = Path.Combine (new_path, part);
 56
 57            return new_path;
 58        }
 59
 60
 61        // Recursively sets access rights of a folder to 'Normal'
 62        public static void ClearAttributes (string path)
 63        {
 64            if (Directory.Exists (path)) {
 65                string [] folders = Directory .GetDirectories (path);
 66
 67                foreach (string folder in folders)
 68                    ClearAttributes (folder);
 69
 70                string [] files = Directory .GetFiles(path);
 71
 72                foreach (string file in files)
 73                    if (!IsSymlink (file))
 74                        File.SetAttributes (file, FileAttributes.Normal);
 75            }
 76        }
 77
 78
 79        // Check if a file is a symbolic link
 80        public static bool IsSymlink (string file)
 81        {
 82            FileAttributes attr = File.GetAttributes (file);
 83
 84            return ((attr & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint);
 85        }
 86
 87
 88        // Converts a UNIX timestamp to a more usable time object
 89        public static DateTime UnixTimestampToDateTime (int timestamp)
 90        {
 91            DateTime unix_epoch = new DateTime (1970, 1, 1, 0, 0, 0, 0);
 92            return unix_epoch.AddSeconds (timestamp);
 93        }
 94
 95
 96        // Gets the relative path of two hierarchical absolute paths	
 97        public static string DiffPaths (string target, string source)
 98        {
 99            return target.Replace (source + Path.DirectorySeparatorChar, "");      
100        }
101    }
102}
103