FileSystemInfo.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.FileSystemInfo.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  7. //
  8. // Author: Jim Richardson, [email protected]
  9. // Dan Lewis ([email protected])
  10. // Created: Monday, August 13, 2001
  11. //
  12. //------------------------------------------------------------------------------
  13. //
  14. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.Serialization;
  37. using System.Security;
  38. using System.Security.Permissions;
  39. namespace System.IO {
  40. [Serializable]
  41. [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
  42. [ComVisible (true)]
  43. #if NET_2_1
  44. public abstract class FileSystemInfo {
  45. #else
  46. public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
  47. #region Implementation of ISerializable
  48. [ComVisible(false)]
  49. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  50. {
  51. info.AddValue ("OriginalPath", OriginalPath, typeof(string));
  52. info.AddValue ("FullPath", FullPath, typeof(string));
  53. }
  54. #endregion Implementation of ISerializable
  55. #endif
  56. // public properties
  57. public abstract bool Exists { get; }
  58. public abstract string Name { get; }
  59. public virtual string FullName {
  60. get {
  61. return FullPath;
  62. }
  63. }
  64. public string Extension {
  65. get {
  66. return Path.GetExtension (Name);
  67. }
  68. }
  69. public FileAttributes Attributes {
  70. get {
  71. Refresh (false);
  72. return stat.Attributes;
  73. }
  74. set {
  75. MonoIOError error;
  76. if (!MonoIO.SetFileAttributes (FullName,
  77. value,
  78. out error))
  79. throw MonoIO.GetException (FullName,
  80. error);
  81. Refresh (true);
  82. }
  83. }
  84. public DateTime CreationTime {
  85. get {
  86. Refresh (false);
  87. return DateTime.FromFileTime (stat.CreationTime);
  88. }
  89. set {
  90. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  91. long filetime = value.ToFileTime ();
  92. MonoIOError error;
  93. if (!MonoIO.SetFileTime (FullName, filetime,
  94. -1, -1, out error))
  95. throw MonoIO.GetException (FullName,
  96. error);
  97. Refresh (true);
  98. }
  99. }
  100. [ComVisible(false)]
  101. public DateTime CreationTimeUtc {
  102. get {
  103. return CreationTime.ToUniversalTime ();
  104. }
  105. set {
  106. CreationTime = value.ToLocalTime ();
  107. }
  108. }
  109. public DateTime LastAccessTime {
  110. get {
  111. Refresh (false);
  112. return DateTime.FromFileTime (stat.LastAccessTime);
  113. }
  114. set {
  115. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  116. long filetime = value.ToFileTime ();
  117. MonoIOError error;
  118. if (!MonoIO.SetFileTime (FullName, -1,
  119. filetime, -1,
  120. out error))
  121. throw MonoIO.GetException (FullName,
  122. error);
  123. Refresh (true);
  124. }
  125. }
  126. [ComVisible(false)]
  127. public DateTime LastAccessTimeUtc {
  128. get {
  129. Refresh (false);
  130. return LastAccessTime.ToUniversalTime ();
  131. }
  132. set {
  133. LastAccessTime = value.ToLocalTime ();
  134. }
  135. }
  136. public DateTime LastWriteTime {
  137. get {
  138. Refresh (false);
  139. return DateTime.FromFileTime (stat.LastWriteTime);
  140. }
  141. set {
  142. SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
  143. long filetime = value.ToFileTime ();
  144. MonoIOError error;
  145. if (!MonoIO.SetFileTime (FullName, -1, -1,
  146. filetime, out error))
  147. throw MonoIO.GetException (FullName,
  148. error);
  149. Refresh (true);
  150. }
  151. }
  152. [ComVisible(false)]
  153. public DateTime LastWriteTimeUtc {
  154. get {
  155. Refresh (false);
  156. return LastWriteTime.ToUniversalTime ();
  157. }
  158. set {
  159. LastWriteTime = value.ToLocalTime ();
  160. }
  161. }
  162. // public methods
  163. public abstract void Delete ();
  164. public void Refresh ()
  165. {
  166. Refresh (true);
  167. }
  168. // protected
  169. protected FileSystemInfo ()
  170. {
  171. this.valid = false;
  172. this.FullPath = null;
  173. }
  174. protected FileSystemInfo (SerializationInfo info, StreamingContext context)
  175. {
  176. if (info == null)
  177. {
  178. throw new ArgumentNullException("info");
  179. }
  180. FullPath = info.GetString("FullPath");
  181. OriginalPath = info.GetString("OriginalPath");
  182. }
  183. protected string FullPath;
  184. protected string OriginalPath;
  185. // internal
  186. internal void Refresh (bool force)
  187. {
  188. if (valid && !force)
  189. return;
  190. MonoIOError error;
  191. MonoIO.GetFileStat (FullName, out stat, out error);
  192. /* Don't throw on error here, too much other
  193. * stuff relies on it not doing so...
  194. */
  195. valid = true;
  196. InternalRefresh ();
  197. }
  198. internal virtual void InternalRefresh ()
  199. {
  200. }
  201. internal void CheckPath (string path)
  202. {
  203. if (path == null)
  204. throw new ArgumentNullException ("path");
  205. if (path.Length == 0)
  206. throw new ArgumentException ("An empty file name is not valid.");
  207. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  208. throw new ArgumentException ("Illegal characters in path.");
  209. if (Environment.IsRunningOnWindows) {
  210. int idx = path.IndexOf (':');
  211. if (idx >= 0 && idx != 1)
  212. throw new ArgumentException ("path");
  213. }
  214. }
  215. internal MonoIOStat stat;
  216. internal bool valid;
  217. }
  218. }