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

« back to all changes in this revision

Viewing changes to external/ngit/Sharpen/Sharpen/FilePath.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:
4
4
        using System.Collections.Generic;
5
5
        using System.IO;
6
6
        using System.Threading;
7
 
        using Mono.Unix;
8
7
 
9
8
        public class FilePath
10
9
        {
11
 
                static bool RunningOnLinux = !Environment.OSVersion.Platform.ToString ().StartsWith ("Win");
12
 
                
13
10
                private string path;
14
11
                private static long tempCounter;
15
12
 
18
15
                }
19
16
 
20
17
                public FilePath (string path)
 
18
                        : this ((string) null, path)
21
19
                {
22
 
                        this.path = path;
 
20
 
23
21
                }
24
22
 
25
23
                public FilePath (FilePath other, string child)
 
24
                        : this ((string) other, child)
26
25
                {
27
 
                        this.path = Path.Combine (other.path, child);
 
26
 
28
27
                }
29
28
 
30
29
                public FilePath (string other, string child)
31
30
                {
32
 
                        this.path = Path.Combine (other, child);
 
31
                        if (other == null) {
 
32
                                this.path = child;
 
33
                        } else {
 
34
                                while (child != null && child.Length > 0 && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
 
35
                                        child = child.Substring (1);
 
36
 
 
37
                                if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
 
38
                                        other += Path.DirectorySeparatorChar;
 
39
 
 
40
                                this.path = Path.Combine (other, child);
 
41
                        }
33
42
                }
34
43
                
35
44
                public static implicit operator FilePath (string name)
39
48
 
40
49
                public static implicit operator string (FilePath filePath)
41
50
                {
42
 
                        return filePath.path;
 
51
                        return filePath == null ? null : filePath.path;
43
52
                }
44
53
                
45
54
                public override bool Equals (object obj)
57
66
 
58
67
                public bool CanWrite ()
59
68
                {
60
 
                        if (RunningOnLinux) {
61
 
                                var info = GetUnixFileInfo (path);
62
 
                                return info != null && info.CanAccess (Mono.Unix.Native.AccessModes.W_OK);
63
 
                        }
64
 
                        
65
 
                        return ((File.GetAttributes (path) & FileAttributes.ReadOnly) == 0);
 
69
                        return FileHelper.Instance.CanWrite (this);
66
70
                }
67
71
 
68
72
                public bool CreateNewFile ()
69
73
                {
70
 
                        if (Exists ())
 
74
                        try {
 
75
                                File.Open (path, FileMode.CreateNew).Close ();
 
76
                                return true;
 
77
                        } catch {
71
78
                                return false;
72
 
                        File.OpenWrite (path).Close ();
73
 
                        return true;
 
79
                        }
74
80
                }
75
81
 
76
82
                public static FilePath CreateTempFile ()
104
110
                public bool Delete ()
105
111
                {
106
112
                        try {
107
 
                                if (RunningOnLinux) {
108
 
                                        var info = GetUnixFileInfo (path);
109
 
                                        if (info != null && info.Exists) {
110
 
                                                try {
111
 
                                                        info.Delete ();
112
 
                                                        return true;
113
 
                                                } catch {
114
 
                                                        // If the directory is not empty we return false. JGit relies on this
115
 
                                                        return false;
116
 
                                                }
117
 
                                        }
118
 
                                        return false;
119
 
                                }
120
 
 
121
 
                                if (Directory.Exists (path)) {
122
 
                                        if (Directory.GetFileSystemEntries (path).Length != 0)
123
 
                                                return false;
124
 
                                        MakeDirWritable (path);
125
 
                                        Directory.Delete (path, true);
126
 
                    return true;
127
 
                }
128
 
                else if (File.Exists(path)) {
129
 
                                        MakeFileWritable (path);
130
 
                                        File.Delete (path);
131
 
                    return true;
132
 
                }
133
 
                                return false;
 
113
                                return FileHelper.Instance.Delete (this);
134
114
                        } catch (Exception exception) {
135
115
                                Console.WriteLine (exception);
136
116
                                return false;
143
123
 
144
124
                public bool Exists ()
145
125
                {
146
 
                        if (RunningOnLinux) {
147
 
                                var info = GetUnixFileInfo (path);
148
 
                                return info != null && info.Exists;
149
 
                        }
150
 
 
151
 
                        return (File.Exists (path) || Directory.Exists (path));
 
126
                        return FileHelper.Instance.Exists (this);
152
127
                }
153
128
 
154
129
                public FilePath GetAbsoluteFile ()
195
170
 
196
171
                public bool IsDirectory ()
197
172
                {
198
 
                        try {
199
 
                                if (RunningOnLinux) {
200
 
                                        var info = GetUnixFileInfo (path);
201
 
                                        return info != null && info.Exists && info.FileType == FileTypes.Directory;
202
 
                                }
203
 
                        } catch (DirectoryNotFoundException) {
204
 
                                // If the file /foo/bar exists and we query to see if /foo/bar/baz exists, we get a
205
 
                                // DirectoryNotFound exception for Mono.Unix. In this case the directory definitely
206
 
                                // does not exist.
207
 
                                return false;
208
 
                        }
209
 
                        return Directory.Exists (path);
 
173
                        return FileHelper.Instance.IsDirectory (this);
210
174
                }
211
175
 
212
176
                public bool IsFile ()
213
177
                {
214
 
                        if (RunningOnLinux) {
215
 
                                var info = GetUnixFileInfo (path);
216
 
                                return info != null && info.Exists && (info.FileType == FileTypes.RegularFile || info.FileType == FileTypes.SymbolicLink);
217
 
                        }
218
 
 
219
 
                        return File.Exists (path);
 
178
                        return FileHelper.Instance.IsFile (this);
220
179
                }
221
180
 
222
181
                public long LastModified ()
223
182
                {
224
 
            if (RunningOnLinux) {
225
 
                                var info = GetUnixFileInfo (path);
226
 
                                return info != null && info.Exists ? info.LastWriteTimeUtc.ToMillisecondsSinceEpoch() : 0;
227
 
            }
228
 
 
229
 
            var info2 = new FileInfo(path);
230
 
                        return info2.Exists ? info2.LastWriteTimeUtc.ToMillisecondsSinceEpoch() : 0;
 
183
                        return FileHelper.Instance.LastModified (this);
231
184
                }
232
185
 
233
186
                public long Length ()
234
187
                {
235
 
                        if (RunningOnLinux) {
236
 
                                var info = GetUnixFileInfo (path);
237
 
                                return info != null && info.Exists ? info.Length : 0;
238
 
                        }
239
 
 
240
 
                        // If you call .Length on a file that doesn't exist, an exception is thrown
241
 
                        var info2 = new FileInfo (path);
242
 
                        return info2.Exists ? info2.Length : 0;
 
188
                        return FileHelper.Instance.Length (this);
243
189
                }
244
190
 
245
191
                public string[] List ()
280
226
                        }
281
227
                }
282
228
 
283
 
                private void MakeDirWritable (string dir)
 
229
                static void MakeDirWritable (string dir)
284
230
                {
285
 
                        foreach (string file in Directory.GetFiles (dir)) {
286
 
                                MakeFileWritable (file);
287
 
                        }
288
 
                        foreach (string subdir in Directory.GetDirectories (dir)) {
289
 
                                MakeDirWritable (subdir);
290
 
                        }
 
231
                        FileHelper.Instance.MakeDirWritable (dir);
291
232
                }
292
233
 
293
 
                private void MakeFileWritable (string file)
 
234
                static void MakeFileWritable (string file)
294
235
                {
295
 
                        if (RunningOnLinux) {
296
 
                                var info = GetUnixFileInfo (file);
297
 
                                if (info != null)
298
 
                                        info.FileAccessPermissions |= (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
299
 
                                return;
300
 
                        }
301
 
 
302
 
                        FileAttributes fileAttributes = File.GetAttributes (file);
303
 
                        if ((fileAttributes & FileAttributes.ReadOnly) != 0) {
304
 
                                fileAttributes &= ~FileAttributes.ReadOnly;
305
 
                                File.SetAttributes (file, fileAttributes);
306
 
                        }
 
236
                        FileHelper.Instance.MakeFileWritable (file);
307
237
                }
308
238
 
309
239
                public bool Mkdir ()
337
267
 
338
268
                public bool RenameTo (string name)
339
269
                {
340
 
                        try {
341
 
                                if (RunningOnLinux) {
342
 
                                        var symlink = GetUnixFileInfo (path) as UnixSymbolicLinkInfo;
343
 
                                        if (symlink != null) {
344
 
                                                var newFile = new UnixSymbolicLinkInfo (name);
345
 
                                                newFile.CreateSymbolicLinkTo (symlink.ContentsPath);
346
 
                                        }
347
 
                                }
348
 
 
349
 
                                File.Move (path, name);
350
 
                                return true;
351
 
                        } catch {
352
 
                                return false;
353
 
                        }
354
 
                }
355
 
 
356
 
                public void SetLastModified (long milis)
357
 
                {
358
 
                        // FIXME: I don't know how to change the modified time on a symlink
359
 
                        DateTime utcDateTime = Extensions.MillisToDateTimeOffset (milis, 0L).UtcDateTime;
360
 
                        File.SetLastWriteTimeUtc (path, utcDateTime);
361
 
                }
362
 
 
363
 
                public void SetReadOnly ()
364
 
                {
365
 
                        if (RunningOnLinux) {
366
 
                                var info = GetUnixFileInfo (path);
367
 
                                if (info != null)
368
 
                                        info.FileAccessPermissions &= ~ (FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite | FileAccessPermissions.UserWrite);
369
 
                                return;
370
 
                        }
371
 
 
372
 
                        var fileAttributes = File.GetAttributes (this.path) | FileAttributes.ReadOnly;
373
 
                        File.SetAttributes (path, fileAttributes);
 
270
                        return FileHelper.Instance.RenameTo (this, name);
 
271
                }
 
272
 
 
273
                public bool SetLastModified (long milis)
 
274
                {
 
275
                        return FileHelper.Instance.SetLastModified(this, milis);
 
276
                }
 
277
 
 
278
                public bool SetReadOnly ()
 
279
                {
 
280
                        return FileHelper.Instance.SetReadOnly (this);
374
281
                }
375
282
                
376
283
                public Uri ToURI ()
381
288
                // Don't change the case of this method, since ngit does reflection on it
382
289
                public bool canExecute ()
383
290
                {
384
 
                        if (RunningOnLinux) {
385
 
                                UnixFileInfo fi = new UnixFileInfo (path);
386
 
                                if (!fi.Exists)
387
 
                                        return false;
388
 
                                return 0 != (fi.FileAccessPermissions & (FileAccessPermissions.UserExecute | FileAccessPermissions.GroupExecute | FileAccessPermissions.OtherExecute));
389
 
                        }
390
 
 
391
 
                        return false;
 
291
                        return FileHelper.Instance.CanExecute (this);
392
292
                }
393
293
                
394
294
                // Don't change the case of this method, since ngit does reflection on it
395
295
                public bool setExecutable (bool exec)
396
296
                {
397
 
                        if (RunningOnLinux) {
398
 
                                UnixFileInfo fi = new UnixFileInfo (path);
399
 
                                FileAccessPermissions perms = fi.FileAccessPermissions;
400
 
                                if (exec) {
401
 
                                        if (perms.HasFlag (FileAccessPermissions.UserRead))
402
 
                                                perms |= FileAccessPermissions.UserExecute;
403
 
                                        if (perms.HasFlag (FileAccessPermissions.OtherRead))
404
 
                                                perms |= FileAccessPermissions.OtherExecute;
405
 
                                        if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
406
 
                                                perms |= FileAccessPermissions.GroupExecute;
407
 
                                } else {
408
 
                                        if (perms.HasFlag (FileAccessPermissions.UserRead))
409
 
                                                perms &= ~FileAccessPermissions.UserExecute;
410
 
                                        if (perms.HasFlag (FileAccessPermissions.OtherRead))
411
 
                                                perms &= ~FileAccessPermissions.OtherExecute;
412
 
                                        if ((perms.HasFlag (FileAccessPermissions.GroupRead)))
413
 
                                                perms &= ~FileAccessPermissions.GroupExecute;
414
 
                                }
415
 
                                fi.FileAccessPermissions = perms;
416
 
                                return true;
417
 
                        }
418
 
 
419
 
                        return false;
 
297
                        return FileHelper.Instance.SetExecutable (this, exec);
420
298
                }
421
299
                
422
300
                public string GetParent ()
432
310
                {
433
311
                        return path;
434
312
                }
435
 
 
436
 
                static UnixFileSystemInfo GetUnixFileInfo (string path)
437
 
                {
438
 
                        try {
439
 
                                return Mono.Unix.UnixFileInfo.GetFileSystemEntry (path);
440
 
                        } catch (DirectoryNotFoundException ex) {
441
 
                                // If we have a file /foo/bar and probe the path /foo/bar/baz, we get a DirectoryNotFound exception
442
 
                                // because 'bar' is a file and therefore 'baz' cannot possibly exist. This is annoying.
443
 
                                var inner = ex.InnerException as UnixIOException;
444
 
                                if (inner != null && inner.ErrorCode == Mono.Unix.Native.Errno.ENOTDIR)
445
 
                                        return null;
446
 
                                throw;
447
 
                        }
448
 
                }
449
313
                
450
314
                static internal string pathSeparator {
451
315
                        get { return Path.PathSeparator.ToString (); }