FileSystemInfo.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.FileSystemInfo.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Created: Monday, August 13, 2001
  9. //
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.Private;
  13. namespace System.IO
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public abstract class FileSystemInfo : MarshalByRefObject
  19. {
  20. protected stat status;
  21. private bool inited;
  22. protected string FullPath;
  23. protected string OriginalPath;
  24. protected FileSystemInfo()
  25. {
  26. status.st_dev = 0;
  27. status.st_mode = 0;
  28. status.st_nlink = 0;
  29. status.st_uid = 0;
  30. status.st_gid = 0;
  31. status.st_size = 0;
  32. status.st_atime = 0;
  33. status.st_mtime = 0;
  34. status.st_ctime = 0;
  35. FullPath = OriginalPath = String.Empty;
  36. }
  37. public FileAttributes Attributes
  38. {
  39. get
  40. {
  41. return getAttributes();
  42. }
  43. set
  44. {
  45. //TODO: Implement
  46. }
  47. }
  48. public DateTime CreationTime
  49. {
  50. get
  51. {
  52. if(!inited)
  53. {
  54. update();
  55. }
  56. // TODO: fix next line as far as my research has taken me so far, Unix/Linux don't
  57. // have a creation time and according to my man the ctime if the last time
  58. // one of the chmod flags was changed
  59. return c2csharpTime(status.st_ctime);
  60. }
  61. set
  62. {
  63. //TODO: Implement
  64. }
  65. }
  66. public abstract bool Exists {get;}
  67. public abstract string Name {get;}
  68. public abstract void Delete();
  69. /// <summary>
  70. /// Get the extension of this item
  71. /// </summary>
  72. public string Extension
  73. {
  74. get
  75. {
  76. return Path.GetExtension(getPathName());
  77. }
  78. }
  79. public string FullName
  80. {
  81. get
  82. {
  83. return getPathName();
  84. }
  85. }
  86. public DateTime LastAccessTime
  87. {
  88. get
  89. {
  90. if(!inited)
  91. {
  92. update();
  93. }
  94. return c2csharpTime(status.st_atime);
  95. }
  96. set
  97. {
  98. // TODO: Implement
  99. }
  100. }
  101. public DateTime LastWriteTime
  102. { // TODO: Implement
  103. get
  104. {
  105. if(!inited)
  106. {
  107. update();
  108. }
  109. return c2csharpTime(status.st_mtime);
  110. }
  111. set
  112. { // TODO: Implement
  113. }
  114. }
  115. public override int GetHashCode()
  116. {
  117. return getPathName().GetHashCode();
  118. }
  119. public override bool Equals(object obj)
  120. { // TODO: Implement
  121. return false;
  122. }
  123. new public static bool Equals(object obj1, object obj2)
  124. { // TODO: Implement
  125. return false;
  126. }
  127. public void Refresh()
  128. {
  129. update();
  130. }
  131. unsafe private void update()
  132. {
  133. stat fs;
  134. int nRetCode = Wrapper.stat(getPathName(), &fs);
  135. status = fs;
  136. switch(nRetCode)
  137. {
  138. case 0:
  139. break;
  140. case Wrapper.ENOENT:
  141. case Wrapper.ENOTDIR:
  142. throw new ArgumentException("File not found");
  143. //break; generates warning CS0162 unreachable code
  144. default:
  145. throw new IOException();
  146. //break; generates warning CS0162 unreachable code
  147. }
  148. inited = true;
  149. }
  150. private DateTime c2csharpTime(double seconds)
  151. { // TODO: determine if UTC time which the
  152. // calculation below is in is correct
  153. DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
  154. dt.AddSeconds(seconds);
  155. return dt;
  156. }
  157. protected string getPathName()
  158. {
  159. if(FullPath == String.Empty)
  160. {
  161. FullPath = Path.GetFullPath(OriginalPath);
  162. }
  163. return FullPath;
  164. }
  165. protected FileAttributes getAttributes()
  166. {
  167. if(!inited)
  168. {
  169. update();
  170. }
  171. // TODO: lots more attribute work needed
  172. FileAttributes attrib = 0;
  173. if(((status.st_mode & Wrapper.S_IFMT) & Wrapper.S_IFDIR) != 0)
  174. {
  175. attrib |= FileAttributes.Directory;
  176. }
  177. else
  178. {
  179. attrib |= FileAttributes.Normal;
  180. }
  181. return attrib;
  182. }
  183. }
  184. }