~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.IO;
using Mono.Unix;

namespace Sharpen.Unix
{
	class UnixFileHelper : FileHelper
	{
		static UnixFileSystemInfo GetUnixFileInfo (string path)
		{
			try {
				return Mono.Unix.UnixFileInfo.GetFileSystemEntry (path);
			} catch (DirectoryNotFoundException ex) {
				// If we have a file /foo/bar and probe the path /foo/bar/baz, we get a DirectoryNotFound exception
				// because 'bar' is a file and therefore 'baz' cannot possibly exist. This is annoying.
				var inner = ex.InnerException as UnixIOException;
				if (inner != null && inner.ErrorCode == Mono.Unix.Native.Errno.ENOTDIR)
					return null;
				throw;
			}
		}
		
		public override bool CanExecute (FilePath path)
		{
			UnixFileInfo fi = new UnixFileInfo (path);
			if (!fi.Exists)
				return false;
			return 0 != (fi.FileAccessPermissions & (FileAccessPermissions.UserExecute | FileAccessPermissions.GroupExecute | FileAccessPermissions.OtherExecute));
		}
		
		public override bool CanWrite (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			return info != null && info.CanAccess (Mono.Unix.Native.AccessModes.W_OK);
		}
		
		public override bool Delete (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			if (info != null && info.Exists) {
				try {
					info.Delete ();
					return true;
				} catch {
					// If the directory is not empty we return false. JGit relies on this
					return false;
				}
			}
			return false;
		}
		
		public override bool Exists (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			return info != null && info.Exists;
		}
		
		public override bool IsDirectory (FilePath path)
		{
			try {
				var info = GetUnixFileInfo (path);
				return info != null && info.Exists && info.FileType == FileTypes.Directory;
			} catch (DirectoryNotFoundException) {
				// If the file /foo/bar exists and we query to see if /foo/bar/baz exists, we get a
				// DirectoryNotFound exception for Mono.Unix. In this case the directory definitely
				// does not exist.
				return false;
			}
		}
		
		public override bool IsFile (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			return info != null && info.Exists && (info.FileType == FileTypes.RegularFile || info.FileType == FileTypes.SymbolicLink);
		}
		
		public override long LastModified (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			return info != null && info.Exists ? info.LastWriteTimeUtc.ToMillisecondsSinceEpoch() : 0;
		}
		
		public override long Length (FilePath path)
		{
			var info = GetUnixFileInfo (path);
			return info != null && info.Exists ? info.Length : 0;
		}
		
		public override void MakeFileWritable (FilePath file)
		{
			var info = GetUnixFileInfo (file);
			if (info != null)
				info.FileAccessPermissions |= (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
		}
		
		public override bool RenameTo (FilePath path, string name)
		{
			var symlink = GetUnixFileInfo (path) as UnixSymbolicLinkInfo;
			if (symlink != null) {
				var newFile = new UnixSymbolicLinkInfo (name);
				newFile.CreateSymbolicLinkTo (symlink.ContentsPath);
				return true;
			} else {
				// This call replaces the file if it already exists.
				// File.Move throws an exception if dest already exists
				return Mono.Unix.Native.Stdlib.rename (path, name) == 0;
			}
		}
		
		public override bool SetExecutable (FilePath path, bool exec)
		{
			UnixFileInfo fi = new UnixFileInfo (path);
			FileAccessPermissions perms = fi.FileAccessPermissions;
			if (exec) {
				if (perms.HasFlag (FileAccessPermissions.UserRead))
					perms |= FileAccessPermissions.UserExecute;
				if (perms.HasFlag (FileAccessPermissions.OtherRead))
					perms |= FileAccessPermissions.OtherExecute;
				if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
					perms |= FileAccessPermissions.GroupExecute;
			} else {
				if (perms.HasFlag (FileAccessPermissions.UserRead))
					perms &= ~FileAccessPermissions.UserExecute;
				if (perms.HasFlag (FileAccessPermissions.OtherRead))
					perms &= ~FileAccessPermissions.OtherExecute;
				if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
					perms &= ~FileAccessPermissions.GroupExecute;
			}
			fi.FileAccessPermissions = perms;
			return true;
		}
		
		public override bool SetLastModified(FilePath path, long milis)
		{
			// How can the last write time be set on a symlink?
			return base.SetLastModified(path, milis);
		}
		
		public override bool SetReadOnly (FilePath path)
		{
			try {
				var info = GetUnixFileInfo (path);
				if (info != null)
					info.FileAccessPermissions &= ~ (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
				return true;
			} catch {
				return false;
			}
		}
	}
}