Exemplo n.º 1
0
        internal TorrentFileStream GetStream(TorrentFile file, FileAccess access)
        {
            TorrentFileStream s = FindStream(file.FullPath);

            if (s != null)
            {
                // If we are requesting write access and the current stream does not have it
                if (((access & FileAccess.Write) == FileAccess.Write) && !s.CanWrite)
                {
                    Logger.Log (null, "Didn't have write permission - reopening");
                    CloseAndRemove(s);
                    s = null;
                }
                else
                {
                    // Place the filestream at the end so we know it's been recently used
                    list.Remove(s);
                    list.Add(s);
                }
            }

            if (s == null)
            {
                if (!File.Exists(file.FullPath))
                {
                    Directory.CreateDirectory (Path.GetDirectoryName(file.FullPath));
                    SparseFile.CreateSparse (file.FullPath, file.Length);
                }
                s = new TorrentFileStream (file, FileMode.OpenOrCreate, access, FileShare.Read);
                Add(s);
            }

            return s;
        }
        internal TorrentFileStream GetStream(TorrentFile file, string filePath, FileAccess access)
        {
            var s = FindStream(filePath);

            if (s != null)
            {
                // If we are requesting write access and the current stream does not have it
                if (((access & FileAccess.Write) == FileAccess.Write) && !s.CanWrite)
                {
                    Logger.Log(null, "Didn't have write permission - reopening");
                    CloseAndRemove(s);
                    s = null;
                }
                else
                {
                    // Place the filestream at the end so we know it's been recently used
                    list.Remove(s);
                    list.Add(s);
                }
            }

            if (s == null)
            {
                if (!File.Exists(filePath))
                {
                    SparseFile.CreateSparse(filePath, file.Length);
                }

                s = new TorrentFileStream(filePath, file, FileMode.OpenOrCreate, access, FileShare.Read);
                Add(s);
            }

            return(s);
        }
Exemplo n.º 3
0
        internal bool CloseStream(string path)
        {
            TorrentFileStream s = FindStream(path);

            if (s != null)
            {
                CloseAndRemove(s);
            }

            return(s != null);
        }
Exemplo n.º 4
0
        private void Add(TorrentFileStream stream)
        {
            Logger.Log(null, "Opening filestream: {0}", stream.Path);

            // If we have our maximum number of streams open, just dispose and dump the least recently used one
            if (maxStreams != 0 && list.Count >= list.Capacity)
            {
                Logger.Log(null, "We've reached capacity: {0}", list.Count);
                CloseAndRemove(list[0]);
            }
            list.Add(stream);
        }
        private void Add(TorrentFileStream stream)
        {
            Logger.Log(null, "Opening filestream: {0}", stream.Path);

            // If we have our maximum number of streams open, just dispose and dump the least recently used one
            if (maxStreams != 0 && Streams.Count >= Streams.Capacity)
            {
                Logger.Log(null, "We've reached capacity: {0}", Streams.Count);
                CloseAndRemove(Streams[0]);
            }
            Streams.Add(stream);
        }
Exemplo n.º 6
0
        internal TorrentFileStream GetStream(TorrentFile file, FileAccess access)
        {
            TorrentFileStream s = FindStream(file.FullPath);

            if (s != null)
            {
                // If we are requesting write access and the current stream does not have it
                if (((access & FileAccess.Write) == FileAccess.Write) && !s.CanWrite)
                {
                    Logger.Log(null, "Didn't have write permission - reopening");
                    CloseAndRemove(s);
                    s = null;
                }
                else
                {
                    // Place the filestream at the end so we know it's been recently used
                    list.Remove(s);
                    list.Add(s);
                }
            }

            if (s == null)
            {
                if (!File.Exists(file.FullPath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(file.FullPath));
                    NtfsSparseFile.CreateSparse(file.FullPath, file.Length);
                }
                s = new TorrentFileStream(file, FileMode.OpenOrCreate, access, FileShare.ReadWrite);

                // Ensure that we truncate existing files which are too large
                if (s.Length > file.Length)
                {
                    if (!s.CanWrite)
                    {
                        s.Close();
                        s = new TorrentFileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                    }
                    s.SetLength(file.Length);
                }

                Add(s);
            }

            return(s);
        }
Exemplo n.º 7
0
 private void CloseAndRemove(TorrentFileStream s)
 {
     Logger.Log(null, "Closing and removing: {0}", s.Path);
     list.Remove(s);
     s.Dispose();
 }
 private void CloseAndRemove(TorrentFileStream s)
 {
     Logger.Log (null, "Closing and removing: {0}", s.Path);
     list.Remove(s);
     s.Dispose();
 }