示例#1
0
        public byte[] Read(string filename)
        {
            // Debug.WriteLine("Storage: Reading " + filename);

            byte[] bytes = new byte[] { };;
            // Opening for read only.
            try
            {
                if (IsoStore.FileExists(filename))
                {
                    using (var fileStream = IsoStore.OpenFile(filename, FileMode.Open, FileAccess.Read))
                    {
                        bytes = new byte[fileStream.Length];
                        fileStream.Read(bytes, 0, bytes.Length);
                    }
                }
            }
            catch (FileNotFoundException)
            {
            }
            catch (IsolatedStorageException)
            {
            }

            /*finally
             * {
             * }*/
            return(bytes);
        }
示例#2
0
        public override void DeleteAll(string uniqueName)
        {
            lock (_cache) {
                if (_cache.ContainsKey(uniqueName))
                {
                    _cache.Remove(uniqueName);
                }
            }

            // find the directory.
            //
            var dir = FileItem.DirectoryHash(uniqueName);

            if (IsoStore.DirectoryExists(dir))
            {
                PriorityQueue.AddStorageWorkItem(() =>
                {
                    lock (LockObject) {
                        var files = IsoStore.GetFileNames(dir + "\\*");
                        foreach (var f in files)
                        {
                            var path = Path.Combine(dir, f);
                            DeleteFileHelper(IsoStore, path);
                        }
                    }
                });
            }
        }
示例#3
0
        public override void Write(CacheItemInfo info, byte[] data)
        {
            var fi = new FileItem(info);

            PriorityQueue.AddStorageWorkItem(() =>
            {
                lock (LockObject) {
                    for (int r = 0; r < WriteRetries; r++)
                    {
                        try {
                            FileItem.EnsurePath(IsoStore, fi.FileName);
                            using (Stream stream = IsoStore.OpenFile(fi.FileName, FileMode.Create, FileAccess.Write, FileShare.None)) {
                                stream.Write(data, 0, data.Length);
                                stream.Flush();
                            }
                            _cache[info.UniqueName] = info;
                            break;
                        }
                        catch (IsolatedStorageException) {
                            Debug.WriteLine("Exception writing file: Name={0}, Length={1}", fi.FileName, data.Length);
                            // These IsolatedStorageExceptions seem to happen at random,
                            // haven't yet found a repro.  So for the retry,
                            // if we failed, sleep for a bit and then try again.
                            //
                            Thread.Sleep(50);
                        }
                    }
                }
            });
        }
示例#4
0
 private void AddFolder(string prefix, List <string> list)
 {
     foreach (string file in IsoStore.GetFileNames(prefix + "\\*"))
     {
         list.Add(prefix + "\\" + file);
     }
     foreach (string dir in IsoStore.GetDirectoryNames(prefix + "\\*"))
     {
         AddFolder(prefix + "\\" + dir, list);
     }
 }
示例#5
0
文件: Logger.cs 项目: Oxidda/Kamina
        private static string TryGetFileName()
        {
            string dateTimeAsString = DateTime.Now.Ticks.ToString();
            string logLine          = $"Log{dateTimeAsString}.txt";

            if (IsoStore.FileExists("Log.txt"))
            {
                if (!IsoStore.FileExists(logLine))
                {
                    IsoStore.CopyFile("Log.txt", logLine);
                }
                else
                {
                    return(TryGetFileName());
                }
            }
            return(logLine);
        }
示例#6
0
        public void Delete(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return;
            }

            try
            {
                IsoStore.DeleteFile(filename);
            }
            catch
            {
            }
#if DEBUG
            // Debug.WriteLine("- deleting " + filename);
#endif
        }
示例#7
0
        public override byte[] Read(CacheItemInfo item)
        {
            var fi = new FileItem(item);

            byte[] bytes = null;

            lock (LockObject) {
                if (!IsoStore.FileExists(fi.FileName))
                {
                    return(null);
                }

                using (Stream stream = IsoStore.OpenFile(fi.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, (int)stream.Length);
                }
            }

            return(bytes);
        }
