~ubuntu-branches/ubuntu/quantal/basenji/quantal

« back to all changes in this revision

Viewing changes to Platform/src/Common/IO/FileHelper.cs

  • Committer: Package Import Robot
  • Author(s): Patrick Ulbrich
  • Date: 2011-10-02 18:01:08 UTC
  • Revision ID: package-import@ubuntu.com-20111002180108-alcxkgmgv0oj7ipq
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// FileHelper.cs
 
2
// 
 
3
// Copyright (C) 2008 - 2010 Patrick Ulbrich
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
#if UNIX
 
21
using Platform.Unix.IO;
 
22
#endif
 
23
namespace Platform.Common.IO
 
24
{
 
25
        // required to be in sync with Platform.Unix.IO.UnixFileType
 
26
        public enum FileType
 
27
        {
 
28
                Directory,
 
29
                CharacterDevice,
 
30
                BlockDevice,
 
31
                RegularFile,
 
32
                Fifo,
 
33
                SymbolicLink,
 
34
                Socket
 
35
        }
 
36
        
 
37
        public static class FileHelper
 
38
        {
 
39
                public static FileType GetFileType(string path, bool followSymLinks) {
 
40
#if WIN32
 
41
                        // TODO : test me, is it working?
 
42
                        System.IO.FileAttributes attr = System.IO.File.GetAttributes(path);
 
43
                        return  (attr & System.IO.FileAttributes.Directory) != 0 ? FileType.Directory : FileType.RegularFile; 
 
44
#else
 
45
                        Mono.Unix.Native.Stat stat;
 
46
                        if (followSymLinks)
 
47
                                stat = UnixFileHelper.GetStat(path);
 
48
                        else
 
49
                                stat = UnixFileHelper.GetLStat(path);
 
50
                                
 
51
                        FileType ft;
 
52
                        UnixFileType uft = UnixFileHelper.GetFileType(stat);
 
53
                        switch(uft) {
 
54
                                case UnixFileType.Directory:
 
55
                                        ft = FileType.Directory;
 
56
                                        break;
 
57
                                case UnixFileType.CharacterDevice:
 
58
                                        ft = FileType.CharacterDevice;
 
59
                                        break;
 
60
                                case UnixFileType.BlockDevice:
 
61
                                        ft = FileType.BlockDevice;
 
62
                                        break;
 
63
                                case UnixFileType.RegularFile:
 
64
                                        ft = FileType.RegularFile;
 
65
                                        break;
 
66
                                case UnixFileType.Fifo:
 
67
                                        ft = FileType.Fifo;
 
68
                                        break;
 
69
                                case UnixFileType.SymbolicLink:
 
70
                                        ft = FileType.SymbolicLink;
 
71
                                        break;
 
72
                                case UnixFileType.Socket:
 
73
                                        ft = FileType.Socket;
 
74
                                        break;
 
75
                                default:
 
76
                                        throw new NotImplementedException(string.Format("UnixFileType {0} has no equivalent FileType value yet", uft.ToString()));
 
77
                        }
 
78
                        return ft;
 
79
#endif          
 
80
                }
 
81
                
 
82
                public static string GetCanonicalSymLinkTarget(string symLinkPath) {
 
83
#if WIN32
 
84
                        throw new NotImplementedException();
 
85
#else
 
86
                        return UnixFileHelper.ReadLink(symLinkPath, true);
 
87
#endif          
 
88
                }
 
89
        }
 
90
}