コード例 #1
0
ファイル: FileSystemDefs.cs プロジェクト: z-mohamed/CS422
        public virtual bool ContainsDir(string dirName, bool recursive)
        {
            Dir422 directory = GetDir(dirName);

            if (directory != null)
            {
                return(true);
            }

            if (!recursive)
            {
                return(false);
            }

            IList <Dir422> directories = GetDirs();

            for (int i = 0; i < directories.Count(); i++)
            {
                Dir422 dir = directories [i];

                if (dir.ContainsDir(dirName, true))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #2
0
 public void StdFSDirContainsDirNonRecursiveTest()
 {
     Assert.True(root.ContainsDir("one", false));
     Assert.False(root.ContainsDir("c", false));
 }
コード例 #3
0
ファイル: NUnitTests.cs プロジェクト: jakeknott/CS422
        public void RunTest(FileSys422 mySys)
        {
            Dir422 root = mySys.GetRoot();

            //We should not be able to go above our root.
            Assert.IsNull(root.Parent);

            // Checking that we do not have a file
            Assert.IsFalse(root.ContainsFile("NewFile.txt", false));
            //create the file
            root.CreateFile("NewFile.txt");
            // Check that we can find it.
            Assert.IsTrue(root.ContainsFile("NewFile.txt", false));

            // Same with directory
            Assert.IsFalse(root.ContainsDir("SubDir", false));
            Dir422 subDir = root.CreateDir("SubDir");

            Assert.IsTrue(root.ContainsDir("SubDir", false));

            //Creating a file in a sub dir
            subDir.CreateFile("subText.txt");

            // Testing the recursive methods on files
            Assert.IsFalse(root.ContainsFile("subText.txt", false));
            Assert.IsTrue(root.ContainsFile("subText.txt", true));

            //Testing recurcive method on dirs
            subDir.CreateDir("newSubDir");

            Assert.IsFalse(root.ContainsDir("newSubDir", false));
            Assert.IsTrue(root.ContainsDir("newSubDir", true));

            //Checking getDir
            Dir422 recivedDir = root.GetDir("InvalidDir");

            Assert.IsNull(recivedDir);
            recivedDir = root.GetDir("SubDir");
            Assert.AreEqual("SubDir", recivedDir.Name);

            // Checking that if a file does not exist we return null,
            // otherwise we recived the file we wanted.
            File422 recidedFile = root.GetFile("InvalidFile");

            Assert.IsNull(recidedFile);
            recidedFile = root.GetFile("NewFile.txt");
            Assert.AreEqual("NewFile.txt", recidedFile.Name);

            //Checking the name validation function.
            // All of these methods use the same Validate Name method.
            Assert.IsNull(subDir.CreateFile("file/New.txt"));
            Assert.IsNull(subDir.CreateDir("file/New"));

            string bufString = "hello world";

            byte[] buff        = ASCIIEncoding.ASCII.GetBytes(bufString);
            var    writeStream = recidedFile.OpenReadWrite();

            writeStream.Write(buff, 0, 11);

            var readStream = recidedFile.OpenReadOnly();

            Assert.IsNull(readStream);

            writeStream.Dispose();

            readStream = recidedFile.OpenReadOnly();
            Assert.IsNotNull(readStream);

            //First read 'hello ' from each stream
            byte[] readBuf = new byte[6];
            readStream.Read(readBuf, 0, 6);
            Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf));

            //Having two streams open for read
            var readStream2 = recidedFile.OpenReadOnly();

            Assert.IsNotNull(readStream2);

            byte[] readBuf2 = new byte[6];
            readStream2.Read(readBuf2, 0, 6);
            Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf2));

            //Next read 'world' from each stream
            readBuf = new byte[5];
            readStream.Read(readBuf, 0, 5);
            Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf));

            readBuf2 = new byte[5];
            readStream2.Read(readBuf2, 0, 5);
            Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf2));

            //try to open a stream to write while there are streams open for read
            writeStream = recidedFile.OpenReadWrite();
            Assert.IsNull(writeStream);

            //Close streams and try again
            readStream.Close();
            readStream2.Close();

            writeStream = recidedFile.OpenReadWrite();
            Assert.IsNotNull(writeStream);
        }
コード例 #4
0
        public override void Handler(WebRequest Req)
        {
            Dir422 Root = m_FS.GetRoot();

            //Remove Last / here!!!!
            if (Req.URI.LastIndexOf('/') == Req.URI.Length - 1)
            {
                Req.URI = Req.URI.Substring(0, Req.URI.Length - 1);
            }
            //int x = 0;


            //1. Percent-decode URI.
            // THIS NOT GOOD ENOUGH!!!

            Req.URI = Utility.PercentDecode(Req.URI);
            //string test = Req.URI.Replace ("%20", " ");;

            // Set name of requested file||dir.
            string uriName = Utility.NameFromPath(Req.URI);

            //2. If it refers to file somewhere in the shared folder.
            if (Root.ContainsFile(uriName, true))
            {
                string path = Req.URI.Substring(0, Req.URI.LastIndexOf("/"));

                Dir422     Dir          = Utility.TraverseToDir(Root, path);
                StdFSFile  File         = (StdFSFile)Dir.GetFile(uriName);
                FileStream MyFileStream = (FileStream)File.OpenReadOnly();

                if (Req.headers.ContainsKey("Range"))
                {
                    // process partial response here
                    Console.WriteLine("Process Partial Request");
                    int x = 0;
                }
                else
                {
                    string contentType = Utility.ContentType(uriName);
                    string response    = Utility.BuildFileResponseString(
                        MyFileStream.Length.ToString(), contentType);

                    //if (contentType != "video/mp4")
                    //{
                    byte[] sendResponseString = Encoding.ASCII.GetBytes(response);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);
                    //}

                    //byte[] sendResponseString = Encoding.ASCII.GetBytes(response);

                    int    read = 0;
                    byte[] send = new byte[7500];

                    while (read < MyFileStream.Length)
                    {
                        read = read + MyFileStream.Read(send, 0, send.Length);
                        Req.bodyRequest.Write(send, 0, send.Length);
                    }
                }
            }

            //3. Else if it refers to a folder somewhere in the shared folder.
            //	 Or is the shared directory
            else if (Root.ContainsDir(uriName, true) || Req.URI == ServiceURI || uriName == "")
            {
                if (Req.URI == ServiceURI || uriName == "")
                {
                    string dirHTMLListing = BuildDirHTML(Root);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
                else
                {
                    Dir422 Dir            = Utility.TraverseToDir(Root, Req.URI);
                    string dirHTMLListing = BuildDirHTML(Dir);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
            }

            //4.Else it’s a bad URI.
            else
            {
                Req.WriteNotFoundResponse("File or directory not found");
            }
        }
コード例 #5
0
 public void b_CreateContainDir()
 {
     root.CreateDir("TestDir");
     Assert.AreEqual(true, root.ContainsDir("TestDir", false));
 }
コード例 #6
0
 public void b_CreateContainsDirectory()
 {
     root.CreateDir("FooDir");
     Assert.AreEqual(true, root.ContainsDir("FooDir", false));
 }