IsolatedStorageFile.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. //
  2. // System.IO.IsolatedStorage.IsolatedStorageFile
  3. //
  4. // Authors
  5. // Jonathan Pryor ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Jonathan Pryor
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if !MOONLIGHT
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Runtime.InteropServices;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using System.Security;
  36. using System.Security.Cryptography;
  37. using System.Security.Permissions;
  38. using System.Security.Policy;
  39. using System.Text;
  40. using System.Threading;
  41. using Mono.Security.Cryptography;
  42. namespace System.IO.IsolatedStorage {
  43. // This is a terribly named class. It doesn't actually represent a file as
  44. // much as a directory
  45. [ComVisible (true)]
  46. // FIXME: Further limit the assertion when imperative Assert is implemented
  47. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  48. public sealed class IsolatedStorageFile : IsolatedStorage, IDisposable {
  49. private bool _resolved;
  50. private ulong _maxSize;
  51. private Evidence _fullEvidences;
  52. private static Mutex mutex = new Mutex ();
  53. public static IEnumerator GetEnumerator (IsolatedStorageScope scope)
  54. {
  55. Demand (scope);
  56. switch (scope) {
  57. case IsolatedStorageScope.User:
  58. case IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  59. case IsolatedStorageScope.Machine:
  60. break;
  61. default:
  62. string msg = Locale.GetText ("Invalid scope, only User, User|Roaming and Machine are valid");
  63. throw new ArgumentException (msg);
  64. }
  65. return new IsolatedStorageFileEnumerator (scope, GetIsolatedStorageRoot (scope));
  66. }
  67. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope,
  68. Evidence domainEvidence, Type domainEvidenceType,
  69. Evidence assemblyEvidence, Type assemblyEvidenceType)
  70. {
  71. Demand (scope);
  72. bool domain = ((scope & IsolatedStorageScope.Domain) != 0);
  73. if (domain && (domainEvidence == null))
  74. throw new ArgumentNullException ("domainEvidence");
  75. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  76. if (assembly && (assemblyEvidence == null))
  77. throw new ArgumentNullException ("assemblyEvidence");
  78. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  79. if (domain) {
  80. if (domainEvidenceType == null) {
  81. storageFile._domainIdentity = GetDomainIdentityFromEvidence (domainEvidence);
  82. } else {
  83. storageFile._domainIdentity = GetTypeFromEvidence (domainEvidence, domainEvidenceType);
  84. }
  85. if (storageFile._domainIdentity == null)
  86. throw new IsolatedStorageException (Locale.GetText ("Couldn't find domain identity."));
  87. }
  88. if (assembly) {
  89. if (assemblyEvidenceType == null) {
  90. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (assemblyEvidence);
  91. } else {
  92. storageFile._assemblyIdentity = GetTypeFromEvidence (assemblyEvidence, assemblyEvidenceType);
  93. }
  94. if (storageFile._assemblyIdentity == null)
  95. throw new IsolatedStorageException (Locale.GetText ("Couldn't find assembly identity."));
  96. }
  97. storageFile.PostInit ();
  98. return storageFile;
  99. }
  100. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object domainIdentity, object assemblyIdentity)
  101. {
  102. Demand (scope);
  103. if (((scope & IsolatedStorageScope.Domain) != 0) && (domainIdentity == null))
  104. throw new ArgumentNullException ("domainIdentity");
  105. bool assembly = ((scope & IsolatedStorageScope.Assembly) != 0);
  106. if (assembly && (assemblyIdentity == null))
  107. throw new ArgumentNullException ("assemblyIdentity");
  108. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  109. if (assembly)
  110. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  111. storageFile._domainIdentity = domainIdentity;
  112. storageFile._assemblyIdentity = assemblyIdentity;
  113. storageFile.PostInit ();
  114. return storageFile;
  115. }
  116. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
  117. {
  118. Demand (scope);
  119. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  120. if ((scope & IsolatedStorageScope.Domain) != 0) {
  121. if (domainEvidenceType == null)
  122. domainEvidenceType = typeof (Url);
  123. storageFile._domainIdentity = GetTypeFromEvidence (AppDomain.CurrentDomain.Evidence, domainEvidenceType);
  124. }
  125. if ((scope & IsolatedStorageScope.Assembly) != 0) {
  126. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  127. storageFile._fullEvidences = e;
  128. if ((scope & IsolatedStorageScope.Domain) != 0) {
  129. if (assemblyEvidenceType == null)
  130. assemblyEvidenceType = typeof (Url);
  131. storageFile._assemblyIdentity = GetTypeFromEvidence (e, assemblyEvidenceType);
  132. } else {
  133. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  134. }
  135. }
  136. storageFile.PostInit ();
  137. return storageFile;
  138. }
  139. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, object applicationIdentity)
  140. {
  141. Demand (scope);
  142. if (applicationIdentity == null)
  143. throw new ArgumentNullException ("applicationIdentity");
  144. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  145. storageFile._applicationIdentity = applicationIdentity;
  146. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  147. storageFile.PostInit ();
  148. return storageFile;
  149. }
  150. public static IsolatedStorageFile GetStore (IsolatedStorageScope scope, Type applicationEvidenceType)
  151. {
  152. Demand (scope);
  153. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  154. storageFile.InitStore (scope, applicationEvidenceType);
  155. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  156. storageFile.PostInit ();
  157. return storageFile;
  158. }
  159. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByMachine)]
  160. public static IsolatedStorageFile GetMachineStoreForApplication ()
  161. {
  162. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Application;
  163. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  164. storageFile.InitStore (scope, null);
  165. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  166. storageFile.PostInit ();
  167. return storageFile;
  168. }
  169. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByMachine)]
  170. public static IsolatedStorageFile GetMachineStoreForAssembly ()
  171. {
  172. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Assembly;
  173. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  174. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  175. storageFile._fullEvidences = e;
  176. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  177. storageFile.PostInit ();
  178. return storageFile;
  179. }
  180. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByMachine)]
  181. public static IsolatedStorageFile GetMachineStoreForDomain ()
  182. {
  183. IsolatedStorageScope scope = IsolatedStorageScope.Machine | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  184. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  185. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  186. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  187. storageFile._fullEvidences = e;
  188. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  189. storageFile.PostInit ();
  190. return storageFile;
  191. }
  192. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.ApplicationIsolationByUser)]
  193. public static IsolatedStorageFile GetUserStoreForApplication ()
  194. {
  195. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Application;
  196. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  197. storageFile.InitStore (scope, null);
  198. storageFile._fullEvidences = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  199. storageFile.PostInit ();
  200. return storageFile;
  201. }
  202. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
  203. public static IsolatedStorageFile GetUserStoreForAssembly ()
  204. {
  205. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Assembly;
  206. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  207. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  208. storageFile._fullEvidences = e;
  209. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  210. storageFile.PostInit ();
  211. return storageFile;
  212. }
  213. [IsolatedStorageFilePermission (SecurityAction.Demand, UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser)]
  214. public static IsolatedStorageFile GetUserStoreForDomain ()
  215. {
  216. IsolatedStorageScope scope = IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly;
  217. IsolatedStorageFile storageFile = new IsolatedStorageFile (scope);
  218. storageFile._domainIdentity = GetDomainIdentityFromEvidence (AppDomain.CurrentDomain.Evidence);
  219. Evidence e = Assembly.GetCallingAssembly ().UnprotectedGetEvidence ();
  220. storageFile._fullEvidences = e;
  221. storageFile._assemblyIdentity = GetAssemblyIdentityFromEvidence (e);
  222. storageFile.PostInit ();
  223. return storageFile;
  224. }
  225. public static void Remove (IsolatedStorageScope scope)
  226. {
  227. string dir = GetIsolatedStorageRoot (scope);
  228. Directory.Delete (dir, true);
  229. }
  230. // internal static stuff
  231. // Security Note: We're using InternalGetFolderPath because
  232. // IsolatedStorage must be able to work even if we do not have
  233. // FileIOPermission's PathDiscovery permissions
  234. internal static string GetIsolatedStorageRoot (IsolatedStorageScope scope)
  235. {
  236. // IsolatedStorageScope mixes several flags into one.
  237. // This first level deals with the root directory - it
  238. // is decided based on User, User+Roaming or Machine
  239. string root = null;
  240. if ((scope & IsolatedStorageScope.User) != 0) {
  241. if ((scope & IsolatedStorageScope.Roaming) != 0) {
  242. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.LocalApplicationData);
  243. } else {
  244. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.ApplicationData);
  245. }
  246. } else if ((scope & IsolatedStorageScope.Machine) != 0) {
  247. root = Environment.InternalGetFolderPath (Environment.SpecialFolder.CommonApplicationData);
  248. }
  249. if (root == null) {
  250. string msg = Locale.GetText ("Couldn't access storage location for '{0}'.");
  251. throw new IsolatedStorageException (String.Format (msg, scope));
  252. }
  253. return Path.Combine (root, ".isolated-storage");
  254. }
  255. private static void Demand (IsolatedStorageScope scope)
  256. {
  257. if (SecurityManager.SecurityEnabled) {
  258. IsolatedStorageFilePermission isfp = new IsolatedStorageFilePermission (PermissionState.None);
  259. isfp.UsageAllowed = ScopeToContainment (scope);
  260. isfp.Demand ();
  261. }
  262. }
  263. private static IsolatedStorageContainment ScopeToContainment (IsolatedStorageScope scope)
  264. {
  265. switch (scope) {
  266. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  267. return IsolatedStorageContainment.DomainIsolationByUser;
  268. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User:
  269. return IsolatedStorageContainment.AssemblyIsolationByUser;
  270. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  271. return IsolatedStorageContainment.DomainIsolationByRoamingUser;
  272. case IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  273. return IsolatedStorageContainment.AssemblyIsolationByRoamingUser;
  274. case IsolatedStorageScope.Application | IsolatedStorageScope.User:
  275. return IsolatedStorageContainment.ApplicationIsolationByUser;
  276. case IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  277. return IsolatedStorageContainment.DomainIsolationByMachine;
  278. case IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine:
  279. return IsolatedStorageContainment.AssemblyIsolationByMachine;
  280. case IsolatedStorageScope.Application | IsolatedStorageScope.Machine:
  281. return IsolatedStorageContainment.ApplicationIsolationByMachine;
  282. case IsolatedStorageScope.Application | IsolatedStorageScope.User | IsolatedStorageScope.Roaming:
  283. return IsolatedStorageContainment.ApplicationIsolationByRoamingUser;
  284. default:
  285. // unknown ?!?! then ask for maximum (unrestricted)
  286. return IsolatedStorageContainment.UnrestrictedIsolatedStorage;
  287. }
  288. }
  289. internal static ulong GetDirectorySize (DirectoryInfo di)
  290. {
  291. ulong size = 0;
  292. foreach (FileInfo fi in di.GetFiles ())
  293. size += (ulong) fi.Length;
  294. foreach (DirectoryInfo d in di.GetDirectories ())
  295. size += GetDirectorySize (d);
  296. return size;
  297. }
  298. // non-static stuff
  299. private DirectoryInfo directory;
  300. private IsolatedStorageFile (IsolatedStorageScope scope)
  301. {
  302. storage_scope = scope;
  303. }
  304. internal IsolatedStorageFile (IsolatedStorageScope scope, string location)
  305. {
  306. storage_scope = scope;
  307. directory = new DirectoryInfo (location);
  308. if (!directory.Exists) {
  309. string msg = Locale.GetText ("Invalid storage.");
  310. throw new IsolatedStorageException (msg);
  311. }
  312. // load the identities
  313. }
  314. ~IsolatedStorageFile ()
  315. {
  316. }
  317. private void PostInit ()
  318. {
  319. string root = GetIsolatedStorageRoot (Scope);
  320. string dir = null;
  321. if (_applicationIdentity != null) {
  322. dir = String.Format ("a{0}{1}", SeparatorInternal, GetNameFromIdentity (_applicationIdentity));
  323. } else if (_domainIdentity != null) {
  324. dir = String.Format ("d{0}{1}{0}{2}", SeparatorInternal,
  325. GetNameFromIdentity (_domainIdentity), GetNameFromIdentity (_assemblyIdentity));
  326. } else if (_assemblyIdentity != null) {
  327. dir = String.Format ("d{0}none{0}{1}", SeparatorInternal, GetNameFromIdentity (_assemblyIdentity));
  328. } else {
  329. throw new IsolatedStorageException (Locale.GetText ("No code identity available."));
  330. }
  331. root = Path.Combine (root, dir);
  332. // identities have been selected
  333. directory = new DirectoryInfo (root);
  334. if (!directory.Exists) {
  335. try {
  336. directory.Create ();
  337. SaveIdentities (root);
  338. }
  339. catch (IOException) {
  340. }
  341. }
  342. }
  343. [CLSCompliant(false)]
  344. public override ulong CurrentSize {
  345. get { return GetDirectorySize (directory); }
  346. }
  347. [CLSCompliant(false)]
  348. public override ulong MaximumSize {
  349. // return an ulong but default is signed long
  350. get {
  351. if (!SecurityManager.SecurityEnabled)
  352. return Int64.MaxValue;
  353. if (_resolved)
  354. return _maxSize;
  355. Evidence e = null;
  356. if (_fullEvidences != null) {
  357. // if possible use the complete evidences we had
  358. // for computing the X identity
  359. e = _fullEvidences;
  360. } else {
  361. e = new Evidence ();
  362. // otherwise use what was provided
  363. if (_assemblyIdentity != null)
  364. e.AddHost (_assemblyIdentity);
  365. }
  366. if (e.Count < 1) {
  367. throw new InvalidOperationException (
  368. Locale.GetText ("Couldn't get the quota from the available evidences."));
  369. }
  370. PermissionSet denied = null;
  371. PermissionSet ps = SecurityManager.ResolvePolicy (e, null, null, null, out denied);
  372. IsolatedStoragePermission isp = GetPermission (ps);
  373. if (isp == null) {
  374. if (ps.IsUnrestricted ()) {
  375. _maxSize = Int64.MaxValue; /* default value */
  376. } else {
  377. throw new InvalidOperationException (
  378. Locale.GetText ("No quota from the available evidences."));
  379. }
  380. } else {
  381. _maxSize = (ulong) isp.UserQuota;
  382. }
  383. _resolved = true;
  384. return _maxSize;
  385. }
  386. }
  387. internal string Root {
  388. get { return directory.FullName; }
  389. }
  390. // methods
  391. public void Close ()
  392. {
  393. }
  394. public void CreateDirectory (string dir)
  395. {
  396. if (dir == null)
  397. throw new ArgumentNullException ("dir");
  398. if (dir.IndexOfAny (Path.PathSeparatorChars) < 0) {
  399. if (directory.GetFiles (dir).Length > 0)
  400. throw new IOException (Locale.GetText ("Directory name already exists as a file."));
  401. directory.CreateSubdirectory (dir);
  402. } else {
  403. string[] dirs = dir.Split (Path.PathSeparatorChars);
  404. DirectoryInfo dinfo = directory;
  405. for (int i = 0; i < dirs.Length; i++) {
  406. if (dinfo.GetFiles (dirs [i]).Length > 0)
  407. throw new IOException (Locale.GetText (
  408. "Part of the directory name already exists as a file."));
  409. dinfo = dinfo.CreateSubdirectory (dirs [i]);
  410. }
  411. }
  412. }
  413. public void DeleteDirectory (string dir)
  414. {
  415. try {
  416. DirectoryInfo subdir = directory.CreateSubdirectory (dir);
  417. subdir.Delete ();
  418. }
  419. catch {
  420. // hide the real exception to avoid leaking the full path
  421. throw new IsolatedStorageException (Locale.GetText ("Could not delete directory '{0}'", dir));
  422. }
  423. }
  424. public void DeleteFile (string file)
  425. {
  426. File.Delete (Path.Combine (directory.FullName, file));
  427. }
  428. public void Dispose ()
  429. {
  430. // nothing to dispose, anyway we want to please the tools
  431. GC.SuppressFinalize (this);
  432. }
  433. public string[] GetDirectoryNames (string searchPattern)
  434. {
  435. if (searchPattern == null)
  436. throw new ArgumentNullException ("searchPattern");
  437. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  438. // so we need to split them to get the right results
  439. string path = Path.GetDirectoryName (searchPattern);
  440. string pattern = Path.GetFileName (searchPattern);
  441. DirectoryInfo[] adi = null;
  442. if (path == null || path.Length == 0) {
  443. adi = directory.GetDirectories (searchPattern);
  444. } else {
  445. DirectoryInfo[] subdirs = directory.GetDirectories (path);
  446. // we're looking for a single result, identical to path (no pattern here)
  447. // we're also looking for something under the current path (not outside isolated storage)
  448. if ((subdirs.Length == 1) && (subdirs [0].Name == path) && (subdirs [0].FullName.IndexOf (directory.FullName) >= 0)) {
  449. adi = subdirs [0].GetDirectories (pattern);
  450. } else {
  451. // CAS, even in FullTrust, normally enforce IsolatedStorage
  452. throw new SecurityException ();
  453. }
  454. }
  455. return GetNames (adi);
  456. }
  457. private string[] GetNames (FileSystemInfo[] afsi)
  458. {
  459. string[] r = new string[afsi.Length];
  460. for (int i = 0; i != afsi.Length; ++i)
  461. r[i] = afsi[i].Name;
  462. return r;
  463. }
  464. public string[] GetFileNames (string searchPattern)
  465. {
  466. if (searchPattern == null)
  467. throw new ArgumentNullException ("searchPattern");
  468. // note: IsolatedStorageFile accept a "dir/file" pattern which is not allowed by DirectoryInfo
  469. // so we need to split them to get the right results
  470. string path = Path.GetDirectoryName (searchPattern);
  471. string pattern = Path.GetFileName (searchPattern);
  472. FileInfo[] afi = null;
  473. if (path == null || path.Length == 0) {
  474. afi = directory.GetFiles (searchPattern);
  475. } else {
  476. DirectoryInfo[] subdirs = directory.GetDirectories (path);
  477. // we're looking for a single result, identical to path (no pattern here)
  478. // we're also looking for something under the current path (not outside isolated storage)
  479. if ((subdirs.Length == 1) && (subdirs [0].Name == path) && (subdirs [0].FullName.IndexOf (directory.FullName) >= 0)) {
  480. afi = subdirs [0].GetFiles (pattern);
  481. } else {
  482. // CAS, even in FullTrust, normally enforce IsolatedStorage
  483. throw new SecurityException ();
  484. }
  485. }
  486. return GetNames (afi);
  487. }
  488. public override void Remove ()
  489. {
  490. directory.Delete (true);
  491. }
  492. protected override IsolatedStoragePermission GetPermission (PermissionSet ps)
  493. {
  494. if (ps == null)
  495. return null;
  496. return (IsolatedStoragePermission) ps.GetPermission (typeof (IsolatedStorageFilePermission));
  497. }
  498. // internal stuff
  499. private string GetNameFromIdentity (object identity)
  500. {
  501. // Note: Default evidences return an XML string with ToString
  502. byte[] id = Encoding.UTF8.GetBytes (identity.ToString ());
  503. SHA1 hash = SHA1.Create ();
  504. // this create an unique name for an identity - bad identities like Url
  505. // results in bad (i.e. changing) names.
  506. byte[] full = hash.ComputeHash (id, 0, id.Length);
  507. byte[] half = new byte [10];
  508. Buffer.BlockCopy (full, 0, half, 0, half.Length);
  509. return CryptoConvert.ToHex (half);
  510. }
  511. private static object GetTypeFromEvidence (Evidence e, Type t)
  512. {
  513. foreach (object o in e) {
  514. if (o.GetType () == t)
  515. return o;
  516. }
  517. return null;
  518. }
  519. internal static object GetAssemblyIdentityFromEvidence (Evidence e)
  520. {
  521. // we prefer...
  522. // a. a Publisher evidence
  523. object identity = GetTypeFromEvidence (e, typeof (Publisher));
  524. if (identity != null)
  525. return identity;
  526. // b. a StrongName evidence
  527. identity = GetTypeFromEvidence (e, typeof (StrongName));
  528. if (identity != null)
  529. return identity;
  530. // c. a Url evidence
  531. return GetTypeFromEvidence (e, typeof (Url));
  532. }
  533. internal static object GetDomainIdentityFromEvidence (Evidence e)
  534. {
  535. // we prefer...
  536. // a. a ApplicationDirectory evidence
  537. object identity = GetTypeFromEvidence (e, typeof (ApplicationDirectory));
  538. if (identity != null)
  539. return identity;
  540. // b. a Url evidence
  541. return GetTypeFromEvidence (e, typeof (Url));
  542. }
  543. [Serializable]
  544. private struct Identities {
  545. public object Application;
  546. public object Assembly;
  547. public object Domain;
  548. public Identities (object application, object assembly, object domain)
  549. {
  550. Application = application;
  551. Assembly = assembly;
  552. Domain = domain;
  553. }
  554. }
  555. /*
  556. [SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)]
  557. private void LoadIdentities (string root)
  558. {
  559. if (!File.Exists (root + ".storage"))
  560. throw new IsolatedStorageException (Locale.GetText ("Missing identities."));
  561. BinaryFormatter deformatter = new BinaryFormatter ();
  562. using (FileStream fs = File.OpenRead (root + ".storage")) {
  563. Identities identities = (Identities) deformatter.Deserialize (fs);
  564. _applicationIdentity = identities.Application;
  565. _assemblyIdentity = identities.Assembly;
  566. _domainIdentity = identities.Domain;
  567. }
  568. }
  569. */
  570. [SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)]
  571. private void SaveIdentities (string root)
  572. {
  573. Identities identities = new Identities (_applicationIdentity, _assemblyIdentity, _domainIdentity);
  574. BinaryFormatter formatter = new BinaryFormatter ();
  575. mutex.WaitOne ();
  576. try {
  577. using (FileStream fs = File.Create (root + ".storage")) {
  578. formatter.Serialize (fs, identities);
  579. }
  580. }
  581. finally {
  582. mutex.ReleaseMutex ();
  583. }
  584. }
  585. }
  586. }
  587. #endif