2017-04-01 17:36:06 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace SharpCifs.Util.Sharpen
|
|
|
|
{
|
|
|
|
public class FilePath
|
2017-07-07 20:12:21 -07:00
|
|
|
{
|
|
|
|
private string _path;
|
|
|
|
private static long _tempCounter;
|
|
|
|
|
|
|
|
public FilePath ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath (string path)
|
|
|
|
: this ((string) null, path)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath (FilePath other, string child)
|
|
|
|
: this ((string) other, child)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath (string other, string child)
|
|
|
|
{
|
|
|
|
if (other == null) {
|
|
|
|
_path = child;
|
|
|
|
} else {
|
|
|
|
while (!string.IsNullOrEmpty(child) && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
|
|
|
|
child = child.Substring (1);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
|
|
|
|
other += Path.DirectorySeparatorChar;
|
|
|
|
|
|
|
|
_path = Path.Combine (other, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static implicit operator FilePath (string name)
|
|
|
|
{
|
|
|
|
return new FilePath (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static implicit operator string (FilePath filePath)
|
|
|
|
{
|
|
|
|
return filePath == null ? null : filePath._path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals (object obj)
|
|
|
|
{
|
|
|
|
FilePath other = obj as FilePath;
|
|
|
|
if (other == null)
|
|
|
|
return false;
|
|
|
|
return GetCanonicalPath () == other.GetCanonicalPath ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode ()
|
|
|
|
{
|
|
|
|
return _path.GetHashCode ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CreateNewFile ()
|
|
|
|
{
|
|
|
|
try {
|
2017-04-01 17:36:06 -07:00
|
|
|
//Stream.`Close` method deleted
|
|
|
|
//File.Open (_path, FileMode.CreateNew).Close ();
|
|
|
|
File.Open(_path, FileMode.CreateNew).Dispose();
|
|
|
|
return true;
|
2017-07-07 20:12:21 -07:00
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FilePath CreateTempFile ()
|
|
|
|
{
|
|
|
|
return new FilePath (Path.GetTempFileName ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FilePath CreateTempFile (string prefix, string suffix)
|
|
|
|
{
|
|
|
|
return CreateTempFile (prefix, suffix, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FilePath CreateTempFile (string prefix, string suffix, FilePath directory)
|
|
|
|
{
|
|
|
|
string file;
|
|
|
|
if (prefix == null) {
|
|
|
|
throw new ArgumentNullException ("prefix");
|
|
|
|
}
|
|
|
|
if (prefix.Length < 3) {
|
|
|
|
throw new ArgumentException ("prefix must have at least 3 characters");
|
|
|
|
}
|
|
|
|
string str = (directory == null) ? Path.GetTempPath () : directory.GetPath ();
|
|
|
|
do {
|
|
|
|
file = Path.Combine (str, prefix + Interlocked.Increment (ref _tempCounter) + suffix);
|
|
|
|
} while (File.Exists (file));
|
|
|
|
|
|
|
|
new FileOutputStream (file).Close ();
|
|
|
|
return new FilePath (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteOnExit ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FilePath GetAbsoluteFile ()
|
|
|
|
{
|
|
|
|
return new FilePath (Path.GetFullPath (_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetAbsolutePath ()
|
|
|
|
{
|
|
|
|
return Path.GetFullPath (_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath GetCanonicalFile ()
|
|
|
|
{
|
|
|
|
return new FilePath (GetCanonicalPath ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetCanonicalPath ()
|
|
|
|
{
|
|
|
|
string p = Path.GetFullPath (_path);
|
|
|
|
p.TrimEnd (Path.DirectorySeparatorChar);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetName ()
|
|
|
|
{
|
|
|
|
return Path.GetFileName (_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath GetParentFile ()
|
|
|
|
{
|
|
|
|
return new FilePath (Path.GetDirectoryName (_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetPath ()
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsAbsolute ()
|
|
|
|
{
|
|
|
|
return Path.IsPathRooted (_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsDirectory ()
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.IsDirectory(this);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
|
2017-07-07 20:12:21 -07:00
|
|
|
public bool IsFile ()
|
|
|
|
{
|
|
|
|
return false; //FileHelper.Instance.IsFile (this);
|
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
|
2017-07-07 20:12:21 -07:00
|
|
|
public long LastModified ()
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return 0; // FileHelper.Instance.LastModified(this);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
|
2017-07-07 20:12:21 -07:00
|
|
|
public long Length ()
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return 0; // FileHelper.Instance.Length(this);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public string[] List ()
|
|
|
|
{
|
|
|
|
return List (null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string[] List (IFilenameFilter filter)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (IsFile ())
|
|
|
|
return null;
|
|
|
|
List<string> list = new List<string> ();
|
|
|
|
foreach (string filePth in Directory.GetFileSystemEntries (_path)) {
|
|
|
|
string fileName = Path.GetFileName (filePth);
|
|
|
|
if ((filter == null) || filter.Accept (this, fileName)) {
|
|
|
|
list.Add (fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list.ToArray ();
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public FilePath[] ListFiles ()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (IsFile ())
|
|
|
|
return null;
|
|
|
|
List<FilePath> list = new List<FilePath> ();
|
|
|
|
foreach (string filePath in Directory.GetFileSystemEntries (_path)) {
|
|
|
|
list.Add (new FilePath (filePath));
|
|
|
|
}
|
|
|
|
return list.ToArray ();
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MakeDirWritable (string dir)
|
|
|
|
{
|
|
|
|
//FileHelper.Instance.MakeDirWritable (dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MakeFileWritable (string file)
|
|
|
|
{
|
|
|
|
//FileHelper.Instance.MakeFileWritable (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Mkdir ()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (Directory.Exists (_path))
|
|
|
|
return false;
|
|
|
|
Directory.CreateDirectory (_path);
|
|
|
|
return true;
|
|
|
|
} catch (Exception) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Mkdirs ()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (Directory.Exists (_path))
|
|
|
|
return false;
|
|
|
|
Directory.CreateDirectory (_path);
|
|
|
|
return true;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool RenameTo (FilePath file)
|
|
|
|
{
|
|
|
|
return RenameTo (file._path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool RenameTo (string name)
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.RenameTo(this, name);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
|
2017-07-07 20:12:21 -07:00
|
|
|
public bool SetLastModified (long milis)
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.SetLastModified(this, milis);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
|
2017-07-07 20:12:21 -07:00
|
|
|
public bool SetReadOnly ()
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.SetReadOnly(this);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Uri ToUri ()
|
|
|
|
{
|
|
|
|
return new Uri (_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't change the case of this method, since ngit does reflection on it
|
|
|
|
public bool CanExecute ()
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.CanExecute(this);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't change the case of this method, since ngit does reflection on it
|
|
|
|
public bool SetExecutable (bool exec)
|
|
|
|
{
|
2017-04-01 17:36:06 -07:00
|
|
|
return false; // FileHelper.Instance.SetExecutable(this, exec);
|
2017-07-07 20:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetParent ()
|
|
|
|
{
|
|
|
|
string p = Path.GetDirectoryName (_path);
|
|
|
|
if (string.IsNullOrEmpty(p) || p == _path)
|
|
|
|
return null;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString ()
|
|
|
|
{
|
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static internal string PathSeparator {
|
|
|
|
get { return Path.PathSeparator.ToString (); }
|
|
|
|
}
|
|
|
|
|
|
|
|
static internal char PathSeparatorChar {
|
|
|
|
get { return Path.PathSeparator; }
|
|
|
|
}
|
|
|
|
|
|
|
|
static internal char SeparatorChar {
|
|
|
|
get { return Path.DirectorySeparatorChar; }
|
|
|
|
}
|
|
|
|
|
|
|
|
static internal string Separator {
|
|
|
|
get { return Path.DirectorySeparatorChar.ToString (); }
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 17:36:06 -07:00
|
|
|
}
|