示例#8
0
        private IEnumerable <string> GetFilesRecursive(string root)
        {
            string search = Path.Combine(root, "*");

            List <string> files = new List <string>();

            try {
                files.AddRange(IsoStore.GetFileNames(search));
            }
            // These catch statements help with some rare exception stacks
            // similar to this:
            //
            // System.IO.IsolatedStorage.IsolatedStorageException
            //   at System.IO.IsolatedStorage.IsolatedStorageFile.EnsureAccessToPath(String PathAllowed, String PathRequested)
            //   at System.IO.IsolatedStorage.IsolatedStorageFile.GetFileDirectoryNames(String path, String msg, Boolean file)
            //   at System.IO.IsolatedStorage.IsolatedStorageFile.GetFileNames(String searchPattern)
            //   at AgFx.IsoStore.HashedIsoStoreProvider.GetItems(String uniqueName)
            //   at AgFx.IsoStore.HashedIsoStoreProvider.GetLastestExpiringItem(String uniqueName)
            //   at AgFx.CacheEntry.CacheValueLoader.FindCacheItem()
            //   at AgFx.CacheEntry.CacheValueLoader.get_IsValid()
            //   at AgFx.CacheEntry.LoadInternal(Boolean force)
            //   at AgFx.CacheEntry.<>c__DisplayClass2.<Load>b__0()
            //   at AgFx.PriorityQueue.WorkerThread.<>c__DisplayClass5.<WorkerThreadProc>b__3(Object s)
            //   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
            //   at System.Threading.Timer.ring()
            //
            catch (InvalidOperationException)
            {
            }
            catch (IsolatedStorageException)
            {
            }

            foreach (var d in IsoStore.GetDirectoryNames(search))
            {
                files.AddRange(GetFilesRecursive(Path.Combine(root, d)));
            }
            return(files);
        }
示例#9
0
        public void Write(string filename, byte[] data)
        {
            using (var fileStream = IsoStore.OpenFile(filename, FileMode.Create, FileAccess.Write, FileShare.Write)) // IsoStore.CreateFile(filename))
            {
                try
                {
                    if (data != null && data.Length > 0)
                    {
                        Debug.WriteLine("Storage: Writing " + filename);

                        fileStream.Write(data, 0, data.Length);
                    }
                }
                catch
                {
                }
                //finally
                //{
                //fileStream.Close();
                //}
            }
        }
示例#10
0
        public override IEnumerable <CacheItemInfo> GetItems(string uniqueName)
        {
            CacheItemInfo item;

            if (_cache.TryGetValue(uniqueName, out item))
            {
                return(new CacheItemInfo[] { item });
            }

            // find the directory.
            //
            var dir = FileItem.DirectoryHash(uniqueName);


            if (IsoStore.DirectoryExists(dir))
            {
                lock (LockObject) {
                    string[] files = null;

                    try
                    {
                        files = IsoStore.GetFileNames(dir + "\\*");
                    }
                    catch (IsolatedStorageException)
                    {
                        // intermittent IsoStore exceptions on shutdown.
                        files = new string[0];
                    }

                    List <CacheItemInfo> items = new List <CacheItemInfo>();

                    foreach (var f in files)
                    {
                        CacheItemInfo cii = FileItem.FromFileName(f);

                        if (cii != null)
                        {
                            items.Add(cii);
                        }
                    }

                    var orderedItems = from i in items
                                       where i.UniqueName == uniqueName
                                       orderby i.ExpirationTime descending
                                       select i;

                    foreach (var i in orderedItems)
                    {
                        if (item == null)
                        {
                            item = i;
                            continue;
                        }

                        Delete(i);
                    }

                    if (item != null)
                    {
                        _cache[uniqueName] = item;
                        return(new CacheItemInfo[] { item });
                    }
                }
            }
            return(new CacheItemInfo[0]);
        }