PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Foundation/NSFileManager.cs

https://github.com/kjpou1/maccore
C# | 296 lines | 231 code | 30 blank | 35 comment | 55 complexity | 9a035b886d0a8904223c9db0d85078c6 MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // NSData.cs:
  3. // Author:
  4. // Miguel de Icaza
  5. //
  6. // Copyright 2011, Novell, Inc.
  7. // Copyright 2011, 2012 Xamarin Inc
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //
  29. using MonoMac.ObjCRuntime;
  30. using System;
  31. namespace MonoMac.Foundation {
  32. public enum NSFileType {
  33. Directory, Regular, SymbolicLink, Socket, CharacterSpecial, BlockSpecial, Unknown
  34. }
  35. public class NSFileAttributes {
  36. public NSFileAttributes () {}
  37. public bool? AppendOnly { get; set; }
  38. public bool? Busy { get; set; }
  39. public bool? FileExtensionHidden { get; set; }
  40. public NSDate CreationDate { get; set; }
  41. public string OwnerAccountName { get; set; }
  42. public uint? DeviceIdentifier { get; set; }
  43. public uint? FileGroupOwnerAccountID { get; set; }
  44. public bool? Immutable { get; set; }
  45. public NSDate ModificationDate { get; set; }
  46. public uint? FileOwnerAccountID { get; set; }
  47. public uint? HfsTypeCode { get; set; }
  48. public uint? PosixPermissions { get; set; }
  49. public uint? FileReferenceCount { get; set; }
  50. public uint? FileSystemFileNumber { get; set; }
  51. public ulong? FileSize { get; set; }
  52. public NSFileType? FileType { get; set; }
  53. //public bool? ProtectedFile { get; set; }
  54. internal NSDictionary ToDictionary ()
  55. {
  56. var dict = new NSMutableDictionary ();
  57. if (AppendOnly.HasValue)
  58. dict.SetObject (NSNumber.FromBoolean (AppendOnly.Value), NSFileManager.AppendOnly);
  59. if (Busy.HasValue)
  60. dict.SetObject (NSNumber.FromBoolean (Busy.Value), NSFileManager.Busy);
  61. if (CreationDate != null)
  62. dict.SetObject (CreationDate, NSFileManager.CreationDate);
  63. if (ModificationDate != null)
  64. dict.SetObject (ModificationDate, NSFileManager.ModificationDate);
  65. if (OwnerAccountName != null)
  66. dict.SetObject (new NSString (OwnerAccountName), NSFileManager.OwnerAccountName);
  67. if (DeviceIdentifier.HasValue)
  68. dict.SetObject (NSNumber.FromUInt32 (DeviceIdentifier.Value), NSFileManager.DeviceIdentifier);
  69. if (FileExtensionHidden.HasValue)
  70. dict.SetObject (NSNumber.FromBoolean (FileExtensionHidden.Value), NSFileManager.ExtensionHidden);
  71. if (FileGroupOwnerAccountID.HasValue)
  72. dict.SetObject (NSNumber.FromUInt32 (FileGroupOwnerAccountID.Value), NSFileManager.GroupOwnerAccountID);
  73. if (FileOwnerAccountID.HasValue)
  74. dict.SetObject (NSNumber.FromUInt32 (FileOwnerAccountID.Value), NSFileManager.OwnerAccountID);
  75. if (HfsTypeCode.HasValue)
  76. dict.SetObject (NSNumber.FromUInt32 (HfsTypeCode.Value), NSFileManager.HfsTypeCode);
  77. if (PosixPermissions.HasValue)
  78. dict.SetObject (NSNumber.FromUInt32 (PosixPermissions.Value), NSFileManager.PosixPermissions);
  79. if (FileReferenceCount.HasValue)
  80. dict.SetObject (NSNumber.FromUInt32 (FileReferenceCount.Value), NSFileManager.ReferenceCount);
  81. if (FileSystemFileNumber.HasValue)
  82. dict.SetObject (NSNumber.FromUInt32 (FileSystemFileNumber.Value), NSFileManager.SystemFileNumber);
  83. if (FileSize.HasValue)
  84. dict.SetObject (NSNumber.FromUInt64 (FileSize.Value), NSFileManager.Size);
  85. if (Immutable.HasValue)
  86. dict.SetObject (NSNumber.FromBoolean (Immutable.Value), NSFileManager.Immutable);
  87. //if (ProtectedFile.HasValue)
  88. //dict.SetObject (NSNumber.FromBoolean (ProtectedFile.Value), NSFileManager.ProtectedFile);
  89. if (FileType.HasValue){
  90. NSString v = null;
  91. switch (FileType.Value){
  92. case NSFileType.Directory:
  93. v = NSFileManager.TypeDirectory; break;
  94. case NSFileType.Regular:
  95. v = NSFileManager.TypeRegular; break;
  96. case NSFileType.SymbolicLink:
  97. v = NSFileManager.TypeSymbolicLink; break;
  98. case NSFileType.Socket:
  99. v = NSFileManager.TypeSocket; break;
  100. case NSFileType.CharacterSpecial:
  101. v = NSFileManager.TypeCharacterSpecial; break;
  102. case NSFileType.BlockSpecial:
  103. v = NSFileManager.TypeBlockSpecial; break;
  104. default:
  105. v = NSFileManager.TypeUnknown; break;
  106. }
  107. dict.SetObject (v, NSFileManager.NSFileType);
  108. }
  109. return dict;
  110. }
  111. internal static bool fetch (NSDictionary dict, NSString key, ref bool b)
  112. {
  113. var k = dict.ObjectForKey (key) as NSNumber;
  114. if (k == null)
  115. return false;
  116. b = k.BoolValue;
  117. return true;
  118. }
  119. internal static bool fetch (NSDictionary dict, NSString key, ref uint b)
  120. {
  121. var k = dict.ObjectForKey (key) as NSNumber;
  122. if (k == null)
  123. return false;
  124. b = k.UInt32Value;
  125. return true;
  126. }
  127. internal static bool fetch (NSDictionary dict, NSString key, ref ulong b)
  128. {
  129. var k = dict.ObjectForKey (key) as NSNumber;
  130. if (k == null)
  131. return false;
  132. b = k.UInt64Value;
  133. return true;
  134. }
  135. public static NSFileAttributes FromDict (NSDictionary dict)
  136. {
  137. if (dict == null)
  138. return null;
  139. var ret = new NSFileAttributes ();
  140. bool b = false;
  141. if (fetch (dict, NSFileManager.AppendOnly, ref b))
  142. ret.AppendOnly = b;
  143. if (fetch (dict, NSFileManager.Busy, ref b))
  144. ret.Busy = b;
  145. if (fetch (dict, NSFileManager.Immutable, ref b))
  146. ret.Immutable = b;
  147. //if (fetch (dict, NSFileManager.ProtectedFile, ref b))
  148. //ret.ProtectedFile = b;
  149. if (fetch (dict, NSFileManager.ExtensionHidden, ref b))
  150. ret.FileExtensionHidden = b;
  151. var date = dict.ObjectForKey (NSFileManager.CreationDate) as NSDate;
  152. if (date != null)
  153. ret.CreationDate = date;
  154. date = dict.ObjectForKey (NSFileManager.ModificationDate) as NSDate;
  155. if (date != null)
  156. ret.ModificationDate = date;
  157. var name = dict.ObjectForKey (NSFileManager.OwnerAccountName) as NSString;
  158. if (name != null)
  159. ret.OwnerAccountName = name.ToString ();
  160. uint u = 0;
  161. if (fetch (dict, NSFileManager.DeviceIdentifier, ref u))
  162. ret.DeviceIdentifier = u;
  163. if (fetch (dict, NSFileManager.GroupOwnerAccountID, ref u))
  164. ret.FileGroupOwnerAccountID = u;
  165. if (fetch (dict, NSFileManager.OwnerAccountID, ref u))
  166. ret.FileOwnerAccountID = u;
  167. if (fetch (dict, NSFileManager.HfsTypeCode, ref u))
  168. ret.HfsTypeCode = u;
  169. if (fetch (dict, NSFileManager.PosixPermissions, ref u))
  170. ret.PosixPermissions = u;
  171. if (fetch (dict, NSFileManager.ReferenceCount, ref u))
  172. ret.FileReferenceCount = u;
  173. if (fetch (dict, NSFileManager.SystemFileNumber, ref u))
  174. ret.FileSystemFileNumber = u;
  175. ulong l = 0;
  176. if (fetch (dict, NSFileManager.Size, ref l))
  177. ret.FileSize = l;
  178. return ret;
  179. }
  180. }
  181. public class NSFileSystemAttributes {
  182. NSDictionary dict;
  183. internal NSFileSystemAttributes (NSDictionary dict)
  184. {
  185. this.dict = dict;
  186. }
  187. public ulong Size { get; internal set; }
  188. public ulong FreeSize { get; internal set; }
  189. public long Nodes { get; internal set; }
  190. public long FreeNodes { get; internal set; }
  191. public uint Number { get; internal set; }
  192. internal static NSFileSystemAttributes FromDict (NSDictionary dict)
  193. {
  194. if (dict == null)
  195. return null;
  196. var ret = new NSFileSystemAttributes (dict);
  197. ulong l = 0;
  198. uint i = 0;
  199. ret.Size = NSFileAttributes.fetch (dict, NSFileManager.SystemSize, ref l) ? l : 0;
  200. ret.FreeSize = NSFileAttributes.fetch (dict, NSFileManager.SystemFreeSize, ref l) ? l : 0;
  201. ret.Nodes = NSFileAttributes.fetch (dict, NSFileManager.SystemNodes, ref l) ? (long) l : 0;
  202. ret.FreeNodes= NSFileAttributes.fetch (dict, NSFileManager.SystemFreeNodes, ref l) ? (long) l : 0;
  203. ret.Number = NSFileAttributes.fetch (dict, NSFileManager.SystemFreeNodes, ref i) ? i : 0;
  204. return ret;
  205. }
  206. // For source code compatibility with users that had done manual NSDictionary lookups before
  207. public static implicit operator NSDictionary (NSFileSystemAttributes attr)
  208. {
  209. return attr.dict;
  210. }
  211. }
  212. public partial class NSFileManager {
  213. public bool SetAttributes (NSFileAttributes attributes, string path, out NSError error)
  214. {
  215. if (attributes == null)
  216. throw new ArgumentNullException ("attributes");
  217. return SetAttributes (attributes.ToDictionary (), path, out error);
  218. }
  219. public bool SetAttributes (NSFileAttributes attributes, string path)
  220. {
  221. NSError ignore;
  222. if (attributes == null)
  223. throw new ArgumentNullException ("attributes");
  224. return SetAttributes (attributes.ToDictionary (), path, out ignore);
  225. }
  226. public bool CreateDirectory (string path, bool createIntermediates, NSFileAttributes attributes, out NSError error)
  227. {
  228. var dict = attributes == null ? null : attributes.ToDictionary ();
  229. return CreateDirectory (path, createIntermediates, dict, out error);
  230. }
  231. public bool CreateDirectory (string path, bool createIntermediates, NSFileAttributes attributes)
  232. {
  233. NSError error;
  234. var dict = attributes == null ? null : attributes.ToDictionary ();
  235. return CreateDirectory (path, createIntermediates, dict, out error);
  236. }
  237. public bool CreateFile (string path, NSData data, NSFileAttributes attributes)
  238. {
  239. var dict = attributes == null ? null : attributes.ToDictionary ();
  240. return CreateFile (path, data, dict);
  241. }
  242. public NSFileAttributes GetAttributes (string path, out NSError error)
  243. {
  244. return NSFileAttributes.FromDict (_GetAttributes (path, out error));
  245. }
  246. public NSFileAttributes GetAttributes (string path)
  247. {
  248. NSError error;
  249. return NSFileAttributes.FromDict (_GetAttributes (path, out error));
  250. }
  251. public NSFileSystemAttributes GetFileSystemAttributes (string path)
  252. {
  253. NSError error;
  254. return NSFileSystemAttributes.FromDict (_GetFileSystemAttributes (path, out error));
  255. }
  256. public NSFileSystemAttributes GetFileSystemAttributes (string path, out NSError error)
  257. {
  258. return NSFileSystemAttributes.FromDict (_GetFileSystemAttributes (path, out error));
  259. }
  260. public string CurrentDirectory {
  261. get { return GetCurrentDirectory (); }
  262. // ignore boolean return value
  263. set { ChangeCurrentDirectory (value); }
  264. }
  265. }
  266. }