PageRenderTime 54ms CodeModel.GetById 28ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Libraries/Builtins/FileTest.cs

https://github.com/dsblank/main
C# | 211 lines | 163 code | 32 blank | 16 comment | 12 complexity | 2af058f66bd35cb25d186f6f5eb1636b MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16using System.IO;
 17using IronRuby.Runtime;
 18using Microsoft.Scripting.Runtime;
 19
 20#if CLR2
 21using Microsoft.Scripting.Utils;
 22using System;
 23#else
 24using System;
 25#endif
 26
 27namespace IronRuby.Builtins {
 28    // TODO: conversion: to_io, to_path, to_str
 29
 30    [RubyModule("FileTest")]
 31    public static class FileTest {
 32        [RubyMethod("blockdev?", RubyMethodAttributes.PublicSingleton)]
 33        [RubyMethod("blockdev?", RubyMethodAttributes.PrivateInstance)]
 34        public static bool IsBlockDevice(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 35            return RubyFileOps.RubyStatOps.IsBlockDevice(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
 36        }
 37
 38        [RubyMethod("chardev?", RubyMethodAttributes.PublicSingleton)]
 39        [RubyMethod("chardev?", RubyMethodAttributes.PrivateInstance)]
 40        public static bool IsCharDevice(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 41            return RubyFileOps.RubyStatOps.IsCharDevice(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
 42        }
 43
 44        [RubyMethod("directory?", RubyMethodAttributes.PublicSingleton)]
 45        [RubyMethod("directory?", RubyMethodAttributes.PrivateInstance)]
 46        public static bool IsDirectory(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 47            return DirectoryExists(self.Context, Protocols.CastToPath(toPath, path));
 48        }
 49
 50        [RubyMethod("executable?", RubyMethodAttributes.PublicSingleton)]
 51        [RubyMethod("executable?", RubyMethodAttributes.PrivateInstance)]
 52        [RubyMethod("executable_real?", RubyMethodAttributes.PublicSingleton)]
 53        [RubyMethod("executable_real?", RubyMethodAttributes.PrivateInstance)]
 54        public static bool IsExecutable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 55            return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => RubyFileOps.RubyStatOps.IsExecutable(fsi));
 56        }
 57
 58        [RubyMethod("exist?", RubyMethodAttributes.PublicSingleton)]
 59        [RubyMethod("exist?", RubyMethodAttributes.PrivateInstance)]
 60        [RubyMethod("exists?", RubyMethodAttributes.PublicSingleton)]
 61        [RubyMethod("exists?", RubyMethodAttributes.PrivateInstance)]
 62        public static bool Exists(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 63            var p = Protocols.CastToPath(toPath, path);
 64            return FileExists(self.Context, p) || DirectoryExists(self.Context, p);
 65        }
 66
 67        [RubyMethod("file?", RubyMethodAttributes.PublicSingleton)]
 68        [RubyMethod("file?", RubyMethodAttributes.PrivateInstance)]
 69        public static bool IsFile(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 70            return FileExists(self.Context, Protocols.CastToPath(toPath, path));
 71        }
 72
 73        [RubyMethod("grpowned?", RubyMethodAttributes.PublicSingleton)]
 74        [RubyMethod("grpowned?", RubyMethodAttributes.PrivateInstance)]
 75        public static bool IsGroupOwned(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 76            return RubyFileOps.RubyStatOps.IsGroupOwned(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
 77        }
 78
 79        [RubyMethod("identical?", RubyMethodAttributes.PublicSingleton)]
 80        [RubyMethod("identical?", RubyMethodAttributes.PrivateInstance)]
 81        public static bool AreIdentical(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path1, object path2) {
 82            FileSystemInfo info1, info2;
 83
 84            return RubyFileOps.RubyStatOps.TryCreate(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path1)), out info1)
 85                && RubyFileOps.RubyStatOps.TryCreate(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path2)), out info2)
 86                && RubyFileOps.RubyStatOps.AreIdentical(self.Context, info1, info2);
 87        }
 88
 89        [RubyMethod("owned?", RubyMethodAttributes.PublicSingleton)]
 90        [RubyMethod("owned?", RubyMethodAttributes.PrivateInstance)]
 91        public static bool IsUserOwned(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 92            return RubyFileOps.RubyStatOps.IsUserOwned(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
 93        }
 94
 95        [RubyMethod("pipe?", RubyMethodAttributes.PublicSingleton)]
 96        [RubyMethod("pipe?", RubyMethodAttributes.PrivateInstance)]
 97        public static bool IsPipe(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
 98            return RubyFileOps.RubyStatOps.IsPipe(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
 99        }
100
101        [RubyMethod("readable?", RubyMethodAttributes.PublicSingleton)]
102        [RubyMethod("readable?", RubyMethodAttributes.PrivateInstance)]
103        [RubyMethod("readable_real?", RubyMethodAttributes.PublicSingleton)]
104        [RubyMethod("readable_real?", RubyMethodAttributes.PrivateInstance)]
105        public static bool IsReadable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
106            return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => { 
107                return RubyFileOps.RubyStatOps.IsReadable(fsi); });
108        }
109
110        [RubyMethod("setgid?", RubyMethodAttributes.PublicSingleton)]
111        [RubyMethod("setgid?", RubyMethodAttributes.PrivateInstance)]
112        public static bool IsSetGid(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
113            return RubyFileOps.RubyStatOps.IsSetGid(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
114        }
115
116        [RubyMethod("setuid?", RubyMethodAttributes.PublicSingleton)]
117        [RubyMethod("setuid?", RubyMethodAttributes.PrivateInstance)]
118        public static bool IsSetUid(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
119            return RubyFileOps.RubyStatOps.IsSetUid(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
120        }
121
122        [RubyMethod("size", RubyMethodAttributes.PublicSingleton)]
123        [RubyMethod("size", RubyMethodAttributes.PrivateInstance)]
124        public static int Size(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
125            return RubyFileOps.RubyStatOps.Size(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
126        }
127
128        [RubyMethod("size?", RubyMethodAttributes.PublicSingleton)]
129        [RubyMethod("size?", RubyMethodAttributes.PrivateInstance)]
130        public static object NullableSize(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
131            FileSystemInfo fsi;
132            if (RubyFileOps.RubyStatOps.TryCreate(self.Context, Protocols.CastToPath(toPath, path).ConvertToString(), out fsi)) {
133                return RubyFileOps.RubyStatOps.NullableSize(fsi);
134            } else {
135                return null;
136            }
137        }
138
139        [RubyMethod("socket?", RubyMethodAttributes.PublicSingleton)]
140        [RubyMethod("socket?", RubyMethodAttributes.PrivateInstance)]
141        public static bool IsSocket(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
142            return RubyFileOps.RubyStatOps.IsSocket(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
143        }
144
145        [RubyMethod("sticky?", RubyMethodAttributes.PublicSingleton)]
146        [RubyMethod("sticky?", RubyMethodAttributes.PrivateInstance)]
147        public static object IsSticky(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
148            return RubyFileOps.RubyStatOps.IsSticky(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
149        }
150
151#if !SILVERLIGHT
152        [RubyMethod("symlink?", RubyMethodAttributes.PublicSingleton, BuildConfig = "!SILVERLIGHT")]
153        [RubyMethod("symlink?", RubyMethodAttributes.PrivateInstance, BuildConfig = "!SILVERLIGHT")]
154        public static bool IsSymLink(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
155            return RubyFileOps.RubyStatOps.IsSymLink(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
156        }
157#endif
158
159        [RubyMethod("writable?", RubyMethodAttributes.PublicSingleton)]
160        [RubyMethod("writable?", RubyMethodAttributes.PrivateInstance)]
161        [RubyMethod("writable_real?", RubyMethodAttributes.PublicSingleton)]
162        [RubyMethod("writable_real?", RubyMethodAttributes.PrivateInstance)]
163        public static bool IsWritable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
164            return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => { 
165                return RubyFileOps.RubyStatOps.IsWritable(fsi); });
166        }
167
168        [RubyMethod("zero?", RubyMethodAttributes.PublicSingleton)]
169        [RubyMethod("zero?", RubyMethodAttributes.PrivateInstance)]
170        public static bool IsZeroLength(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
171            string strPath = self.Context.DecodePath(Protocols.CastToPath(toPath, path));
172
173            // NUL/nul is a special-cased filename on Windows
174            if (strPath.ToUpperInvariant() == "NUL") {
175                return RubyFileOps.RubyStatOps.IsZeroLength(RubyFileOps.RubyStatOps.Create(self.Context, strPath));
176            }
177
178            if (self.Context.Platform.DirectoryExists(strPath) || !self.Context.Platform.FileExists(strPath)) {
179                return false;
180            }
181
182            return RubyFileOps.RubyStatOps.IsZeroLength(RubyFileOps.RubyStatOps.Create(self.Context, strPath));
183        }
184
185        internal static bool FileExists(RubyContext/*!*/ context, MutableString/*!*/ path) {
186            return context.Platform.FileExists(context.DecodePath(path));
187        }
188
189        internal static bool DirectoryExists(RubyContext/*!*/ context, MutableString/*!*/ path) {
190            return context.Platform.DirectoryExists(context.DecodePath(path));
191        }
192
193        internal static bool Exists(RubyContext/*!*/ context, MutableString/*!*/ path) {
194            var strPath = context.DecodePath(path);
195            return context.Platform.DirectoryExists(strPath) || context.Platform.FileExists(strPath);
196        }
197
198        private static bool RunIfFileExists(RubyContext/*!*/ context, MutableString/*!*/ path, Func<FileSystemInfo, bool> del) {
199            return RunIfFileExists(context, path.ConvertToString(), del);
200        }
201
202        private static bool RunIfFileExists(RubyContext/*!*/ context, string/*!*/ path, Func<FileSystemInfo, bool> del) {
203            FileSystemInfo fsi;
204            if (RubyFileOps.RubyStatOps.TryCreate(context, path, out fsi)) {
205                return del(fsi);
206            } else {
207                return false;
208            }
209        }
210    }
211}