PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/thomo13/ironruby
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. using System.IO;
  16. using IronRuby.Runtime;
  17. using Microsoft.Scripting.Runtime;
  18. #if CLR2
  19. using Microsoft.Scripting.Utils;
  20. using System;
  21. #else
  22. using System;
  23. #endif
  24. namespace IronRuby.Builtins {
  25. // TODO: conversion: to_io, to_path, to_str
  26. [RubyModule("FileTest")]
  27. public static class FileTest {
  28. [RubyMethod("blockdev?", RubyMethodAttributes.PublicSingleton)]
  29. [RubyMethod("blockdev?", RubyMethodAttributes.PrivateInstance)]
  30. public static bool IsBlockDevice(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  31. return RubyFileOps.RubyStatOps.IsBlockDevice(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  32. }
  33. [RubyMethod("chardev?", RubyMethodAttributes.PublicSingleton)]
  34. [RubyMethod("chardev?", RubyMethodAttributes.PrivateInstance)]
  35. public static bool IsCharDevice(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  36. return RubyFileOps.RubyStatOps.IsCharDevice(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  37. }
  38. [RubyMethod("directory?", RubyMethodAttributes.PublicSingleton)]
  39. [RubyMethod("directory?", RubyMethodAttributes.PrivateInstance)]
  40. public static bool IsDirectory(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  41. return DirectoryExists(self.Context, Protocols.CastToPath(toPath, path));
  42. }
  43. [RubyMethod("executable?", RubyMethodAttributes.PublicSingleton)]
  44. [RubyMethod("executable?", RubyMethodAttributes.PrivateInstance)]
  45. [RubyMethod("executable_real?", RubyMethodAttributes.PublicSingleton)]
  46. [RubyMethod("executable_real?", RubyMethodAttributes.PrivateInstance)]
  47. public static bool IsExecutable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  48. return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => RubyFileOps.RubyStatOps.IsExecutable(fsi));
  49. }
  50. [RubyMethod("exist?", RubyMethodAttributes.PublicSingleton)]
  51. [RubyMethod("exist?", RubyMethodAttributes.PrivateInstance)]
  52. [RubyMethod("exists?", RubyMethodAttributes.PublicSingleton)]
  53. [RubyMethod("exists?", RubyMethodAttributes.PrivateInstance)]
  54. public static bool Exists(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  55. var p = Protocols.CastToPath(toPath, path);
  56. return FileExists(self.Context, p) || DirectoryExists(self.Context, p);
  57. }
  58. [RubyMethod("file?", RubyMethodAttributes.PublicSingleton)]
  59. [RubyMethod("file?", RubyMethodAttributes.PrivateInstance)]
  60. public static bool IsFile(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  61. return FileExists(self.Context, Protocols.CastToPath(toPath, path));
  62. }
  63. [RubyMethod("grpowned?", RubyMethodAttributes.PublicSingleton)]
  64. [RubyMethod("grpowned?", RubyMethodAttributes.PrivateInstance)]
  65. public static bool IsGroupOwned(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  66. return RubyFileOps.RubyStatOps.IsGroupOwned(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  67. }
  68. [RubyMethod("identical?", RubyMethodAttributes.PublicSingleton)]
  69. [RubyMethod("identical?", RubyMethodAttributes.PrivateInstance)]
  70. public static bool AreIdentical(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path1, object path2) {
  71. FileSystemInfo info1, info2;
  72. return RubyFileOps.RubyStatOps.TryCreate(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path1)), out info1)
  73. && RubyFileOps.RubyStatOps.TryCreate(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path2)), out info2)
  74. && RubyFileOps.RubyStatOps.AreIdentical(self.Context, info1, info2);
  75. }
  76. [RubyMethod("owned?", RubyMethodAttributes.PublicSingleton)]
  77. [RubyMethod("owned?", RubyMethodAttributes.PrivateInstance)]
  78. public static bool IsUserOwned(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  79. return RubyFileOps.RubyStatOps.IsUserOwned(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  80. }
  81. [RubyMethod("pipe?", RubyMethodAttributes.PublicSingleton)]
  82. [RubyMethod("pipe?", RubyMethodAttributes.PrivateInstance)]
  83. public static bool IsPipe(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  84. return RubyFileOps.RubyStatOps.IsPipe(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  85. }
  86. [RubyMethod("readable?", RubyMethodAttributes.PublicSingleton)]
  87. [RubyMethod("readable?", RubyMethodAttributes.PrivateInstance)]
  88. [RubyMethod("readable_real?", RubyMethodAttributes.PublicSingleton)]
  89. [RubyMethod("readable_real?", RubyMethodAttributes.PrivateInstance)]
  90. public static bool IsReadable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  91. return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => {
  92. return RubyFileOps.RubyStatOps.IsReadable(fsi); });
  93. }
  94. [RubyMethod("setgid?", RubyMethodAttributes.PublicSingleton)]
  95. [RubyMethod("setgid?", RubyMethodAttributes.PrivateInstance)]
  96. public static bool IsSetGid(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  97. return RubyFileOps.RubyStatOps.IsSetGid(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  98. }
  99. [RubyMethod("setuid?", RubyMethodAttributes.PublicSingleton)]
  100. [RubyMethod("setuid?", RubyMethodAttributes.PrivateInstance)]
  101. public static bool IsSetUid(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  102. return RubyFileOps.RubyStatOps.IsSetUid(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  103. }
  104. [RubyMethod("size", RubyMethodAttributes.PublicSingleton)]
  105. [RubyMethod("size", RubyMethodAttributes.PrivateInstance)]
  106. public static int Size(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  107. return RubyFileOps.RubyStatOps.Size(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  108. }
  109. [RubyMethod("size?", RubyMethodAttributes.PublicSingleton)]
  110. [RubyMethod("size?", RubyMethodAttributes.PrivateInstance)]
  111. public static object NullableSize(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  112. FileSystemInfo fsi;
  113. if (RubyFileOps.RubyStatOps.TryCreate(self.Context, Protocols.CastToPath(toPath, path).ConvertToString(), out fsi)) {
  114. return RubyFileOps.RubyStatOps.NullableSize(fsi);
  115. } else {
  116. return null;
  117. }
  118. }
  119. [RubyMethod("socket?", RubyMethodAttributes.PublicSingleton)]
  120. [RubyMethod("socket?", RubyMethodAttributes.PrivateInstance)]
  121. public static bool IsSocket(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  122. return RubyFileOps.RubyStatOps.IsSocket(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  123. }
  124. [RubyMethod("sticky?", RubyMethodAttributes.PublicSingleton)]
  125. [RubyMethod("sticky?", RubyMethodAttributes.PrivateInstance)]
  126. public static object IsSticky(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  127. return RubyFileOps.RubyStatOps.IsSticky(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  128. }
  129. #if !SILVERLIGHT
  130. [RubyMethod("symlink?", RubyMethodAttributes.PublicSingleton, BuildConfig = "!SILVERLIGHT")]
  131. [RubyMethod("symlink?", RubyMethodAttributes.PrivateInstance, BuildConfig = "!SILVERLIGHT")]
  132. public static bool IsSymLink(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  133. return RubyFileOps.RubyStatOps.IsSymLink(RubyFileOps.RubyStatOps.Create(self.Context, Protocols.CastToPath(toPath, path)));
  134. }
  135. #endif
  136. [RubyMethod("writable?", RubyMethodAttributes.PublicSingleton)]
  137. [RubyMethod("writable?", RubyMethodAttributes.PrivateInstance)]
  138. [RubyMethod("writable_real?", RubyMethodAttributes.PublicSingleton)]
  139. [RubyMethod("writable_real?", RubyMethodAttributes.PrivateInstance)]
  140. public static bool IsWritable(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  141. return RunIfFileExists(self.Context, Protocols.CastToPath(toPath, path), (FileSystemInfo fsi) => {
  142. return RubyFileOps.RubyStatOps.IsWritable(fsi); });
  143. }
  144. [RubyMethod("zero?", RubyMethodAttributes.PublicSingleton)]
  145. [RubyMethod("zero?", RubyMethodAttributes.PrivateInstance)]
  146. public static bool IsZeroLength(ConversionStorage<MutableString>/*!*/ toPath, RubyModule/*!*/ self, object path) {
  147. string strPath = self.Context.DecodePath(Protocols.CastToPath(toPath, path));
  148. // NUL/nul is a special-cased filename on Windows
  149. if (strPath.ToUpperInvariant() == "NUL") {
  150. return RubyFileOps.RubyStatOps.IsZeroLength(RubyFileOps.RubyStatOps.Create(self.Context, strPath));
  151. }
  152. if (self.Context.Platform.DirectoryExists(strPath) || !self.Context.Platform.FileExists(strPath)) {
  153. return false;
  154. }
  155. return RubyFileOps.RubyStatOps.IsZeroLength(RubyFileOps.RubyStatOps.Create(self.Context, strPath));
  156. }
  157. internal static bool FileExists(RubyContext/*!*/ context, MutableString/*!*/ path) {
  158. return context.Platform.FileExists(context.DecodePath(path));
  159. }
  160. internal static bool DirectoryExists(RubyContext/*!*/ context, MutableString/*!*/ path) {
  161. return context.Platform.DirectoryExists(context.DecodePath(path));
  162. }
  163. internal static bool Exists(RubyContext/*!*/ context, MutableString/*!*/ path) {
  164. var strPath = context.DecodePath(path);
  165. return context.Platform.DirectoryExists(strPath) || context.Platform.FileExists(strPath);
  166. }
  167. private static bool RunIfFileExists(RubyContext/*!*/ context, MutableString/*!*/ path, Func<FileSystemInfo, bool> del) {
  168. return RunIfFileExists(context, path.ConvertToString(), del);
  169. }
  170. private static bool RunIfFileExists(RubyContext/*!*/ context, string/*!*/ path, Func<FileSystemInfo, bool> del) {
  171. FileSystemInfo fsi;
  172. if (RubyFileOps.RubyStatOps.TryCreate(context, path, out fsi)) {
  173. return del(fsi);
  174. } else {
  175. return false;
  176. }
  177. }
  178. }
  179. }