Exemplo n.º 1
0
        /**
         * RemoveDirectory removes a shared directory from the shared
         * directory list. All files contained in the directory or its sub
         * directories are removed from the shared files list. The
         * FileSystemWatcher for this directory is disabled and removed.
         */
        public bool RemoveDirectory(string sharedDirectory)
        {
            if (sharedDirectory == null)
                throw new ArgumentNullException("sharedDirectory");

            try
            {
                // remove shared directory and watcher
                try
                {
                    m_SharedDirectories.Lock();
                    if (m_DirectoryWatchers.ContainsKey(sharedDirectory))
                    {
                        m_DirectoryWatchers[sharedDirectory].EnableRaisingEvents = false;
                        m_DirectoryWatchers.Remove(sharedDirectory);
                    }
                    if (m_SharedDirectories.Contains(sharedDirectory))
                    {
                        m_SharedDirectories.Remove(sharedDirectory);
                    }
                }
                finally
                {
                    m_SharedDirectories.Unlock();
                }

                // remove all files in hash queue that contain the directory path
                try
                {
                    m_HashingQueue.Lock();
                    Regensburger.RCollections.LinkBased.RQueue<string> newHashingQueue = new Regensburger.RCollections.LinkBased.RQueue<string>();
                    while (!m_HashingQueue.IsEmpty)
                    {
                        string fileToHash = m_HashingQueue.Dequeue();
                        if (!fileToHash.Contains(sharedDirectory))
                        {
                            newHashingQueue.Enqueue(fileToHash);
                        }
                    }
                    while (!newHashingQueue.IsEmpty)
                    {
                        m_HashingQueue.Enqueue(newHashingQueue.Dequeue());
                    }
                }
                finally
                {
                    m_HashingQueue.Unlock();
                }

                // remove all shared files containing the directory path
                RHashtable<string, SharedFile> sharedFilesCopy = new RHashtable<string, SharedFile>(m_SharedFiles);
                foreach (SharedFile sharedFile in sharedFilesCopy.Values)
                {
                    if (sharedFile.FilePath.Contains(sharedDirectory))
                    {
                        RemoveFile(sharedFile.FilePath);
                    }
                }

                m_Logger.Log("Removed directory \"{0}\"", sharedDirectory);
                return true;
            }
            catch (Exception ex)
            {
                m_Logger.Log(ex, "An exception was thrown while removing a shared directory!");
                return false;
            }
        }