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

« back to all changes in this revision

Viewing changes to external/ngit/Sharpen.Unix/UnixFileHelper.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.IO;
 
3
using Mono.Unix;
 
4
 
 
5
namespace Sharpen.Unix
 
6
{
 
7
        class UnixFileHelper : FileHelper
 
8
        {
 
9
                static UnixFileSystemInfo GetUnixFileInfo (string path)
 
10
                {
 
11
                        try {
 
12
                                return Mono.Unix.UnixFileInfo.GetFileSystemEntry (path);
 
13
                        } catch (DirectoryNotFoundException ex) {
 
14
                                // If we have a file /foo/bar and probe the path /foo/bar/baz, we get a DirectoryNotFound exception
 
15
                                // because 'bar' is a file and therefore 'baz' cannot possibly exist. This is annoying.
 
16
                                var inner = ex.InnerException as UnixIOException;
 
17
                                if (inner != null && inner.ErrorCode == Mono.Unix.Native.Errno.ENOTDIR)
 
18
                                        return null;
 
19
                                throw;
 
20
                        }
 
21
                }
 
22
                
 
23
                public override bool CanExecute (FilePath path)
 
24
                {
 
25
                        UnixFileInfo fi = new UnixFileInfo (path);
 
26
                        if (!fi.Exists)
 
27
                                return false;
 
28
                        return 0 != (fi.FileAccessPermissions & (FileAccessPermissions.UserExecute | FileAccessPermissions.GroupExecute | FileAccessPermissions.OtherExecute));
 
29
                }
 
30
                
 
31
                public override bool CanWrite (FilePath path)
 
32
                {
 
33
                        var info = GetUnixFileInfo (path);
 
34
                        return info != null && info.CanAccess (Mono.Unix.Native.AccessModes.W_OK);
 
35
                }
 
36
                
 
37
                public override bool Delete (FilePath path)
 
38
                {
 
39
                        var info = GetUnixFileInfo (path);
 
40
                        if (info != null && info.Exists) {
 
41
                                try {
 
42
                                        info.Delete ();
 
43
                                        return true;
 
44
                                } catch {
 
45
                                        // If the directory is not empty we return false. JGit relies on this
 
46
                                        return false;
 
47
                                }
 
48
                        }
 
49
                        return false;
 
50
                }
 
51
                
 
52
                public override bool Exists (FilePath path)
 
53
                {
 
54
                        var info = GetUnixFileInfo (path);
 
55
                        return info != null && info.Exists;
 
56
                }
 
57
                
 
58
                public override bool IsDirectory (FilePath path)
 
59
                {
 
60
                        try {
 
61
                                var info = GetUnixFileInfo (path);
 
62
                                return info != null && info.Exists && info.FileType == FileTypes.Directory;
 
63
                        } catch (DirectoryNotFoundException) {
 
64
                                // If the file /foo/bar exists and we query to see if /foo/bar/baz exists, we get a
 
65
                                // DirectoryNotFound exception for Mono.Unix. In this case the directory definitely
 
66
                                // does not exist.
 
67
                                return false;
 
68
                        }
 
69
                }
 
70
                
 
71
                public override bool IsFile (FilePath path)
 
72
                {
 
73
                        var info = GetUnixFileInfo (path);
 
74
                        return info != null && info.Exists && (info.FileType == FileTypes.RegularFile || info.FileType == FileTypes.SymbolicLink);
 
75
                }
 
76
                
 
77
                public override long LastModified (FilePath path)
 
78
                {
 
79
                        var info = GetUnixFileInfo (path);
 
80
                        return info != null && info.Exists ? info.LastWriteTimeUtc.ToMillisecondsSinceEpoch() : 0;
 
81
                }
 
82
                
 
83
                public override long Length (FilePath path)
 
84
                {
 
85
                        var info = GetUnixFileInfo (path);
 
86
                        return info != null && info.Exists ? info.Length : 0;
 
87
                }
 
88
                
 
89
                public override void MakeFileWritable (FilePath file)
 
90
                {
 
91
                        var info = GetUnixFileInfo (file);
 
92
                        if (info != null)
 
93
                                info.FileAccessPermissions |= (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
 
94
                }
 
95
                
 
96
                public override bool RenameTo (FilePath path, string name)
 
97
                {
 
98
                        var symlink = GetUnixFileInfo (path) as UnixSymbolicLinkInfo;
 
99
                        if (symlink != null) {
 
100
                                var newFile = new UnixSymbolicLinkInfo (name);
 
101
                                newFile.CreateSymbolicLinkTo (symlink.ContentsPath);
 
102
                                return true;
 
103
                        } else {
 
104
                                // This call replaces the file if it already exists.
 
105
                                // File.Move throws an exception if dest already exists
 
106
                                return Mono.Unix.Native.Stdlib.rename (path, name) == 0;
 
107
                        }
 
108
                }
 
109
                
 
110
                public override bool SetExecutable (FilePath path, bool exec)
 
111
                {
 
112
                        UnixFileInfo fi = new UnixFileInfo (path);
 
113
                        FileAccessPermissions perms = fi.FileAccessPermissions;
 
114
                        if (exec) {
 
115
                                if (perms.HasFlag (FileAccessPermissions.UserRead))
 
116
                                        perms |= FileAccessPermissions.UserExecute;
 
117
                                if (perms.HasFlag (FileAccessPermissions.OtherRead))
 
118
                                        perms |= FileAccessPermissions.OtherExecute;
 
119
                                if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
 
120
                                        perms |= FileAccessPermissions.GroupExecute;
 
121
                        } else {
 
122
                                if (perms.HasFlag (FileAccessPermissions.UserRead))
 
123
                                        perms &= ~FileAccessPermissions.UserExecute;
 
124
                                if (perms.HasFlag (FileAccessPermissions.OtherRead))
 
125
                                        perms &= ~FileAccessPermissions.OtherExecute;
 
126
                                if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
 
127
                                        perms &= ~FileAccessPermissions.GroupExecute;
 
128
                        }
 
129
                        fi.FileAccessPermissions = perms;
 
130
                        return true;
 
131
                }
 
132
                
 
133
                public override bool SetLastModified(FilePath path, long milis)
 
134
                {
 
135
                        // How can the last write time be set on a symlink?
 
136
                        return base.SetLastModified(path, milis);
 
137
                }
 
138
                
 
139
                public override bool SetReadOnly (FilePath path)
 
140
                {
 
141
                        try {
 
142
                                var info = GetUnixFileInfo (path);
 
143
                                if (info != null)
 
144
                                        info.FileAccessPermissions &= ~ (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
 
145
                                return true;
 
146
                        } catch {
 
147
                                return false;
 
148
                        }
 
149
                }
 
150
        }
 
151
}
 
152